fix phpdoc comment
[lhc/web/wiklou.git] / includes / SpecialSearch.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 * Run text & title search and display the output
22 * @package MediaWiki
23 * @subpackage SpecialPage
24 */
25
26 /** */
27 require_once( 'SearchEngine.php' );
28 require_once( 'Revision.php' );
29
30 function wfSpecialSearch( $par='' ) {
31 global $wgRequest, $wgUser;
32
33 $search = $wgRequest->getText( 'search', $par );
34 $searchPage = new SpecialSearch( $wgRequest, $wgUser );
35 if( $wgRequest->getVal( 'fulltext' ) ||
36 !is_null( $wgRequest->getVal( 'offset' ) ) ||
37 !is_null ($wgRequest->getVal( 'searchx' ) ) ) {
38 $searchPage->showResults( $search );
39 } else {
40 $searchPage->goResult( $search );
41 }
42 }
43
44 /**
45 * @todo document
46 * @package MediaWiki
47 * @subpackage SpecialPage
48 */
49 class SpecialSearch {
50
51 /**
52 * Set up basic search parameters from the request and user settings.
53 * Typically you'll pass $wgRequest and $wgUser.
54 *
55 * @param WebRequest $request
56 * @param User $user
57 * @access public
58 */
59 function SpecialSearch( &$request, &$user ) {
60 list( $this->limit, $this->offset ) = $request->getLimitOffset( 20, 'searchlimit' );
61
62 if( $request->getCheck( 'searchx' ) ) {
63 $this->namespaces = $this->powerSearch( $request );
64 } else {
65 $this->namespaces = $this->userNamespaces( $user );
66 }
67
68 $this->searchRedirects = false;
69 }
70
71 /**
72 * If an exact title match can be found, jump straight ahead to
73 * @param string $term
74 * @access public
75 */
76 function goResult( $term ) {
77 global $wgOut;
78 global $wgGoToEdit;
79
80 $this->setupPage( $term );
81
82 # Try to go to page as entered.
83 #
84 $t = Title::newFromText( $term );
85
86 # If the string cannot be used to create a title
87 if( is_null( $t ) ){
88 return $this->showResults( $term );
89 }
90
91 # If there's an exact or very near match, jump right there.
92 $t = SearchEngine::getNearMatch( $term );
93 if( !is_null( $t ) ) {
94 $wgOut->redirect( $t->getFullURL() );
95 return;
96 }
97
98 # No match, generate an edit URL
99 $t = Title::newFromText( $term );
100 if( is_null( $t ) ) {
101 $editurl = ''; # hrm...
102 } else {
103 # If the feature is enabled, go straight to the edit page
104 if ( $wgGoToEdit ) {
105 $wgOut->redirect( $t->getFullURL( 'action=edit' ) );
106 return;
107 } else {
108 $editurl = $t->escapeLocalURL( 'action=edit' );
109 }
110 }
111 # FIXME: HTML in wiki message
112 $wgOut->addHTML( '<p>' . wfMsg('nogomatch', $editurl, htmlspecialchars( $term ) ) . "</p>\n" );
113
114 return $this->showResults( $term );
115 }
116
117 /**
118 * @param string $term
119 * @access public
120 */
121 function showResults( $term ) {
122 $fname = 'SpecialSearch::showResults';
123 wfProfileIn( $fname );
124
125 $this->setupPage( $term );
126
127 global $wgUser, $wgOut;
128 $sk = $wgUser->getSkin();
129 $wgOut->addWikiText( wfMsg( 'searchresulttext' ) );
130
131 #if ( !$this->parseQuery() ) {
132 if( '' === trim( $term ) ) {
133 $wgOut->addWikiText(
134 '==' . wfMsg( 'badquery' ) . "==\n" .
135 wfMsg( 'badquerytext' ) );
136 wfProfileOut( $fname );
137 return;
138 }
139
140 global $wgDisableTextSearch;
141 if ( $wgDisableTextSearch ) {
142 global $wgForwardSearchUrl;
143 if( $wgForwardSearchUrl ) {
144 $url = str_replace( '$1', urlencode( $term ), $wgForwardSearchUrl );
145 $wgOut->redirect( $url );
146 return;
147 }
148 global $wgInputEncoding;
149 $wgOut->addHTML( wfMsg( 'searchdisabled' ) );
150 $wgOut->addHTML( wfMsg( 'googlesearch',
151 htmlspecialchars( $term ),
152 htmlspecialchars( $wgInputEncoding ) ) );
153 wfProfileOut( $fname );
154 return;
155 }
156
157 $search =& SearchEngine::create();
158 $search->setLimitOffset( $this->limit, $this->offset );
159 $search->setNamespaces( $this->namespaces );
160 $titleMatches = $search->searchTitle( $term );
161 $textMatches = $search->searchText( $term );
162
163 $num = $titleMatches->numRows() + $textMatches->numRows();
164 if ( $num >= $this->limit ) {
165 $top = wfShowingResults( $this->offset, $this->limit );
166 } else {
167 $top = wfShowingResultsNum( $this->offset, $this->limit, $num );
168 }
169 $wgOut->addHTML( "<p>{$top}</p>\n" );
170
171 if( $num || $this->offset ) {
172 $prevnext = wfViewPrevNext( $this->offset, $this->limit,
173 'Special:Search',
174 wfArrayToCGI(
175 $this->powerSearchOptions(),
176 array( 'search' => $term ) ) );
177 $wgOut->addHTML( "<br />{$prevnext}\n" );
178 }
179
180 global $wgContLang;
181 $tm = $wgContLang->convertForSearchResult( $search->termMatches() );
182 $terms = implode( '|', $tm );
183
184 if( $titleMatches->numRows() ) {
185 $wgOut->addWikiText( '==' . wfMsg( 'titlematches' ) . "==\n" );
186 $wgOut->addHTML( $this->showMatches( $titleMatches, $terms ) );
187 } else {
188 $wgOut->addWikiText( '==' . wfMsg( 'notitlematches' ) . "==\n" );
189 }
190
191 if( $textMatches->numRows() ) {
192 $wgOut->addWikiText( '==' . wfMsg( 'textmatches' ) . "==\n" );
193 $wgOut->addHTML( $this->showMatches( $textMatches, $terms ) );
194 } elseif( $num == 0 ) {
195 # Don't show the 'no text matches' if we received title matches
196 $wgOut->addWikiText( '==' . wfMsg( 'notextmatches' ) . "==\n" );
197 }
198
199 if ( $num == 0 ) {
200 $wgOut->addWikiText( wfMsg( 'nonefound' ) );
201 }
202 if( $num || $this->offset ) {
203 $wgOut->addHTML( "<p>{$prevnext}</p>\n" );
204 }
205 $wgOut->addHTML( $this->powerSearchBox( $term ) );
206 wfProfileOut( $fname );
207 }
208
209 #------------------------------------------------------------------
210 # Private methods below this line
211
212 /**
213 *
214 */
215 function setupPage( $term ) {
216 global $wgOut;
217 $wgOut->setPageTitle( wfMsg( 'searchresults' ) );
218 $wgOut->setSubtitle( wfMsg( 'searchquery', htmlspecialchars( $term ) ) );
219 $wgOut->setArticleRelated( false );
220 $wgOut->setRobotpolicy( 'noindex,nofollow' );
221 }
222
223 /**
224 * Extract default namespaces to search from the given user's
225 * settings, returning a list of index numbers.
226 *
227 * @param User $user
228 * @return array
229 * @access private
230 */
231 function userNamespaces( &$user ) {
232 $arr = array();
233 foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
234 if( $user->getOption( 'searchNs' . $ns ) ) {
235 $arr[] = $ns;
236 }
237 }
238 return $arr;
239 }
240
241 /**
242 * Extract "power search" namespace settings from the request object,
243 * returning a list of index numbers to search.
244 *
245 * @param WebRequest $request
246 * @return array
247 * @access private
248 */
249 function powerSearch( &$request ) {
250 $arr = array();
251 foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
252 if( $request->getCheck( 'ns' . $ns ) ) {
253 $arr[] = $ns;
254 }
255 }
256 return $arr;
257 }
258
259 /**
260 * Reconstruct the 'power search' options for links
261 * @return array
262 * @access private
263 */
264 function powerSearchOptions() {
265 $opt = array();
266 foreach( $this->namespaces as $n ) {
267 $opt['ns' . $n] = 1;
268 }
269 $opt['redirs'] = $this->searchRedirects ? 1 : 0;
270 $opt['searchx'] = 1;
271 return $opt;
272 }
273
274 /**
275 * @param ResultWrapper $matches
276 * @param string $terms partial regexp for highlighting terms
277 */
278 function showMatches( &$matches, $terms ) {
279 $fname = 'SpecialSearch::showMatches';
280 wfProfileIn( $fname );
281
282 global $wgOut;
283 $off = $this->offset + 1;
284 $out = "<ol start='{$off}'>\n";
285
286 while( $row = $matches->fetchObject() ) {
287 $out .= $this->showHit( $row, $terms );
288 }
289 $out .= "</ol>\n";
290
291 // convert the whole thing to desired language variant
292 global $wgContLang;
293 $out = $wgContLang->convert( $out );
294 wfProfileOut( $fname );
295 return $out;
296 }
297
298 /**
299 * Format a single hit result
300 * @param object $row
301 * @param string $terms partial regexp for highlighting terms
302 */
303 function showHit( $row, $terms ) {
304 $fname = 'SpecialSearch::showHit';
305 wfProfileIn( $fname );
306 global $wgUser, $wgContLang;
307
308 $t = Title::makeTitle( $row->page_namespace, $row->page_title );
309 if( is_null( $t ) ) {
310 wfProfileOut( $fname );
311 return "<!-- Broken link in search result -->\n";
312 }
313 $sk =& $wgUser->getSkin();
314
315 $contextlines = $wgUser->getOption( 'contextlines' );
316 if ( '' == $contextlines ) { $contextlines = 5; }
317 $contextchars = $wgUser->getOption( 'contextchars' );
318 if ( '' == $contextchars ) { $contextchars = 50; }
319
320 $link = $sk->makeKnownLinkObj( $t, '' );
321 $text = Revision::getRevisionText( $row );
322 $size = wfMsg( 'nbytes', strlen( $text ) );
323
324 $lines = explode( "\n", $text );
325
326 $max = IntVal( $contextchars ) + 1;
327 $pat1 = "/(.*)($terms)(.{0,$max})/i";
328
329 $lineno = 0;
330
331 $extract = '';
332 wfProfileIn( "$fname-extract" );
333 foreach ( $lines as $line ) {
334 if ( 0 == $contextlines ) {
335 break;
336 }
337 ++$lineno;
338 if ( ! preg_match( $pat1, $line, $m ) ) {
339 continue;
340 }
341 --$contextlines;
342 $pre = $wgContLang->truncate( $m[1], -$contextchars, '...' );
343
344 if ( count( $m ) < 3 ) {
345 $post = '';
346 } else {
347 $post = $wgContLang->truncate( $m[3], $contextchars, '...' );
348 }
349
350 $found = $m[2];
351
352 $line = htmlspecialchars( $pre . $found . $post );
353 $pat2 = '/(' . $terms . ")/i";
354 $line = preg_replace( $pat2,
355 "<span class='searchmatch'>\\1</span>", $line );
356
357 $extract .= "<br /><small>{$lineno}: {$line}</small>\n";
358 }
359 wfProfileOut( "$fname-extract" );
360 wfProfileOut( $fname );
361 return "<li>{$link} ({$size}){$extract}</li>\n";
362 }
363
364 function powerSearchBox( $term ) {
365 $namespaces = '';
366 foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
367 $checked = in_array( $ns, $this->namespaces )
368 ? ' checked="checked"'
369 : '';
370 $name = str_replace( '_', ' ', $name );
371 if( '' == $name ) {
372 $name = wfMsg( 'blanknamespace' );
373 }
374 $namespaces .= " <label><input type='checkbox' value=\"1\" name=\"" .
375 "ns{$ns}\"{$checked} />{$name}</label>\n";
376 }
377
378 $checked = $this->searchRedirects
379 ? ' checked="checked"'
380 : '';
381 $redirect = "<input type='checkbox' value='1' name=\"redirs\"{$checked} />\n";
382
383 $searchField = "<input type='text' name=\"search\" value=\"" .
384 htmlspecialchars( $term ) ."\" width=\"80\" />\n";
385
386 $searchButton = '<input type="submit" name="searchx" value="' .
387 htmlspecialchars( wfMsg('powersearch') ) . "\" />\n";
388
389 $ret = wfMsg( 'powersearchtext',
390 $namespaces, $redirect, $searchField,
391 '', '', '', '', '', # Dummy placeholders
392 $searchButton );
393
394 $title = Title::makeTitle( NS_SPECIAL, 'Search' );
395 $action = $title->escapeLocalURL();
396 return "<br /><br />\n<form id=\"powersearch\" method=\"get\" " .
397 "action=\"$action\">\n{$ret}\n</form>\n";
398 }
399 }
400
401 ?>