apisubscriptions.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Base class for showing subscription information in the API
  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 Dan Moore <dan@moore.cx>
  25. * @author Evan Prodromou <evan@status.net>
  26. * @author Zach Copley <zach@status.net>
  27. * @copyright 2009 StatusNet, Inc.
  28. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  29. * @link http://status.net/
  30. */
  31. if (!defined('STATUSNET')) {
  32. exit(1);
  33. }
  34. /**
  35. * This class outputs a list of profiles as Twitter-style user and status objects.
  36. * It is used by the API methods /api/statuses/(friends|followers). To support the
  37. * social graph methods it also can output a simple list of IDs.
  38. *
  39. * @category API
  40. * @package StatusNet
  41. * @author Dan Moore <dan@moore.cx>
  42. * @author Evan Prodromou <evan@status.net>
  43. * @author Zach Copley <zach@status.net>
  44. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  45. * @link http://status.net/
  46. */
  47. abstract class ApiSubscriptionsAction extends ApiBareAuthAction
  48. {
  49. var $profiles = null;
  50. var $tag = null;
  51. var $lite = null;
  52. var $ids_only = null;
  53. /**
  54. * Take arguments for running
  55. *
  56. * @param array $args $_REQUEST args
  57. *
  58. * @return boolean success flag
  59. */
  60. protected function prepare(array $args=array())
  61. {
  62. parent::prepare($args);
  63. $this->tag = $this->arg('tag');
  64. // Note: Twitter no longer supports 'lite'
  65. $this->lite = $this->arg('lite');
  66. $this->ids_only = $this->arg('ids_only');
  67. // If called as a social graph method, show 5000 per page, otherwise 100
  68. $this->count = isset($this->ids_only) ?
  69. 5000 : (int)$this->arg('count', 100);
  70. $this->target = $this->getTargetProfile($this->arg('id'));
  71. if (!($this->target instanceof Profile)) {
  72. // TRANS: Client error displayed when requesting a list of followers for a non-existing user.
  73. $this->clientError(_('No such user.'), 404);
  74. }
  75. $this->profiles = $this->getProfiles();
  76. return true;
  77. }
  78. /**
  79. * Handle the request
  80. *
  81. * Show the profiles
  82. *
  83. * @return void
  84. */
  85. protected function handle()
  86. {
  87. parent::handle();
  88. if (!in_array($this->format, array('xml', 'json'))) {
  89. // TRANS: Client error displayed when coming across a non-supported API method.
  90. $this->clientError(_('API method not found.'), 404);
  91. }
  92. $this->initDocument($this->format);
  93. if (isset($this->ids_only)) {
  94. $this->showIds();
  95. } else {
  96. $this->showProfiles(isset($this->lite) ? false : true);
  97. }
  98. $this->endDocument($this->format);
  99. }
  100. /**
  101. * Get profiles related to the type of subscriber/subscription action
  102. *
  103. * @return array Profiles
  104. */
  105. abstract protected function getProfiles();
  106. /**
  107. * Is this action read only?
  108. *
  109. * @param array $args other arguments
  110. *
  111. * @return boolean true
  112. */
  113. function isReadOnly($args)
  114. {
  115. return true;
  116. }
  117. /**
  118. * When was this feed last modified?
  119. *
  120. * @return string datestamp of the latest profile in the stream
  121. */
  122. function lastModified()
  123. {
  124. if (!empty($this->profiles) && (count($this->profiles) > 0)) {
  125. return strtotime($this->profiles[0]->created);
  126. }
  127. return null;
  128. }
  129. /**
  130. * An entity tag for this action
  131. *
  132. * Returns an Etag based on the action name, language, user ID, and
  133. * timestamps of the first and last profiles in the subscriptions list
  134. * There's also an indicator to show whether this action is being called
  135. * as /api/statuses/(friends|followers) or /api/(friends|followers)/ids
  136. *
  137. * @return string etag
  138. */
  139. function etag()
  140. {
  141. if (!empty($this->profiles) && (count($this->profiles) > 0)) {
  142. $last = count($this->profiles) - 1;
  143. return '"' . implode(
  144. ':',
  145. array($this->arg('action'),
  146. common_user_cache_hash($this->auth_user),
  147. common_language(),
  148. $this->target->id,
  149. // Caching tags.
  150. isset($this->ids_only) ? 'IDs' : 'Profiles',
  151. strtotime($this->profiles[0]->created),
  152. strtotime($this->profiles[$last]->created))
  153. )
  154. . '"';
  155. }
  156. return null;
  157. }
  158. /**
  159. * Show the profiles as Twitter-style useres and statuses
  160. *
  161. * @param boolean $include_statuses Whether to include the latest status
  162. * with each user. Default true.
  163. *
  164. * @return void
  165. */
  166. function showProfiles($include_statuses = true)
  167. {
  168. switch ($this->format) {
  169. case 'xml':
  170. $this->elementStart('users', array('type' => 'array',
  171. 'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
  172. foreach ($this->profiles as $profile) {
  173. $this->showProfile(
  174. $profile,
  175. $this->format,
  176. null,
  177. $include_statuses
  178. );
  179. }
  180. $this->elementEnd('users');
  181. break;
  182. case 'json':
  183. $arrays = array();
  184. foreach ($this->profiles as $profile) {
  185. $arrays[] = $this->twitterUserArray(
  186. $profile,
  187. $include_statuses
  188. );
  189. }
  190. print json_encode($arrays);
  191. break;
  192. default:
  193. // TRANS: Client error displayed when requesting profiles of followers in an unsupported format.
  194. $this->clientError(_('Unsupported format.'));
  195. break;
  196. }
  197. }
  198. /**
  199. * Show the IDs of the profiles only. 5000 per page. To support
  200. * the 'social graph' methods: /api/(friends|followers)/ids
  201. *
  202. * @return void
  203. */
  204. function showIds()
  205. {
  206. switch ($this->format) {
  207. case 'xml':
  208. $this->elementStart('ids');
  209. foreach ($this->profiles as $profile) {
  210. $this->element('id', null, $profile->id);
  211. }
  212. $this->elementEnd('ids');
  213. break;
  214. case 'json':
  215. $ids = array();
  216. foreach ($this->profiles as $profile) {
  217. $ids[] = (int)$profile->id;
  218. }
  219. print json_encode($ids);
  220. break;
  221. default:
  222. // TRANS: Client error displayed when requesting IDs of followers in an unsupported format.
  223. $this->clientError(_('Unsupported format.'));
  224. break;
  225. }
  226. }
  227. }