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