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