3 * Notice search action class.
9 * @author Evan Prodromou <evan@controlyourself.ca>
10 * @author Robin Millette <millette@controlyourself.ca>
11 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
12 * @link http://laconi.ca/
14 * Laconica - a distributed open-source microblogging tool
15 * Copyright (C) 2008, Controlez-Vous, Inc.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU Affero General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU Affero General Public License for more details.
27 * You should have received a copy of the GNU Affero General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 if (!defined('LACONICA')) {
35 require_once INSTALLDIR.'/lib/searchaction.php';
38 * Notice search action class.
42 * @author Evan Prodromou <evan@controlyourself.ca>
43 * @author Robin Millette <millette@controlyourself.ca>
44 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
45 * @link http://laconi.ca/
46 * @todo common parent for people and content search?
48 class NoticesearchAction extends SearchAction
53 * @return string instruction text
55 function getInstructions()
57 return _('Search for notices on %%site.name%% by their contents. Separate search terms by spaces; they must be 3 characters or more.');
63 * @return string title
67 return _('Text search');
73 * @param string $q search query
74 * @param integer $page page number
78 function showResults($q, $page)
80 $notice = new Notice();
82 $search_engine = $notice->getSearchEngine('identica_notices');
83 $search_engine->set_sort_mode('chron');
84 // Ask for an extra to see if there's more.
85 $search_engine->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
86 if (false === $search_engine->query($q)) {
89 $cnt = $notice->find();
92 $terms = preg_split('/[\s,]+/', $q);
93 $this->elementStart('ul', array('id' => 'notices'));
94 for ($i = 0; $i < min($cnt, NOTICES_PER_PAGE); $i++) {
95 if ($notice->fetch()) {
96 $this->showNotice($notice, $terms);
102 $this->elementEnd('ul');
104 $this->element('p', 'error', _('No results'));
107 $this->pagination($page > 1, $cnt > NOTICES_PER_PAGE,
108 $page, 'noticesearch', array('q' => $q));
114 * @param array $arr array containing the query
121 $q = $this->trimmed('q');
123 $this->element('link', array('rel' => 'alternate',
124 'href' => common_local_url('noticesearchrss',
126 'type' => 'application/rss+xml',
127 'title' => _('Search Stream Feed')));
134 * @param class $notice notice
135 * @param array $terms terms to highlight
139 * @todo refactor and combine with StreamAction::showNotice()
141 function showNotice($notice, $terms)
143 $profile = $notice->getProfile();
145 common_log_db_error($notice, 'SELECT', __FILE__);
146 $this->serverError(_('Notice without matching profile'));
150 $this->elementStart('li', array('class' => 'notice_single',
151 'id' => 'notice-' . $notice->id));
152 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
153 $this->elementStart('a', array('href' => $profile->profileurl));
154 $this->element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE),
155 'class' => 'avatar stream',
156 'width' => AVATAR_STREAM_SIZE,
157 'height' => AVATAR_STREAM_SIZE,
159 ($profile->fullname) ? $profile->fullname :
160 $profile->nickname));
161 $this->elementEnd('a');
162 $this->element('a', array('href' => $profile->profileurl,
163 'class' => 'nickname'),
165 // FIXME: URL, image, video, audio
166 $this->elementStart('p', array('class' => 'content'));
167 if ($notice->rendered) {
168 $this->raw($this->highlight($notice->rendered, $terms));
170 // XXX: may be some uncooked notices in the DB,
171 // we cook them right now. This should probably disappear in future
172 // versions (>> 0.4.x)
173 $this->raw($this->highlight(common_render_content($notice->content, $notice), $terms));
175 $this->elementEnd('p');
176 $noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
177 $this->elementStart('p', 'time');
178 $this->element('a', array('class' => 'permalink',
179 'href' => $noticeurl,
180 'title' => common_exact_date($notice->created)),
181 common_date_string($notice->created));
182 if ($notice->reply_to) {
183 $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
185 $this->element('a', array('class' => 'inreplyto',
186 'href' => $replyurl),
187 _('in reply to...'));
190 $this->elementStart('a',
191 array('href' => common_local_url('newnotice',
192 array('replyto' => $profile->nickname)),
193 'onclick' => 'doreply("'.$profile->nickname.'"); return false',
194 'title' => _('reply'),
195 'class' => 'replybutton'));
196 $this->hidden('posttoken', common_session_token());
198 $this->raw('→');
199 $this->elementEnd('a');
200 $this->elementEnd('p');
201 $this->elementEnd('li');
205 * Highlist query terms
207 * @param string $text notice text
208 * @param array $terms terms to highlight
212 function highlight($text, $terms)
214 /* Highligh serach terms */
215 $pattern = '/('.implode('|', array_map('htmlspecialchars', $terms)).')/i';
216 $result = preg_replace($pattern, '<strong>\\1</strong>', $text);
218 /* Remove highlighting from inside links, loop incase multiple highlights in links */
219 $pattern = '/(href="[^"]*)<strong>('.implode('|', array_map('htmlspecialchars', $terms)).')<\/strong>([^"]*")/iU';
221 $result = preg_replace($pattern, '\\1\\2\\3', $result, -1, $count);