3 * StatusNet, the distributed open-source microblogging tool
5 * Action for showing Twitter-like JSON search results
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.
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.
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/>.
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/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
34 require_once INSTALLDIR.'/lib/apiprivateauth.php';
35 require_once INSTALLDIR.'/lib/jsonsearchresultslist.php';
38 * Action handler for Twitter-compatible API search
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/
47 class ApiSearchJSONAction extends ApiPrivateAuthAction
60 * @param array $args Web and URL arguments
62 * @return boolean true if nothing goes wrong
64 function prepare($args)
66 parent::prepare($args);
68 $this->query = $this->trimmed('q');
69 $this->lang = $this->trimmed('lang');
70 $this->rpp = $this->trimmed('rpp');
76 if ($this->rpp > 100) {
80 $this->page = $this->trimmed('page');
86 // TODO: Suppport max_id -- we need to tweak the backend
87 // Search classes to support it.
89 $this->since_id = $this->trimmed('since_id');
90 $this->geocode = $this->trimmed('geocode');
92 if (!empty($this->auth_user)) {
93 $this->auth_profile = $this->auth_user->getProfile();
95 $this->auth_profile = null;
104 * @param array $args Arguments from $_REQUEST
108 function handle($args)
110 parent::handle($args);
111 $this->showResults();
115 * Show search results
119 function showResults()
121 $q = strtolower($this->query);
123 // TODO: Support search operators like from: and to:, boolean, etc.
125 if (preg_match('/^#([\pL\pN_\-\.]{1,64})$/ue', $q)) {
126 $stream = new TagNoticeStream(substr($q, 1), $this->auth_profile);
127 } else if ($this->isAnURL($q)) {
128 $canon = File_redirection::_canonUrl($q);
129 $file = File::getKV('url', $canon);
131 $stream = new FileNoticeStream($file, $this->auth_profile);
134 $stream = new SearchNoticeStream($q, $this->auth_profile);
137 if (empty($stream)) {
138 // XXX: This is hackish, but need some simple way to say "There's no results"
139 $notice = new ArrayWrapper(array());
141 $notice = $stream->getNotices(($this->page - 1) * $this->rpp, $this->rpp + 1);
144 // TODO: max_id, lang, geocode
146 $results = new JSONSearchResultsList($notice, $q, $this->rpp, $this->page, $this->since_id);
148 $this->initDocument('json');
150 $this->endDocument('json');
153 function isAnURL($q) {
155 '(?:^|[\s\<\>\(\)\[\]\{\}\\\'\\\";]+)(?![\@\!\#])'.
158 '(?:'. //Known protocols
160 '(?:(?:https?|ftps?|mms|rtsp|gopher|news|nntp|telnet|wais|file|prospero|webcal|irc)://)'.
162 '(?:(?:mailto|aim|tel|xmpp):)'.
164 '(?:[\pN\pL\-\_\+\%\~]+(?::[\pN\pL\-\_\+\%\~]+)?\@)?'. //user:pass@
167 '\[[\pN\pL\-\_\:\.]+(?<![\.\:])\]'. //[dns]
169 '[\pN\pL\-\_\:\.]+(?<![\.\:])'. //dns
173 '|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'. //IPv4
175 '\[?(?:(?:(?:[0-9A-Fa-f]{1,4}:){7}(?:(?:[0-9A-Fa-f]{1,4})|:))|(?:(?:[0-9A-Fa-f]{1,4}:){6}(?::|(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})|(?::[0-9A-Fa-f]{1,4})))|(?:(?:[0-9A-Fa-f]{1,4}:){5}(?:(?::(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1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+(?:\.[\pN\pL\-\_]+)*\.'.
179 //tld list from http://data.iana.org/TLD/tlds-alpha-by-domain.txt, also added local, loc, and onion
180 '(?:AC|AD|AE|AERO|AF|AG|AI|AL|AM|AN|AO|AQ|AR|ARPA|AS|ASIA|AT|AU|AW|AX|AZ|BA|BB|BD|BE|BF|BG|BH|BI|BIZ|BJ|BM|BN|BO|BR|BS|BT|BV|BW|BY|BZ|CA|CAT|CC|CD|CF|CG|CH|CI|CK|CL|CM|CN|CO|COM|COOP|CR|CU|CV|CX|CY|CZ|DE|DJ|DK|DM|DO|DZ|EC|EDU|EE|EG|ER|ES|ET|EU|FI|FJ|FK|FM|FO|FR|GA|GB|GD|GE|GF|GG|GH|GI|GL|GM|GN|GOV|GP|GQ|GR|GS|GT|GU|GW|GY|HK|HM|HN|HR|HT|HU|ID|IE|IL|IM|IN|INFO|INT|IO|IQ|IR|IS|IT|JE|JM|JO|JOBS|JP|KE|KG|KH|KI|KM|KN|KP|KR|KW|KY|KZ|LA|LB|LC|LI|LK|LR|LS|LT|LU|LV|LY|MA|MC|MD|ME|MG|MH|MIL|MK|ML|MM|MN|MO|MOBI|MP|MQ|MR|MS|MT|MU|MUSEUM|MV|MW|MX|MY|MZ|NA|NAME|NC|NE|NET|NF|NG|NI|NL|NO|NP|NR|NU|NZ|OM|ORG|PA|PE|PF|PG|PH|PK|PL|PM|PN|PR|PRO|PS|PT|PW|PY|QA|RE|RO|RS|RU|RW|SA|SB|SC|SD|SE|SG|SH|SI|SJ|SK|SL|SM|SN|SO|SR|ST|SU|SV|SY|SZ|TC|TD|TEL|TF|TG|TH|TJ|TK|TL|TM|TN|TO|TP|TR|TRAVEL|TT|TV|TW|TZ|UA|UG|UK|US|UY|UZ|VA|VC|VE|VG|VI|VN|VU|WF|WS|XN--0ZWM56D|测试|XN--11B5BS3A9AJ6G|परीक्षा|XN--80AKHBYKNJ4F|испытание|XN--9T4B11YI5A|테스트|XN--DEBA0AD|טעסט|XN--G6W251D|測試|XN--HGBK6AJ7F53BBA|آزمایشی|XN--HLCJ6AYA9ESC7A|பரிட்சை|XN--JXALPDLP|δοκιμή|XN--KGBECHTV|إختبار|XN--ZCKZAH|テスト|YE|YT|YU|ZA|ZM|ZW|local|loc|onion)'.
184 '(?:\:\d+)?'. //:port
185 '(?:/[\pN\pL$\,\!\(\)\.\:\-\_\+\/\=\&\;\%\~\*\$\+\'@]*)?'. // /path
186 '(?:\?[\pN\pL\$\,\!\(\)\.\:\-\_\+\/\=\&\;\%\~\*\$\+\'@\/]*)?'. // ?query string
187 '(?:\#[\pN\pL$\,\!\(\)\.\:\-\_\+\/\=\&\;\%\~\*\$\+\'\@/\?\#]*)?'. // #fragment
188 ')(?<![\?\.\,\#\,])'.
191 return preg_match($regex, $q);
195 * Do we need to write to the database?
197 * @return boolean true
199 function isReadOnly($args)