3 * Notice search action class.
9 * @author Evan Prodromou <evan@status.net>
10 * @author Robin Millette <millette@status.net>
11 * @author Sarven Capadisli <csarven@status.net>
12 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
13 * @link http://status.net/
15 * StatusNet - the distributed open-source microblogging tool
16 * Copyright (C) 2008, 2009, StatusNet, Inc.
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU Affero General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU Affero General Public License for more details.
28 * You should have received a copy of the GNU Affero General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 if (!defined('STATUSNET') && !defined('LACONICA')) {
36 require_once INSTALLDIR.'/lib/searchaction.php';
39 * Notice search action class.
43 * @author Evan Prodromou <evan@status.net>
44 * @author Robin Millette <millette@status.net>
45 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
46 * @link http://status.net/
47 * @todo common parent for people and content search?
49 class NoticesearchAction extends SearchAction
53 function prepare(array $args = array())
55 parent::prepare($args);
57 $this->q = $this->trimmed('q');
59 // FIXME: very dependent on tag format
60 if (preg_match('/^#([\pL\pN_\-\.]{1,64})/ue', $this->q)) {
61 common_redirect(common_local_url('tag',
62 array('tag' => common_canonical_tag(substr($this->q, 1)))),
66 if (!empty($this->q)) {
68 $stream = new SearchNoticeStream($this->q, $this->scoped);
69 $page = $this->trimmed('page');
77 $this->notice = $stream->getNotices((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
80 common_set_returnto($this->selfUrl());
88 * @return string instruction text
90 function getInstructions()
92 // TRANS: Instructions for Notice search page.
93 // TRANS: %%site.name%% is the name of the StatusNet site.
94 return _('Search for notices on %%site.name%% by their contents. Separate search terms by spaces; they must be 3 characters or more.');
100 * @return string title
104 // TRANS: Title of the page where users can search for notices.
105 return _('Text search');
110 $q = $this->trimmed('q');
116 return array(new Feed(Feed::RSS1, common_local_url('noticesearchrss',
118 // TRANS: Test in RSS notice search.
119 // TRANS: %1$s is the query, %2$s is the StatusNet site name.
120 sprintf(_('Search results for "%1$s" on %2$s'),
121 $q, common_config('site', 'name'))));
127 * @param string $q search query
128 * @param integer $page page number
132 function showResults($q, $page)
134 if (Event::handle('StartNoticeSearchShowResults', array($this, $q, $this->notice))) {
135 if ($this->notice->N === 0) {
136 $this->showEmptyResults($q, $page);
138 $terms = preg_split('/[\s,]+/', $q);
139 $nl = new SearchNoticeList($this->notice, $this, $terms);
141 $this->pagination($page > 1,
142 $cnt > NOTICES_PER_PAGE,
147 Event::handle('EndNoticeSearchShowResults', array($this, $q, $this->notice));
151 function showEmptyResults($q, $page)
153 // TRANS: Text for notice search results is the query had no results.
154 $this->element('p', 'error', _('No results.'));
156 $this->searchSuggestions($q);
157 if (common_logged_in()) {
158 // TRANS: Text for logged in users making a query for notices without results.
159 // TRANS: This message contains a Markdown link.
160 $message = sprintf(_('Be the first to [post on this topic](%%%%action.newnotice%%%%?status_textarea=%s)!'), urlencode($q));
163 // TRANS: Text for not logged in users making a query for notices without results.
164 // TRANS: This message contains Markdown links.
165 $message = sprintf(_('Why not [register an account](%%%%action.register%%%%) and be the first to [post on this topic](%%%%action.newnotice%%%%?status_textarea=%s)!'), urlencode($q));
168 $this->elementStart('div', 'guide');
169 $this->raw(common_markup_to_html($message));
170 $this->elementEnd('div');
174 function showScripts()
176 parent::showScripts();
177 $this->autofocus('q');
181 class SearchNoticeList extends NoticeList {
182 function __construct($notice, $out=null, $terms)
184 parent::__construct($notice, $out);
185 $this->terms = $terms;
188 function newListItem($notice)
190 return new SearchNoticeListItem($notice, $this->out, $this->terms);
194 class SearchNoticeListItem extends NoticeListItem {
195 function __construct($notice, $out=null, $terms)
197 parent::__construct($notice, $out);
198 $this->terms = $terms;
201 function showContent()
203 // FIXME: URL, image, video, audio
204 $this->out->elementStart('p', array('class' => 'e-content'));
205 $this->out->raw($this->highlight($this->notice->getRendered(), $this->terms));
206 $this->out->elementEnd('p');
211 * Highlist query terms
213 * @param string $text notice text
214 * @param array $terms terms to highlight
218 function highlight($text, $terms)
220 /* Highligh search terms */
221 $options = implode('|', array_map('preg_quote', array_map('htmlspecialchars', $terms),
222 array_fill(0, sizeof($terms), '/')));
223 $pattern = "/($options)/i";
226 /* Divide up into text (highlight me) and tags (don't touch) */
227 $chunks = preg_split('/(<[^>]+>)/', $text, 0, PREG_SPLIT_DELIM_CAPTURE);
228 foreach ($chunks as $i => $chunk) {
230 // odd: delimiter (tag)
233 // even: freetext between tags
234 $result .= preg_replace($pattern, '<strong>\\1</strong>', $chunk);