]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/noticesearch.php
Link rtsp, mms & tel URI schemes, correct pseudo-protocol ones.
[quix0rs-gnu-social.git] / actions / noticesearch.php
1 <?php
2 /**
3  * Notice search action class.
4  *
5  * PHP version 5
6  *
7  * @category Action
8  * @package  Laconica
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/
14  *
15  * Laconica - a distributed open-source microblogging tool
16  * Copyright (C) 2008, Controlez-Vous, Inc.
17  *
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.
22  *
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.
27  *
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/>.
30  */
31
32 if (!defined('LACONICA')) {
33     exit(1);
34 }
35
36 require_once INSTALLDIR.'/lib/searchaction.php';
37
38 /**
39  * Notice search action class.
40  *
41  * @category Action
42  * @package  Laconica
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?
48  */
49 class NoticesearchAction extends SearchAction
50 {
51     /**
52      * Get instructions
53      * 
54      * @return string instruction text 
55      */
56     function getInstructions()
57     {
58         return _('Search for notices on %%site.name%% by their contents. Separate search terms by spaces; they must be 3 characters or more.');
59     }
60
61     /**
62      * Get title
63      * 
64      * @return string title
65      */
66     function title()
67     {
68         return _('Text search');
69     }
70
71     /**
72      * Show results
73      *
74      * @param string  $q    search query
75      * @param integer $page page number
76      *
77      * @return void
78      */
79     function showResults($q, $page)
80     {
81         $notice        = new Notice();
82         $q             = strtolower($q);
83         $search_engine = $notice->getSearchEngine('identica_notices');
84         $search_engine->set_sort_mode('chron');
85         // Ask for an extra to see if there's more.
86         $search_engine->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
87         if (false === $search_engine->query($q)) {
88             $cnt = 0;
89         } else {
90             $cnt = $notice->find();
91         }
92         if ($cnt > 0) {
93             $terms = preg_split('/[\s,]+/', $q);
94             $this->elementStart('ul', array('class' => 'notices'));
95             for ($i = 0; $i < min($cnt, NOTICES_PER_PAGE); $i++) {
96                 if ($notice->fetch()) {
97                     $this->showNotice($notice, $terms);
98                 } else {
99                     // shouldn't happen!
100                     break;
101                 }
102             }
103             $this->elementEnd('ul');
104         } else {
105             $this->element('p', 'error', _('No results'));
106         }
107
108         $this->pagination($page > 1, $cnt > NOTICES_PER_PAGE,
109                           $page, 'noticesearch', array('q' => $q));
110     }
111
112     /**
113      * Show header
114      *
115      * @param array $arr array containing the query
116      *
117      * @return void
118      */
119
120     function extraHead()
121     {
122         $q = $this->trimmed('q');
123         if ($q) {
124             $this->element('link', array('rel' => 'alternate',
125                                          'href' => common_local_url('noticesearchrss',
126                                                                     array('q' => $q)),
127                                          'type' => 'application/rss+xml',
128                                          'title' => _('Search Stream Feed')));
129         }
130     }
131
132     /**
133      * Show notice
134      *
135      * @param class $notice notice
136      * @param array $terms  terms to highlight
137      *
138      * @return void
139      *
140      * @todo refactor and combine with StreamAction::showNotice()
141      */
142     function showNotice($notice, $terms)
143     {
144         $profile = $notice->getProfile();
145         if (!$profile) {
146             common_log_db_error($notice, 'SELECT', __FILE__);
147             $this->serverError(_('Notice without matching profile'));
148             return;
149         }
150         // XXX: RDFa
151         $this->elementStart('li', array('class' => 'hentry notice',
152                                           'id' => 'notice-' . $notice->id));
153
154         $this->elementStart('div', 'entry-title');
155         $this->elementStart('span', 'vcard author');
156         $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
157         $this->elementStart('a', array('href' => $profile->profileurl));
158         $this->element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE),
159                                     'class' => 'avatar photo',
160                                     'width' => AVATAR_STREAM_SIZE,
161                                     'height' => AVATAR_STREAM_SIZE,
162                                     'alt' =>
163                                     ($profile->fullname) ? $profile->fullname :
164                                     $profile->nickname));
165         $this->element('span', 'nickname fn', $profile->nickname);
166         $this->elementEnd('a');
167         $this->elementEnd('span');
168
169         // FIXME: URL, image, video, audio
170         $this->elementStart('p', array('class' => 'entry-content'));
171         if ($notice->rendered) {
172             $this->raw($this->highlight($notice->rendered, $terms));
173         } else {
174             // XXX: may be some uncooked notices in the DB,
175             // we cook them right now. This should probably disappear in future
176             // versions (>> 0.4.x)
177             $this->raw($this->highlight(common_render_content($notice->content, $notice), $terms));
178         }
179         $this->elementEnd('p');
180         $this->elementEnd('div');
181
182         $noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
183         $this->elementStart('div', 'entry-content');
184         $this->elementStart('dl', 'timestamp');
185         $this->element('dt', null, _('Published'));
186         $this->elementStart('dd', null);
187         $this->elementStart('a', array('rel' => 'bookmark',
188                                        'href' => $noticeurl));
189         $dt = common_date_iso8601($notice->created);
190         $this->element('abbr', array('class' => 'published',
191                                           'title' => $dt),
192                             common_date_string($notice->created));
193         $this->elementEnd('a');
194         $this->elementEnd('dd');
195         $this->elementEnd('dl');
196
197         if ($notice->reply_to) {
198             $replyurl = common_local_url('shownotice',
199                                          array('notice' => $this->notice->reply_to));
200             $this->elementStart('dl', 'response');
201             $this->element('dt', null, _('To'));
202             $this->elementStart('dd');
203             $this->element('a', array('href' => $replyurl,
204                                            'rel' => 'in-reply-to'),
205                                 _('in reply to'));
206             $this->elementEnd('dd');
207             $this->elementEnd('dl');
208         }
209         $this->elementEnd('div');
210
211         $this->elementStart('div', 'notice-options');
212
213         $reply_url = common_local_url('newnotice',
214                                       array('replyto' => $profile->nickname));
215
216         $this->elementStart('dl', 'notice_reply');
217         $this->element('dt', null, _('Reply to this notice'));
218         $this->elementStart('dd');
219         $this->elementStart('a', array('href' => $reply_url,
220                                        'title' => _('Reply to this notice')));
221         $this->text(_('Reply'));
222         $this->element('span', 'notice_id', $notice->id);
223         $this->elementEnd('a');
224         $this->elementEnd('dd');
225         $this->elementEnd('dl');
226
227         $this->elementStart('a',
228                              array('href' => common_local_url('newnotice',
229                                                               array('replyto' => $profile->nickname)),
230                                    'onclick' => 'doreply("'.$profile->nickname.'"); return false',
231                                    'title' => _('reply'),
232                                    'class' => 'replybutton'));
233         $this->hidden('posttoken', common_session_token());
234         $this->elementEnd('a');
235         $this->elementEnd('div');
236         $this->elementEnd('li');
237     }
238
239     /**
240      * Highlist query terms
241      *
242      * @param string $text  notice text
243      * @param array  $terms terms to highlight
244      *
245      * @return void
246      */
247     function highlight($text, $terms)
248     {
249         /* Highligh serach terms */
250         $pattern = '/('.implode('|', array_map('htmlspecialchars', $terms)).')/i';
251         $result  = preg_replace($pattern, '<strong>\\1</strong>', $text);
252
253         /* Remove highlighting from inside links, loop incase multiple highlights in links */
254         $pattern = '/(href="[^"]*)<strong>('.implode('|', array_map('htmlspecialchars', $terms)).')<\/strong>([^"]*")/iU';
255         do {
256             $result = preg_replace($pattern, '\\1\\2\\3', $result, -1, $count);
257         } while ($count);
258         return $result;
259     }
260
261     function isReadOnly()
262     {
263         return true;
264     }
265 }
266