(bug 13616) Replace underscores in search terms by spaces
[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 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 * Run text & title search and display the output
22 * @addtogroup SpecialPage
23 */
24
25 /**
26 * Entry point
27 *
28 * @param $par String: (default '')
29 */
30 function wfSpecialSearch( $par = '' ) {
31 global $wgRequest, $wgUser;
32
33 $search = str_replace( array('_', "\n"), " ",
34 $wgRequest->getText( 'search', $par ) );
35 $searchPage = new SpecialSearch( $wgRequest, $wgUser );
36 if( $wgRequest->getVal( 'fulltext' ) ||
37 !is_null( $wgRequest->getVal( 'offset' ) ) ||
38 !is_null ($wgRequest->getVal( 'searchx' ) ) ) {
39 $searchPage->showResults( $search );
40 } else {
41 $searchPage->goResult( $search );
42 }
43 }
44
45 /**
46 * implements Special:Search - Run text & title search and display the output
47 * @addtogroup 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 * @public
58 */
59 function SpecialSearch( &$request, &$user ) {
60 list( $this->limit, $this->offset ) = $request->getLimitOffset( 20, 'searchlimit' );
61
62 $this->namespaces = $this->powerSearch( $request );
63 if( empty( $this->namespaces ) ) {
64 $this->namespaces = $this->userNamespaces( $user );
65 }
66
67 $this->searchRedirects = $request->getcheck( 'redirs' ) ? true : false;
68 }
69
70 /**
71 * If an exact title match can be found, jump straight ahead to it.
72 * @param string $term
73 * @public
74 */
75 function goResult( $term ) {
76 global $wgOut;
77 global $wgGoToEdit;
78
79 $this->setupPage( $term );
80
81 # Try to go to page as entered.
82 $t = Title::newFromText( $term );
83
84 # If the string cannot be used to create a title
85 if( is_null( $t ) ){
86 return $this->showResults( $term );
87 }
88
89 # If there's an exact or very near match, jump right there.
90 $t = SearchEngine::getNearMatch( $term );
91 if( !is_null( $t ) ) {
92 $wgOut->redirect( $t->getFullURL() );
93 return;
94 }
95
96 # No match, generate an edit URL
97 $t = Title::newFromText( $term );
98 if( ! is_null( $t ) ) {
99 wfRunHooks( 'SpecialSearchNogomatch', array( &$t ) );
100 # If the feature is enabled, go straight to the edit page
101 if ( $wgGoToEdit ) {
102 $wgOut->redirect( $t->getFullURL( 'action=edit' ) );
103 return;
104 }
105 }
106 if( $t->quickUserCan( 'create' ) && $t->quickUserCan( 'edit' ) ) {
107 $wgOut->addWikiMsg( 'noexactmatch', wfEscapeWikiText( $term ) );
108 } else {
109 $wgOut->addWikiMsg( 'noexactmatch-nocreate', wfEscapeWikiText( $term ) );
110 }
111
112 return $this->showResults( $term );
113 }
114
115 /**
116 * @param string $term
117 * @public
118 */
119 function showResults( $term ) {
120 $fname = 'SpecialSearch::showResults';
121 wfProfileIn( $fname );
122
123 $this->setupPage( $term );
124
125 global $wgOut;
126 $wgOut->addWikiMsg( 'searchresulttext' );
127
128 if( '' === trim( $term ) ) {
129 // Empty query -- straight view of search form
130 $wgOut->setSubtitle( '' );
131 $wgOut->addHTML( $this->powerSearchBox( $term ) );
132 $wgOut->addHTML( $this->powerSearchFocus() );
133 wfProfileOut( $fname );
134 return;
135 }
136
137 global $wgDisableTextSearch;
138 if ( $wgDisableTextSearch ) {
139 global $wgForwardSearchUrl;
140 if( $wgForwardSearchUrl ) {
141 $url = str_replace( '$1', urlencode( $term ), $wgForwardSearchUrl );
142 $wgOut->redirect( $url );
143 return;
144 }
145 global $wgInputEncoding;
146 $wgOut->addHTML( wfMsg( 'searchdisabled' ) );
147 $wgOut->addHTML(
148 wfMsg( 'googlesearch',
149 htmlspecialchars( $term ),
150 htmlspecialchars( $wgInputEncoding ),
151 htmlspecialchars( wfMsg( 'searchbutton' ) )
152 )
153 );
154 wfProfileOut( $fname );
155 return;
156 }
157
158 $wgOut->addHTML( $this->shortDialog( $term ) );
159
160 $search = SearchEngine::create();
161 $search->setLimitOffset( $this->limit, $this->offset );
162 $search->setNamespaces( $this->namespaces );
163 $search->showRedirects = $this->searchRedirects;
164 $rewritten = $search->replacePrefixes($term);
165
166 $titleMatches = $search->searchTitle( $rewritten );
167
168 // Sometimes the search engine knows there are too many hits
169 if ($titleMatches instanceof SearchResultTooMany) {
170 $wgOut->addWikiText( '==' . wfMsg( 'toomanymatches' ) . "==\n" );
171 $wgOut->addHTML( $this->powerSearchBox( $term ) );
172 $wgOut->addHTML( $this->powerSearchFocus() );
173 wfProfileOut( $fname );
174 return;
175 }
176 $textMatches = $search->searchText( $rewritten );
177
178 // did you mean...
179 if($textMatches && $textMatches->hasSuggestion()){
180 global $wgScript;
181 $fulltext = htmlspecialchars(wfMsg('search'));
182 $suggestLink = '<a href="'.$wgScript.'?title=Special:Search&amp;search='.
183 urlencode($textMatches->getSuggestionQuery()).'&amp;fulltext='.$fulltext.'">'
184 .$textMatches->getSuggestionSnippet().'</a>';
185 $wgOut->addHTML('<div class="searchdidyoumean">'.wfMsg('search-suggest',$suggestLink).'</div>');
186 }
187
188
189 $num = ( $titleMatches ? $titleMatches->numRows() : 0 )
190 + ( $textMatches ? $textMatches->numRows() : 0);
191 $totalNum = 0;
192 if($titleMatches && !is_null($titleMatches->getTotalHits()))
193 $totalNum += $titleMatches->getTotalHits();
194 if($textMatches && !is_null($textMatches->getTotalHits()))
195 $totalNum += $textMatches->getTotalHits();
196 if ( $num > 0 ) {
197 if ( $totalNum > 0 ){
198 $top = wfMsgExt('showingresultstotal',array( 'parseinline' ), $this->offset+1, $this->offset+$num, $totalNum);
199 } elseif ( $num >= $this->limit ) {
200 $top = wfShowingResults( $this->offset, $this->limit );
201 } else {
202 $top = wfShowingResultsNum( $this->offset, $this->limit, $num );
203 }
204 $wgOut->addHTML( "<p>{$top}</p>\n" );
205 }
206
207 if( $num || $this->offset ) {
208 $prevnext = wfViewPrevNext( $this->offset, $this->limit,
209 SpecialPage::getTitleFor( 'Search' ),
210 wfArrayToCGI(
211 $this->powerSearchOptions(),
212 array( 'search' => $term ) ),
213 ($num < $this->limit) );
214 $wgOut->addHTML( "<p>{$prevnext}</p>\n" );
215 wfRunHooks( 'SpecialSearchResults', array( $term, $titleMatches, $textMatches ) );
216 } else {
217 wfRunHooks( 'SpecialSearchNoResults', array( $term ) );
218 }
219
220 if( $titleMatches ) {
221 if( $titleMatches->numRows() ) {
222 $wgOut->wrapWikiMsg( "==$1==\n", 'titlematches' );
223 $wgOut->addHTML( $this->showMatches( $titleMatches ) );
224 } else {
225 $wgOut->wrapWikiMsg( "==$1==\n", 'notitlematches' );
226 }
227 $titleMatches->free();
228 }
229
230 if( $textMatches ) {
231 if( $textMatches->numRows() ) {
232 if($titleMatches)
233 $wgOut->wrapWikiMsg( "==$1==\n", 'textmatches' );
234 else // if no title matches the heading is redundant
235 $wgOut->addHTML("<hr/>");
236 $wgOut->addHTML( $this->showMatches( $textMatches ) );
237 } elseif( $num == 0 ) {
238 # Don't show the 'no text matches' if we received title matches
239 $wgOut->wrapWikiMsg( "==$1==\n", 'notextmatches' );
240 }
241 $textMatches->free();
242 }
243
244 if ( $num == 0 ) {
245 $wgOut->addWikiMsg( 'nonefound' );
246 }
247 if( $num || $this->offset ) {
248 $wgOut->addHTML( "<p>{$prevnext}</p>\n" );
249 }
250 $wgOut->addHTML( $this->powerSearchBox( $term ) );
251 wfProfileOut( $fname );
252 }
253
254 #------------------------------------------------------------------
255 # Private methods below this line
256
257 /**
258 *
259 */
260 function setupPage( $term ) {
261 global $wgOut;
262 $wgOut->setPageTitle( wfMsg( 'searchresults' ) );
263 $subtitlemsg = ( Title::newFromText($term) ? 'searchsubtitle' : 'searchsubtitleinvalid' );
264 $wgOut->setSubtitle( $wgOut->parse( wfMsg( $subtitlemsg, wfEscapeWikiText($term) ) ) );
265 $wgOut->setArticleRelated( false );
266 $wgOut->setRobotpolicy( 'noindex,nofollow' );
267 }
268
269 /**
270 * Extract default namespaces to search from the given user's
271 * settings, returning a list of index numbers.
272 *
273 * @param User $user
274 * @return array
275 * @private
276 */
277 function userNamespaces( &$user ) {
278 $arr = array();
279 foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
280 if( $user->getOption( 'searchNs' . $ns ) ) {
281 $arr[] = $ns;
282 }
283 }
284 return $arr;
285 }
286
287 /**
288 * Extract "power search" namespace settings from the request object,
289 * returning a list of index numbers to search.
290 *
291 * @param WebRequest $request
292 * @return array
293 * @private
294 */
295 function powerSearch( &$request ) {
296 $arr = array();
297 foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
298 if( $request->getCheck( 'ns' . $ns ) ) {
299 $arr[] = $ns;
300 }
301 }
302 return $arr;
303 }
304
305 /**
306 * Reconstruct the 'power search' options for links
307 * @return array
308 * @private
309 */
310 function powerSearchOptions() {
311 $opt = array();
312 foreach( $this->namespaces as $n ) {
313 $opt['ns' . $n] = 1;
314 }
315 $opt['redirs'] = $this->searchRedirects ? 1 : 0;
316 return $opt;
317 }
318
319
320
321 /**
322 * @param SearchResultSet $matches
323 * @param string $terms partial regexp for highlighting terms
324 */
325 function showMatches( &$matches ) {
326 $fname = 'SpecialSearch::showMatches';
327 wfProfileIn( $fname );
328
329 global $wgContLang;
330 $tm = $wgContLang->convertForSearchResult( $matches->termMatches() );
331 $terms = implode( '|', $tm );
332
333 $off = $this->offset + 1;
334 $out = "<ul start='{$off}' class='mw-search-results'>\n";
335
336 while( $result = $matches->next() ) {
337 $out .= $this->showHit( $result, $terms );
338 }
339 $out .= "</ul>\n";
340
341 // convert the whole thing to desired language variant
342 global $wgContLang;
343 $out = $wgContLang->convert( $out );
344 wfProfileOut( $fname );
345 return $out;
346 }
347
348 /**
349 * Format a single hit result
350 * @param SearchResult $result
351 * @param string $terms partial regexp for highlighting terms
352 */
353 function showHit( $result, $terms ) {
354 $fname = 'SpecialSearch::showHit';
355 wfProfileIn( $fname );
356 global $wgUser, $wgContLang, $wgLang;
357
358 $t = $result->getTitle();
359 if( is_null( $t ) ) {
360 wfProfileOut( $fname );
361 return "<!-- Broken link in search result -->\n";
362 }
363 $sk = $wgUser->getSkin();
364
365 //$contextlines = $wgUser->getOption( 'contextlines', 5 );
366 $contextlines = 2; // Hardcode this. Old defaults sucked. :)
367 $contextchars = $wgUser->getOption( 'contextchars', 50 );
368
369 $link = $sk->makeKnownLinkObj( $t, $result->getTitleSnippet());
370
371 //If page content is not readable, just return the title.
372 //This is not quite safe, but better than showing excerpts from non-readable pages
373 //Note that hiding the entry entirely would screw up paging.
374 if (!$t->userCanRead()) {
375 return "<li>{$link}</li>\n";
376 }
377
378 $revision = Revision::newFromTitle( $t );
379 // If the page doesn't *exist*... our search index is out of date.
380 // The least confusing at this point is to drop the result.
381 // You may get less results, but... oh well. :P
382 if( !$revision ) {
383 return "<!-- missing page " .
384 htmlspecialchars( $t->getPrefixedText() ) . "-->\n";
385 }
386
387 if( is_null( $result->getScore() ) ) {
388 // Search engine doesn't report scoring info
389 $score = '';
390 } else {
391 $percent = sprintf( '%2.1f', $result->getScore() * 100 );
392 $score = wfMsg( 'search-result-score', $wgLang->formatNum( $percent ) )
393 . ' - ';
394 }
395
396 // try to fetch everything from the search engine backend
397 // then fill-in what couldn't be fetched
398 $extract = $result->getTextSnippet();
399 $byteSize = $result->getByteSize();
400 $wordCount = $result->getWordCount();
401 $timestamp = $result->getTimestamp();
402 $redirectTitle = $result->getRedirectTitle();
403 $redirectText = $result->getRedirectSnippet();
404 $sectionTitle = $result->getSectionTitle();
405 $sectionText = $result->getSectionSnippet();
406
407 // fallback
408 if( is_null($extract) || is_null($wordCount) || is_null($byteSize) ){
409 $text = $revision->getText();
410 if( is_null($extract) )
411 $extract = $this->extractText( $text, $terms, $contextlines, $contextchars );
412 if( is_null($byteSize) )
413 $byteSize = strlen( $text );
414 if( is_null($wordCount) )
415 $wordCount = str_word_count( $text );
416 }
417 if( is_null($timestamp) ){
418 $timestamp = $revision->getTimestamp();
419 }
420
421 // format description
422 $size = wfMsgExt( 'search-result-size', array( 'parsemag', 'escape' ),
423 $sk->formatSize( $byteSize ),
424 $wordCount );
425 $date = $wgLang->timeanddate( $timestamp );
426
427 // format redirects / sections
428 $redirect = '';
429 if( !is_null($redirectTitle) )
430 $redirect = "<span class='searchalttitle'>"
431 .wfMsg('search-redirect',$sk->makeKnownLinkObj( $redirectTitle, $redirectText))
432 ."</span>";
433 $section = '';
434 if( !is_null($sectionTitle) )
435 $section = "<span class='searchalttitle'>"
436 .wfMsg('search-section', $sk->makeKnownLinkObj( $sectionTitle, $sectionText))
437 ."</span>";
438 // wrap extract
439 $extract = "<div class='searchresult'>".$extract."</div>";
440
441 // Include a thumbnail for media files...
442 if( $t->getNamespace() == NS_IMAGE ) {
443 $img = wfFindFile( $t );
444 if( $img ) {
445 $thumb = $img->getThumbnail( 120, 120 );
446 if( $thumb ) {
447 $desc = $img->getShortDesc();
448 wfProfileOut( $fname );
449 // Ugly table. :D
450 // Float doesn't seem to interact well with the bullets.
451 // Table messes up vertical alignment of the bullet, but I'm
452 // not sure what more I can do about that. :(
453 return "<li>" .
454 '<table class="searchResultImage">' .
455 '<tr>' .
456 '<td width="120" align="center">' .
457 $thumb->toHtml( array( 'desc-link' => true ) ) .
458 '</td>' .
459 '<td valign="top">' .
460 $link .
461 $extract .
462 "<div class='mw-search-result-data'>{$score}{$desc} - {$date}</div>" .
463 '</td>' .
464 '</tr>' .
465 '</table>' .
466 "</li>\n";
467 }
468 }
469 }
470
471 wfProfileOut( $fname );
472 return "<li>{$link} {$redirect} {$section} {$extract}\n" .
473 "<div class='mw-search-result-data'>{$score}{$size} - {$date}</div>" .
474 "</li>\n";
475
476 }
477
478 private function extractText( $text, $terms, $contextlines, $contextchars ) {
479 global $wgLang, $wgContLang;
480 $fname = __METHOD__;
481
482 $lines = explode( "\n", $text );
483
484 $max = intval( $contextchars ) + 1;
485 $pat1 = "/(.*)($terms)(.{0,$max})/i";
486
487 $lineno = 0;
488
489 $extract = "";
490 wfProfileIn( "$fname-extract" );
491 foreach ( $lines as $line ) {
492 if ( 0 == $contextlines ) {
493 break;
494 }
495 ++$lineno;
496 $m = array();
497 if ( ! preg_match( $pat1, $line, $m ) ) {
498 continue;
499 }
500 --$contextlines;
501 $pre = $wgContLang->truncate( $m[1], -$contextchars, ' ... ' );
502
503 if ( count( $m ) < 3 ) {
504 $post = '';
505 } else {
506 $post = $wgContLang->truncate( $m[3], $contextchars, ' ... ' );
507 }
508
509 $found = $m[2];
510
511 $line = htmlspecialchars( $pre . $found . $post );
512 $pat2 = '/(' . $terms . ")/i";
513 $line = preg_replace( $pat2,
514 "<span class='searchmatch'>\\1</span>", $line );
515
516 $extract .= "${line}\n";
517 }
518 wfProfileOut( "$fname-extract" );
519
520 return $extract;
521 }
522
523 /**
524 * Generates the power search box at bottom of [[Special:Search]]
525 * @param $term string: search term
526 * @return $out string: HTML form
527 */
528 function powerSearchBox( $term ) {
529 global $wgScript;
530
531 $namespaces = '';
532 foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
533 $name = str_replace( '_', ' ', $name );
534 if( '' == $name ) {
535 $name = wfMsg( 'blanknamespace' );
536 }
537 $namespaces .= Xml::openElement( 'span', array( 'style' => 'white-space: nowrap' ) ) .
538 Xml::checkLabel( $name, "ns{$ns}", $name, in_array( $ns, $this->namespaces ) ) .
539 Xml::closeElement( 'span' ) . "\n";
540 }
541
542 $redirect = Xml::check( 'redirs', $this->searchRedirects, array( 'value' => '1' ) );
543 $searchField = Xml::input( 'search', 50, $term, array( 'type' => 'text', 'id' => 'powerSearchText' ) );
544 $searchButton = Xml::submitButton( wfMsg( 'powersearch' ), array( 'name' => 'fulltext' ) ) . "\n";
545
546 $out = Xml::openElement( 'form', array( 'id' => 'powersearch', 'method' => 'get', 'action' => $wgScript ) ) .
547 Xml::openElement( 'fieldset' ) .
548 Xml::element( 'legend', array( ), wfMsg( 'powersearch-legend' ) ) .
549 Xml::hidden( 'title', 'Special:Search' ) .
550 wfMsgExt( 'powersearchtext', array( 'parse', 'replaceafter' ),
551 $namespaces, $redirect, $searchField,
552 '', '', '', '', '', # Dummy placeholders
553 $searchButton ) .
554 Xml::closeElement( 'fieldset' ) .
555 Xml::closeElement( 'form' );
556
557 return $out;
558 }
559
560 function powerSearchFocus() {
561 return "<script type='text/javascript'>" .
562 "document.getElementById('powerSearchText').focus();" .
563 "</script>";
564 }
565
566 function shortDialog($term) {
567 global $wgScript;
568
569 $out = Xml::openElement( 'form', array(
570 'id' => 'search',
571 'method' => 'get',
572 'action' => $wgScript
573 ));
574 $out .= Xml::hidden( 'title', 'Special:Search' );
575 $out .= Xml::input( 'search', 50, $term ) . ' ';
576 foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
577 if( in_array( $ns, $this->namespaces ) ) {
578 $out .= Xml::hidden( "ns{$ns}", '1' );
579 }
580 }
581 $out .= Xml::submitButton( wfMsg( 'searchbutton' ), array( 'name' => 'fulltext' ) );
582 $out .= Xml::closeElement( 'form' );
583
584 return $out;
585 }
586 }