3 * Notice search action class.
9 * @author Evan Prodromou <evan@controlyourself.ca>
10 * @author Robin Millette <millette@controlyourself.ca>
11 * @author Sarven Capadisli <csarven@controlyourself.ca>
12 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
13 * @link http://laconi.ca/
15 * Laconica - a distributed open-source microblogging tool
16 * Copyright (C) 2008, Controlez-Vous, 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('LACONICA')) {
36 require_once INSTALLDIR.'/lib/searchaction.php';
39 * Notice search action class.
43 * @author Evan Prodromou <evan@controlyourself.ca>
44 * @author Robin Millette <millette@controlyourself.ca>
45 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
46 * @link http://laconi.ca/
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');
84 * @param string $q search query
85 * @param integer $page page number
89 function showResults($q, $page)
91 $notice = new Notice();
93 $search_engine = $notice->getSearchEngine('identica_notices');
94 $search_engine->set_sort_mode('chron');
95 // Ask for an extra to see if there's more.
96 $search_engine->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
97 if (false === $search_engine->query($q)) {
100 $cnt = $notice->find();
103 $terms = preg_split('/[\s,]+/', $q);
104 $this->elementStart('ul', array('class' => 'notices'));
105 for ($i = 0; $i < min($cnt, NOTICES_PER_PAGE); $i++) {
106 if ($notice->fetch()) {
107 $this->showNotice($notice, $terms);
113 $this->elementEnd('ul');
115 $this->element('p', 'error', _('No results'));
118 $this->pagination($page > 1, $cnt > NOTICES_PER_PAGE,
119 $page, 'noticesearch', array('q' => $q));
125 * @param array $arr array containing the query
132 $q = $this->trimmed('q');
134 $this->element('link', array('rel' => 'alternate',
135 'href' => common_local_url('noticesearchrss',
137 'type' => 'application/rss+xml',
138 'title' => _('Search Stream Feed')));
145 * @param class $notice notice
146 * @param array $terms terms to highlight
150 * @todo refactor and combine with StreamAction::showNotice()
152 function showNotice($notice, $terms)
154 $profile = $notice->getProfile();
156 common_log_db_error($notice, 'SELECT', __FILE__);
157 $this->serverError(_('Notice without matching profile'));
161 $this->elementStart('li', array('class' => 'hentry notice',
162 'id' => 'notice-' . $notice->id));
164 $this->elementStart('div', 'entry-title');
165 $this->elementStart('span', 'vcard author');
166 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
167 $this->elementStart('a', array('href' => $profile->profileurl,
169 $this->element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE),
170 'class' => 'avatar photo',
171 'width' => AVATAR_STREAM_SIZE,
172 'height' => AVATAR_STREAM_SIZE,
174 ($profile->fullname) ? $profile->fullname :
175 $profile->nickname));
176 $this->element('span', 'nickname fn', $profile->nickname);
177 $this->elementEnd('a');
178 $this->elementEnd('span');
180 // FIXME: URL, image, video, audio
181 $this->elementStart('p', array('class' => 'entry-content'));
182 if ($notice->rendered) {
183 $this->raw($this->highlight($notice->rendered, $terms));
185 // XXX: may be some uncooked notices in the DB,
186 // we cook them right now. This should probably disappear in future
187 // versions (>> 0.4.x)
188 $this->raw($this->highlight(common_render_content($notice->content, $notice), $terms));
190 $this->elementEnd('p');
191 $this->elementEnd('div');
193 $noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
194 $this->elementStart('div', 'entry-content');
195 $this->elementStart('dl', 'timestamp');
196 $this->element('dt', null, _('Published'));
197 $this->elementStart('dd', null);
198 $this->elementStart('a', array('rel' => 'bookmark',
199 'href' => $noticeurl));
200 $dt = common_date_iso8601($notice->created);
201 $this->element('abbr', array('class' => 'published',
203 common_date_string($notice->created));
204 $this->elementEnd('a');
205 $this->elementEnd('dd');
206 $this->elementEnd('dl');
208 if ($notice->reply_to) {
209 $replyurl = common_local_url('shownotice',
210 array('notice' => $this->notice->reply_to));
211 $this->elementStart('dl', 'response');
212 $this->element('dt', null, _('To'));
213 $this->elementStart('dd');
214 $this->element('a', array('href' => $replyurl,
215 'rel' => 'in-reply-to'),
217 $this->elementEnd('dd');
218 $this->elementEnd('dl');
220 $this->elementEnd('div');
222 $this->elementStart('div', 'notice-options');
224 $reply_url = common_local_url('newnotice',
225 array('replyto' => $profile->nickname));
227 $this->elementStart('dl', 'notice_reply');
228 $this->element('dt', null, _('Reply to this notice'));
229 $this->elementStart('dd');
230 $this->elementStart('a', array('href' => $reply_url,
231 'title' => _('Reply to this notice')));
232 $this->text(_('Reply'));
233 $this->element('span', 'notice_id', $notice->id);
234 $this->elementEnd('a');
235 $this->elementEnd('dd');
236 $this->elementEnd('dl');
237 $this->elementEnd('div');
238 $this->elementEnd('li');
242 * Highlist query terms
244 * @param string $text notice text
245 * @param array $terms terms to highlight
249 function highlight($text, $terms)
251 /* Highligh serach terms */
252 $pattern = '/('.implode('|', array_map('htmlspecialchars', $terms)).')/i';
253 $result = preg_replace($pattern, '<strong>\\1</strong>', $text);
255 /* Remove highlighting from inside links, loop incase multiple highlights in links */
256 $pattern = '/(href="[^"]*)<strong>('.implode('|', array_map('htmlspecialchars', $terms)).')<\/strong>([^"]*")/iU';
258 $result = preg_replace($pattern, '\\1\\2\\3', $result, -1, $count);
263 function isReadOnly()