]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/noticesearch.php
Merge branch '1.0.x' into testing
[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     protected $q = null;
52
53     function prepare($args)
54     {
55         parent::prepare($args);
56
57         $this->q = $this->trimmed('q');
58
59         // FIXME: very dependent on tag format
60         if (preg_match('/^#([\pL\pN_\-\.]{1,64})/ue', $this->q)) {
61             common_redirect(common_local_url('tag',
62                                              array('tag' => common_canonical_tag(substr($this->q, 1)))),
63                             303);
64         }
65
66         common_set_returnto($this->selfUrl());
67
68         return true;
69     }
70
71     /**
72      * Get instructions
73      *
74      * @return string instruction text
75      */
76     function getInstructions()
77     {
78         // TRANS: Instructions for Notice search page.
79         // TRANS: %%site.name%% is the name of the StatusNet site.
80         return _('Search for notices on %%site.name%% by their contents. Separate search terms by spaces; they must be 3 characters or more.');
81     }
82
83     /**
84      * Get title
85      *
86      * @return string title
87      */
88     function title()
89     {
90         // TRANS: Title of the page where users can search for notices.
91         return _('Text search');
92     }
93
94     function getFeeds()
95     {
96         $q = $this->trimmed('q');
97
98         if (!$q) {
99             return null;
100         }
101
102         return array(new Feed(Feed::RSS1, common_local_url('noticesearchrss',
103                                                            array('q' => $q)),
104                               // TRANS: Test in RSS notice search.
105                               // TRANS: %1$s is the query, %2$s is the StatusNet site name.
106                               sprintf(_('Search results for "%1$s" on %2$s'),
107                                       $q, common_config('site', 'name'))));
108     }
109
110     /**
111      * Show results
112      *
113      * @param string  $q    search query
114      * @param integer $page page number
115      *
116      * @return void
117      */
118     function showResults($q, $page)
119     {
120         $notice        = new Notice();
121
122         $search_engine = $notice->getSearchEngine('notice');
123         $search_engine->set_sort_mode('chron');
124         // Ask for an extra to see if there's more.
125         $search_engine->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
126         if (false === $search_engine->query($q)) {
127             $cnt = 0;
128         } else {
129             $cnt = $notice->find();
130         }
131         if ($cnt === 0) {
132             // TRANS: Text for notice search results is the query had no results.
133             $this->element('p', 'error', _('No results.'));
134
135             $this->searchSuggestions($q);
136             if (common_logged_in()) {
137                 // TRANS: Text for logged in users making a query for notices without results.
138                 // TRANS: This message contains a Markdown link.
139                 $message = sprintf(_('Be the first to [post on this topic](%%%%action.newnotice%%%%?status_textarea=%s)!'), urlencode($q));
140             }
141             else {
142                 // TRANS: Text for not logged in users making a query for notices without results.
143                 // TRANS: This message contains Markdown links.
144                 $message = sprintf(_('Why not [register an account](%%%%action.register%%%%) and be the first to [post on this topic](%%%%action.newnotice%%%%?status_textarea=%s)!'), urlencode($q));
145             }
146
147             $this->elementStart('div', 'guide');
148             $this->raw(common_markup_to_html($message));
149             $this->elementEnd('div');
150             return;
151         }
152         if (Event::handle('StartNoticeSearchShowResults', array($this, $q, $notice))) {
153             $terms = preg_split('/[\s,]+/', $q);
154             $nl = new SearchNoticeList($notice, $this, $terms);
155             $cnt = $nl->show();
156             $this->pagination($page > 1, $cnt > NOTICES_PER_PAGE,
157                               $page, 'noticesearch', array('q' => $q));
158             Event::handle('EndNoticeSearchShowResults', array($this, $q, $notice));
159         }
160     }
161
162     function showScripts()
163     {
164         parent::showScripts();
165         $this->autofocus('q');
166     }
167 }
168
169 class SearchNoticeList extends NoticeList {
170     function __construct($notice, $out=null, $terms)
171     {
172         parent::__construct($notice, $out);
173         $this->terms = $terms;
174     }
175
176     function newListItem($notice)
177     {
178         return new SearchNoticeListItem($notice, $this->out, $this->terms);
179     }
180 }
181
182 class SearchNoticeListItem extends NoticeListItem {
183     function __construct($notice, $out=null, $terms)
184     {
185         parent::__construct($notice, $out);
186         $this->terms = $terms;
187     }
188
189     function showContent()
190     {
191         // FIXME: URL, image, video, audio
192         $this->out->elementStart('p', array('class' => 'entry-content'));
193         if ($this->notice->rendered) {
194             $this->out->raw($this->highlight($this->notice->rendered, $this->terms));
195         } else {
196             // XXX: may be some uncooked notices in the DB,
197             // we cook them right now. This should probably disappear in future
198             // versions (>> 0.4.x)
199             $this->out->raw($this->highlight(common_render_content($this->notice->content, $this->notice), $this->terms));
200         }
201         $this->out->elementEnd('p');
202
203     }
204
205     /**
206      * Highlist query terms
207      *
208      * @param string $text  notice text
209      * @param array  $terms terms to highlight
210      *
211      * @return void
212      */
213     function highlight($text, $terms)
214     {
215         /* Highligh search terms */
216         $options = implode('|', array_map('preg_quote', array_map('htmlspecialchars', $terms),
217                                                             array_fill(0, sizeof($terms), '/')));
218         $pattern = "/($options)/i";
219         $result = '';
220
221         /* Divide up into text (highlight me) and tags (don't touch) */
222         $chunks = preg_split('/(<[^>]+>)/', $text, 0, PREG_SPLIT_DELIM_CAPTURE);
223         foreach ($chunks as $i => $chunk) {
224             if ($i % 2 == 1) {
225                 // odd: delimiter (tag)
226                 $result .= $chunk;
227             } else {
228                 // even: freetext between tags
229                 $result .= preg_replace($pattern, '<strong>\\1</strong>', $chunk);
230             }
231         }
232
233         return $result;
234     }
235 }