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