apigrouplistall.php 5.4 KB

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