]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapisearchjson.php
57a5fb0ca17fd0189819397494444b54ca0a139e
[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@controlyourself.ca>
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://laconi.ca/
28  */
29
30 if (!defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/twitterapi.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@controlyourself.ca>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://laconi.ca/
45  * @see      TwitterapiAction
46  */
47
48 class TwitapisearchjsonAction extends TwitterapiAction
49 {
50     var $query;
51     var $lang;
52     var $rpp;
53     var $page;
54     var $since_id;
55     var $limit;
56     var $geocode;
57
58     /**
59      * Initialization.
60      *
61      * @param array $args Web and URL arguments
62      *
63      * @return boolean true if nothing goes wrong
64      */
65
66     function prepare($args)
67     {
68         parent::prepare($args);
69
70         $this->query = $this->trimmed('q');
71         $this->lang  = $this->trimmed('lang');
72         $this->rpp   = $this->trimmed('rpp');
73
74         if (!$this->rpp) {
75             $this->rpp = 15;
76         }
77
78         if ($this->rpp > 100) {
79             $this->rpp = 100;
80         }
81
82         $this->page = $this->trimmed('page');
83
84         if (!$this->page) {
85             $this->page = 1;
86         }
87
88         $this->since_id = $this->trimmed('since_id');
89         $this->geocode  = $this->trimmed('geocode');
90
91         return true;
92     }
93
94     /**
95      * Handle a request
96      *
97      * @param array $args Arguments from $_REQUEST
98      *
99      * @return void
100      */
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
114     function showResults()
115     {
116
117         // TODO: Support search operators like from: and to:, boolean, etc.
118
119         $notice = new Notice();
120
121         // lcase it for comparison
122         $q = strtolower($this->query);
123
124         $search_engine = $notice->getSearchEngine('identica_notices');
125         $search_engine->set_sort_mode('chron');
126         $search_engine->limit(($this->page - 1) * $this->rpp, $this->rpp + 1, true);
127         if (false === $search_engine->query($q)) {
128             $cnt = 0;
129         } else {
130             $cnt = $notice->find();
131         }
132
133         // TODO: since_id, lang, geocode
134
135         $results = new JSONSearchResultsList($notice, $q, $this->rpp, $this->page);
136
137         $this->init_document('json');
138         $results->show();
139         $this->end_document('json');
140     }
141
142     /**
143      * Do we need to write to the database?
144      *
145      * @return boolean true
146      */
147
148     function isReadOnly($args)
149     {
150         return true;
151     }
152 }