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
51 function prepare($args)
53 parent::prepare($args);
55 common_set_returnto($this->selfUrl());
63 * @return string instruction text
65 function getInstructions()
67 // TRANS: Instructions for Notice search page.
68 // TRANS: %%site.name%% is the name of the StatusNet site.
69 return _('Search for notices on %%site.name%% by their contents. Separate search terms by spaces; they must be 3 characters or more.');
75 * @return string title
79 // TRANS: Title of the page where users can search for notices.
80 return _('Text search');
85 $q = $this->trimmed('q');
91 return array(new Feed(Feed::RSS1, common_local_url('noticesearchrss',
93 // TRANS: Test in RSS notice search.
94 // TRANS: %1$s is the query, %2$s is the StatusNet site name.
95 sprintf(_('Search results for "%1$s" on %2$s'),
96 $q, common_config('site', 'name'))));
102 * @param string $q search query
103 * @param integer $page page number
107 function showResults($q, $page)
109 $notice = new Notice();
111 $search_engine = $notice->getSearchEngine('notice');
112 $search_engine->set_sort_mode('chron');
113 // Ask for an extra to see if there's more.
114 $search_engine->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
115 if (false === $search_engine->query($q)) {
118 $cnt = $notice->find();
121 // TRANS: Text for notice search results is the query had no results.
122 $this->element('p', 'error', _('No results.'));
124 $this->searchSuggestions($q);
125 if (common_logged_in()) {
126 // TRANS: Text for logged in users making a query for notices without results.
127 // TRANS: This message contains a Markdown link.
128 $message = sprintf(_('Be the first to [post on this topic](%%%%action.newnotice%%%%?status_textarea=%s)!'), urlencode($q));
131 // TRANS: Text for not logged in users making a query for notices without results.
132 // TRANS: This message contains Markdown links.
133 $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));
136 $this->elementStart('div', 'guide');
137 $this->raw(common_markup_to_html($message));
138 $this->elementEnd('div');
141 $terms = preg_split('/[\s,]+/', $q);
142 $nl = new SearchNoticeList($notice, $this, $terms);
144 $this->pagination($page > 1, $cnt > NOTICES_PER_PAGE,
145 $page, 'noticesearch', array('q' => $q));
148 function showScripts()
150 parent::showScripts();
151 $this->autofocus('q');
155 class SearchNoticeList extends NoticeList {
156 function __construct($notice, $out=null, $terms)
158 parent::__construct($notice, $out);
159 $this->terms = $terms;
162 function newListItem($notice)
164 return new SearchNoticeListItem($notice, $this->out, $this->terms);
168 class SearchNoticeListItem extends NoticeListItem {
169 function __construct($notice, $out=null, $terms)
171 parent::__construct($notice, $out);
172 $this->terms = $terms;
175 function showContent()
177 // FIXME: URL, image, video, audio
178 $this->out->elementStart('p', array('class' => 'entry-content'));
179 if ($this->notice->rendered) {
180 $this->out->raw($this->highlight($this->notice->rendered, $this->terms));
182 // XXX: may be some uncooked notices in the DB,
183 // we cook them right now. This should probably disappear in future
184 // versions (>> 0.4.x)
185 $this->out->raw($this->highlight(common_render_content($this->notice->content, $this->notice), $this->terms));
187 $this->out->elementEnd('p');
192 * Highlist query terms
194 * @param string $text notice text
195 * @param array $terms terms to highlight
199 function highlight($text, $terms)
201 /* Highligh search terms */
202 $options = implode('|', array_map('preg_quote', array_map('htmlspecialchars', $terms),
203 array_fill(0, sizeof($terms), '/')));
204 $pattern = "/($options)/i";
207 /* Divide up into text (highlight me) and tags (don't touch) */
208 $chunks = preg_split('/(<[^>]+>)/', $text, 0, PREG_SPLIT_DELIM_CAPTURE);
209 foreach ($chunks as $i => $chunk) {
211 // odd: delimiter (tag)
214 // even: freetext between tags
215 $result .= preg_replace($pattern, '<strong>\\1</strong>', $chunk);