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