]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/noticesearch.php
Merge branch 'sgmurphy-clone/0.7.x' into 0.7.x
[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     function prepare($args)
53     {
54         parent::prepare($args);
55
56         common_set_returnto($this->selfUrl());
57
58         return true;
59     }
60     
61     /**
62      * Get instructions
63      * 
64      * @return string instruction text 
65      */
66     function getInstructions()
67     {
68         return _('Search for notices on %%site.name%% by their contents. Separate search terms by spaces; they must be 3 characters or more.');
69     }
70
71     /**
72      * Get title
73      * 
74      * @return string title
75      */
76     function title()
77     {
78         return _('Text search');
79     }
80
81
82     function showExportData()
83     {
84         $q = $this->trimmed('q');
85         if (!$q) {
86             return;
87         }
88         $fl = new FeedList($this);
89         $fl->show(array(0 => array('href' => common_local_url('noticesearchrss', array('q' => $q)),
90                                    'type' => 'rss',
91                                    'version' => 'RSS 1.0',
92                                    'item' => 'noticesearchrss')));
93     }
94
95
96
97     function showFeeds()
98     {  
99         $q = $this->trimmed('q');
100         if (!$q) {
101             return;
102         }
103
104        $this->element('link', array('rel' => 'alternate',
105                                          'href' => common_local_url('noticesearchrss',
106                                                                     array('q' => $q)),
107                                          'type' => 'application/rss+xml',
108                                          'title' => _('Search Stream Feed')));
109     }
110
111
112     /**
113      * Show header
114      *
115      * @param array $arr array containing the query
116      *
117      * @return void
118      */
119
120     function extraHead2()
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
134
135
136
137     /**
138      * Show results
139      *
140      * @param string  $q    search query
141      * @param integer $page page number
142      *
143      * @return void
144      */
145     function showResults($q, $page)
146     {
147         $notice        = new Notice();
148         $q             = strtolower($q);
149         $search_engine = $notice->getSearchEngine('identica_notices');
150         $search_engine->set_sort_mode('chron');
151         // Ask for an extra to see if there's more.
152         $search_engine->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
153         if (false === $search_engine->query($q)) {
154             $cnt = 0;
155         } else {
156             $cnt = $notice->find();
157         }
158         if ($cnt > 0) {
159             $terms = preg_split('/[\s,]+/', $q);
160             $this->elementStart('ul', array('class' => 'notices'));
161             for ($i = 0; $i < min($cnt, NOTICES_PER_PAGE); $i++) {
162                 if ($notice->fetch()) {
163                     $this->showNotice($notice, $terms);
164                 } else {
165                     // shouldn't happen!
166                     break;
167                 }
168             }
169             $this->elementEnd('ul');
170         } else {
171             $this->element('p', 'error', _('No results'));
172         }
173
174         $this->pagination($page > 1, $cnt > NOTICES_PER_PAGE,
175                           $page, 'noticesearch', array('q' => $q));
176     }
177
178     /**
179      * Show notice
180      *
181      * @param class $notice notice
182      * @param array $terms  terms to highlight
183      *
184      * @return void
185      *
186      * @todo refactor and combine with StreamAction::showNotice()
187      */
188     function showNotice($notice, $terms)
189     {
190         $profile = $notice->getProfile();
191         if (!$profile) {
192             common_log_db_error($notice, 'SELECT', __FILE__);
193             $this->serverError(_('Notice without matching profile'));
194             return;
195         }
196         // XXX: RDFa
197         $this->elementStart('li', array('class' => 'hentry notice',
198                                           'id' => 'notice-' . $notice->id));
199
200         $this->elementStart('div', 'entry-title');
201         $this->elementStart('span', 'vcard author');
202         $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
203         $this->elementStart('a', array('href' => $profile->profileurl,
204                                        'class' => 'url'));
205         $this->element('img', array('src' => ($avatar) ? $avatar->displayUrl() : Avatar::defaultImage(AVATAR_STREAM_SIZE),
206                                     'class' => 'avatar photo',
207                                     'width' => AVATAR_STREAM_SIZE,
208                                     'height' => AVATAR_STREAM_SIZE,
209                                     'alt' =>
210                                     ($profile->fullname) ? $profile->fullname :
211                                     $profile->nickname));
212         $this->element('span', 'nickname fn', $profile->nickname);
213         $this->elementEnd('a');
214         $this->elementEnd('span');
215
216         // FIXME: URL, image, video, audio
217         $this->elementStart('p', array('class' => 'entry-content'));
218         if ($notice->rendered) {
219             $this->raw($this->highlight($notice->rendered, $terms));
220         } else {
221             // XXX: may be some uncooked notices in the DB,
222             // we cook them right now. This should probably disappear in future
223             // versions (>> 0.4.x)
224             $this->raw($this->highlight(common_render_content($notice->content, $notice), $terms));
225         }
226         $this->elementEnd('p');
227         $this->elementEnd('div');
228
229         $noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
230         $this->elementStart('div', 'entry-content');
231         $this->elementStart('dl', 'timestamp');
232         $this->element('dt', null, _('Published'));
233         $this->elementStart('dd', null);
234         $this->elementStart('a', array('rel' => 'bookmark',
235                                        'href' => $noticeurl));
236         $dt = common_date_iso8601($notice->created);
237         $this->element('abbr', array('class' => 'published',
238                                           'title' => $dt),
239                             common_date_string($notice->created));
240         $this->elementEnd('a');
241         $this->elementEnd('dd');
242         $this->elementEnd('dl');
243
244         if ($notice->reply_to) {
245             $replyurl = common_local_url('shownotice',
246                                          array('notice' => $this->notice->reply_to));
247             $this->elementStart('dl', 'response');
248             $this->element('dt', null, _('To'));
249             $this->elementStart('dd');
250             $this->element('a', array('href' => $replyurl,
251                                            'rel' => 'in-reply-to'),
252                                 _('in reply to'));
253             $this->elementEnd('dd');
254             $this->elementEnd('dl');
255         }
256         $this->elementEnd('div');
257
258         $this->elementStart('div', 'notice-options');
259
260         $reply_url = common_local_url('newnotice',
261                                       array('replyto' => $profile->nickname));
262
263         $this->elementStart('dl', 'notice_reply');
264         $this->element('dt', null, _('Reply to this notice'));
265         $this->elementStart('dd');
266         $this->elementStart('a', array('href' => $reply_url,
267                                        'title' => _('Reply to this notice')));
268         $this->text(_('Reply'));
269         $this->element('span', 'notice_id', $notice->id);
270         $this->elementEnd('a');
271         $this->elementEnd('dd');
272         $this->elementEnd('dl');
273         $this->elementEnd('div');
274         $this->elementEnd('li');
275     }
276
277     /**
278      * Highlist query terms
279      *
280      * @param string $text  notice text
281      * @param array  $terms terms to highlight
282      *
283      * @return void
284      */
285     function highlight($text, $terms)
286     {
287         /* Highligh serach terms */
288         $pattern = '/('.implode('|', array_map('htmlspecialchars', $terms)).')/i';
289         $result  = preg_replace($pattern, '<strong>\\1</strong>', $text);
290
291         /* Remove highlighting from inside links, loop incase multiple highlights in links */
292         $pattern = '/(href="[^"]*)<strong>('.implode('|', array_map('htmlspecialchars', $terms)).')<\/strong>([^"]*")/iU';
293         do {
294             $result = preg_replace($pattern, '\\1\\2\\3', $result, -1, $count);
295         } while ($count);
296         return $result;
297     }
298
299     function isReadOnly()
300     {
301         return true;
302     }
303 }
304