]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apisubscriptions.php
Merge branch 'master' of git@gitorious.org:statusnet/mainline
[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_language(),
185                       $this->user->id,
186                       isset($this->ids_only) ? 'IDs' : 'Profiles',
187                       strtotime($this->profiles[0]->created),
188                       strtotime($this->profiles[$last]->created))
189             )
190             . '"';
191         }
192
193         return null;
194     }
195
196     /**
197      * Show the profiles as Twitter-style useres and statuses
198      *
199      * @param boolean $include_statuses Whether to include the latest status
200      *                                  with each user. Default true.
201      *
202      * @return void
203      */
204
205     function showProfiles($include_statuses = true)
206     {
207         switch ($this->format) {
208         case 'xml':
209             $this->elementStart('users', array('type' => 'array'));
210             foreach ($this->profiles as $profile) {
211                 $this->showProfile(
212                     $profile,
213                     $this->format,
214                     null,
215                     $include_statuses
216                 );
217             }
218             $this->elementEnd('users');
219             break;
220         case 'json':
221             $arrays = array();
222             foreach ($this->profiles as $profile) {
223                 $arrays[] = $this->twitterUserArray(
224                     $profile,
225                     $include_statuses
226                 );
227             }
228             print json_encode($arrays);
229             break;
230         default:
231             $this->clientError(_('Unsupported format.'));
232             break;
233         }
234     }
235
236     /**
237      * Show the IDs of the profiles only. 5000 per page. To support
238      * the 'social graph' methods: /api/(friends|followers)/ids
239      *
240      * @return void
241      */
242
243     function showIds()
244     {
245         switch ($this->format) {
246         case 'xml':
247             $this->elementStart('ids');
248             foreach ($this->profiles as $profile) {
249                 $this->element('id', null, $profile->id);
250             }
251             $this->elementEnd('ids');
252             break;
253         case 'json':
254             $ids = array();
255             foreach ($this->profiles as $profile) {
256                 $ids[] = (int)$profile->id;
257             }
258             print json_encode($ids);
259             break;
260         default:
261             $this->clientError(_('Unsupported format.'));
262             break;
263         }
264     }
265
266 }