]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/noticesearch.php
782c8fe98dd63d40225a1fec47175905e118daf3
[quix0rs-gnu-social.git] / actions / noticesearch.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/searchaction.php');
23
24 # XXX common parent for people and content search?
25
26 class NoticesearchAction extends SearchAction {
27
28     function get_instructions()
29     {
30         return _('Search for notices on %%site.name%% by their contents. Separate search terms by spaces; they must be 3 characters or more.');
31     }
32
33     function get_title()
34     {
35         return _('Text search');
36     }
37
38     function show_results($q, $page)
39     {
40
41         $notice = new Notice();
42
43         # lcase it for comparison
44         $q = strtolower($q);
45
46         $search_engine = $notice->getSearchEngine('identica_notices');
47
48         $search_engine->set_sort_mode('chron');
49         # Ask for an extra to see if there's more.
50         $search_engine->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
51
52         if (false === $search_engine->query($q)) {
53             $cnt = 0;
54         }
55         else {
56             $cnt = $notice->find();
57         }
58         if ($cnt > 0) {
59             $terms = preg_split('/[\s,]+/', $q);
60             common_element_start('ul', array('id' => 'notices'));
61             for ($i = 0; $i < min($cnt, NOTICES_PER_PAGE); $i++) {
62                 if ($notice->fetch()) {
63                     $this->show_notice($notice, $terms);
64                 } else {
65                     // shouldn't happen!
66                     break;
67                 }
68             }
69             common_element_end('ul');
70         } else {
71             common_element('p', 'error', _('No results'));
72         }
73
74         common_pagination($page > 1, $cnt > NOTICES_PER_PAGE,
75                           $page, 'noticesearch', array('q' => $q));
76     }
77
78     function show_header($arr)
79     {
80         if ($arr) {
81             $q = $arr[0];
82         }
83         if ($q) {
84             common_element('link', array('rel' => 'alternate',
85                                          'href' => common_local_url('noticesearchrss',
86                                                                     array('q' => $q)),
87                                          'type' => 'application/rss+xml',
88                                          'title' => _('Search Stream Feed')));
89         }
90     }
91
92     # XXX: refactor and combine with StreamAction::show_notice()
93
94     function show_notice($notice, $terms)
95     {
96         $profile = $notice->getProfile();
97         if (!$profile) {
98             common_log_db_error($notice, 'SELECT', __FILE__);
99             $this->server_error(_('Notice without matching profile'));
100             return;
101         }
102         # XXX: RDFa
103         common_element_start('li', array('class' => 'notice_single',
104                                           'id' => 'notice-' . $notice->id));
105         $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
106         common_element_start('a', array('href' => $profile->profileurl));
107         common_element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE),
108                                     'class' => 'avatar stream',
109                                     'width' => AVATAR_STREAM_SIZE,
110                                     'height' => AVATAR_STREAM_SIZE,
111                                     'alt' =>
112                                     ($profile->fullname) ? $profile->fullname :
113                                     $profile->nickname));
114         common_element_end('a');
115         common_element('a', array('href' => $profile->profileurl,
116                                   'class' => 'nickname'),
117                        $profile->nickname);
118         # FIXME: URL, image, video, audio
119         common_element_start('p', array('class' => 'content'));
120         if ($notice->rendered) {
121             common_raw($this->highlight($notice->rendered, $terms));
122         } else {
123             # XXX: may be some uncooked notices in the DB,
124             # we cook them right now. This should probably disappear in future
125             # versions (>> 0.4.x)
126             common_raw($this->highlight(common_render_content($notice->content, $notice), $terms));
127         }
128         common_element_end('p');
129         $noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
130         common_element_start('p', 'time');
131         common_element('a', array('class' => 'permalink',
132                                   'href' => $noticeurl,
133                                   'title' => common_exact_date($notice->created)),
134                        common_date_string($notice->created));
135         if ($notice->reply_to) {
136             $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
137             common_text(' (');
138             common_element('a', array('class' => 'inreplyto',
139                                       'href' => $replyurl),
140                            _('in reply to...'));
141             common_text(')');
142         }
143         common_element_start('a',
144                              array('href' => common_local_url('newnotice',
145                                                               array('replyto' => $profile->nickname)),
146                                    'onclick' => 'doreply("'.$profile->nickname.'"); return false',
147                                    'title' => _('reply'),
148                                    'class' => 'replybutton'));
149         common_hidden('posttoken', common_session_token());
150         
151         common_raw('&rarr;');
152         common_element_end('a');
153         common_element_end('p');
154         common_element_end('li');
155     }
156
157     function highlight($text, $terms)
158     {
159         /* Highligh serach terms */
160         $pattern = '/('.implode('|',array_map('htmlspecialchars', $terms)).')/i';
161         $result = preg_replace($pattern, '<strong>\\1</strong>', $text);
162
163         /* Remove highlighting from inside links, loop incase multiple highlights in links */
164         $pattern = '/(href="[^"]*)<strong>('.implode('|',array_map('htmlspecialchars', $terms)).')<\/strong>([^"]*")/iU';
165         do {
166             $result = preg_replace($pattern, '\\1\\2\\3', $result, -1, $count);
167         } while ($count);
168         return $result;
169     }
170 }