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($args)
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 common_set_returnto($this->selfUrl());
74 * @return string instruction text
76 function getInstructions()
78 // TRANS: Instructions for Notice search page.
79 // TRANS: %%site.name%% is the name of the StatusNet site.
80 return _('Search for notices on %%site.name%% by their contents. Separate search terms by spaces; they must be 3 characters or more.');
86 * @return string title
90 // TRANS: Title of the page where users can search for notices.
91 return _('Text search');
96 $q = $this->trimmed('q');
102 return array(new Feed(Feed::RSS1, common_local_url('noticesearchrss',
104 // TRANS: Test in RSS notice search.
105 // TRANS: %1$s is the query, %2$s is the StatusNet site name.
106 sprintf(_('Search results for "%1$s" on %2$s'),
107 $q, common_config('site', 'name'))));
113 * @param string $q search query
114 * @param integer $page page number
118 function showResults($q, $page)
120 $notice = new Notice();
122 $search_engine = $notice->getSearchEngine('notice');
123 $search_engine->set_sort_mode('chron');
124 // Ask for an extra to see if there's more.
125 $search_engine->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
126 if (false === $search_engine->query($q)) {
129 $cnt = $notice->find();
132 // TRANS: Text for notice search results is the query had no results.
133 $this->element('p', 'error', _('No results.'));
135 $this->searchSuggestions($q);
136 if (common_logged_in()) {
137 // TRANS: Text for logged in users making a query for notices without results.
138 // TRANS: This message contains a Markdown link.
139 $message = sprintf(_('Be the first to [post on this topic](%%%%action.newnotice%%%%?status_textarea=%s)!'), urlencode($q));
142 // TRANS: Text for not logged in users making a query for notices without results.
143 // TRANS: This message contains Markdown links.
144 $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));
147 $this->elementStart('div', 'guide');
148 $this->raw(common_markup_to_html($message));
149 $this->elementEnd('div');
152 if (Event::handle('StartNoticeSearchShowResults', array($this, $q, $notice))) {
153 $terms = preg_split('/[\s,]+/', $q);
154 $nl = new SearchNoticeList($notice, $this, $terms);
156 $this->pagination($page > 1, $cnt > NOTICES_PER_PAGE,
157 $page, 'noticesearch', array('q' => $q));
158 Event::handle('EndNoticeSearchShowResults', array($this, $q, $notice));
162 function showScripts()
164 parent::showScripts();
165 $this->autofocus('q');
169 class SearchNoticeList extends NoticeList {
170 function __construct($notice, $out=null, $terms)
172 parent::__construct($notice, $out);
173 $this->terms = $terms;
176 function newListItem($notice)
178 return new SearchNoticeListItem($notice, $this->out, $this->terms);
182 class SearchNoticeListItem extends NoticeListItem {
183 function __construct($notice, $out=null, $terms)
185 parent::__construct($notice, $out);
186 $this->terms = $terms;
189 function showContent()
191 // FIXME: URL, image, video, audio
192 $this->out->elementStart('p', array('class' => 'entry-content'));
193 if ($this->notice->rendered) {
194 $this->out->raw($this->highlight($this->notice->rendered, $this->terms));
196 // XXX: may be some uncooked notices in the DB,
197 // we cook them right now. This should probably disappear in future
198 // versions (>> 0.4.x)
199 $this->out->raw($this->highlight(common_render_content($this->notice->content, $this->notice), $this->terms));
201 $this->out->elementEnd('p');
206 * Highlist query terms
208 * @param string $text notice text
209 * @param array $terms terms to highlight
213 function highlight($text, $terms)
215 /* Highligh search terms */
216 $options = implode('|', array_map('preg_quote', array_map('htmlspecialchars', $terms),
217 array_fill(0, sizeof($terms), '/')));
218 $pattern = "/($options)/i";
221 /* Divide up into text (highlight me) and tags (don't touch) */
222 $chunks = preg_split('/(<[^>]+>)/', $text, 0, PREG_SPLIT_DELIM_CAPTURE);
223 foreach ($chunks as $i => $chunk) {
225 // odd: delimiter (tag)
228 // even: freetext between tags
229 $result .= preg_replace($pattern, '<strong>\\1</strong>', $chunk);