]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/noticesearch.php
Make statuses/home_timeline return the same thing as statuses/friends_timeline to...
[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  StatusNet
9  * @author   Evan Prodromou <evan@status.net>
10  * @author   Robin Millette <millette@status.net>
11  * @author   Sarven Capadisli <csarven@status.net>
12  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
13  * @link     http://status.net/
14  *
15  * StatusNet - the distributed open-source microblogging tool
16  * Copyright (C) 2008, 2009, StatusNet, 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('STATUSNET') && !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  StatusNet
43  * @author   Evan Prodromou <evan@status.net>
44  * @author   Robin Millette <millette@status.net>
45  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
46  * @link     http://status.net/
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     function getFeeds()
82     {
83         $q = $this->trimmed('q');
84
85         if (!$q) {
86             return null;
87         }
88
89         return array(new Feed(Feed::RSS1, common_local_url('noticesearchrss',
90                                                            array('q' => $q)),
91                               sprintf(_('Search results for "%s" on %s'),
92                                       $q, common_config('site', 'name'))));
93     }
94
95     /**
96      * Show results
97      *
98      * @param string  $q    search query
99      * @param integer $page page number
100      *
101      * @return void
102      */
103     function showResults($q, $page)
104     {
105         $notice        = new Notice();
106
107         $search_engine = $notice->getSearchEngine('identica_notices');
108         $search_engine->set_sort_mode('chron');
109         // Ask for an extra to see if there's more.
110         $search_engine->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
111         if (false === $search_engine->query($q)) {
112             $cnt = 0;
113         } else {
114             $cnt = $notice->find();
115         }
116         if ($cnt === 0) {
117             $this->element('p', 'error', _('No results.'));
118
119             $this->searchSuggestions($q);
120             if (common_logged_in()) {
121                 $message = sprintf(_('Be the first to [post on this topic](%%%%action.newnotice%%%%?status_textarea=%s)!'), urlencode($q));
122             }
123             else {
124                 $message = sprintf(_('Why not [register an account](%%%%action.%s%%%%) and be the first to [post on this topic](%%%%action.newnotice%%%%?status_textarea=%s)!'),
125                                    (!common_config('site','openidonly')) ? 'register' : 'openidlogin',
126                                    urlencode($q));
127             }
128
129             $this->elementStart('div', 'guide');
130             $this->raw(common_markup_to_html($message));
131             $this->elementEnd('div');
132             return;
133         }
134         $terms = preg_split('/[\s,]+/', $q);
135         $nl = new SearchNoticeList($notice, $this, $terms);
136         $cnt = $nl->show();
137         $this->pagination($page > 1, $cnt > NOTICES_PER_PAGE,
138                           $page, 'noticesearch', array('q' => $q));
139     }
140 }
141
142 class SearchNoticeList extends NoticeList {
143     function __construct($notice, $out=null, $terms)
144     {
145         parent::__construct($notice, $out);
146         $this->terms = $terms;
147     }
148
149     function newListItem($notice)
150     {
151         return new SearchNoticeListItem($notice, $this->out, $this->terms);
152     }
153 }
154
155 class SearchNoticeListItem extends NoticeListItem {
156     function __construct($notice, $out=null, $terms)
157     {
158         parent::__construct($notice, $out);
159         $this->terms = $terms;
160     }
161
162     function showContent()
163     {
164         // FIXME: URL, image, video, audio
165         $this->out->elementStart('p', array('class' => 'entry-content'));
166         if ($this->notice->rendered) {
167             $this->out->raw($this->highlight($this->notice->rendered, $this->terms));
168         } else {
169             // XXX: may be some uncooked notices in the DB,
170             // we cook them right now. This should probably disappear in future
171             // versions (>> 0.4.x)
172             $this->out->raw($this->highlight(common_render_content($this->notice->content, $this->notice), $this->terms));
173         }
174         $this->out->elementEnd('p');
175
176     }
177
178     /**
179      * Highlist query terms
180      *
181      * @param string $text  notice text
182      * @param array  $terms terms to highlight
183      *
184      * @return void
185      */
186     function highlight($text, $terms)
187     {
188         /* Highligh search terms */
189         $options = implode('|', array_map('preg_quote', array_map('htmlspecialchars', $terms),
190                                                             array_fill(0, sizeof($terms), '/')));
191         $pattern = "/($options)/i";
192         $result  = preg_replace($pattern, '<strong>\\1</strong>', $text);
193
194         /* Remove highlighting from inside links, loop incase multiple highlights in links */
195         $pattern = '/(href="[^"]*)<strong>('.$options.')<\/strong>([^"]*")/iU';
196         do {
197             $result = preg_replace($pattern, '\\1\\2\\3', $result, -1, $count);
198         } while ($count);
199         return $result;
200     }
201 }
202