MediaWiki  REL1_20
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' ) ) {
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', $this->msg( 'searchresults-title', $term )->plain() ) );
00453                 }
00454                 // add javascript specific to special:search
00455                 $out->addModules( 'mediawiki.special.search' );
00456         }
00457 
00465         protected function powerSearch( &$request ) {
00466                 $arr = array();
00467                 foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
00468                         if( $request->getCheck( 'ns' . $ns ) ) {
00469                                 $arr[] = $ns;
00470                         }
00471                 }
00472 
00473                 return $arr;
00474         }
00475 
00481         protected function powerSearchOptions() {
00482                 $opt = array();
00483                 $opt['redirs'] = $this->searchRedirects ? 1 : 0;
00484                 if( $this->profile !== 'advanced' ) {
00485                         $opt['profile'] = $this->profile;
00486                 } else {
00487                         foreach( $this->namespaces as $n ) {
00488                                 $opt['ns' . $n] = 1;
00489                         }
00490                 }
00491                 return $opt + $this->extraParams;
00492         }
00493 
00501         protected function showMatches( &$matches ) {
00502                 global $wgContLang;
00503                 wfProfileIn( __METHOD__ );
00504 
00505                 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
00506 
00507                 $out = "";
00508                 $infoLine = $matches->getInfo();
00509                 if( !is_null($infoLine) ) {
00510                         $out .= "\n<!-- {$infoLine} -->\n";
00511                 }
00512                 $out .= "<ul class='mw-search-results'>\n";
00513                 $result = $matches->next();
00514                 while( $result ) {
00515                         $out .= $this->showHit( $result, $terms );
00516                         $result = $matches->next();
00517                 }
00518                 $out .= "</ul>\n";
00519 
00520                 // convert the whole thing to desired language variant
00521                 $out = $wgContLang->convert( $out );
00522                 wfProfileOut( __METHOD__ );
00523                 return $out;
00524         }
00525 
00534         protected function showHit( $result, $terms ) {
00535                 wfProfileIn( __METHOD__ );
00536 
00537                 if( $result->isBrokenTitle() ) {
00538                         wfProfileOut( __METHOD__ );
00539                         return "<!-- Broken link in search result -->\n";
00540                 }
00541 
00542                 $t = $result->getTitle();
00543 
00544                 $titleSnippet = $result->getTitleSnippet($terms);
00545 
00546                 if( $titleSnippet == '' )
00547                         $titleSnippet = null;
00548 
00549                 $link_t = clone $t;
00550 
00551                 wfRunHooks( 'ShowSearchHitTitle',
00552                                         array( &$link_t, &$titleSnippet, $result, $terms, $this ) );
00553 
00554                 $link = Linker::linkKnown(
00555                         $link_t,
00556                         $titleSnippet
00557                 );
00558 
00559                 //If page content is not readable, just return the title.
00560                 //This is not quite safe, but better than showing excerpts from non-readable pages
00561                 //Note that hiding the entry entirely would screw up paging.
00562                 if( !$t->userCan( 'read' ) ) {
00563                         wfProfileOut( __METHOD__ );
00564                         return "<li>{$link}</li>\n";
00565                 }
00566 
00567                 // If the page doesn't *exist*... our search index is out of date.
00568                 // The least confusing at this point is to drop the result.
00569                 // You may get less results, but... oh well. :P
00570                 if( $result->isMissingRevision() ) {
00571                         wfProfileOut( __METHOD__ );
00572                         return "<!-- missing page " . htmlspecialchars( $t->getPrefixedText() ) . "-->\n";
00573                 }
00574 
00575                 // format redirects / relevant sections
00576                 $redirectTitle = $result->getRedirectTitle();
00577                 $redirectText = $result->getRedirectSnippet($terms);
00578                 $sectionTitle = $result->getSectionTitle();
00579                 $sectionText = $result->getSectionSnippet($terms);
00580                 $redirect = '';
00581 
00582                 if( !is_null($redirectTitle) ) {
00583                         if( $redirectText == '' )
00584                                 $redirectText = null;
00585 
00586                         $redirect = "<span class='searchalttitle'>" .
00587                                 $this->msg( 'search-redirect' )->rawParams(
00588                                         Linker::linkKnown( $redirectTitle, $redirectText ) )->text() .
00589                                 "</span>";
00590                 }
00591 
00592                 $section = '';
00593 
00594                 if( !is_null($sectionTitle) ) {
00595                         if( $sectionText == '' )
00596                                 $sectionText = null;
00597 
00598                         $section = "<span class='searchalttitle'>" .
00599                                 $this->msg( 'search-section' )->rawParams(
00600                                         Linker::linkKnown( $sectionTitle, $sectionText ) )->text() .
00601                                 "</span>";
00602                 }
00603 
00604                 // format text extract
00605                 $extract = "<div class='searchresult'>".$result->getTextSnippet($terms)."</div>";
00606 
00607                 $lang = $this->getLanguage();
00608 
00609                 // format score
00610                 if( is_null( $result->getScore() ) ) {
00611                         // Search engine doesn't report scoring info
00612                         $score = '';
00613                 } else {
00614                         $percent = sprintf( '%2.1f', $result->getScore() * 100 );
00615                         $score = $this->msg( 'search-result-score' )->numParams( $percent )->text()
00616                                 . ' - ';
00617                 }
00618 
00619                 // format description
00620                 $byteSize = $result->getByteSize();
00621                 $wordCount = $result->getWordCount();
00622                 $timestamp = $result->getTimestamp();
00623                 $size = $this->msg( 'search-result-size', $lang->formatSize( $byteSize ) )
00624                         ->numParams( $wordCount )->escaped();
00625 
00626                 if( $t->getNamespace() == NS_CATEGORY ) {
00627                         $cat = Category::newFromTitle( $t );
00628                         $size = $this->msg( 'search-result-category-size' )
00629                                 ->numParams( $cat->getPageCount(), $cat->getSubcatCount(), $cat->getFileCount() )
00630                                 ->escaped();
00631                 }
00632 
00633                 $date = $lang->userTimeAndDate( $timestamp, $this->getUser() );
00634 
00635                 // link to related articles if supported
00636                 $related = '';
00637                 if( $result->hasRelated() ) {
00638                         $st = SpecialPage::getTitleFor( 'Search' );
00639                         $stParams = array_merge(
00640                                 $this->powerSearchOptions(),
00641                                 array(
00642                                         'search' => $this->msg( 'searchrelated' )->inContentLanguage()->text() .
00643                                                 ':' . $t->getPrefixedText(),
00644                                         'fulltext' => $this->msg( 'search' )->text()
00645                                 )
00646                         );
00647 
00648                         $related = ' -- ' . Linker::linkKnown(
00649                                 $st,
00650                                 $this->msg( 'search-relatedarticle' )->text(),
00651                                 array(),
00652                                 $stParams
00653                         );
00654                 }
00655 
00656                 // Include a thumbnail for media files...
00657                 if( $t->getNamespace() == NS_FILE ) {
00658                         $img = wfFindFile( $t );
00659                         if( $img ) {
00660                                 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
00661                                 if( $thumb ) {
00662                                         $desc = $this->msg( 'parentheses' )->rawParams( $img->getShortDesc() )->escaped();
00663                                         wfProfileOut( __METHOD__ );
00664                                         // Float doesn't seem to interact well with the bullets.
00665                                         // Table messes up vertical alignment of the bullets.
00666                                         // Bullets are therefore disabled (didn't look great anyway).
00667                                         return "<li>" .
00668                                                 '<table class="searchResultImage">' .
00669                                                 '<tr>' .
00670                                                 '<td width="120" style="text-align: center; vertical-align: top;">' .
00671                                                 $thumb->toHtml( array( 'desc-link' => true ) ) .
00672                                                 '</td>' .
00673                                                 '<td style="vertical-align: top;">' .
00674                                                 $link .
00675                                                 $extract .
00676                                                 "<div class='mw-search-result-data'>{$score}{$desc} - {$date}{$related}</div>" .
00677                                                 '</td>' .
00678                                                 '</tr>' .
00679                                                 '</table>' .
00680                                                 "</li>\n";
00681                                 }
00682                         }
00683                 }
00684 
00685                 wfProfileOut( __METHOD__ );
00686                 return "<li><div class='mw-search-result-heading'>{$link} {$redirect} {$section}</div> {$extract}\n" .
00687                         "<div class='mw-search-result-data'>{$score}{$size} - {$date}{$related}</div>" .
00688                         "</li>\n";
00689 
00690         }
00691 
00700         protected function showInterwiki( &$matches, $query ) {
00701                 global $wgContLang;
00702                 wfProfileIn( __METHOD__ );
00703                 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
00704 
00705                 $out = "<div id='mw-search-interwiki'><div id='mw-search-interwiki-caption'>".
00706                         $this->msg( 'search-interwiki-caption' )->text()  . "</div>\n";
00707                 $out .= "<ul class='mw-search-iwresults'>\n";
00708 
00709                 // work out custom project captions
00710                 $customCaptions = array();
00711                 $customLines = explode( "\n", $this->msg( 'search-interwiki-custom' )->text() ); // format per line <iwprefix>:<caption>
00712                 foreach($customLines as $line) {
00713                         $parts = explode(":",$line,2);
00714                         if(count($parts) == 2) // validate line
00715                                 $customCaptions[$parts[0]] = $parts[1];
00716                 }
00717 
00718                 $prev = null;
00719                 $result = $matches->next();
00720                 while( $result ) {
00721                         $out .= $this->showInterwikiHit( $result, $prev, $terms, $query, $customCaptions );
00722                         $prev = $result->getInterwikiPrefix();
00723                         $result = $matches->next();
00724                 }
00725                 // TODO: should support paging in a non-confusing way (not sure how though, maybe via ajax)..
00726                 $out .= "</ul></div>\n";
00727 
00728                 // convert the whole thing to desired language variant
00729                 $out = $wgContLang->convert( $out );
00730                 wfProfileOut( __METHOD__ );
00731                 return $out;
00732         }
00733 
00745         protected function showInterwikiHit( $result, $lastInterwiki, $terms, $query, $customCaptions) {
00746                 wfProfileIn( __METHOD__ );
00747 
00748                 if( $result->isBrokenTitle() ) {
00749                         wfProfileOut( __METHOD__ );
00750                         return "<!-- Broken link in search result -->\n";
00751                 }
00752 
00753                 $t = $result->getTitle();
00754 
00755                 $titleSnippet = $result->getTitleSnippet($terms);
00756 
00757                 if( $titleSnippet == '' )
00758                         $titleSnippet = null;
00759 
00760                 $link = Linker::linkKnown(
00761                         $t,
00762                         $titleSnippet
00763                 );
00764 
00765                 // format redirect if any
00766                 $redirectTitle = $result->getRedirectTitle();
00767                 $redirectText = $result->getRedirectSnippet($terms);
00768                 $redirect = '';
00769                 if( !is_null($redirectTitle) ) {
00770                         if( $redirectText == '' )
00771                                 $redirectText = null;
00772 
00773                         $redirect = "<span class='searchalttitle'>" .
00774                                 $this->msg( 'search-redirect' )->rawParams(
00775                                         Linker::linkKnown( $redirectTitle, $redirectText ) )->text() .
00776                                 "</span>";
00777                 }
00778 
00779                 $out = "";
00780                 // display project name
00781                 if(is_null($lastInterwiki) || $lastInterwiki != $t->getInterwiki()) {
00782                         if( array_key_exists($t->getInterwiki(),$customCaptions) ) {
00783                                 // captions from 'search-interwiki-custom'
00784                                 $caption = $customCaptions[$t->getInterwiki()];
00785                         } else {
00786                                 // default is to show the hostname of the other wiki which might suck
00787                                 // if there are many wikis on one hostname
00788                                 $parsed = wfParseUrl( $t->getFullURL() );
00789                                 $caption = $this->msg( 'search-interwiki-default', $parsed['host'] )->text();
00790                         }
00791                         // "more results" link (special page stuff could be localized, but we might not know target lang)
00792                         $searchTitle = Title::newFromText($t->getInterwiki().":Special:Search");
00793                         $searchLink = Linker::linkKnown(
00794                                 $searchTitle,
00795                                 $this->msg( 'search-interwiki-more' )->text(),
00796                                 array(),
00797                                 array(
00798                                         'search' => $query,
00799                                         'fulltext' => 'Search'
00800                                 )
00801                         );
00802                         $out .= "</ul><div class='mw-search-interwiki-project'><span class='mw-search-interwiki-more'>
00803                                 {$searchLink}</span>{$caption}</div>\n<ul>";
00804                 }
00805 
00806                 $out .= "<li>{$link} {$redirect}</li>\n";
00807                 wfProfileOut( __METHOD__ );
00808                 return $out;
00809         }
00810 
00816         protected function getProfileForm( $profile, $term ) {
00817                 // Hidden stuff
00818                 $opts = array();
00819                 $opts['redirs'] = $this->searchRedirects;
00820                 $opts['profile'] = $this->profile;
00821 
00822                 if ( $profile === 'advanced' ) {
00823                         return $this->powerSearchBox( $term, $opts );
00824                 } else {
00825                         $form = '';
00826                         wfRunHooks( 'SpecialSearchProfileForm', array( $this, &$form, $profile, $term, $opts ) );
00827                         return $form;
00828                 }
00829         }
00830 
00838         protected function powerSearchBox( $term, $opts ) {
00839                 // Groups namespaces into rows according to subject
00840                 $rows = array();
00841                 foreach( SearchEngine::searchableNamespaces() as $namespace => $name ) {
00842                         $subject = MWNamespace::getSubject( $namespace );
00843                         if( !array_key_exists( $subject, $rows ) ) {
00844                                 $rows[$subject] = "";
00845                         }
00846                         $name = str_replace( '_', ' ', $name );
00847                         if( $name == '' ) {
00848                                 $name = $this->msg( 'blanknamespace' )->text();
00849                         }
00850                         $rows[$subject] .=
00851                                 Xml::openElement(
00852                                         'td', array( 'style' => 'white-space: nowrap' )
00853                                 ) .
00854                                 Xml::checkLabel(
00855                                         $name,
00856                                         "ns{$namespace}",
00857                                         "mw-search-ns{$namespace}",
00858                                         in_array( $namespace, $this->namespaces )
00859                                 ) .
00860                                 Xml::closeElement( 'td' );
00861                 }
00862                 $rows = array_values( $rows );
00863                 $numRows = count( $rows );
00864 
00865                 // Lays out namespaces in multiple floating two-column tables so they'll
00866                 // be arranged nicely while still accommodating different screen widths
00867                 $namespaceTables = '';
00868                 for( $i = 0; $i < $numRows; $i += 4 ) {
00869                         $namespaceTables .= Xml::openElement(
00870                                 'table',
00871                                 array( 'cellpadding' => 0, 'cellspacing' => 0 )
00872                         );
00873                         for( $j = $i; $j < $i + 4 && $j < $numRows; $j++ ) {
00874                                 $namespaceTables .= Xml::tags( 'tr', null, $rows[$j] );
00875                         }
00876                         $namespaceTables .= Xml::closeElement( 'table' );
00877                 }
00878 
00879                 $showSections = array( 'namespaceTables' => $namespaceTables );
00880 
00881                 // Show redirects check only if backend supports it
00882                 if( $this->getSearchEngine()->supports( 'list-redirects' ) ) {
00883                         $showSections['redirects'] =
00884                                 Xml::checkLabel( $this->msg( 'powersearch-redir' )->text(), 'redirs', 'redirs', $this->searchRedirects );
00885                 }
00886 
00887                 wfRunHooks( 'SpecialSearchPowerBox', array( &$showSections, $term, $opts ) );
00888 
00889                 $hidden = '';
00890                 unset( $opts['redirs'] );
00891                 foreach( $opts as $key => $value ) {
00892                         $hidden .= Html::hidden( $key, $value );
00893                 }
00894                 // Return final output
00895                 return
00896                         Xml::openElement(
00897                                 'fieldset',
00898                                 array( 'id' => 'mw-searchoptions', 'style' => 'margin:0em;' )
00899                         ) .
00900                         Xml::element( 'legend', null, $this->msg('powersearch-legend' )->text() ) .
00901                         Xml::tags( 'h4', null, $this->msg( 'powersearch-ns' )->parse() ) .
00902                         Html::element( 'div', array( 'id' => 'mw-search-togglebox' ) ) .
00903                         Xml::element( 'div', array( 'class' => 'divider' ), '', false ) .
00904                         implode( Xml::element( 'div', array( 'class' => 'divider' ), '', false ), $showSections ) .
00905                         $hidden .
00906                         Xml::closeElement( 'fieldset' );
00907         }
00908 
00912         protected function getSearchProfiles() {
00913                 // Builds list of Search Types (profiles)
00914                 $nsAllSet = array_keys( SearchEngine::searchableNamespaces() );
00915 
00916                 $profiles = array(
00917                         'default' => array(
00918                                 'message' => 'searchprofile-articles',
00919                                 'tooltip' => 'searchprofile-articles-tooltip',
00920                                 'namespaces' => SearchEngine::defaultNamespaces(),
00921                                 'namespace-messages' => SearchEngine::namespacesAsText(
00922                                         SearchEngine::defaultNamespaces()
00923                                 ),
00924                         ),
00925                         'images' => array(
00926                                 'message' => 'searchprofile-images',
00927                                 'tooltip' => 'searchprofile-images-tooltip',
00928                                 'namespaces' => array( NS_FILE ),
00929                         ),
00930                         'help' => array(
00931                                 'message' => 'searchprofile-project',
00932                                 'tooltip' => 'searchprofile-project-tooltip',
00933                                 'namespaces' => SearchEngine::helpNamespaces(),
00934                                 'namespace-messages' => SearchEngine::namespacesAsText(
00935                                         SearchEngine::helpNamespaces()
00936                                 ),
00937                         ),
00938                         'all' => array(
00939                                 'message' => 'searchprofile-everything',
00940                                 'tooltip' => 'searchprofile-everything-tooltip',
00941                                 'namespaces' => $nsAllSet,
00942                         ),
00943                         'advanced' => array(
00944                                 'message' => 'searchprofile-advanced',
00945                                 'tooltip' => 'searchprofile-advanced-tooltip',
00946                                 'namespaces' => self::NAMESPACES_CURRENT,
00947                         )
00948                 );
00949 
00950                 wfRunHooks( 'SpecialSearchProfiles', array( &$profiles ) );
00951 
00952                 foreach( $profiles as &$data ) {
00953                         if ( !is_array( $data['namespaces'] ) ) continue;
00954                         sort( $data['namespaces'] );
00955                 }
00956 
00957                 return $profiles;
00958         }
00959 
00966         protected function formHeader( $term, $resultsShown, $totalNum ) {
00967                 $out = Xml::openElement('div', array( 'class' =>  'mw-search-formheader' ) );
00968 
00969                 $bareterm = $term;
00970                 if( $this->startsWithImage( $term ) ) {
00971                         // Deletes prefixes
00972                         $bareterm = substr( $term, strpos( $term, ':' ) + 1 );
00973                 }
00974 
00975                 $profiles = $this->getSearchProfiles();
00976                 $lang = $this->getLanguage();
00977 
00978                 // Outputs XML for Search Types
00979                 $out .= Xml::openElement( 'div', array( 'class' => 'search-types' ) );
00980                 $out .= Xml::openElement( 'ul' );
00981                 foreach ( $profiles as $id => $profile ) {
00982                         if ( !isset( $profile['parameters'] ) ) {
00983                                 $profile['parameters'] = array();
00984                         }
00985                         $profile['parameters']['profile'] = $id;
00986 
00987                         $tooltipParam = isset( $profile['namespace-messages'] ) ?
00988                                 $lang->commaList( $profile['namespace-messages'] ) : null;
00989                         $out .= Xml::tags(
00990                                 'li',
00991                                 array(
00992                                         'class' => $this->profile === $id ? 'current' : 'normal'
00993                                 ),
00994                                 $this->makeSearchLink(
00995                                         $bareterm,
00996                                         array(),
00997                                         $this->msg( $profile['message'] )->text(),
00998                                         $this->msg( $profile['tooltip'], $tooltipParam )->text(),
00999                                         $profile['parameters']
01000                                 )
01001                         );
01002                 }
01003                 $out .= Xml::closeElement( 'ul' );
01004                 $out .= Xml::closeElement('div') ;
01005 
01006                 // Results-info
01007                 if ( $resultsShown > 0 ) {
01008                         if ( $totalNum > 0 ){
01009                                 $top = $this->msg( 'showingresultsheader' )
01010                                         ->numParams( $this->offset + 1, $this->offset + $resultsShown, $totalNum )
01011                                         ->params( wfEscapeWikiText( $term ) )
01012                                         ->numParams( $resultsShown )
01013                                         ->parse();
01014                         } elseif ( $resultsShown >= $this->limit ) {
01015                                 $top = $this->msg( 'showingresults' )
01016                                         ->numParams( $this->limit, $this->offset + 1 )
01017                                         ->parse();
01018                         } else {
01019                                 $top = $this->msg( 'showingresultsnum' )
01020                                         ->numParams( $this->limit, $this->offset + 1, $resultsShown )
01021                                         ->parse();
01022                         }
01023                         $out .= Xml::tags( 'div', array( 'class' => 'results-info' ),
01024                                 Xml::tags( 'ul', null, Xml::tags( 'li', null, $top ) )
01025                         );
01026                 }
01027 
01028                 $out .= Xml::element( 'div', array( 'style' => 'clear:both' ), '', false );
01029                 $out .= Xml::closeElement('div');
01030 
01031                 return $out;
01032         }
01033 
01038         protected function shortDialog( $term ) {
01039                 $out = Html::hidden( 'title', $this->getTitle()->getPrefixedText() );
01040                 $out .= Html::hidden( 'profile', $this->profile ) . "\n";
01041                 // Term box
01042                 $out .= Html::input( 'search', $term, 'search', array(
01043                         'id' => $this->profile === 'advanced' ? 'powerSearchText' : 'searchText',
01044                         'size' => '50',
01045                         'autofocus'
01046                 ) ) . "\n";
01047                 $out .= Html::hidden( 'fulltext', 'Search' ) . "\n";
01048                 $out .= Xml::submitButton( $this->msg( 'searchbutton' )->text() ) . "\n";
01049                 return $out . $this->didYouMeanHtml;
01050         }
01051 
01062         protected function makeSearchLink( $term, $namespaces, $label, $tooltip, $params = array() ) {
01063                 $opt = $params;
01064                 foreach( $namespaces as $n ) {
01065                         $opt['ns' . $n] = 1;
01066                 }
01067                 $opt['redirs'] = $this->searchRedirects;
01068 
01069                 $stParams = array_merge(
01070                         array(
01071                                 'search' => $term,
01072                                 'fulltext' => $this->msg( 'search' )->text()
01073                         ),
01074                         $opt
01075                 );
01076 
01077                 return Xml::element(
01078                         'a',
01079                         array(
01080                                 'href' => $this->getTitle()->getLocalURL( $stParams ),
01081                                 'title' => $tooltip),
01082                         $label
01083                 );
01084         }
01085 
01092         protected function startsWithImage( $term ) {
01093                 global $wgContLang;
01094 
01095                 $p = explode( ':', $term );
01096                 if( count( $p ) > 1 ) {
01097                         return $wgContLang->getNsIndex( $p[0] ) == NS_FILE;
01098                 }
01099                 return false;
01100         }
01101 
01108         protected function startsWithAll( $term ) {
01109 
01110                 $allkeyword = $this->msg( 'searchall' )->inContentLanguage()->text();
01111 
01112                 $p = explode( ':', $term );
01113                 if( count( $p ) > 1 ) {
01114                         return $p[0]  == $allkeyword;
01115                 }
01116                 return false;
01117         }
01118 
01124         public function getSearchEngine() {
01125                 if ( $this->searchEngine === null ) {
01126                         $this->searchEngine = SearchEngine::create();
01127                 }
01128                 return $this->searchEngine;
01129         }
01130 
01140         public function setExtraParam( $key, $value ) {
01141                 $this->extraParams[$key] = $value;
01142         }
01143 
01144 }