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
52 function prepare($args)
54 parent::prepare($args);
56 common_set_returnto($this->selfUrl());
64 * @return string instruction text
66 function getInstructions()
68 return _('Search for notices on %%site.name%% by their contents. Separate search terms by spaces; they must be 3 characters or more.');
74 * @return string title
78 return _('Text search');
83 $q = $this->trimmed('q');
89 return array(new Feed(Feed::RSS1, common_local_url('noticesearchrss',
91 sprintf(_('Search results for "%1$s" on %2$s'),
92 $q, common_config('site', 'name'))));
98 * @param string $q search query
99 * @param integer $page page number
103 function showResults($q, $page)
105 $notice = new Notice();
107 $search_engine = $notice->getSearchEngine('notice');
108 $search_engine->set_sort_mode('chron');
109 // Ask for an extra to see if there's more.
110 $search_engine->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
111 if (false === $search_engine->query($q)) {
114 $cnt = $notice->find();
117 $this->element('p', 'error', _('No results.'));
119 $this->searchSuggestions($q);
120 if (common_logged_in()) {
121 $message = sprintf(_('Be the first to [post on this topic](%%%%action.newnotice%%%%?status_textarea=%s)!'), urlencode($q));
124 $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));
127 $this->elementStart('div', 'guide');
128 $this->raw(common_markup_to_html($message));
129 $this->elementEnd('div');
132 $terms = preg_split('/[\s,]+/', $q);
133 $nl = new SearchNoticeList($notice, $this, $terms);
135 $this->pagination($page > 1, $cnt > NOTICES_PER_PAGE,
136 $page, 'noticesearch', array('q' => $q));
139 function showScripts()
141 parent::showScripts();
142 $this->autofocus('q');
146 class SearchNoticeList extends NoticeList {
147 function __construct($notice, $out=null, $terms)
149 parent::__construct($notice, $out);
150 $this->terms = $terms;
153 function newListItem($notice)
155 return new SearchNoticeListItem($notice, $this->out, $this->terms);
159 class SearchNoticeListItem extends NoticeListItem {
160 function __construct($notice, $out=null, $terms)
162 parent::__construct($notice, $out);
163 $this->terms = $terms;
166 function showContent()
168 // FIXME: URL, image, video, audio
169 $this->out->elementStart('p', array('class' => 'entry-content'));
170 if ($this->notice->rendered) {
171 $this->out->raw($this->highlight($this->notice->rendered, $this->terms));
173 // XXX: may be some uncooked notices in the DB,
174 // we cook them right now. This should probably disappear in future
175 // versions (>> 0.4.x)
176 $this->out->raw($this->highlight(common_render_content($this->notice->content, $this->notice), $this->terms));
178 $this->out->elementEnd('p');
183 * Highlist query terms
185 * @param string $text notice text
186 * @param array $terms terms to highlight
190 function highlight($text, $terms)
192 /* Highligh search terms */
193 $options = implode('|', array_map('preg_quote', array_map('htmlspecialchars', $terms),
194 array_fill(0, sizeof($terms), '/')));
195 $pattern = "/($options)/i";
196 $result = preg_replace($pattern, '<strong>\\1</strong>', $text);
198 /* Remove highlighting from inside links, loop incase multiple highlights in links */
199 $pattern = '/(\w+="[^"]*)<strong>('.$options.')<\/strong>([^"]*")/iU';
201 $result = preg_replace($pattern, '\\1\\2\\3', $result, -1, $count);