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