MediaWiki  master
SpecialSearch.php
Go to the documentation of this file.
00001 <?php
00030 class SpecialSearch extends SpecialPage {
00039         protected $profile;
00040         function getProfile() { return $this->profile; }
00041 
00043         protected $searchEngine;
00044 
00046         protected $extraParams = array();
00047 
00049         protected $mPrefix;
00050 
00054         protected $limit, $offset;
00055 
00059         protected $namespaces;
00060         function getNamespaces() { return $this->namespaces; }
00061 
00065         protected $searchRedirects;
00066 
00070         protected $didYouMeanHtml, $fulltext;
00071 
00072         const NAMESPACES_CURRENT = 'sense';
00073 
00074         public function __construct() {
00075                 parent::__construct( 'Search' );
00076         }
00077 
00083         public function execute( $par ) {
00084                 $this->setHeaders();
00085                 $this->outputHeader();
00086                 $out = $this->getOutput();
00087                 $out->allowClickjacking();
00088                 $out->addModuleStyles( 'mediawiki.special' );
00089 
00090                 // Strip underscores from title parameter; most of the time we'll want
00091                 // text form here. But don't strip underscores from actual text params!
00092                 $titleParam = str_replace( '_', ' ', $par );
00093 
00094                 $request = $this->getRequest();
00095 
00096                 // Fetch the search term
00097                 $search = str_replace( "\n", " ", $request->getText( 'search', $titleParam ) );
00098 
00099                 $this->load();
00100 
00101                 if ( $request->getVal( 'fulltext' )
00102                         || !is_null( $request->getVal( 'offset' ) )
00103                         || !is_null( $request->getVal( 'searchx' ) ) )
00104                 {
00105                         $this->showResults( $search );
00106                 } else {
00107                         $this->goResult( $search );
00108                 }
00109         }
00110 
00116         public function load() {
00117                 $request = $this->getRequest();
00118                 list( $this->limit, $this->offset ) = $request->getLimitOffset( 20, 'searchlimit' );
00119                 $this->mPrefix = $request->getVal( 'prefix', '' );
00120 
00121                 $user = $this->getUser();
00122 
00123                 # Extract manually requested namespaces
00124                 $nslist = $this->powerSearch( $request );
00125                 if ( !count( $nslist ) ) {
00126                         # Fallback to user preference
00127                         $nslist = SearchEngine::userNamespaces( $user );
00128                 }
00129 
00130                 $profile = null;
00131                 if ( !count( $nslist ) ) {
00132                         $profile = 'default';
00133                 }
00134 
00135                 $profile = $request->getVal( 'profile', $profile );
00136                 $profiles = $this->getSearchProfiles();
00137                 if ( $profile === null ) {
00138                         // BC with old request format
00139                         $profile = 'advanced';
00140                         foreach( $profiles as $key => $data ) {
00141                                 if ( $nslist === $data['namespaces'] && $key !== 'advanced') {
00142                                         $profile = $key;
00143                                 }
00144                         }
00145                         $this->namespaces = $nslist;
00146                 } elseif ( $profile === 'advanced' ) {
00147                         $this->namespaces = $nslist;
00148                 } else {
00149                         if ( isset( $profiles[$profile]['namespaces'] ) ) {
00150                                 $this->namespaces = $profiles[$profile]['namespaces'];
00151                         } else {
00152                                 // Unknown profile requested
00153                                 $profile = 'default';
00154                                 $this->namespaces = $profiles['default']['namespaces'];
00155                         }
00156                 }
00157 
00158                 // Redirects defaults to true, but we don't know whether it was ticked of or just missing
00159                 $default = $request->getBool( 'profile' ) ? 0 : 1;
00160                 $this->searchRedirects = $request->getBool( 'redirs', $default ) ? 1 : 0;
00161                 $this->didYouMeanHtml = ''; # html of did you mean... link
00162                 $this->fulltext = $request->getVal('fulltext');
00163                 $this->profile = $profile;
00164         }
00165 
00171         public function goResult( $term ) {
00172                 $this->setupPage( $term );
00173                 # Try to go to page as entered.
00174                 $t = Title::newFromText( $term );
00175                 # If the string cannot be used to create a title
00176                 if( is_null( $t ) ) {
00177                         $this->showResults( $term );
00178                         return;
00179                 }
00180                 # If there's an exact or very near match, jump right there.
00181                 $t = SearchEngine::getNearMatch( $term );
00182 
00183                 if ( !wfRunHooks( 'SpecialSearchGo', array( &$t, &$term ) ) ) {
00184                         # Hook requested termination
00185                         return;
00186                 }
00187 
00188                 if( !is_null( $t ) ) {
00189                         $this->getOutput()->redirect( $t->getFullURL() );
00190                         return;
00191                 }
00192                 # No match, generate an edit URL
00193                 $t = Title::newFromText( $term );
00194                 if( !is_null( $t ) ) {
00195                         global $wgGoToEdit;
00196                         wfRunHooks( 'SpecialSearchNogomatch', array( &$t ) );
00197                         wfDebugLog( 'nogomatch', $t->getText(), false );
00198 
00199                         # If the feature is enabled, go straight to the edit page
00200                         if( $wgGoToEdit ) {
00201                                 $this->getOutput()->redirect( $t->getFullURL( array( 'action' => 'edit' ) ) );
00202                                 return;
00203                         }
00204                 }
00205                 $this->showResults( $term );
00206         }
00207 
00211         public function showResults( $term ) {
00212                 global $wgDisableTextSearch, $wgSearchForwardUrl, $wgContLang, $wgScript;
00213                 wfProfileIn( __METHOD__ );
00214 
00215                 $search = $this->getSearchEngine();
00216                 $search->setLimitOffset( $this->limit, $this->offset );
00217                 $search->setNamespaces( $this->namespaces );
00218                 $search->showRedirects = $this->searchRedirects; // BC
00219                 $search->setFeatureData( 'list-redirects', $this->searchRedirects );
00220                 $search->prefix = $this->mPrefix;
00221                 $term = $search->transformSearchTerm($term);
00222 
00223                 wfRunHooks( 'SpecialSearchSetupEngine', array( $this, $this->profile, $search ) );
00224 
00225                 $this->setupPage( $term );
00226 
00227                 $out = $this->getOutput();
00228 
00229                 if ( $wgDisableTextSearch ) {
00230                         if ( $wgSearchForwardUrl ) {
00231                                 $url = str_replace( '$1', urlencode( $term ), $wgSearchForwardUrl );
00232                                 $out->redirect( $url );
00233                         } else {
00234                                 $out->addHTML(
00235                                         Xml::openElement( 'fieldset' ) .
00236                                         Xml::element( 'legend', null, $this->msg( 'search-external' )->text() ) .
00237                                         Xml::element( 'p', array( 'class' => 'mw-searchdisabled' ), $this->msg( 'searchdisabled' )->text() ) .
00238                                         $this->msg( 'googlesearch' )->rawParams(
00239                                                 htmlspecialchars( $term ),
00240                                                 'UTF-8',
00241                                                 $this->msg( 'searchbutton' )->escaped()
00242                                         )->text() .
00243                                         Xml::closeElement( 'fieldset' )
00244                                 );
00245                         }
00246                         wfProfileOut( __METHOD__ );
00247                         return;
00248                 }
00249 
00250                 $t = Title::newFromText( $term );
00251 
00252                 // fetch search results
00253                 $rewritten = $search->replacePrefixes($term);
00254 
00255                 $titleMatches = $search->searchTitle( $rewritten );
00256                 if( !( $titleMatches instanceof SearchResultTooMany ) ) {
00257                         $textMatches = $search->searchText( $rewritten );
00258                 }
00259 
00260                 // did you mean... suggestions
00261                 if( $textMatches && $textMatches->hasSuggestion() ) {
00262                         $st = SpecialPage::getTitleFor( 'Search' );
00263 
00264                         # mirror Go/Search behaviour of original request ..
00265                         $didYouMeanParams = array( 'search' => $textMatches->getSuggestionQuery() );
00266 
00267                         if( $this->fulltext != null ) {
00268                                 $didYouMeanParams['fulltext'] = $this->fulltext;
00269                         }
00270 
00271                         $stParams = array_merge(
00272                                 $didYouMeanParams,
00273                                 $this->powerSearchOptions()
00274                         );
00275 
00276                         $suggestionSnippet = $textMatches->getSuggestionSnippet();
00277 
00278                         if( $suggestionSnippet == '' ) {
00279                                 $suggestionSnippet = null;
00280                         }
00281 
00282                         $suggestLink = Linker::linkKnown(
00283                                 $st,
00284                                 $suggestionSnippet,
00285                                 array(),
00286                                 $stParams
00287                         );
00288 
00289                         $this->didYouMeanHtml = '<div class="searchdidyoumean">' . $this->msg( 'search-suggest' )->rawParams( $suggestLink )->text() . '</div>';
00290                 }
00291                 // start rendering the page
00292                 $out->addHtml(
00293                         Xml::openElement(
00294                                 'form',
00295                                 array(
00296                                         'id' => ( $this->profile === 'advanced' ? 'powersearch' : 'search' ),
00297                                         'method' => 'get',
00298                                         'action' => $wgScript
00299                                 )
00300                         )
00301                 );
00302                 $out->addHtml(
00303                         Xml::openElement( 'table', array( 'id' => 'mw-search-top-table', 'cellpadding' => 0, 'cellspacing' => 0 ) ) .
00304                         Xml::openElement( 'tr' ) .
00305                         Xml::openElement( 'td' ) . "\n" .
00306                         $this->shortDialog( $term ) .
00307                         Xml::closeElement('td') .
00308                         Xml::closeElement('tr') .
00309                         Xml::closeElement('table')
00310                 );
00311 
00312                 // Sometimes the search engine knows there are too many hits
00313                 if( $titleMatches instanceof SearchResultTooMany ) {
00314                         $out->wrapWikiMsg( "==$1==\n", 'toomanymatches' );
00315                         wfProfileOut( __METHOD__ );
00316                         return;
00317                 }
00318 
00319                 $filePrefix = $wgContLang->getFormattedNsText(NS_FILE).':';
00320                 if( trim( $term ) === '' || $filePrefix === trim( $term ) ) {
00321                         $out->addHTML( $this->formHeader( $term, 0, 0 ) );
00322                         $out->addHtml( $this->getProfileForm( $this->profile, $term ) );
00323                         $out->addHTML( '</form>' );
00324                         // Empty query -- straight view of search form
00325                         wfProfileOut( __METHOD__ );
00326                         return;
00327                 }
00328 
00329                 // Get number of results
00330                 $titleMatchesNum = $titleMatches ? $titleMatches->numRows() : 0;
00331                 $textMatchesNum = $textMatches ? $textMatches->numRows() : 0;
00332                 // Total initial query matches (possible false positives)
00333                 $num = $titleMatchesNum + $textMatchesNum;
00334 
00335                 // Get total actual results (after second filtering, if any)
00336                 $numTitleMatches = $titleMatches && !is_null( $titleMatches->getTotalHits() ) ?
00337                         $titleMatches->getTotalHits() : $titleMatchesNum;
00338                 $numTextMatches = $textMatches && !is_null( $textMatches->getTotalHits() ) ?
00339                         $textMatches->getTotalHits() : $textMatchesNum;
00340 
00341                 // get total number of results if backend can calculate it
00342                 $totalRes = 0;
00343                 if($titleMatches && !is_null( $titleMatches->getTotalHits() ) )
00344                         $totalRes += $titleMatches->getTotalHits();
00345                 if($textMatches && !is_null( $textMatches->getTotalHits() ))
00346                         $totalRes += $textMatches->getTotalHits();
00347 
00348                 // show number of results and current offset
00349                 $out->addHTML( $this->formHeader( $term, $num, $totalRes ) );
00350                 $out->addHtml( $this->getProfileForm( $this->profile, $term ) );
00351 
00352 
00353                 $out->addHtml( Xml::closeElement( 'form' ) );
00354                 $out->addHtml( "<div class='searchresults'>" );
00355 
00356                 // prev/next links
00357                 if( $num || $this->offset ) {
00358                         // Show the create link ahead
00359                         $this->showCreateLink( $t );
00360                         $prevnext = $this->getLanguage()->viewPrevNext( $this->getTitle(), $this->offset, $this->limit,
00361                                 $this->powerSearchOptions() + array( 'search' => $term ),
00362                                 max( $titleMatchesNum, $textMatchesNum ) < $this->limit
00363                         );
00364                         //$out->addHTML( "<p class='mw-search-pager-top'>{$prevnext}</p>\n" );
00365                         wfRunHooks( 'SpecialSearchResults', array( $term, &$titleMatches, &$textMatches ) );
00366                 } else {
00367                         wfRunHooks( 'SpecialSearchNoResults', array( $term ) );
00368                 }
00369 
00370                 $out->parserOptions()->setEditSection( false );
00371                 if( $titleMatches ) {
00372                         if( $numTitleMatches > 0 ) {
00373                                 $out->wrapWikiMsg( "==$1==\n", 'titlematches' );
00374                                 $out->addHTML( $this->showMatches( $titleMatches ) );
00375                         }
00376                         $titleMatches->free();
00377                 }
00378                 if( $textMatches ) {
00379                         // output appropriate heading
00380                         if( $numTextMatches > 0 && $numTitleMatches > 0 ) {
00381                                 // if no title matches the heading is redundant
00382                                 $out->wrapWikiMsg( "==$1==\n", 'textmatches' );
00383                         } elseif( $totalRes == 0 ) {
00384                                 # Don't show the 'no text matches' if we received title matches
00385                                 # $out->wrapWikiMsg( "==$1==\n", 'notextmatches' );
00386                         }
00387                         // show interwiki results if any
00388                         if( $textMatches->hasInterwikiResults() ) {
00389                                 $out->addHTML( $this->showInterwiki( $textMatches->getInterwikiResults(), $term ) );
00390                         }
00391                         // show results
00392                         if( $numTextMatches > 0 ) {
00393                                 $out->addHTML( $this->showMatches( $textMatches ) );
00394                         }
00395 
00396                         $textMatches->free();
00397                 }
00398                 if( $num === 0 ) {
00399                         $out->wrapWikiMsg( "<p class=\"mw-search-nonefound\">\n$1</p>", array( 'search-nonefound', wfEscapeWikiText( $term ) ) );
00400                         $this->showCreateLink( $t );
00401                 }
00402                 $out->addHtml( "</div>" );
00403 
00404                 if( $num || $this->offset ) {
00405                         $out->addHTML( "<p class='mw-search-pager-bottom'>{$prevnext}</p>\n" );
00406                 }
00407                 wfProfileOut( __METHOD__ );
00408         }
00409 
00413         protected function showCreateLink( $t ) {
00414                 // show direct page/create link if applicable
00415 
00416                 // Check DBkey !== '' in case of fragment link only.
00417                 if( is_null( $t ) || $t->getDBkey() === '' ) {
00418                         // invalid title
00419                         // preserve the paragraph for margins etc...
00420                         $this->getOutput()->addHtml( '<p></p>' );
00421                         return;
00422                 }
00423 
00424                 if( $t->isKnown() ) {
00425                         $messageName = 'searchmenu-exists';
00426                 } elseif( $t->userCan( 'create', $this->getUser() ) ) {
00427                         $messageName = 'searchmenu-new';
00428                 } else {
00429                         $messageName = 'searchmenu-new-nocreate';
00430                 }
00431                 $params = array( $messageName, wfEscapeWikiText( $t->getPrefixedText() ) );
00432                 wfRunHooks( 'SpecialSearchCreateLink', array( $t, &$params ) );
00433 
00434                 // Extensions using the hook might still return an empty $messageName
00435                 if( $messageName ) {
00436                         $this->getOutput()->wrapWikiMsg( "<p class=\"mw-search-createlink\">\n$1</p>", $params );
00437                 } else {
00438                         // preserve the paragraph for margins etc...
00439                         $this->getOutput()->addHtml( '<p></p>' );
00440                 }
00441         }
00442 
00446         protected function setupPage( $term ) {
00447                 # Should advanced UI be used?
00448                 $this->searchAdvanced = ($this->profile === 'advanced');
00449                 $out = $this->getOutput();
00450                 if( strval( $term ) !== ''  ) {
00451                         $out->setPageTitle( $this->msg( 'searchresults' ) );
00452                         $out->setHTMLTitle( $this->msg( 'pagetitle' )->rawParams(
00453                                 $this->msg( 'searchresults-title' )->rawParams( $term )->text()
00454                         ) );
00455                 }
00456                 // add javascript specific to special:search
00457                 $out->addModules( 'mediawiki.special.search' );
00458         }
00459 
00467         protected function powerSearch( &$request ) {
00468                 $arr = array();
00469                 foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
00470                         if( $request->getCheck( 'ns' . $ns ) ) {
00471                                 $arr[] = $ns;
00472                         }
00473                 }
00474 
00475                 return $arr;
00476         }
00477 
00483         protected function powerSearchOptions() {
00484                 $opt = array();
00485                 $opt['redirs'] = $this->searchRedirects ? 1 : 0;
00486                 if( $this->profile !== 'advanced' ) {
00487                         $opt['profile'] = $this->profile;
00488                 } else {
00489                         foreach( $this->namespaces as $n ) {
00490                                 $opt['ns' . $n] = 1;
00491                         }
00492                 }
00493                 return $opt + $this->extraParams;
00494         }
00495 
00503         protected function showMatches( &$matches ) {
00504                 global $wgContLang;
00505                 wfProfileIn( __METHOD__ );
00506 
00507                 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
00508 
00509                 $out = "";
00510                 $infoLine = $matches->getInfo();
00511                 if( !is_null($infoLine) ) {
00512                         $out .= "\n<!-- {$infoLine} -->\n";
00513                 }
00514                 $out .= "<ul class='mw-search-results'>\n";
00515                 $result = $matches->next();
00516                 while( $result ) {
00517                         $out .= $this->showHit( $result, $terms );
00518                         $result = $matches->next();
00519                 }
00520                 $out .= "</ul>\n";
00521 
00522                 // convert the whole thing to desired language variant
00523                 $out = $wgContLang->convert( $out );
00524                 wfProfileOut( __METHOD__ );
00525                 return $out;
00526         }
00527 
00536         protected function showHit( $result, $terms ) {
00537                 wfProfileIn( __METHOD__ );
00538 
00539                 if( $result->isBrokenTitle() ) {
00540                         wfProfileOut( __METHOD__ );
00541                         return "<!-- Broken link in search result -->\n";
00542                 }
00543 
00544                 $t = $result->getTitle();
00545 
00546                 $titleSnippet = $result->getTitleSnippet($terms);
00547 
00548                 if( $titleSnippet == '' )
00549                         $titleSnippet = null;
00550 
00551                 $link_t = clone $t;
00552 
00553                 wfRunHooks( 'ShowSearchHitTitle',
00554                                         array( &$link_t, &$titleSnippet, $result, $terms, $this ) );
00555 
00556                 $link = Linker::linkKnown(
00557                         $link_t,
00558                         $titleSnippet
00559                 );
00560 
00561                 //If page content is not readable, just return the title.
00562                 //This is not quite safe, but better than showing excerpts from non-readable pages
00563                 //Note that hiding the entry entirely would screw up paging.
00564                 if( !$t->userCan( 'read', $this->getUser() ) ) {
00565                         wfProfileOut( __METHOD__ );
00566                         return "<li>{$link}</li>\n";
00567                 }
00568 
00569                 // If the page doesn't *exist*... our search index is out of date.
00570                 // The least confusing at this point is to drop the result.
00571                 // You may get less results, but... oh well. :P
00572                 if( $result->isMissingRevision() ) {
00573                         wfProfileOut( __METHOD__ );
00574                         return "<!-- missing page " . htmlspecialchars( $t->getPrefixedText() ) . "-->\n";
00575                 }
00576 
00577                 // format redirects / relevant sections
00578                 $redirectTitle = $result->getRedirectTitle();
00579                 $redirectText = $result->getRedirectSnippet($terms);
00580                 $sectionTitle = $result->getSectionTitle();
00581                 $sectionText = $result->getSectionSnippet($terms);
00582                 $redirect = '';
00583 
00584                 if( !is_null($redirectTitle) ) {
00585                         if( $redirectText == '' )
00586                                 $redirectText = null;
00587 
00588                         $redirect = "<span class='searchalttitle'>" .
00589                                 $this->msg( 'search-redirect' )->rawParams(
00590                                         Linker::linkKnown( $redirectTitle, $redirectText ) )->text() .
00591                                 "</span>";
00592                 }
00593 
00594                 $section = '';
00595 
00596                 if( !is_null($sectionTitle) ) {
00597                         if( $sectionText == '' )
00598                                 $sectionText = null;
00599 
00600                         $section = "<span class='searchalttitle'>" .
00601                                 $this->msg( 'search-section' )->rawParams(
00602                                         Linker::linkKnown( $sectionTitle, $sectionText ) )->text() .
00603                                 "</span>";
00604                 }
00605 
00606                 // format text extract
00607                 $extract = "<div class='searchresult'>".$result->getTextSnippet($terms)."</div>";
00608 
00609                 $lang = $this->getLanguage();
00610 
00611                 // format score
00612                 if( is_null( $result->getScore() ) ) {
00613                         // Search engine doesn't report scoring info
00614                         $score = '';
00615                 } else {
00616                         $percent = sprintf( '%2.1f', $result->getScore() * 100 );
00617                         $score = $this->msg( 'search-result-score' )->numParams( $percent )->text()
00618                                 . ' - ';
00619                 }
00620 
00621                 // format description
00622                 $byteSize = $result->getByteSize();
00623                 $wordCount = $result->getWordCount();
00624                 $timestamp = $result->getTimestamp();
00625                 $size = $this->msg( 'search-result-size', $lang->formatSize( $byteSize ) )
00626                         ->numParams( $wordCount )->escaped();
00627 
00628                 if( $t->getNamespace() == NS_CATEGORY ) {
00629                         $cat = Category::newFromTitle( $t );
00630                         $size = $this->msg( 'search-result-category-size' )
00631                                 ->numParams( $cat->getPageCount(), $cat->getSubcatCount(), $cat->getFileCount() )
00632                                 ->escaped();
00633                 }
00634 
00635                 $date = $lang->userTimeAndDate( $timestamp, $this->getUser() );
00636 
00637                 // link to related articles if supported
00638                 $related = '';
00639                 if( $result->hasRelated() ) {
00640                         $st = SpecialPage::getTitleFor( 'Search' );
00641                         $stParams = array_merge(
00642                                 $this->powerSearchOptions(),
00643                                 array(
00644                                         'search' => $this->msg( 'searchrelated' )->inContentLanguage()->text() .
00645                                                 ':' . $t->getPrefixedText(),
00646                                         'fulltext' => $this->msg( 'search' )->text()
00647                                 )
00648                         );
00649 
00650                         $related = ' -- ' . Linker::linkKnown(
00651                                 $st,
00652                                 $this->msg( 'search-relatedarticle' )->text(),
00653                                 array(),
00654                                 $stParams
00655                         );
00656                 }
00657 
00658                 // Include a thumbnail for media files...
00659                 if( $t->getNamespace() == NS_FILE ) {
00660                         $img = wfFindFile( $t );
00661                         if( $img ) {
00662                                 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
00663                                 if( $thumb ) {
00664                                         $desc = $this->msg( 'parentheses' )->rawParams( $img->getShortDesc() )->escaped();
00665                                         wfProfileOut( __METHOD__ );
00666                                         // Float doesn't seem to interact well with the bullets.
00667                                         // Table messes up vertical alignment of the bullets.
00668                                         // Bullets are therefore disabled (didn't look great anyway).
00669                                         return "<li>" .
00670                                                 '<table class="searchResultImage">' .
00671                                                 '<tr>' .
00672                                                 '<td style="width: 120px; text-align: center; vertical-align: top;">' .
00673                                                 $thumb->toHtml( array( 'desc-link' => true ) ) .
00674                                                 '</td>' .
00675                                                 '<td style="vertical-align: top;">' .
00676                                                 $link .
00677                                                 $extract .
00678                                                 "<div class='mw-search-result-data'>{$score}{$desc} - {$date}{$related}</div>" .
00679                                                 '</td>' .
00680                                                 '</tr>' .
00681                                                 '</table>' .
00682                                                 "</li>\n";
00683                                 }
00684                         }
00685                 }
00686 
00687                 $html = null;
00688 
00689                 if ( wfRunHooks( 'ShowSearchHit', array (
00690                         $this, $result, $terms,
00691                         &$link, &$redirect, &$section, &$extract,
00692                         &$score, &$size, &$date, &$related,
00693                         &$html
00694                 ) ) ) {
00695                         $html = "<li><div class='mw-search-result-heading'>{$link} {$redirect} {$section}</div> {$extract}\n" .
00696                                 "<div class='mw-search-result-data'>{$score}{$size} - {$date}{$related}</div>" .
00697                                 "</li>\n";
00698                 }
00699 
00700                 wfProfileOut( __METHOD__ );
00701                 return $html;
00702         }
00703 
00712         protected function showInterwiki( &$matches, $query ) {
00713                 global $wgContLang;
00714                 wfProfileIn( __METHOD__ );
00715                 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
00716 
00717                 $out = "<div id='mw-search-interwiki'><div id='mw-search-interwiki-caption'>".
00718                         $this->msg( 'search-interwiki-caption' )->text()  . "</div>\n";
00719                 $out .= "<ul class='mw-search-iwresults'>\n";
00720 
00721                 // work out custom project captions
00722                 $customCaptions = array();
00723                 $customLines = explode( "\n", $this->msg( 'search-interwiki-custom' )->text() ); // format per line <iwprefix>:<caption>
00724                 foreach($customLines as $line) {
00725                         $parts = explode(":",$line,2);
00726                         if(count($parts) == 2) // validate line
00727                                 $customCaptions[$parts[0]] = $parts[1];
00728                 }
00729 
00730                 $prev = null;
00731                 $result = $matches->next();
00732                 while( $result ) {
00733                         $out .= $this->showInterwikiHit( $result, $prev, $terms, $query, $customCaptions );
00734                         $prev = $result->getInterwikiPrefix();
00735                         $result = $matches->next();
00736                 }
00737                 // TODO: should support paging in a non-confusing way (not sure how though, maybe via ajax)..
00738                 $out .= "</ul></div>\n";
00739 
00740                 // convert the whole thing to desired language variant
00741                 $out = $wgContLang->convert( $out );
00742                 wfProfileOut( __METHOD__ );
00743                 return $out;
00744         }
00745 
00757         protected function showInterwikiHit( $result, $lastInterwiki, $terms, $query, $customCaptions) {
00758                 wfProfileIn( __METHOD__ );
00759 
00760                 if( $result->isBrokenTitle() ) {
00761                         wfProfileOut( __METHOD__ );
00762                         return "<!-- Broken link in search result -->\n";
00763                 }
00764 
00765                 $t = $result->getTitle();
00766 
00767                 $titleSnippet = $result->getTitleSnippet($terms);
00768 
00769                 if( $titleSnippet == '' )
00770                         $titleSnippet = null;
00771 
00772                 $link = Linker::linkKnown(
00773                         $t,
00774                         $titleSnippet
00775                 );
00776 
00777                 // format redirect if any
00778                 $redirectTitle = $result->getRedirectTitle();
00779                 $redirectText = $result->getRedirectSnippet($terms);
00780                 $redirect = '';
00781                 if( !is_null($redirectTitle) ) {
00782                         if( $redirectText == '' )
00783                                 $redirectText = null;
00784 
00785                         $redirect = "<span class='searchalttitle'>" .
00786                                 $this->msg( 'search-redirect' )->rawParams(
00787                                         Linker::linkKnown( $redirectTitle, $redirectText ) )->text() .
00788                                 "</span>";
00789                 }
00790 
00791                 $out = "";
00792                 // display project name
00793                 if(is_null($lastInterwiki) || $lastInterwiki != $t->getInterwiki()) {
00794                         if( array_key_exists($t->getInterwiki(),$customCaptions) ) {
00795                                 // captions from 'search-interwiki-custom'
00796                                 $caption = $customCaptions[$t->getInterwiki()];
00797                         } else {
00798                                 // default is to show the hostname of the other wiki which might suck
00799                                 // if there are many wikis on one hostname
00800                                 $parsed = wfParseUrl( $t->getFullURL() );
00801                                 $caption = $this->msg( 'search-interwiki-default', $parsed['host'] )->text();
00802                         }
00803                         // "more results" link (special page stuff could be localized, but we might not know target lang)
00804                         $searchTitle = Title::newFromText($t->getInterwiki().":Special:Search");
00805                         $searchLink = Linker::linkKnown(
00806                                 $searchTitle,
00807                                 $this->msg( 'search-interwiki-more' )->text(),
00808                                 array(),
00809                                 array(
00810                                         'search' => $query,
00811                                         'fulltext' => 'Search'
00812                                 )
00813                         );
00814                         $out .= "</ul><div class='mw-search-interwiki-project'><span class='mw-search-interwiki-more'>
00815                                 {$searchLink}</span>{$caption}</div>\n<ul>";
00816                 }
00817 
00818                 $out .= "<li>{$link} {$redirect}</li>\n";
00819                 wfProfileOut( __METHOD__ );
00820                 return $out;
00821         }
00822 
00828         protected function getProfileForm( $profile, $term ) {
00829                 // Hidden stuff
00830                 $opts = array();
00831                 $opts['redirs'] = $this->searchRedirects;
00832                 $opts['profile'] = $this->profile;
00833 
00834                 if ( $profile === 'advanced' ) {
00835                         return $this->powerSearchBox( $term, $opts );
00836                 } else {
00837                         $form = '';
00838                         wfRunHooks( 'SpecialSearchProfileForm', array( $this, &$form, $profile, $term, $opts ) );
00839                         return $form;
00840                 }
00841         }
00842 
00850         protected function powerSearchBox( $term, $opts ) {
00851                 // Groups namespaces into rows according to subject
00852                 $rows = array();
00853                 foreach( SearchEngine::searchableNamespaces() as $namespace => $name ) {
00854                         $subject = MWNamespace::getSubject( $namespace );
00855                         if( !array_key_exists( $subject, $rows ) ) {
00856                                 $rows[$subject] = "";
00857                         }
00858                         $name = str_replace( '_', ' ', $name );
00859                         if( $name == '' ) {
00860                                 $name = $this->msg( 'blanknamespace' )->text();
00861                         }
00862                         $rows[$subject] .=
00863                                 Xml::openElement(
00864                                         'td', array( 'style' => 'white-space: nowrap' )
00865                                 ) .
00866                                 Xml::checkLabel(
00867                                         $name,
00868                                         "ns{$namespace}",
00869                                         "mw-search-ns{$namespace}",
00870                                         in_array( $namespace, $this->namespaces )
00871                                 ) .
00872                                 Xml::closeElement( 'td' );
00873                 }
00874                 $rows = array_values( $rows );
00875                 $numRows = count( $rows );
00876 
00877                 // Lays out namespaces in multiple floating two-column tables so they'll
00878                 // be arranged nicely while still accommodating different screen widths
00879                 $namespaceTables = '';
00880                 for( $i = 0; $i < $numRows; $i += 4 ) {
00881                         $namespaceTables .= Xml::openElement(
00882                                 'table',
00883                                 array( 'cellpadding' => 0, 'cellspacing' => 0 )
00884                         );
00885                         for( $j = $i; $j < $i + 4 && $j < $numRows; $j++ ) {
00886                                 $namespaceTables .= Xml::tags( 'tr', null, $rows[$j] );
00887                         }
00888                         $namespaceTables .= Xml::closeElement( 'table' );
00889                 }
00890 
00891                 $showSections = array( 'namespaceTables' => $namespaceTables );
00892 
00893                 // Show redirects check only if backend supports it
00894                 if( $this->getSearchEngine()->supports( 'list-redirects' ) ) {
00895                         $showSections['redirects'] =
00896                                 Xml::checkLabel( $this->msg( 'powersearch-redir' )->text(), 'redirs', 'redirs', $this->searchRedirects );
00897                 }
00898 
00899                 wfRunHooks( 'SpecialSearchPowerBox', array( &$showSections, $term, $opts ) );
00900 
00901                 $hidden = '';
00902                 unset( $opts['redirs'] );
00903                 foreach( $opts as $key => $value ) {
00904                         $hidden .= Html::hidden( $key, $value );
00905                 }
00906                 // Return final output
00907                 return
00908                         Xml::openElement(
00909                                 'fieldset',
00910                                 array( 'id' => 'mw-searchoptions', 'style' => 'margin:0em;' )
00911                         ) .
00912                         Xml::element( 'legend', null, $this->msg('powersearch-legend' )->text() ) .
00913                         Xml::tags( 'h4', null, $this->msg( 'powersearch-ns' )->parse() ) .
00914                         Html::element( 'div', array( 'id' => 'mw-search-togglebox' ) ) .
00915                         Xml::element( 'div', array( 'class' => 'divider' ), '', false ) .
00916                         implode( Xml::element( 'div', array( 'class' => 'divider' ), '', false ), $showSections ) .
00917                         $hidden .
00918                         Xml::closeElement( 'fieldset' );
00919         }
00920 
00924         protected function getSearchProfiles() {
00925                 // Builds list of Search Types (profiles)
00926                 $nsAllSet = array_keys( SearchEngine::searchableNamespaces() );
00927 
00928                 $profiles = array(
00929                         'default' => array(
00930                                 'message' => 'searchprofile-articles',
00931                                 'tooltip' => 'searchprofile-articles-tooltip',
00932                                 'namespaces' => SearchEngine::defaultNamespaces(),
00933                                 'namespace-messages' => SearchEngine::namespacesAsText(
00934                                         SearchEngine::defaultNamespaces()
00935                                 ),
00936                         ),
00937                         'images' => array(
00938                                 'message' => 'searchprofile-images',
00939                                 'tooltip' => 'searchprofile-images-tooltip',
00940                                 'namespaces' => array( NS_FILE ),
00941                         ),
00942                         'help' => array(
00943                                 'message' => 'searchprofile-project',
00944                                 'tooltip' => 'searchprofile-project-tooltip',
00945                                 'namespaces' => SearchEngine::helpNamespaces(),
00946                                 'namespace-messages' => SearchEngine::namespacesAsText(
00947                                         SearchEngine::helpNamespaces()
00948                                 ),
00949                         ),
00950                         'all' => array(
00951                                 'message' => 'searchprofile-everything',
00952                                 'tooltip' => 'searchprofile-everything-tooltip',
00953                                 'namespaces' => $nsAllSet,
00954                         ),
00955                         'advanced' => array(
00956                                 'message' => 'searchprofile-advanced',
00957                                 'tooltip' => 'searchprofile-advanced-tooltip',
00958                                 'namespaces' => self::NAMESPACES_CURRENT,
00959                         )
00960                 );
00961 
00962                 wfRunHooks( 'SpecialSearchProfiles', array( &$profiles ) );
00963 
00964                 foreach( $profiles as &$data ) {
00965                         if ( !is_array( $data['namespaces'] ) ) continue;
00966                         sort( $data['namespaces'] );
00967                 }
00968 
00969                 return $profiles;
00970         }
00971 
00978         protected function formHeader( $term, $resultsShown, $totalNum ) {
00979                 $out = Xml::openElement('div', array( 'class' =>  'mw-search-formheader' ) );
00980 
00981                 $bareterm = $term;
00982                 if( $this->startsWithImage( $term ) ) {
00983                         // Deletes prefixes
00984                         $bareterm = substr( $term, strpos( $term, ':' ) + 1 );
00985                 }
00986 
00987                 $profiles = $this->getSearchProfiles();
00988                 $lang = $this->getLanguage();
00989 
00990                 // Outputs XML for Search Types
00991                 $out .= Xml::openElement( 'div', array( 'class' => 'search-types' ) );
00992                 $out .= Xml::openElement( 'ul' );
00993                 foreach ( $profiles as $id => $profile ) {
00994                         if ( !isset( $profile['parameters'] ) ) {
00995                                 $profile['parameters'] = array();
00996                         }
00997                         $profile['parameters']['profile'] = $id;
00998 
00999                         $tooltipParam = isset( $profile['namespace-messages'] ) ?
01000                                 $lang->commaList( $profile['namespace-messages'] ) : null;
01001                         $out .= Xml::tags(
01002                                 'li',
01003                                 array(
01004                                         'class' => $this->profile === $id ? 'current' : 'normal'
01005                                 ),
01006                                 $this->makeSearchLink(
01007                                         $bareterm,
01008                                         array(),
01009                                         $this->msg( $profile['message'] )->text(),
01010                                         $this->msg( $profile['tooltip'], $tooltipParam )->text(),
01011                                         $profile['parameters']
01012                                 )
01013                         );
01014                 }
01015                 $out .= Xml::closeElement( 'ul' );
01016                 $out .= Xml::closeElement('div') ;
01017 
01018                 // Results-info
01019                 if ( $resultsShown > 0 ) {
01020                         if ( $totalNum > 0 ){
01021                                 $top = $this->msg( 'showingresultsheader' )
01022                                         ->numParams( $this->offset + 1, $this->offset + $resultsShown, $totalNum )
01023                                         ->params( wfEscapeWikiText( $term ) )
01024                                         ->numParams( $resultsShown )
01025                                         ->parse();
01026                         } elseif ( $resultsShown >= $this->limit ) {
01027                                 $top = $this->msg( 'showingresults' )
01028                                         ->numParams( $this->limit, $this->offset + 1 )
01029                                         ->parse();
01030                         } else {
01031                                 $top = $this->msg( 'showingresultsnum' )
01032                                         ->numParams( $this->limit, $this->offset + 1, $resultsShown )
01033                                         ->parse();
01034                         }
01035                         $out .= Xml::tags( 'div', array( 'class' => 'results-info' ),
01036                                 Xml::tags( 'ul', null, Xml::tags( 'li', null, $top ) )
01037                         );
01038                 }
01039 
01040                 $out .= Xml::element( 'div', array( 'style' => 'clear:both' ), '', false );
01041                 $out .= Xml::closeElement('div');
01042 
01043                 return $out;
01044         }
01045 
01050         protected function shortDialog( $term ) {
01051                 $out = Html::hidden( 'title', $this->getTitle()->getPrefixedText() );
01052                 $out .= Html::hidden( 'profile', $this->profile ) . "\n";
01053                 // Term box
01054                 $out .= Html::input( 'search', $term, 'search', array(
01055                         'id' => $this->profile === 'advanced' ? 'powerSearchText' : 'searchText',
01056                         'size' => '50',
01057                         'autofocus'
01058                 ) ) . "\n";
01059                 $out .= Html::hidden( 'fulltext', 'Search' ) . "\n";
01060                 $out .= Xml::submitButton( $this->msg( 'searchbutton' )->text() ) . "\n";
01061                 return $out . $this->didYouMeanHtml;
01062         }
01063 
01074         protected function makeSearchLink( $term, $namespaces, $label, $tooltip, $params = array() ) {
01075                 $opt = $params;
01076                 foreach( $namespaces as $n ) {
01077                         $opt['ns' . $n] = 1;
01078                 }
01079                 $opt['redirs'] = $this->searchRedirects;
01080 
01081                 $stParams = array_merge(
01082                         array(
01083                                 'search' => $term,
01084                                 'fulltext' => $this->msg( 'search' )->text()
01085                         ),
01086                         $opt
01087                 );
01088 
01089                 return Xml::element(
01090                         'a',
01091                         array(
01092                                 'href' => $this->getTitle()->getLocalURL( $stParams ),
01093                                 'title' => $tooltip),
01094                         $label
01095                 );
01096         }
01097 
01104         protected function startsWithImage( $term ) {
01105                 global $wgContLang;
01106 
01107                 $p = explode( ':', $term );
01108                 if( count( $p ) > 1 ) {
01109                         return $wgContLang->getNsIndex( $p[0] ) == NS_FILE;
01110                 }
01111                 return false;
01112         }
01113 
01120         protected function startsWithAll( $term ) {
01121 
01122                 $allkeyword = $this->msg( 'searchall' )->inContentLanguage()->text();
01123 
01124                 $p = explode( ':', $term );
01125                 if( count( $p ) > 1 ) {
01126                         return $p[0]  == $allkeyword;
01127                 }
01128                 return false;
01129         }
01130 
01136         public function getSearchEngine() {
01137                 if ( $this->searchEngine === null ) {
01138                         $this->searchEngine = SearchEngine::create();
01139                 }
01140                 return $this->searchEngine;
01141         }
01142 
01152         public function setExtraParam( $key, $value ) {
01153                 $this->extraParams[$key] = $value;
01154         }
01155 
01156 }