apigrouplistall.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. $group->selectAdd();
  115. $group->selectAdd('user_group.*');
  116. $group->joinAdd(['id', 'local_group:group_id']);
  117. $group->orderBy('user_group.created DESC, user_group.id DESC');
  118. $offset = ((int) $this->page - 1) * (int) $this->count;
  119. $group->limit($offset, $this->count);
  120. $groups = [];
  121. if ($group->find()) {
  122. while ($group->fetch()) {
  123. $groups[] = clone $group;
  124. }
  125. }
  126. return $groups;
  127. }
  128. /**
  129. * Is this action read only?
  130. *
  131. * @param array $args other arguments
  132. *
  133. * @return boolean true
  134. */
  135. public function isReadOnly($args)
  136. {
  137. return true;
  138. }
  139. /**
  140. * When was this feed last modified?
  141. *
  142. * @return string datestamp of the site's latest group
  143. */
  144. public function lastModified()
  145. {
  146. if (!empty($this->groups) && (count($this->groups) > 0)) {
  147. return strtotime($this->groups[0]->created);
  148. }
  149. return null;
  150. }
  151. /**
  152. * An entity tag for this list of groups
  153. *
  154. * Returns an Etag based on the action name, language, and
  155. * timestamps of the first and last group the user has joined
  156. *
  157. * @return string etag
  158. */
  159. public function etag()
  160. {
  161. if (!empty($this->groups) && (count($this->groups) > 0)) {
  162. $last = count($this->groups) - 1;
  163. return '"' . implode(
  164. ':',
  165. array($this->arg('action'),
  166. common_user_cache_hash($this->auth_user),
  167. common_language(),
  168. strtotime($this->groups[0]->created),
  169. strtotime($this->groups[$last]->created))
  170. )
  171. . '"';
  172. }
  173. return null;
  174. }
  175. }