]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apisubscriptions.php
Merge branch '0.9.x' into refactor-api
[quix0rs-gnu-social.git] / actions / apisubscriptions.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Base class for showing subscription information in the API
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/apibareauth.php';
35
36 /**
37  * This class outputs a list of profiles as Twitter-style user and status objects.
38  * It is used by the API methods /api/statuses/(friends|followers). To support the
39  * social graph methods it also can output a simple list of IDs.
40  *
41  * @category API
42  * @package  StatusNet
43  * @author   Zach Copley <zach@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
46  */
47
48 class ApiSubscriptionsAction extends ApiBareAuthAction
49 {
50
51     var $page     = null;
52     var $count    = null;
53     var $user     = null;
54     var $profiles = null;
55     var $format   = null;
56     var $tag      = null;
57     var $lite     = null;
58     var $ids_only = null;
59
60     /**
61      * Take arguments for running
62      *
63      * @param array $args $_REQUEST args
64      *
65      * @return boolean success flag
66      *
67      */
68
69     function prepare($args)
70     {
71         parent::prepare($args);
72
73         $this->page     = (int)$this->arg('page', 1);
74         $this->tag      = $this->arg('tag');
75         $this->format   = $this->arg('format');
76
77         // Note: Twitter no longer supports 'lite'
78         $this->lite     = $this->arg('lite');
79
80         $this->ids_only = $this->arg('ids_only');
81
82         // If called as a social graph method, show 5000 per page, otherwise 100
83
84         $this->count    = isset($this->ids_only) ?
85             5000 : (int)$this->arg('count', 100);
86
87         if ($this->requiresAuth()) {
88             if ($this->checkBasicAuthUser() == false) {
89                 return false;
90             }
91         }
92
93         $this->user = $this->getTargetUser($this->arg('id'));
94
95         if (empty($this->user)) {
96             $this->clientError(_('No such user!'), 404, $this->format);
97             return false;
98         }
99
100         $this->profiles = $this->getProfiles();
101
102         return true;
103     }
104
105     /**
106      * Handle the request
107      *
108      * Show the profiles
109      *
110      * @param array $args $_REQUEST data (unused)
111      *
112      * @return void
113      */
114
115     function handle($args)
116     {
117         parent::handle($args);
118
119         if (!in_array($this->format, array('xml', 'json'))) {
120             $this->clientError(_('API method not found!'), $code = 404);
121             return;
122         }
123
124         $this->init_document($this->format);
125
126         if (isset($this->ids_only)) {
127             $this->showIds();
128         } else {
129             $this->showProfiles(isset($this->lite) ? false : true);
130         }
131
132         $this->end_document($this->format);
133     }
134
135     /**
136      * Get profiles - should get overrrided
137      *
138      * @return array Profiles
139      */
140
141     function getProfiles()
142     {
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 profile in the stream
162      */
163
164     function lastModified()
165     {
166         if (!empty($this->profiles) && (count($this->profiles) > 0)) {
167             return strtotime($this->profiles[0]->created);
168         }
169
170         return null;
171     }
172
173     /**
174      * An entity tag for this action
175      *
176      * Returns an Etag based on the action name, language, user ID, and
177      * timestamps of the first and last profiles in the subscriptions list
178      * There's also an indicator to show whether this action is being called
179      * as /api/statuses/(friends|followers) or /api/(friends|followers)/ids
180      *
181      * @return string etag
182      */
183
184     function etag()
185     {
186         if (!empty($this->profiles) && (count($this->profiles) > 0)) {
187
188             $last = count($this->profiles) - 1;
189
190             return '"' . implode(
191                 ':',
192                 array($this->arg('action'),
193                       common_language(),
194                       $this->user->id,
195                       isset($this->ids_only) ? 'IDs' : 'Profiles',
196                       strtotime($this->profiles[0]->created),
197                       strtotime($this->profiles[$last]->created))
198             )
199             . '"';
200         }
201
202         return null;
203     }
204
205     /**
206      * Show the profiles as Twitter-style useres and statuses
207      *
208      * @param boolean $include_statuses Whether to include the latest status
209      *                                  with each user. Default true.
210      *
211      * @return void
212      */
213
214     function showProfiles($include_statuses = true)
215     {
216         switch ($this->format) {
217         case 'xml':
218             $this->elementStart('users', array('type' => 'array'));
219             foreach ($this->profiles as $profile) {
220                 $this->show_profile(
221                     $profile,
222                     $this->format,
223                     null,
224                     $include_statuses
225                 );
226             }
227             $this->elementEnd('users');
228             break;
229         case 'json':
230             $arrays = array();
231             foreach ($this->profiles as $profile) {
232                 $arrays[] = $this->twitter_user_array(
233                     $profile,
234                     $include_statuses
235                 );
236             }
237             print json_encode($arrays);
238             break;
239         default:
240             $this->clientError(_('Unsupported format.'));
241             break;
242         }
243     }
244
245     /**
246      * Show the IDs of the profiles only. 5000 per page. To support
247      * the 'social graph' methods: /api/(friends|followers)/ids
248      *
249      * @return void
250      */
251
252     function showIds()
253     {
254         switch ($this->format) {
255         case 'xml':
256             $this->elementStart('ids');
257             foreach ($this->profiles as $profile) {
258                 $this->element('id', null, $profile->id);
259             }
260             $this->elementEnd('ids');
261             break;
262         case 'json':
263             $ids = array();
264             foreach ($this->profiles as $profile) {
265                 $ids[] = (int)$profile->id;
266             }
267             print json_encode($ids);
268             break;
269         default:
270             $this->clientError(_('Unsupported format.'));
271             break;
272         }
273     }
274
275 }