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