apitimelinetag.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Show the latest notices for a given tag
  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-2010 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 the 20 most recent notices tagged by a given tag
  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 ApiTimelineTagAction extends ApiPrivateAuthAction
  49. {
  50. var $notices = null;
  51. protected function prepare(array $args=array())
  52. {
  53. parent::prepare($args);
  54. $this->tag = $this->arg('tag');
  55. $this->notices = $this->getNotices();
  56. return true;
  57. }
  58. /**
  59. * Handle the request
  60. *
  61. * Just show the notices
  62. *
  63. * @param array $args $_REQUEST data (unused)
  64. *
  65. * @return void
  66. */
  67. protected function handle()
  68. {
  69. parent::handle();
  70. $this->showTimeline();
  71. }
  72. /**
  73. * Show the timeline of notices
  74. *
  75. * @return void
  76. */
  77. function showTimeline()
  78. {
  79. $sitename = common_config('site', 'name');
  80. $sitelogo = (common_config('site', 'logo')) ? common_config('site', 'logo') : Theme::path('logo.png');
  81. // TRANS: Title for timeline with lastest notices with a given tag.
  82. // TRANS: %s is the tag.
  83. $title = sprintf(_("Notices tagged with %s"), $this->tag);
  84. $subtitle = sprintf(
  85. // TRANS: Subtitle for timeline with lastest notices with a given tag.
  86. // TRANS: %1$s is the tag, $2$s is the StatusNet sitename.
  87. _('Updates tagged with %1$s on %2$s!'),
  88. $this->tag,
  89. $sitename
  90. );
  91. $taguribase = TagURI::base();
  92. $id = "tag:$taguribase:TagTimeline:".$this->tag;
  93. $link = common_local_url(
  94. 'tag',
  95. array('tag' => $this->tag)
  96. );
  97. $self = $this->getSelfUri();
  98. switch($this->format) {
  99. case 'xml':
  100. $this->showXmlTimeline($this->notices);
  101. break;
  102. case 'rss':
  103. $this->showRssTimeline(
  104. $this->notices,
  105. $title,
  106. $link,
  107. $subtitle,
  108. null,
  109. $sitelogo,
  110. $self
  111. );
  112. break;
  113. case 'atom':
  114. header('Content-Type: application/atom+xml; charset=utf-8');
  115. $atom = new AtomNoticeFeed($this->auth_user);
  116. $atom->setId($id);
  117. $atom->setTitle($title);
  118. $atom->setSubtitle($subtitle);
  119. $atom->setLogo($sitelogo);
  120. $atom->setUpdated('now');
  121. $atom->addLink($link);
  122. $atom->setSelfLink($self);
  123. $atom->addEntryFromNotices($this->notices);
  124. $this->raw($atom->getString());
  125. break;
  126. case 'json':
  127. $this->showJsonTimeline($this->notices);
  128. break;
  129. case 'as':
  130. header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
  131. $doc = new ActivityStreamJSONDocument($this->auth_user);
  132. $doc->setTitle($title);
  133. $doc->addLink($link, 'alternate', 'text/html');
  134. $doc->addItemsFromNotices($this->notices);
  135. $this->raw($doc->asString());
  136. break;
  137. default:
  138. // TRANS: Client error displayed when coming across a non-supported API method.
  139. $this->clientError(_('API method not found.'), $code = 404);
  140. break;
  141. }
  142. }
  143. /**
  144. * Get notices
  145. *
  146. * @return array notices
  147. */
  148. function getNotices()
  149. {
  150. $notice = Notice_tag::getStream($this->tag)->getNotices(($this->page - 1) * $this->count,
  151. $this->count + 1,
  152. $this->since_id,
  153. $this->max_id);
  154. return $notice->fetchAll();
  155. }
  156. /**
  157. * Is this action read only?
  158. *
  159. * @param array $args other arguments
  160. *
  161. * @return boolean true
  162. */
  163. function isReadOnly($args)
  164. {
  165. return true;
  166. }
  167. /**
  168. * When was this feed last modified?
  169. *
  170. * @return string datestamp of the latest notice in the stream
  171. */
  172. function lastModified()
  173. {
  174. if (!empty($this->notices) && (count($this->notices) > 0)) {
  175. return strtotime($this->notices[0]->created);
  176. }
  177. return null;
  178. }
  179. /**
  180. * An entity tag for this stream
  181. *
  182. * Returns an Etag based on the action name, language, and
  183. * timestamps of the first and last notice in the timeline
  184. *
  185. * @return string etag
  186. */
  187. function etag()
  188. {
  189. if (!empty($this->notices) && (count($this->notices) > 0)) {
  190. $last = count($this->notices) - 1;
  191. return '"' . implode(
  192. ':',
  193. array($this->arg('action'),
  194. common_user_cache_hash($this->auth_user),
  195. common_language(),
  196. $this->tag,
  197. strtotime($this->notices[0]->created),
  198. strtotime($this->notices[$last]->created))
  199. )
  200. . '"';
  201. }
  202. return null;
  203. }
  204. }