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