]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinetag.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / actions / apitimelinetag.php
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
34 if (!defined('STATUSNET')) {
35     exit(1);
36 }
37
38 /**
39  * Returns the 20 most recent notices tagged by a given tag
40  *
41  * @category API
42  * @package  StatusNet
43  * @author   Craig Andrews <candrews@integralblue.com>
44  * @author   Evan Prodromou <evan@status.net>
45  * @author   Jeffery To <jeffery.to@gmail.com>
46  * @author   Zach Copley <zach@status.net>
47  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
48  * @link     http://status.net/
49  */
50 class ApiTimelineTagAction extends ApiPrivateAuthAction
51 {
52     var $notices = null;
53
54     protected function prepare(array $args=array())
55     {
56         parent::prepare($args);
57
58         $this->tag     = $this->arg('tag');
59         $this->notices = $this->getNotices();
60
61         return true;
62     }
63
64     /**
65      * Handle the request
66      *
67      * Just show the notices
68      *
69      * @param array $args $_REQUEST data (unused)
70      *
71      * @return void
72      */
73     protected function handle()
74     {
75         parent::handle();
76         $this->showTimeline();
77     }
78
79     /**
80      * Show the timeline of notices
81      *
82      * @return void
83      */
84     function showTimeline()
85     {
86         $sitename   = common_config('site', 'name');
87         $sitelogo   = (common_config('site', 'logo')) ? common_config('site', 'logo') : Theme::path('logo.png');
88         // TRANS: Title for timeline with lastest notices with a given tag.
89         // TRANS: %s is the tag.
90         $title      = sprintf(_("Notices tagged with %s"), $this->tag);
91         $subtitle   = sprintf(
92             // TRANS: Subtitle for timeline with lastest notices with a given tag.
93             // TRANS: %1$s is the tag, $2$s is the StatusNet sitename.
94             _('Updates tagged with %1$s on %2$s!'),
95             $this->tag,
96             $sitename
97         );
98         $taguribase = TagURI::base();
99         $id         = "tag:$taguribase:TagTimeline:".$this->tag;
100
101         $link = common_local_url(
102             'tag',
103             array('tag' => $this->tag)
104         );
105
106         $self = $this->getSelfUri();
107
108         switch($this->format) {
109         case 'xml':
110             $this->showXmlTimeline($this->notices);
111             break;
112         case 'rss':
113             $this->showRssTimeline(
114                 $this->notices,
115                 $title,
116                 $link,
117                 $subtitle,
118                 null,
119                 $sitelogo,
120                 $self
121             );
122             break;
123         case 'atom':
124             header('Content-Type: application/atom+xml; charset=utf-8');
125
126             $atom = new AtomNoticeFeed($this->auth_user);
127
128             $atom->setId($id);
129             $atom->setTitle($title);
130             $atom->setSubtitle($subtitle);
131             $atom->setLogo($sitelogo);
132             $atom->setUpdated('now');
133
134             $atom->addLink($link);
135             $atom->setSelfLink($self);
136
137             $atom->addEntryFromNotices($this->notices);
138             $this->raw($atom->getString());
139
140             break;
141         case 'json':
142             $this->showJsonTimeline($this->notices);
143             break;
144         case 'as':
145             header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
146             $doc = new ActivityStreamJSONDocument($this->auth_user);
147             $doc->setTitle($title);
148             $doc->addLink($link, 'alternate', 'text/html');
149             $doc->addItemsFromNotices($this->notices);
150             $this->raw($doc->asString());
151             break;
152         default:
153             // TRANS: Client error displayed when coming across a non-supported API method.
154             $this->clientError(_('API method not found.'), $code = 404);
155             break;
156         }
157     }
158
159     /**
160      * Get notices
161      *
162      * @return array notices
163      */
164     function getNotices()
165     {
166         $notice = Notice_tag::getStream($this->tag)->getNotices(($this->page - 1) * $this->count,
167                                                                  $this->count + 1,
168                                                                  $this->since_id,
169                                                                  $this->max_id);
170
171         return $notice->fetchAll();
172     }
173
174     /**
175      * Is this action read only?
176      *
177      * @param array $args other arguments
178      *
179      * @return boolean true
180      */
181     function isReadOnly(array $args=array())
182     {
183         return true;
184     }
185
186     /**
187      * When was this feed last modified?
188      *
189      * @return string datestamp of the latest notice in the stream
190      */
191     function lastModified()
192     {
193         if (!empty($this->notices) && (count($this->notices) > 0)) {
194             return strtotime($this->notices[0]->created);
195         }
196
197         return null;
198     }
199
200     /**
201      * An entity tag for this stream
202      *
203      * Returns an Etag based on the action name, language, and
204      * timestamps of the first and last notice in the timeline
205      *
206      * @return string etag
207      */
208     function etag()
209     {
210         if (!empty($this->notices) && (count($this->notices) > 0)) {
211
212             $last = count($this->notices) - 1;
213
214             return '"' . implode(
215                 ':',
216                 array($this->arg('action'),
217                       common_user_cache_hash($this->auth_user),
218                       common_language(),
219                       $this->tag,
220                       strtotime($this->notices[0]->created),
221                       strtotime($this->notices[$last]->created))
222             )
223             . '"';
224         }
225
226         return null;
227     }
228 }