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