3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once(INSTALLDIR.'/lib/searchaction.php');
23 define(NOTICES_PER_PAGE, 20);
25 # XXX common parent for people and content search?
27 class NoticesearchAction extends SearchAction {
29 function get_instructions() {
30 return _t('Search for notices on %%site.name%% by their contents. ' .
31 'Separate search terms by spaces; they must be 3 characters or more.');
34 function get_title() {
35 return _t('Text search');
38 function show_results($q, $page) {
40 $notice = new Notice();
42 # lcase it for comparison
44 $notice->whereAdd('MATCH(content) against (\''.addslashes($q).'\')');
46 # Ask for an extra to see if there's more.
48 $notice->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
50 $cnt = $notice->find();
53 $terms = preg_split('/[\s,]+/', $q);
54 common_element_start('ul', array('id' => 'notices'));
55 for ($i = 0; $i < min($cnt, NOTICES_PER_PAGE); $i++) {
56 if ($notice->fetch()) {
57 $this->show_notice($notice, $terms);
63 common_element_end('ul');
65 common_element('p', 'error', _t('No results'));
68 common_pagination($page > 1, $cnt > NOTICES_PER_PAGE,
69 $page, 'noticesearch', array('q' => $q));
72 function show_header($arr) {
77 common_element('link', array('rel' => 'alternate',
78 'href' => common_local_url('noticesearchrss',
80 'type' => 'application/rss+xml',
81 'title' => _t('Search Stream Feed')));
85 # XXX: refactor and combine with StreamAction::show_notice()
87 function show_notice($notice, $terms) {
88 $profile = $notice->getProfile();
90 common_element_start('li', array('class' => 'notice_single',
91 'id' => 'notice-' . $notice->id));
92 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
93 common_element_start('a', array('href' => $profile->profileurl));
94 common_element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE),
95 'class' => 'avatar stream',
96 'width' => AVATAR_STREAM_SIZE,
97 'height' => AVATAR_STREAM_SIZE,
99 ($profile->fullname) ? $profile->fullname :
100 $profile->nickname));
101 common_element_end('a');
102 common_element('a', array('href' => $profile->profileurl,
103 'class' => 'nickname'),
105 # FIXME: URL, image, video, audio
106 common_element_start('p', array('class' => 'content'));
107 if ($notice->rendered) {
108 common_raw($this->highlight($notice->rendered, $terms));
110 # XXX: may be some uncooked notices in the DB,
111 # we cook them right now. This should probably disappear in future
112 # versions (>> 0.4.x)
113 common_raw($this->highlight(common_render_content($notice->content, $notice), $terms));
115 common_element_end('p');
116 $noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
117 common_element_start('p', 'time');
118 common_element('a', array('class' => 'permalink',
119 'href' => $noticeurl,
120 'title' => common_exact_date($notice->created)),
121 common_date_string($notice->created));
122 if ($notice->reply_to) {
123 $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
125 common_element('a', array('class' => 'inreplyto',
126 'href' => $replyurl),
127 _t('in reply to...'));
130 common_element_start('a',
131 array('href' => common_local_url('newnotice',
132 array('replyto' => $profile->nickname)),
133 'onclick' => 'doreply("'.$profile->nickname.'"); return false',
134 'title' => _t('reply'),
135 'class' => 'replybutton'));
136 common_raw('→');
137 common_element_end('a');
138 common_element_end('p');
139 common_element_end('li');
142 function highlight($text, $terms) {
143 $pattern = '/('.implode('|',array_map('htmlspecialchars', $terms)).')/i';
144 $result = preg_replace($pattern, '<strong>\\1</strong>', $text);