]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinepublic.php
10bde6f37d1beafad7f78db7297c1536f5db1944
[quix0rs-gnu-social.git] / actions / apitimelinepublic.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show the public timeline
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 most recent notices (default 20) posted by everybody
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 ApiTimelinePublicAction 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->max_id   = (int)$this->arg('max_id', 0);
67         $this->since_id = (int)$this->arg('since_id', 0);
68         $this->since    = $this->arg('since');
69
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
85     function handle($args)
86     {
87         parent::handle($args);
88         $this->showTimeline();
89     }
90
91     /**
92      * Show the timeline of notices
93      *
94      * @return void
95      */
96
97     function showTimeline()
98     {
99         $sitename   = common_config('site', 'name');
100         $title      = sprintf(_("%s public timeline"), $sitename);
101         $taguribase = common_config('integration', 'taguri');
102         $id         = "tag:$taguribase:PublicTimeline";
103         $link       = common_root_url();
104         $subtitle   = sprintf(_("%s updates from everyone!"), $sitename);
105
106         switch($this->arg('format')) {
107         case 'xml':
108             $this->show_xml_timeline($this->notices);
109             break;
110         case 'rss':
111             $this->show_rss_timeline($this->notices, $title, $link, $subtitle);
112             break;
113         case 'atom':
114             $selfuri = common_root_url() . 'api/statuses/public_timeline.atom';
115             $this->show_atom_timeline(
116                 $this->notices, $title, $id, $link,
117                 $subtitle, null, $selfuri
118             );
119             break;
120         case 'json':
121             $this->show_json_timeline($this->notices);
122             break;
123         default:
124             $this->clientError(_('API method not found!'), $code = 404);
125             break;
126         }
127     }
128
129     /**
130      * Get notices
131      *
132      * @return array notices
133      */
134
135     function getNotices()
136     {
137         $notices = array();
138
139         $notice = Notice::publicStream(
140             ($this->page - 1) * $this->count, $this->count, $this->since_id,
141             $this->max_id, $this->since
142         );
143
144         while ($notice->fetch()) {
145             $notices[] = clone($notice);
146         }
147
148         return $notices;
149     }
150
151     /**
152      * Is this action read only?
153      *
154      * @param array $args other arguments
155      *
156      * @return boolean true
157      */
158
159     function isReadOnly($args)
160     {
161         return true;
162     }
163
164     /**
165      * When was this feed last modified?
166      *
167      * @return string datestamp of the latest notice in the stream
168      */
169
170     function lastModified()
171     {
172         if (!empty($this->notices) && (count($this->notices) > 0)) {
173             return strtotime($this->notices[0]->created);
174         }
175
176         return null;
177     }
178
179     /**
180      * An entity tag for this stream
181      *
182      * Returns an Etag based on the action name, language, and
183      * timestamps of the first and last notice in the timeline
184      *
185      * @return string etag
186      */
187
188     function etag()
189     {
190         if (!empty($this->notices) && (count($this->notices) > 0)) {
191
192             $last = count($this->notices) - 1;
193
194             return '"' . implode(
195                 ':',
196                 array($this->arg('action'),
197                       common_language(),
198                       strtotime($this->notices[0]->created),
199                       strtotime($this->notices[$last]->created))
200             )
201             . '"';
202         }
203
204         return null;
205     }
206
207 }