]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinetag.php
1a50520f4543ca11642baeebb6e52e73ceaf719a
[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         $link       = common_local_url(
104             'tag',
105             array('tag' => $this->tag)
106         );
107         $subtitle   = sprintf(
108             _('Updates tagged with %1$s on %2$s!'),
109             $this->tag,
110             $sitename
111         );
112         $taguribase = common_config('integration', 'taguri');
113         $id         = "tag:$taguribase:TagTimeline:".$tag;
114
115         switch($this->format) {
116         case 'xml':
117             $this->showXmlTimeline($this->notices);
118             break;
119         case 'rss':
120             $this->showRssTimeline($this->notices, $title, $link, $subtitle, null, $sitelogo);
121             break;
122         case 'atom':
123             $selfuri = common_root_url() .
124                 'api/statusnet/tags/timeline/' .
125                     $this->tag . '.atom';
126             $this->showAtomTimeline(
127                 $this->notices,
128                 $title,
129                 $id,
130                 $link,
131                 $subtitle,
132                 null,
133                 $selfuri,
134                 $sitelogo
135             );
136             break;
137         case 'json':
138             $this->showJsonTimeline($this->notices);
139             break;
140         default:
141             $this->clientError(_('API method not found!'), $code = 404);
142             break;
143         }
144     }
145
146     /**
147      * Get notices
148      *
149      * @return array notices
150      */
151
152     function getNotices()
153     {
154         $notices = array();
155
156         $notice = Notice_tag::getStream(
157             $this->tag,
158             ($this->page - 1) * $this->count,
159             $this->count + 1
160         );
161
162         while ($notice->fetch()) {
163             $notices[] = clone($notice);
164         }
165
166         return $notices;
167     }
168
169     /**
170      * Is this action read only?
171      *
172      * @param array $args other arguments
173      *
174      * @return boolean true
175      */
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
188     function lastModified()
189     {
190         if (!empty($this->notices) && (count($this->notices) > 0)) {
191             return strtotime($this->notices[0]->created);
192         }
193
194         return null;
195     }
196
197     /**
198      * An entity tag for this stream
199      *
200      * Returns an Etag based on the action name, language, and
201      * timestamps of the first and last notice in the timeline
202      *
203      * @return string etag
204      */
205
206     function etag()
207     {
208         if (!empty($this->notices) && (count($this->notices) > 0)) {
209
210             $last = count($this->notices) - 1;
211
212             return '"' . implode(
213                 ':',
214                 array($this->arg('action'),
215                       common_language(),
216                       $this->tag,
217                       strtotime($this->notices[0]->created),
218                       strtotime($this->notices[$last]->created))
219             )
220             . '"';
221         }
222
223         return null;
224     }
225
226 }