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