]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapisearchjson.php
Merge branch 'testing' of gitorious.org:statusnet/mainline into 0.9.x
[quix0rs-gnu-social.git] / actions / twitapisearchjson.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Action for showing Twitter-like JSON 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/jsonsearchresultslist.php';
35
36 /**
37  * Action handler for Twitter-compatible API search
38  *
39  * @category Search
40  * @package  StatusNet
41  * @author   Zach Copley <zach@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  * @see      ApiAction
45  */
46
47 class TwitapisearchjsonAction extends ApiAction
48 {
49     var $query;
50     var $lang;
51     var $rpp;
52     var $page;
53     var $since_id;
54     var $limit;
55     var $geocode;
56
57     /**
58      * Initialization.
59      *
60      * @param array $args Web and URL arguments
61      *
62      * @return boolean true if nothing goes wrong
63      */
64
65     function prepare($args)
66     {
67         parent::prepare($args);
68
69         $this->query = $this->trimmed('q');
70         $this->lang  = $this->trimmed('lang');
71         $this->rpp   = $this->trimmed('rpp');
72
73         if (!$this->rpp) {
74             $this->rpp = 15;
75         }
76
77         if ($this->rpp > 100) {
78             $this->rpp = 100;
79         }
80
81         $this->page = $this->trimmed('page');
82
83         if (!$this->page) {
84             $this->page = 1;
85         }
86
87         $this->since_id = $this->trimmed('since_id');
88         $this->geocode  = $this->trimmed('geocode');
89
90         return true;
91     }
92
93     /**
94      * Handle a request
95      *
96      * @param array $args Arguments from $_REQUEST
97      *
98      * @return void
99      */
100
101     function handle($args)
102     {
103         parent::handle($args);
104         $this->showResults();
105     }
106
107     /**
108      * Show search results
109      *
110      * @return void
111      */
112
113     function showResults()
114     {
115
116         // TODO: Support search operators like from: and to:, boolean, etc.
117
118         $notice = new Notice();
119
120         // lcase it for comparison
121         $q = strtolower($this->query);
122
123         $search_engine = $notice->getSearchEngine('notice');
124         $search_engine->set_sort_mode('chron');
125         $search_engine->limit(($this->page - 1) * $this->rpp, $this->rpp + 1, true);
126         if (false === $search_engine->query($q)) {
127             $cnt = 0;
128         } else {
129             $cnt = $notice->find();
130         }
131
132         // TODO: since_id, lang, geocode
133
134         $results = new JSONSearchResultsList($notice, $q, $this->rpp, $this->page);
135
136         $this->initDocument('json');
137         $results->show();
138         $this->endDocument('json');
139     }
140
141     /**
142      * Do we need to write to the database?
143      *
144      * @return boolean true
145      */
146
147     function isReadOnly($args)
148     {
149         return true;
150     }
151 }