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