The official, canonical postActiv repository. http://www.postactiv.com

apigrouplistall.php 6.3KB

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