]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinetag.php
These same params are used in most API actions; moved to base API class
[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    Zach Copley <zach@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/api.php';
35
36 /**
37  * Returns the 20 most recent notices tagged by a given tag
38  *
39  * @category API
40  * @package  StatusNet
41  * @author   Zach Copley <zach@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  */
45
46 class ApiTimelineTagAction extends ApiAction
47 {
48
49     var $notices = null;
50
51     /**
52      * Take arguments for running
53      *
54      * @param array $args $_REQUEST args
55      *
56      * @return boolean success flag
57      *
58      */
59
60     function prepare($args)
61     {
62         parent::prepare($args);
63
64         $this->tag     = $this->arg('tag');
65         $this->notices = $this->getNotices();
66
67         return true;
68     }
69
70     /**
71      * Handle the request
72      *
73      * Just show the notices
74      *
75      * @param array $args $_REQUEST data (unused)
76      *
77      * @return void
78      */
79
80     function handle($args)
81     {
82         parent::handle($args);
83         $this->showTimeline();
84     }
85
86     /**
87      * Show the timeline of notices
88      *
89      * @return void
90      */
91
92     function showTimeline()
93     {
94         $sitename   = common_config('site', 'name');
95         $title      = sprintf(_("Notices tagged with %s"), $this->tag);
96         $link       = common_local_url(
97             'tag',
98             array('tag' => $this->tag)
99         );
100         $subtitle   = sprintf(
101             _('Updates tagged with %1$s on %2$s!'),
102             $this->tag,
103             $sitename
104         );
105         $taguribase = common_config('integration', 'taguri');
106         $id         = "tag:$taguribase:TagTimeline:".$tag;
107
108         switch($this->format) {
109         case 'xml':
110             $this->show_xml_timeline($this->notices);
111             break;
112         case 'rss':
113             $this->show_rss_timeline($this->notices, $title, $link, $subtitle);
114             break;
115         case 'atom':
116             $selfuri = common_root_url() .
117                 'api/statusnet/tags/timeline/' .
118                     $this->tag . '.atom';
119             $this->show_atom_timeline(
120                 $this->notices,
121                 $title,
122                 $id,
123                 $link,
124                 $subtitle,
125                 null,
126                 $selfuri
127             );
128             break;
129         case 'json':
130             $this->show_json_timeline($this->notices);
131             break;
132         default:
133             $this->clientError(_('API method not found!'), $code = 404);
134             break;
135         }
136     }
137
138     /**
139      * Get notices
140      *
141      * @return array notices
142      */
143
144     function getNotices()
145     {
146         $notices = array();
147
148         $notice = Notice_tag::getStream(
149             $this->tag,
150             ($this->page - 1) * $this->count,
151             $this->count + 1
152         );
153
154         while ($notice->fetch()) {
155             $notices[] = clone($notice);
156         }
157
158         return $notices;
159     }
160
161     /**
162      * Is this action read only?
163      *
164      * @param array $args other arguments
165      *
166      * @return boolean true
167      */
168
169     function isReadOnly($args)
170     {
171         return true;
172     }
173
174     /**
175      * When was this feed last modified?
176      *
177      * @return string datestamp of the latest notice in the stream
178      */
179
180     function lastModified()
181     {
182         if (!empty($this->notices) && (count($this->notices) > 0)) {
183             return strtotime($this->notices[0]->created);
184         }
185
186         return null;
187     }
188
189     /**
190      * An entity tag for this stream
191      *
192      * Returns an Etag based on the action name, language, and
193      * timestamps of the first and last notice in the timeline
194      *
195      * @return string etag
196      */
197
198     function etag()
199     {
200         if (!empty($this->notices) && (count($this->notices) > 0)) {
201
202             $last = count($this->notices) - 1;
203
204             return '"' . implode(
205                 ':',
206                 array($this->arg('action'),
207                       common_language(),
208                       $this->tag,
209                       strtotime($this->notices[0]->created),
210                       strtotime($this->notices[$last]->created))
211             )
212             . '"';
213         }
214
215         return null;
216     }
217
218 }