]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/noticesearch.php
Merge branch 'master' of /var/www/trunk
[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  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
12  * @link     http://laconi.ca/
13  *
14  * Laconica - a distributed open-source microblogging tool
15  * Copyright (C) 2008, Controlez-Vous, Inc.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU Affero General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU Affero General Public License for more details.
26  *
27  * You should have received a copy of the GNU Affero General Public License
28  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29  */
30
31 if (!defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/searchaction.php';
36
37 /**
38  * Notice search action class.
39  *
40  * @category Action
41  * @package  Laconica
42  * @author   Evan Prodromou <evan@controlyourself.ca>
43  * @author   Robin Millette <millette@controlyourself.ca>
44  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
45  * @link     http://laconi.ca/
46  * @todo     common parent for people and content search?
47  */
48 class NoticesearchAction extends SearchAction
49 {
50     /**
51      * Get instructions
52      * 
53      * @return string instruction text 
54      */
55     function getInstructions()
56     {
57         return _('Search for notices on %%site.name%% by their contents. Separate search terms by spaces; they must be 3 characters or more.');
58     }
59
60     /**
61      * Get title
62      * 
63      * @return string title
64      */
65     function title()
66     {
67         return _('Text search');
68     }
69
70     /**
71      * Show results
72      *
73      * @param string  $q    search query
74      * @param integer $page page number
75      *
76      * @return void
77      */
78     function showResults($q, $page)
79     {
80         $notice        = new Notice();
81         $q             = strtolower($q);
82         $search_engine = $notice->getSearchEngine('identica_notices');
83         $search_engine->set_sort_mode('chron');
84         // Ask for an extra to see if there's more.
85         $search_engine->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
86         if (false === $search_engine->query($q)) {
87             $cnt = 0;
88         } else {
89             $cnt = $notice->find();
90         }
91         if ($cnt > 0) {
92             $terms = preg_split('/[\s,]+/', $q);
93             $this->elementStart('ul', array('id' => 'notices'));
94             for ($i = 0; $i < min($cnt, NOTICES_PER_PAGE); $i++) {
95                 if ($notice->fetch()) {
96                     $this->showNotice($notice, $terms);
97                 } else {
98                     // shouldn't happen!
99                     break;
100                 }
101             }
102             $this->elementEnd('ul');
103         } else {
104             $this->element('p', 'error', _('No results'));
105         }
106
107         $this->pagination($page > 1, $cnt > NOTICES_PER_PAGE,
108                           $page, 'noticesearch', array('q' => $q));
109     }
110
111     /**
112      * Show header
113      *
114      * @param array $arr array containing the query
115      *
116      * @return void
117      */
118
119     function extraHead()
120     {
121         $q = $this->trimmed('q');
122         if ($q) {
123             $this->element('link', array('rel' => 'alternate',
124                                          'href' => common_local_url('noticesearchrss',
125                                                                     array('q' => $q)),
126                                          'type' => 'application/rss+xml',
127                                          'title' => _('Search Stream Feed')));
128         }
129     }
130
131     /**
132      * Show notice
133      *
134      * @param class $notice notice
135      * @param array $terms  terms to highlight
136      *
137      * @return void
138      *
139      * @todo refactor and combine with StreamAction::showNotice()
140      */
141     function showNotice($notice, $terms)
142     {
143         $profile = $notice->getProfile();
144         if (!$profile) {
145             common_log_db_error($notice, 'SELECT', __FILE__);
146             $this->serverError(_('Notice without matching profile'));
147             return;
148         }
149         // XXX: RDFa
150         $this->elementStart('li', array('class' => 'notice_single',
151                                           'id' => 'notice-' . $notice->id));
152         $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
153         $this->elementStart('a', array('href' => $profile->profileurl));
154         $this->element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE),
155                                     'class' => 'avatar stream',
156                                     'width' => AVATAR_STREAM_SIZE,
157                                     'height' => AVATAR_STREAM_SIZE,
158                                     'alt' =>
159                                     ($profile->fullname) ? $profile->fullname :
160                                     $profile->nickname));
161         $this->elementEnd('a');
162         $this->element('a', array('href' => $profile->profileurl,
163                                   'class' => 'nickname'),
164                        $profile->nickname);
165         // FIXME: URL, image, video, audio
166         $this->elementStart('p', array('class' => 'content'));
167         if ($notice->rendered) {
168             $this->raw($this->highlight($notice->rendered, $terms));
169         } else {
170             // XXX: may be some uncooked notices in the DB,
171             // we cook them right now. This should probably disappear in future
172             // versions (>> 0.4.x)
173             $this->raw($this->highlight(common_render_content($notice->content, $notice), $terms));
174         }
175         $this->elementEnd('p');
176         $noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
177         $this->elementStart('p', 'time');
178         $this->element('a', array('class' => 'permalink',
179                                   'href' => $noticeurl,
180                                   'title' => common_exact_date($notice->created)),
181                        common_date_string($notice->created));
182         if ($notice->reply_to) {
183             $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
184             $this->text(' (');
185             $this->element('a', array('class' => 'inreplyto',
186                                       'href' => $replyurl),
187                            _('in reply to...'));
188             $this->text(')');
189         }
190         $this->elementStart('a',
191                              array('href' => common_local_url('newnotice',
192                                                               array('replyto' => $profile->nickname)),
193                                    'onclick' => 'doreply("'.$profile->nickname.'"); return false',
194                                    'title' => _('reply'),
195                                    'class' => 'replybutton'));
196         $this->hidden('posttoken', common_session_token());
197         
198         $this->raw('&rarr;');
199         $this->elementEnd('a');
200         $this->elementEnd('p');
201         $this->elementEnd('li');
202     }
203
204     /**
205      * Highlist query terms
206      *
207      * @param string $text  notice text
208      * @param array  $terms terms to highlight
209      *
210      * @return void
211      */
212     function highlight($text, $terms)
213     {
214         /* Highligh serach terms */
215         $pattern = '/('.implode('|', array_map('htmlspecialchars', $terms)).')/i';
216         $result  = preg_replace($pattern, '<strong>\\1</strong>', $text);
217
218         /* Remove highlighting from inside links, loop incase multiple highlights in links */
219         $pattern = '/(href="[^"]*)<strong>('.implode('|', array_map('htmlspecialchars', $terms)).')<\/strong>([^"]*")/iU';
220         do {
221             $result = preg_replace($pattern, '\\1\\2\\3', $result, -1, $count);
222         } while ($count);
223         return $result;
224     }
225 }
226