]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinetag.php
2a23bb72a29296af1af3d0f52a05d956aebce02d
[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/twitterapi.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 TwitterapiAction
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->page     = (int)$this->arg('page', 1);
65         $this->count    = (int)$this->arg('count', 20);
66         $this->tag      = $this->arg('tag');
67         $this->format   = $this->arg('format');
68
69         $this->notices = $this->getNotices();
70
71         return true;
72     }
73
74     /**
75      * Handle the request
76      *
77      * Just show the notices
78      *
79      * @param array $args $_REQUEST data (unused)
80      *
81      * @return void
82      */
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
96     function showTimeline()
97     {
98         $sitename   = common_config('site', 'name');
99         $title      = sprintf(_("Notices tagged with %s"), $this->tag);
100         $link       = common_local_url(
101             'tag',
102             array('tag' => $this->tag)
103         );
104         $subtitle   = sprintf(
105             _('Updates tagged with %1$s on %2$s!'),
106             $this->tag,
107             $sitename
108         );
109         $taguribase = common_config('integration', 'taguri');
110         $id         = "tag:$taguribase:TagTimeline:".$tag;
111
112         switch($this->format) {
113         case 'xml':
114             $this->show_xml_timeline($this->notices);
115             break;
116         case 'rss':
117             $this->show_rss_timeline($this->notices, $title, $link, $subtitle);
118             break;
119         case 'atom':
120             $selfuri = common_root_url() .
121                 'api/statusnet/tags/timeline/' .
122                     $this->tag . '.atom';
123             $this->show_atom_timeline(
124                 $this->notices,
125                 $title,
126                 $id,
127                 $link,
128                 $subtitle,
129                 null,
130                 $selfuri
131             );
132             break;
133         case 'json':
134             $this->show_json_timeline($this->notices);
135             break;
136         default:
137             $this->clientError(_('API method not found!'), $code = 404);
138             break;
139         }
140     }
141
142     /**
143      * Get notices
144      *
145      * @return array notices
146      */
147
148     function getNotices()
149     {
150         $notices = array();
151
152         $notice = Notice_tag::getStream(
153             $this->tag,
154             ($this->page - 1) * $this->count,
155             $this->count + 1
156         );
157
158         while ($notice->fetch()) {
159             $notices[] = clone($notice);
160         }
161
162         return $notices;
163     }
164
165     /**
166      * Is this action read only?
167      *
168      * @param array $args other arguments
169      *
170      * @return boolean true
171      */
172
173     function isReadOnly($args)
174     {
175         return true;
176     }
177
178     /**
179      * When was this feed last modified?
180      *
181      * @return string datestamp of the latest notice in the stream
182      */
183
184     function lastModified()
185     {
186         if (!empty($this->notices) && (count($this->notices) > 0)) {
187             return strtotime($this->notices[0]->created);
188         }
189
190         return null;
191     }
192
193     /**
194      * An entity tag for this stream
195      *
196      * Returns an Etag based on the action name, language, and
197      * timestamps of the first and last notice in the timeline
198      *
199      * @return string etag
200      */
201
202     function etag()
203     {
204         if (!empty($this->notices) && (count($this->notices) > 0)) {
205
206             $last = count($this->notices) - 1;
207
208             return '"' . implode(
209                 ':',
210                 array($this->arg('action'),
211                       common_language(),
212                       $this->tag,
213                       strtotime($this->notices[0]->created),
214                       strtotime($this->notices[$last]->created))
215             )
216             . '"';
217         }
218
219         return null;
220     }
221
222 }