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');
83 $q = $this->trimmed('q');
89 return array(new Feed(Feed::RSS1, common_local_url('noticesearchrss',
91 sprintf(_('Search results for "%s" on %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('identica_notices');
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 $terms = preg_split('/[\s,]+/', $q);
118 $this->elementStart('ul', array('class' => 'notices'));
119 for ($i = 0; $i < min($cnt, NOTICES_PER_PAGE); $i++) {
120 if ($notice->fetch()) {
121 $this->showNotice($notice, $terms);
127 $this->elementEnd('ul');
129 $this->element('p', 'error', _('No results'));
132 $this->pagination($page > 1, $cnt > NOTICES_PER_PAGE,
133 $page, 'noticesearch', array('q' => $q));
139 * @param class $notice notice
140 * @param array $terms terms to highlight
144 * @todo refactor and combine with StreamAction::showNotice()
146 function showNotice($notice, $terms)
148 $profile = $notice->getProfile();
150 common_log_db_error($notice, 'SELECT', __FILE__);
151 $this->serverError(_('Notice without matching profile'));
155 $this->elementStart('li', array('class' => 'hentry notice',
156 'id' => 'notice-' . $notice->id));
158 $this->elementStart('div', 'entry-title');
159 $this->elementStart('span', 'vcard author');
160 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
161 $this->elementStart('a', array('href' => $profile->profileurl,
163 $this->element('img', array('src' => ($avatar) ? $avatar->displayUrl() : Avatar::defaultImage(AVATAR_STREAM_SIZE),
164 'class' => 'avatar photo',
165 'width' => AVATAR_STREAM_SIZE,
166 'height' => AVATAR_STREAM_SIZE,
168 ($profile->fullname) ? $profile->fullname :
169 $profile->nickname));
170 $this->element('span', 'nickname fn', $profile->nickname);
171 $this->elementEnd('a');
172 $this->elementEnd('span');
174 // FIXME: URL, image, video, audio
175 $this->elementStart('p', array('class' => 'entry-content'));
176 if ($notice->rendered) {
177 $this->raw($this->highlight($notice->rendered, $terms));
179 // XXX: may be some uncooked notices in the DB,
180 // we cook them right now. This should probably disappear in future
181 // versions (>> 0.4.x)
182 $this->raw($this->highlight(common_render_content($notice->content, $notice), $terms));
184 $this->elementEnd('p');
185 $this->elementEnd('div');
187 $noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
188 $this->elementStart('div', 'entry-content');
189 $this->elementStart('dl', 'timestamp');
190 $this->element('dt', null, _('Published'));
191 $this->elementStart('dd', null);
192 $this->elementStart('a', array('rel' => 'bookmark',
193 'href' => $noticeurl));
194 $dt = common_date_iso8601($notice->created);
195 $this->element('abbr', array('class' => 'published',
197 common_date_string($notice->created));
198 $this->elementEnd('a');
199 $this->elementEnd('dd');
200 $this->elementEnd('dl');
202 if ($notice->reply_to) {
203 $replyurl = common_local_url('shownotice',
204 array('notice' => $this->notice->reply_to));
205 $this->elementStart('dl', 'response');
206 $this->element('dt', null, _('To'));
207 $this->elementStart('dd');
208 $this->element('a', array('href' => $replyurl,
209 'rel' => 'in-reply-to'),
211 $this->elementEnd('dd');
212 $this->elementEnd('dl');
214 $this->elementEnd('div');
216 $this->elementStart('div', 'notice-options');
218 $reply_url = common_local_url('newnotice',
219 array('replyto' => $profile->nickname));
221 $this->elementStart('dl', 'notice_reply');
222 $this->element('dt', null, _('Reply to this notice'));
223 $this->elementStart('dd');
224 $this->elementStart('a', array('href' => $reply_url,
225 'title' => _('Reply to this notice')));
226 $this->text(_('Reply'));
227 $this->element('span', 'notice_id', $notice->id);
228 $this->elementEnd('a');
229 $this->elementEnd('dd');
230 $this->elementEnd('dl');
231 $this->elementEnd('div');
232 $this->elementEnd('li');
236 * Highlist query terms
238 * @param string $text notice text
239 * @param array $terms terms to highlight
243 function highlight($text, $terms)
245 /* Highligh serach terms */
246 $pattern = '/('.implode('|', array_map('htmlspecialchars', $terms)).')/i';
247 $result = preg_replace($pattern, '<strong>\\1</strong>', $text);
249 /* Remove highlighting from inside links, loop incase multiple highlights in links */
250 $pattern = '/(href="[^"]*)<strong>('.implode('|', array_map('htmlspecialchars', $terms)).')<\/strong>([^"]*")/iU';
252 $result = preg_replace($pattern, '\\1\\2\\3', $result, -1, $count);
257 function isReadOnly()