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