]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapisearchatom.php
Merge branch 'testing' of git@gitorious.org:statusnet/mainline into testing
[quix0rs-gnu-social.git] / actions / twitapisearchatom.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Action for showing Twitter-like Atom search results
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  Search
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2008-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') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/api.php';
35
36 /**
37  * Action for outputting search results in Twitter compatible Atom
38  * format.
39  *
40  * TODO: abstract Atom stuff into a ruseable base class like
41  * RSS10Action.
42  *
43  * @category Search
44  * @package  StatusNet
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  * @see      ApiAction
50  */
51
52 class TwitapisearchatomAction extends ApiAction
53 {
54
55     var $cnt;
56     var $query;
57     var $lang;
58     var $rpp;
59     var $page;
60     var $since_id;
61     var $geocode;
62
63     /**
64      * Constructor
65      *
66      * Just wraps the Action constructor.
67      *
68      * @param string  $output URI to output to, default = stdout
69      * @param boolean $indent Whether to indent output, default true
70      *
71      * @see Action::__construct
72      */
73
74     function __construct($output='php://output', $indent=null)
75     {
76         parent::__construct($output, $indent);
77     }
78
79     /**
80      * Do we need to write to the database?
81      *
82      * @return boolean true
83      */
84
85     function isReadonly()
86     {
87         return true;
88     }
89
90     /**
91      * Read arguments and initialize members
92      *
93      * @param array $args Arguments from $_REQUEST
94      *
95      * @return boolean success
96      *
97      */
98
99     function prepare($args)
100     {
101         parent::prepare($args);
102
103         $this->query = $this->trimmed('q');
104         $this->lang  = $this->trimmed('lang');
105         $this->rpp   = $this->trimmed('rpp');
106
107         if (!$this->rpp) {
108             $this->rpp = 15;
109         }
110
111         if ($this->rpp > 100) {
112             $this->rpp = 100;
113         }
114
115         $this->page = $this->trimmed('page');
116
117         if (!$this->page) {
118             $this->page = 1;
119         }
120
121         // TODO: Suppport since_id -- we need to tweak the backend
122         // Search classes to support it.
123
124         $this->since_id = $this->trimmed('since_id');
125         $this->geocode  = $this->trimmed('geocode');
126
127         // TODO: Also, language and geocode
128
129         return true;
130     }
131
132     /**
133      * Handle a request
134      *
135      * @param array $args Arguments from $_REQUEST
136      *
137      * @return void
138      */
139
140     function handle($args)
141     {
142         parent::handle($args);
143         $this->showAtom();
144     }
145
146     /**
147      * Get the notices to output as results. This also sets some class
148      * attrs so we can use them to calculate pagination, and output
149      * since_id and max_id.
150      *
151      * @return array an array of Notice objects sorted in reverse chron
152      */
153
154     function getNotices()
155     {
156         // TODO: Support search operators like from: and to:, boolean, etc.
157
158         $notices = array();
159         $notice = new Notice();
160
161         // lcase it for comparison
162         $q = strtolower($this->query);
163
164         $search_engine = $notice->getSearchEngine('notice');
165         $search_engine->set_sort_mode('chron');
166         $search_engine->limit(($this->page - 1) * $this->rpp,
167             $this->rpp + 1, true);
168         if (false === $search_engine->query($q)) {
169             $this->cnt = 0;
170         } else {
171             $this->cnt = $notice->find();
172         }
173
174         $cnt = 0;
175         $this->max_id = 0;
176
177         if ($this->cnt > 0) {
178             while ($notice->fetch()) {
179
180                 ++$cnt;
181
182                 if (!$this->max_id) {
183                     $this->max_id = $notice->id;
184                 }
185
186                 if ($cnt > $this->rpp) {
187                     break;
188                 }
189
190                 $notices[] = clone($notice);
191             }
192         }
193
194         return $notices;
195     }
196
197     /**
198      * Output search results as an Atom feed
199      *
200      * @return void
201      */
202
203     function showAtom()
204     {
205         $notices = $this->getNotices();
206
207         $this->initAtom();
208         $this->showFeed();
209
210         foreach ($notices as $n) {
211
212             $profile = $n->getProfile();
213
214             // Don't show notices from deleted users
215
216             if (!empty($profile)) {
217                 $this->showEntry($n);
218             }
219         }
220
221         $this->endAtom();
222     }
223
224     /**
225      * Show feed specific Atom elements
226      *
227      * @return void
228      */
229
230     function showFeed()
231     {
232         // TODO: A9 OpenSearch stuff like search.twitter.com?
233
234         $server   = common_config('site', 'server');
235         $sitename = common_config('site', 'name');
236
237         // XXX: Use xmlns:statusnet instead?
238
239         $this->elementStart('feed',
240             array('xmlns' => 'http://www.w3.org/2005/Atom',
241
242                              // XXX: xmlns:twitter causes Atom validation to fail
243                              // It's used for the source attr on notices
244
245                              'xmlns:twitter' => 'http://api.twitter.com/',
246                              'xml:lang' => 'en-US')); // XXX Other locales ?
247
248         $taguribase = common_config('integration', 'taguri');
249         $this->element('id', null, "tag:$taguribase:search/$server");
250
251         $site_uri = common_path(false);
252
253         $search_uri = $site_uri . 'api/search.atom?q=' . urlencode($this->query);
254
255         if ($this->rpp != 15) {
256             $search_uri .= '&rpp=' . $this->rpp;
257         }
258
259         // FIXME: this alternate link is not quite right because our
260         // web-based notice search doesn't support a rpp (responses per
261         // page) param yet
262
263         $this->element('link', array('type' => 'text/html',
264                                      'rel'  => 'alternate',
265                                      'href' => $site_uri . 'search/notice?q=' .
266                                         urlencode($this->query)));
267
268         // self link
269
270         $self_uri = $search_uri;
271         $self_uri .= ($this->page > 1) ? '&page=' . $this->page : '';
272
273         $this->element('link', array('type' => 'application/atom+xml',
274                                      'rel'  => 'self',
275                                      'href' => $self_uri));
276
277         $this->element('title', null, "$this->query - $sitename Search");
278         $this->element('updated', null, common_date_iso8601('now'));
279
280         // XXX: The below "rel" links are not valid Atom, but it's what
281         // Twitter does...
282
283         // refresh link
284
285         $refresh_uri = $search_uri . "&since_id=" . $this->max_id;
286
287         $this->element('link', array('type' => 'application/atom+xml',
288                                      'rel'  => 'refresh',
289                                      'href' => $refresh_uri));
290
291         // pagination links
292
293         if ($this->cnt > $this->rpp) {
294
295             $next_uri = $search_uri . "&max_id=" . $this->max_id .
296                 '&page=' . ($this->page + 1);
297
298             $this->element('link', array('type' => 'application/atom+xml',
299                                          'rel'  => 'next',
300                                          'href' => $next_uri));
301         }
302
303         if ($this->page > 1) {
304
305             $previous_uri = $search_uri . "&max_id=" . $this->max_id .
306                 '&page=' . ($this->page - 1);
307
308             $this->element('link', array('type' => 'application/atom+xml',
309                                          'rel'  => 'previous',
310                                          'href' => $previous_uri));
311         }
312
313     }
314
315     /**
316      * Build an Atom entry similar to search.twitter.com's based on
317      * a given notice
318      *
319      * @param Notice $notice the notice to use
320      *
321      * @return void
322      */
323
324     function showEntry($notice)
325     {
326         $server  = common_config('site', 'server');
327         $profile = $notice->getProfile();
328         $nurl    = common_local_url('shownotice', array('notice' => $notice->id));
329
330         $this->elementStart('entry');
331
332         $taguribase = common_config('integration', 'taguri');
333
334         $this->element('id', null, "tag:$taguribase:$notice->id");
335         $this->element('published', null, common_date_w3dtf($notice->created));
336         $this->element('link', array('type' => 'text/html',
337                                      'rel'  => 'alternate',
338                                      'href' => $nurl));
339         $this->element('title', null, common_xml_safe_str(trim($notice->content)));
340         $this->element('content', array('type' => 'html'), $notice->rendered);
341         $this->element('updated', null, common_date_w3dtf($notice->created));
342         $this->element('link', array('type' => 'image/png',
343                                      // XXX: Twitter uses rel="image" (not valid)
344                                      'rel' => 'related',
345                                      'href' => $profile->avatarUrl()));
346
347         // TODO: Here is where we'd put in a link to an atom feed for threads
348
349         $this->element("twitter:source", null,
350             htmlentities($this->sourceLink($notice->source)));
351
352         $this->elementStart('author');
353
354         $name = $profile->nickname;
355
356         if ($profile->fullname) {
357             $name .= ' (' . $profile->fullname . ')';
358         }
359
360         $this->element('name', null, $name);
361         $this->element('uri', null, common_profile_uri($profile));
362         $this->elementEnd('author');
363
364         $this->elementEnd('entry');
365     }
366
367     /**
368      * Initialize the Atom output, send headers
369      *
370      * @return void
371      */
372
373     function initAtom()
374     {
375         header('Content-Type: application/atom+xml; charset=utf-8');
376         $this->startXml();
377     }
378
379     /**
380      * End the Atom feed
381      *
382      * @return void
383      */
384
385     function endAtom()
386     {
387         $this->elementEnd('feed');
388     }
389
390 }