]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apisearchjson.php
Merge commit 'refs/merge-requests/2227' of git://gitorious.org/statusnet/mainline...
[quix0rs-gnu-social.git] / actions / apisearchjson.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-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 require_once INSTALLDIR.'/lib/jsonsearchresultslist.php';
36
37 /**
38  * Action handler for Twitter-compatible API search
39  *
40  * @category Search
41  * @package  StatusNet
42  * @author   Zach Copley <zach@status.net>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://status.net/
45  * @see      ApiAction
46  */
47 class ApiSearchJSONAction extends ApiPrivateAuthAction
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     function prepare($args)
65     {
66         parent::prepare($args);
67
68         $this->query = $this->trimmed('q');
69         $this->lang  = $this->trimmed('lang');
70         $this->rpp   = $this->trimmed('rpp');
71
72         if (!$this->rpp) {
73             $this->rpp = 15;
74         }
75
76         if ($this->rpp > 100) {
77             $this->rpp = 100;
78         }
79
80         $this->page = $this->trimmed('page');
81
82         if (!$this->page) {
83             $this->page = 1;
84         }
85
86         // TODO: Suppport max_id -- we need to tweak the backend
87         // Search classes to support it.
88
89         $this->since_id = $this->trimmed('since_id');
90         $this->geocode  = $this->trimmed('geocode');
91
92         return true;
93     }
94
95     /**
96      * Handle a request
97      *
98      * @param array $args Arguments from $_REQUEST
99      *
100      * @return void
101      */
102     function handle($args)
103     {
104         parent::handle($args);
105         $this->showResults();
106     }
107
108     /**
109      * Show search results
110      *
111      * @return void
112      */
113     function showResults()
114     {
115         // TODO: Support search operators like from: and to:, boolean, etc.
116
117         $notice = new Notice();
118
119         // lcase it for comparison
120         $q = strtolower($this->query);
121
122         $search_engine = $notice->getSearchEngine('notice');
123         $search_engine->set_sort_mode('chron');
124         $search_engine->limit(($this->page - 1) * $this->rpp, $this->rpp + 1, true);
125         if (false === $search_engine->query($q)) {
126             $cnt = 0;
127         } else {
128             $cnt = $notice->find();
129         }
130
131         // TODO: max_id, lang, geocode
132
133         $results = new JSONSearchResultsList($notice, $q, $this->rpp, $this->page, $this->since_id);
134
135         $this->initDocument('json');
136         $results->show();
137         $this->endDocument('json');
138     }
139
140     /**
141      * Do we need to write to the database?
142      *
143      * @return boolean true
144      */
145     function isReadOnly($args)
146     {
147         return true;
148     }
149 }