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