]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/noticesearch.php
db0d6efadcc88d0d83f501ee23c9c365963316c7
[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 define(NOTICES_PER_PAGE, 20);
24
25 # XXX common parent for people and content search?
26
27 class NoticesearchAction extends SearchAction {
28         
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.');
32         }
33         
34         function get_title() {
35                 return _t('Text search');
36         }
37         
38         function show_results($q, $page) {
39                 
40                 $notice = new Notice();
41
42                 # lcase it for comparison
43                 $q = strtolower($q);
44                 $notice->whereAdd('MATCH(content) against (\''.addslashes($q).'\')');
45
46                 # Ask for an extra to see if there's more.
47                 
48                 $notice->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
49
50                 $cnt = $notice->find();
51
52                 if ($cnt > 0) {
53                         $terms = preg_split('/[\s,]+/', $q);
54                         common_element_start('ul', array('id' => 'notice'));
55                         for ($i = 0; $i < min($cnt, NOTICES_PER_PAGE); $i++) {
56                                 if ($notice->fetch()) {
57                                         $this->show_notice($notice, $terms);
58                                 } else {
59                                         // shouldn't happen!
60                                         break;
61                                 }
62                         }
63                         common_element_end('ul');
64                 } else {
65                         common_element('p', 'error', _t('No results'));
66                 }
67                 
68                 common_pagination($page > 1, $cnt > NOTICES_PER_PAGE,
69                                                   $page, 'noticesearch', array('q' => $q));
70         }
71
72         # XXX: refactor and combine with StreamAction::show_notice()
73         
74         function show_notice($notice, $terms) {
75                 $profile = $notice->getProfile();
76                 # XXX: RDFa
77                 common_element_start('li', array('class' => 'notice_single',
78                                                                                   'id' => 'notice-' . $notice->id));
79                 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
80                 common_element_start('a', array('href' => $profile->profileurl));
81                 common_element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE),
82                                                                         'class' => 'avatar stream',
83                                                                         'width' => AVATAR_STREAM_SIZE,
84                                                                         'height' => AVATAR_STREAM_SIZE,
85                                                                         'alt' =>
86                                                                         ($profile->fullname) ? $profile->fullname :
87                                                                         $profile->nickname));
88                 common_element_end('a');
89                 common_element('a', array('href' => $profile->profileurl,
90                                                                   'class' => 'nickname'),
91                                            $profile->nickname);
92                 # FIXME: URL, image, video, audio
93                 common_element_start('p', array('class' => 'content'));
94                 if ($notice->rendered) {
95                         common_raw($this->highlight($notice->rendered), $terms);
96                 } else {
97                         # XXX: may be some uncooked notices in the DB,
98                         # we cook them right now. This should probably disappear in future
99                         # versions (>> 0.4.x)
100                         common_raw($this->highlight(common_render_content($notice->content, $notice), $terms));
101                 }
102                 common_element_end('p');
103                 $noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
104                 common_element_start('p', 'time');
105                 common_element('a', array('class' => 'permalink',
106                                                                   'href' => $noticeurl,
107                                                                   'title' => common_exact_date($notice->created)),
108                                            common_date_string($notice->created));
109                 if ($notice->reply_to) {
110                         $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
111                         common_text(' (');
112                         common_element('a', array('class' => 'inreplyto',
113                                                                           'href' => $replyurl),
114                                                    _t('in reply to...'));
115                         common_text(')');
116                 }
117                 common_element_start('a', 
118                                                          array('href' => common_local_url('newnotice',
119                                                                                                                           array('replyto' => $profile->nickname)),
120                                                                    'onclick' => 'doreply("'.$profile->nickname.'"); return false',
121                                                                    'title' => _t('reply'),
122                                                                    'class' => 'replybutton'));
123                 common_raw('&rarr;');
124                 common_element_end('a');
125                 common_element_end('p');
126                 common_element_end('li');
127         }
128
129         function highlight($text, $terms) {
130                 $pattern = '/('.implode('|',array_map('htmlspecialchars', $terms)).')/i';
131                 $result = preg_replace($pattern, '<strong>\\1</strong>', $text);
132                 return $result;
133         }
134 }