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