apitimelinetag.php 6.8 KB

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