apigrouplistall.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Show the newest groups
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category API
  23. * @package StatusNet
  24. * @author Craig Andrews <candrews@integralblue.com>
  25. * @author Evan Prodromou <evan@status.net>
  26. * @author Jeffery To <jeffery.to@gmail.com>
  27. * @author Zach Copley <zach@status.net>
  28. * @copyright 2009 StatusNet, Inc.
  29. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  30. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  31. * @link http://status.net/
  32. */
  33. if (!defined('STATUSNET')) {
  34. exit(1);
  35. }
  36. /**
  37. * Returns of the lastest 20 groups for the site
  38. *
  39. * @category API
  40. * @package StatusNet
  41. * @author Craig Andrews <candrews@integralblue.com>
  42. * @author Evan Prodromou <evan@status.net>
  43. * @author Jeffery To <jeffery.to@gmail.com>
  44. * @author Zach Copley <zach@status.net>
  45. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  46. * @link http://status.net/
  47. */
  48. class ApiGroupListAllAction extends ApiPrivateAuthAction
  49. {
  50. var $groups = null;
  51. /**
  52. * Take arguments for running
  53. *
  54. * @param array $args $_REQUEST args
  55. *
  56. * @return boolean success flag
  57. */
  58. function prepare(array $args = array())
  59. {
  60. parent::prepare($args);
  61. $this->user = $this->getTargetUser(null);
  62. $this->groups = $this->getGroups();
  63. return true;
  64. }
  65. /**
  66. * Handle the request
  67. *
  68. * Show the user's groups
  69. *
  70. * @param array $args $_REQUEST data (unused)
  71. *
  72. * @return void
  73. */
  74. function handle()
  75. {
  76. parent::handle();
  77. $sitename = common_config('site', 'name');
  78. // TRANS: Message is used as a title when listing the lastest 20 groups. %s is a site name.
  79. $title = sprintf(_("%s groups"), $sitename);
  80. $taguribase = TagURI::base();
  81. $id = "tag:$taguribase:Groups";
  82. $link = common_local_url('groups');
  83. // TRANS: Message is used as a subtitle when listing the latest 20 groups. %s is a site name.
  84. $subtitle = sprintf(_("groups on %s"), $sitename);
  85. switch($this->format) {
  86. case 'xml':
  87. $this->showXmlGroups($this->groups);
  88. break;
  89. case 'rss':
  90. $this->showRssGroups($this->groups, $title, $link, $subtitle);
  91. break;
  92. case 'atom':
  93. $selfuri = common_root_url() .
  94. 'api/statusnet/groups/list_all.atom';
  95. $this->showAtomGroups(
  96. $this->groups,
  97. $title,
  98. $id,
  99. $link,
  100. $subtitle,
  101. $selfuri
  102. );
  103. break;
  104. case 'json':
  105. $this->showJsonGroups($this->groups);
  106. break;
  107. default:
  108. $this->clientError(
  109. // TRANS: Client error displayed when coming across a non-supported API method.
  110. _('API method not found.'),
  111. 404,
  112. $this->format
  113. );
  114. break;
  115. }
  116. }
  117. /**
  118. * Get groups
  119. *
  120. * @return array groups
  121. */
  122. function getGroups()
  123. {
  124. $qry = 'SELECT user_group.* '.
  125. 'from user_group join local_group on user_group.id = local_group.group_id '.
  126. 'order by created desc ';
  127. $offset = intval($this->page - 1) * intval($this->count);
  128. $limit = intval($this->count);
  129. if (common_config('db', 'type') == 'pgsql') {
  130. $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
  131. } else {
  132. $qry .= ' LIMIT ' . $offset . ', ' . $limit;
  133. }
  134. $group = new User_group();
  135. $group->query($qry);
  136. $groups = array();
  137. while ($group->fetch()) {
  138. $groups[] = clone($group);
  139. }
  140. return $groups;
  141. }
  142. /**
  143. * Is this action read only?
  144. *
  145. * @param array $args other arguments
  146. *
  147. * @return boolean true
  148. */
  149. function isReadOnly($args)
  150. {
  151. return true;
  152. }
  153. /**
  154. * When was this feed last modified?
  155. *
  156. * @return string datestamp of the site's latest group
  157. */
  158. function lastModified()
  159. {
  160. if (!empty($this->groups) && (count($this->groups) > 0)) {
  161. return strtotime($this->groups[0]->created);
  162. }
  163. return null;
  164. }
  165. /**
  166. * An entity tag for this list of groups
  167. *
  168. * Returns an Etag based on the action name, language, and
  169. * timestamps of the first and last group the user has joined
  170. *
  171. * @return string etag
  172. */
  173. function etag()
  174. {
  175. if (!empty($this->groups) && (count($this->groups) > 0)) {
  176. $last = count($this->groups) - 1;
  177. return '"' . implode(
  178. ':',
  179. array($this->arg('action'),
  180. common_user_cache_hash($this->auth_user),
  181. common_language(),
  182. strtotime($this->groups[0]->created),
  183. strtotime($this->groups[$last]->created))
  184. )
  185. . '"';
  186. }
  187. return null;
  188. }
  189. }