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