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