00001 <?php
00002 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
00003 # http://www.mediawiki.org/
00004 #
00005 # This program is free software; you can redistribute it and/or modify
00006 # it under the terms of the GNU General Public License as published by
00007 # the Free Software Foundation; either version 2 of the License, or
00008 # (at your option) any later version.
00009 #
00010 # This program is distributed in the hope that it will be useful,
00011 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013 # GNU General Public License for more details.
00014 #
00015 # You should have received a copy of the GNU General Public License along
00016 # with this program; if not, write to the Free Software Foundation, Inc.,
00017 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018 # http://www.gnu.org/copyleft/gpl.html
00019
00031 function wfSpecialSearch( $par = '' ) {
00032 global $wgRequest, $wgUser, $wgUseOldSearchUI;
00033
00034
00035 $titleParam = str_replace( '_', ' ', $par );
00036
00037 $search = str_replace( "\n", " ", $wgRequest->getText( 'search', $titleParam ) );
00038 $class = $wgUseOldSearchUI ? 'SpecialSearchOld' : 'SpecialSearch';
00039 $searchPage = new $class( $wgRequest, $wgUser );
00040 if( $wgRequest->getVal( 'fulltext' )
00041 || !is_null( $wgRequest->getVal( 'offset' ))
00042 || !is_null( $wgRequest->getVal( 'searchx' )) )
00043 {
00044 $searchPage->showResults( $search );
00045 } else {
00046 $searchPage->goResult( $search );
00047 }
00048 }
00049
00054 class SpecialSearch {
00055
00064 function __construct( &$request, &$user ) {
00065 list( $this->limit, $this->offset ) = $request->getLimitOffset( 20, 'searchlimit' );
00066 $this->mPrefix = $request->getVal('prefix', '');
00067 # Extract requested namespaces
00068 $this->namespaces = $this->powerSearch( $request );
00069 if( empty( $this->namespaces ) ) {
00070 $this->namespaces = SearchEngine::userNamespaces( $user );
00071 }
00072 $this->searchRedirects = $request->getcheck( 'redirs' ) ? true : false;
00073 $this->searchAdvanced = $request->getVal( 'advanced' );
00074 $this->active = 'advanced';
00075 $this->sk = $user->getSkin();
00076 $this->didYouMeanHtml = ''; # html of did you mean... link
00077 $this->fulltext = $request->getVal('fulltext');
00078 }
00079
00084 public function goResult( $term ) {
00085 global $wgOut;
00086 $this->setupPage( $term );
00087 # Try to go to page as entered.
00088 $t = Title::newFromText( $term );
00089 # If the string cannot be used to create a title
00090 if( is_null( $t ) ) {
00091 return $this->showResults( $term );
00092 }
00093 # If there's an exact or very near match, jump right there.
00094 $t = SearchEngine::getNearMatch( $term );
00095 if( !is_null( $t ) ) {
00096 $wgOut->redirect( $t->getFullURL() );
00097 return;
00098 }
00099 # No match, generate an edit URL
00100 $t = Title::newFromText( $term );
00101 if( !is_null( $t ) ) {
00102 global $wgGoToEdit;
00103 wfRunHooks( 'SpecialSearchNogomatch', array( &$t ) );
00104 # If the feature is enabled, go straight to the edit page
00105 if( $wgGoToEdit ) {
00106 $wgOut->redirect( $t->getFullURL( 'action=edit' ) );
00107 return;
00108 }
00109 }
00110 return $this->showResults( $term );
00111 }
00112
00116 public function showResults( $term ) {
00117 global $wgOut, $wgUser, $wgDisableTextSearch, $wgContLang;
00118 wfProfileIn( __METHOD__ );
00119
00120 $sk = $wgUser->getSkin();
00121
00122 $this->searchEngine = SearchEngine::create();
00123 $search =& $this->searchEngine;
00124 $search->setLimitOffset( $this->limit, $this->offset );
00125 $search->setNamespaces( $this->namespaces );
00126 $search->showRedirects = $this->searchRedirects;
00127 $search->prefix = $this->mPrefix;
00128 $term = $search->transformSearchTerm($term);
00129
00130 $this->setupPage( $term );
00131
00132 if( $wgDisableTextSearch ) {
00133 global $wgSearchForwardUrl;
00134 if( $wgSearchForwardUrl ) {
00135 $url = str_replace( '$1', urlencode( $term ), $wgSearchForwardUrl );
00136 $wgOut->redirect( $url );
00137 wfProfileOut( __METHOD__ );
00138 return;
00139 }
00140 global $wgInputEncoding;
00141 $wgOut->addHTML(
00142 Xml::openElement( 'fieldset' ) .
00143 Xml::element( 'legend', null, wfMsg( 'search-external' ) ) .
00144 Xml::element( 'p', array( 'class' => 'mw-searchdisabled' ), wfMsg( 'searchdisabled' ) ) .
00145 wfMsg( 'googlesearch',
00146 htmlspecialchars( $term ),
00147 htmlspecialchars( $wgInputEncoding ),
00148 htmlspecialchars( wfMsg( 'searchbutton' ) )
00149 ) .
00150 Xml::closeElement( 'fieldset' )
00151 );
00152 wfProfileOut( __METHOD__ );
00153 return;
00154 }
00155
00156 $t = Title::newFromText( $term );
00157
00158
00159 $rewritten = $search->replacePrefixes($term);
00160
00161 $titleMatches = $search->searchTitle( $rewritten );
00162 if( !($titleMatches instanceof SearchResultTooMany))
00163 $textMatches = $search->searchText( $rewritten );
00164
00165
00166 if( $textMatches && $textMatches->hasSuggestion() ) {
00167 $st = SpecialPage::getTitleFor( 'Search' );
00168 # mirror Go/Search behaviour of original request ..
00169 $didYouMeanParams = array( 'search' => $textMatches->getSuggestionQuery() );
00170 if($this->fulltext != NULL)
00171 $didYouMeanParams['fulltext'] = $this->fulltext;
00172 $stParams = wfArrayToCGI(
00173 $didYouMeanParams,
00174 $this->powerSearchOptions()
00175 );
00176 $suggestLink = $sk->makeKnownLinkObj( $st,
00177 $textMatches->getSuggestionSnippet(),
00178 $stParams );
00179
00180 $this->didYouMeanHtml = '<div class="searchdidyoumean">'.wfMsg('search-suggest',$suggestLink).'</div>';
00181 }
00182
00183
00184 $wgOut->addHtml(
00185 Xml::openElement( 'table', array( 'border'=>0, 'cellpadding'=>0, 'cellspacing'=>0 ) ) .
00186 Xml::openElement( 'tr' ) .
00187 Xml::openElement( 'td' ) . "\n" .
00188 ( $this->searchAdvanced ? $this->powerSearchBox( $term ) : $this->shortDialog( $term ) ) .
00189 Xml::closeElement('td') .
00190 Xml::closeElement('tr') .
00191 Xml::closeElement('table')
00192 );
00193
00194
00195 if( $titleMatches instanceof SearchResultTooMany ) {
00196 $wgOut->addWikiText( '==' . wfMsg( 'toomanymatches' ) . "==\n" );
00197 wfProfileOut( __METHOD__ );
00198 return;
00199 }
00200
00201 $filePrefix = $wgContLang->getFormattedNsText(NS_FILE).':';
00202 if( '' === trim( $term ) || $filePrefix === trim( $term ) ) {
00203 $wgOut->addHTML( $this->searchAdvanced ? $this->powerSearchFocus() : $this->searchFocus() );
00204
00205 wfProfileOut( __METHOD__ );
00206 return;
00207 }
00208
00209
00210 if( !is_null($t) ) {
00211 if( !$t->exists() ) {
00212 $wgOut->addWikiMsg( 'searchmenu-new', wfEscapeWikiText( $t->getPrefixedText() ) );
00213 } else {
00214 $wgOut->addWikiMsg( 'searchmenu-exists', wfEscapeWikiText( $t->getPrefixedText() ) );
00215 }
00216 }
00217
00218
00219 $titleMatchesSQL = $titleMatches ? $titleMatches->numRows() : 0;
00220 $textMatchesSQL = $textMatches ? $textMatches->numRows() : 0;
00221
00222 $numSQL = $titleMatchesSQL + $textMatchesSQL;
00223
00224 $numTitleMatches = $titleMatches && !is_null( $titleMatches->getTotalHits() ) ?
00225 $titleMatches->getTotalHits() : $titleMatchesSQL;
00226 $numTextMatches = $textMatches && !is_null( $textMatches->getTotalHits() ) ?
00227 $textMatches->getTotalHits() : $textMatchesSQL;
00228 $totalRes = $numTitleMatches + $numTextMatches;
00229
00230
00231 if( $numSQL > 0 ) {
00232 if( $numSQL > 0 ) {
00233 $top = wfMsgExt('showingresultstotal', array( 'parseinline' ),
00234 $this->offset+1, $this->offset+$numSQL, $totalRes, $numSQL );
00235 } elseif( $numSQL >= $this->limit ) {
00236 $top = wfShowingResults( $this->offset, $this->limit );
00237 } else {
00238 $top = wfShowingResultsNum( $this->offset, $this->limit, $numSQL );
00239 }
00240 $wgOut->addHTML( "<p class='mw-search-numberresults'>{$top}</p>\n" );
00241 }
00242
00243
00244 if( $numSQL || $this->offset ) {
00245 $prevnext = wfViewPrevNext( $this->offset, $this->limit,
00246 SpecialPage::getTitleFor( 'Search' ),
00247 wfArrayToCGI( $this->powerSearchOptions(), array( 'search' => $term ) ),
00248 max( $titleMatchesSQL, $textMatchesSQL ) < $this->limit
00249 );
00250 $wgOut->addHTML( "<p class='mw-search-pager-top'>{$prevnext}</p>\n" );
00251 wfRunHooks( 'SpecialSearchResults', array( $term, &$titleMatches, &$textMatches ) );
00252 } else {
00253 wfRunHooks( 'SpecialSearchNoResults', array( $term ) );
00254 }
00255
00256 $wgOut->addHtml( "<div class='searchresults'>" );
00257 if( $titleMatches ) {
00258 if( $numTitleMatches > 0 ) {
00259 $wgOut->wrapWikiMsg( "==$1==\n", 'titlematches' );
00260 $wgOut->addHTML( $this->showMatches( $titleMatches ) );
00261 }
00262 $titleMatches->free();
00263 }
00264 if( $textMatches ) {
00265
00266 if( $numTextMatches > 0 && $numTitleMatches > 0 ) {
00267
00268 $wgOut->wrapWikiMsg( "==$1==\n", 'textmatches' );
00269 } elseif( $totalRes == 0 ) {
00270 # Don't show the 'no text matches' if we received title matches
00271 $wgOut->wrapWikiMsg( "==$1==\n", 'notextmatches' );
00272 }
00273
00274 if( $textMatches->hasInterwikiResults() ) {
00275 $wgOut->addHTML( $this->showInterwiki( $textMatches->getInterwikiResults(), $term ) );
00276 }
00277
00278 if( $numTextMatches > 0 ) {
00279 $wgOut->addHTML( $this->showMatches( $textMatches ) );
00280 }
00281
00282 $textMatches->free();
00283 }
00284 if( $totalRes === 0 ) {
00285 $wgOut->addWikiMsg( 'search-nonefound' );
00286 }
00287 $wgOut->addHtml( "</div>" );
00288 if( $totalRes === 0 ) {
00289 $wgOut->addHTML( $this->searchAdvanced ? $this->powerSearchFocus() : $this->searchFocus() );
00290 }
00291
00292 if( $numSQL || $this->offset ) {
00293 $wgOut->addHTML( "<p class='mw-search-pager-bottom'>{$prevnext}</p>\n" );
00294 }
00295 wfProfileOut( __METHOD__ );
00296 }
00297
00301 protected function setupPage( $term ) {
00302 global $wgOut;
00303
00304 $nsAllSet = array_keys( SearchEngine::searchableNamespaces() );
00305 if( $this->searchAdvanced )
00306 $this->active = 'advanced';
00307 else if( $this->namespaces === NS_FILE || $this->startsWithImage( $term ) )
00308 $this->active = 'images';
00309 elseif( $this->namespaces === $nsAllSet )
00310 $this->active = 'all';
00311 elseif( $this->namespaces === SearchEngine::defaultNamespaces() )
00312 $this->active = 'default';
00313 elseif( $this->namespaces === SearchEngine::projectNamespaces() )
00314 $this->active = 'project';
00315 else
00316 $this->active = 'advanced';
00317 # Should advanced UI be used?
00318 $this->searchAdvanced = ($this->active === 'advanced');
00319 if( !empty( $term ) ) {
00320 $wgOut->setPageTitle( wfMsg( 'searchresults') );
00321 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'searchresults-title', $term ) ) );
00322 }
00323 $wgOut->setArticleRelated( false );
00324 $wgOut->setRobotPolicy( 'noindex,nofollow' );
00325 }
00326
00334 protected function powerSearch( &$request ) {
00335 $arr = array();
00336 foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
00337 if( $request->getCheck( 'ns' . $ns ) ) {
00338 $arr[] = $ns;
00339 }
00340 }
00341 return $arr;
00342 }
00343
00348 protected function powerSearchOptions() {
00349 $opt = array();
00350 foreach( $this->namespaces as $n ) {
00351 $opt['ns' . $n] = 1;
00352 }
00353 $opt['redirs'] = $this->searchRedirects ? 1 : 0;
00354 if( $this->searchAdvanced ) {
00355 $opt['advanced'] = $this->searchAdvanced;
00356 }
00357 return $opt;
00358 }
00359
00365 protected function showMatches( &$matches ) {
00366 global $wgContLang;
00367 wfProfileIn( __METHOD__ );
00368
00369 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
00370
00371 $out = "";
00372 $infoLine = $matches->getInfo();
00373 if( !is_null($infoLine) ) {
00374 $out .= "\n<!-- {$infoLine} -->\n";
00375 }
00376 $off = $this->offset + 1;
00377 $out .= "<ul class='mw-search-results'>\n";
00378 while( $result = $matches->next() ) {
00379 $out .= $this->showHit( $result, $terms );
00380 }
00381 $out .= "</ul>\n";
00382
00383
00384 $out = $wgContLang->convert( $out );
00385 wfProfileOut( __METHOD__ );
00386 return $out;
00387 }
00388
00394 protected function showHit( $result, $terms ) {
00395 global $wgContLang, $wgLang, $wgUser;
00396 wfProfileIn( __METHOD__ );
00397
00398 if( $result->isBrokenTitle() ) {
00399 wfProfileOut( __METHOD__ );
00400 return "<!-- Broken link in search result -->\n";
00401 }
00402
00403 $sk = $wgUser->getSkin();
00404 $t = $result->getTitle();
00405
00406 $link = $this->sk->makeKnownLinkObj( $t, $result->getTitleSnippet($terms));
00407
00408
00409
00410
00411 if( !$t->userCanRead() ) {
00412 wfProfileOut( __METHOD__ );
00413 return "<li>{$link}</li>\n";
00414 }
00415
00416
00417
00418
00419 if( $result->isMissingRevision() ) {
00420 wfProfileOut( __METHOD__ );
00421 return "<!-- missing page " . htmlspecialchars( $t->getPrefixedText() ) . "-->\n";
00422 }
00423
00424
00425 $redirectTitle = $result->getRedirectTitle();
00426 $redirectText = $result->getRedirectSnippet($terms);
00427 $sectionTitle = $result->getSectionTitle();
00428 $sectionText = $result->getSectionSnippet($terms);
00429 $redirect = '';
00430 if( !is_null($redirectTitle) )
00431 $redirect = "<span class='searchalttitle'>"
00432 .wfMsg('search-redirect',$this->sk->makeKnownLinkObj( $redirectTitle, $redirectText))
00433 ."</span>";
00434 $section = '';
00435 if( !is_null($sectionTitle) )
00436 $section = "<span class='searchalttitle'>"
00437 .wfMsg('search-section', $this->sk->makeKnownLinkObj( $sectionTitle, $sectionText))
00438 ."</span>";
00439
00440
00441 $extract = "<div class='searchresult'>".$result->getTextSnippet($terms)."</div>";
00442
00443
00444 if( is_null( $result->getScore() ) ) {
00445
00446 $score = '';
00447 } else {
00448 $percent = sprintf( '%2.1f', $result->getScore() * 100 );
00449 $score = wfMsg( 'search-result-score', $wgLang->formatNum( $percent ) )
00450 . ' - ';
00451 }
00452
00453
00454 $byteSize = $result->getByteSize();
00455 $wordCount = $result->getWordCount();
00456 $timestamp = $result->getTimestamp();
00457 $size = wfMsgExt( 'search-result-size', array( 'parsemag', 'escape' ),
00458 $this->sk->formatSize( $byteSize ), $wordCount );
00459 $date = $wgLang->timeanddate( $timestamp );
00460
00461
00462 $related = '';
00463 if( $result->hasRelated() ) {
00464 $st = SpecialPage::getTitleFor( 'Search' );
00465 $stParams = wfArrayToCGI( $this->powerSearchOptions(),
00466 array('search' => wfMsgForContent('searchrelated').':'.$t->getPrefixedText(),
00467 'fulltext' => wfMsg('search') ));
00468
00469 $related = ' -- ' . $sk->makeKnownLinkObj( $st,
00470 wfMsg('search-relatedarticle'), $stParams );
00471 }
00472
00473
00474 if( $t->getNamespace() == NS_FILE ) {
00475 $img = wfFindFile( $t );
00476 if( $img ) {
00477 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
00478 if( $thumb ) {
00479 $desc = $img->getShortDesc();
00480 wfProfileOut( __METHOD__ );
00481
00482
00483
00484 return "<li>" .
00485 '<table class="searchResultImage">' .
00486 '<tr>' .
00487 '<td width="120" align="center" valign="top">' .
00488 $thumb->toHtml( array( 'desc-link' => true ) ) .
00489 '</td>' .
00490 '<td valign="top">' .
00491 $link .
00492 $extract .
00493 "<div class='mw-search-result-data'>{$score}{$desc} - {$date}{$related}</div>" .
00494 '</td>' .
00495 '</tr>' .
00496 '</table>' .
00497 "</li>\n";
00498 }
00499 }
00500 }
00501
00502 wfProfileOut( __METHOD__ );
00503 return "<li>{$link} {$redirect} {$section} {$extract}\n" .
00504 "<div class='mw-search-result-data'>{$score}{$size} - {$date}{$related}</div>" .
00505 "</li>\n";
00506
00507 }
00508
00514 protected function showInterwiki( &$matches, $query ) {
00515 global $wgContLang;
00516 wfProfileIn( __METHOD__ );
00517 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
00518
00519 $out = "<div id='mw-search-interwiki'><div id='mw-search-interwiki-caption'>".
00520 wfMsg('search-interwiki-caption')."</div>\n";
00521 $off = $this->offset + 1;
00522 $out .= "<ul class='mw-search-iwresults'>\n";
00523
00524
00525 $customCaptions = array();
00526 $customLines = explode("\n",wfMsg('search-interwiki-custom'));
00527 foreach($customLines as $line) {
00528 $parts = explode(":",$line,2);
00529 if(count($parts) == 2)
00530 $customCaptions[$parts[0]] = $parts[1];
00531 }
00532
00533 $prev = null;
00534 while( $result = $matches->next() ) {
00535 $out .= $this->showInterwikiHit( $result, $prev, $terms, $query, $customCaptions );
00536 $prev = $result->getInterwikiPrefix();
00537 }
00538
00539 $out .= "</ul></div>\n";
00540
00541
00542 $out = $wgContLang->convert( $out );
00543 wfProfileOut( __METHOD__ );
00544 return $out;
00545 }
00546
00556 protected function showInterwikiHit( $result, $lastInterwiki, $terms, $query, $customCaptions) {
00557 wfProfileIn( __METHOD__ );
00558 global $wgContLang, $wgLang;
00559
00560 if( $result->isBrokenTitle() ) {
00561 wfProfileOut( __METHOD__ );
00562 return "<!-- Broken link in search result -->\n";
00563 }
00564
00565 $t = $result->getTitle();
00566
00567 $link = $this->sk->makeKnownLinkObj( $t, $result->getTitleSnippet($terms));
00568
00569
00570 $redirectTitle = $result->getRedirectTitle();
00571 $redirectText = $result->getRedirectSnippet($terms);
00572 $redirect = '';
00573 if( !is_null($redirectTitle) )
00574 $redirect = "<span class='searchalttitle'>"
00575 .wfMsg('search-redirect',$this->sk->makeKnownLinkObj( $redirectTitle, $redirectText))
00576 ."</span>";
00577
00578 $out = "";
00579
00580 if(is_null($lastInterwiki) || $lastInterwiki != $t->getInterwiki()) {
00581 if( key_exists($t->getInterwiki(),$customCaptions) )
00582
00583 $caption = $customCaptions[$t->getInterwiki()];
00584 else{
00585
00586
00587 $parsed = parse_url($t->getFullURL());
00588 $caption = wfMsg('search-interwiki-default', $parsed['host']);
00589 }
00590
00591 $searchTitle = Title::newFromText($t->getInterwiki().":Special:Search");
00592 $searchLink = $this->sk->makeKnownLinkObj( $searchTitle, wfMsg('search-interwiki-more'),
00593 wfArrayToCGI(array('search' => $query, 'fulltext' => 'Search')));
00594 $out .= "</ul><div class='mw-search-interwiki-project'><span class='mw-search-interwiki-more'>
00595 {$searchLink}</span>{$caption}</div>\n<ul>";
00596 }
00597
00598 $out .= "<li>{$link} {$redirect}</li>\n";
00599 wfProfileOut( __METHOD__ );
00600 return $out;
00601 }
00602
00603
00609 protected function powerSearchBox( $term ) {
00610 global $wgScript;
00611
00612 $namespaces = SearchEngine::searchableNamespaces();
00613
00614 $tables = $this->namespaceTables( $namespaces );
00615
00616 $redirect = Xml::check( 'redirs', $this->searchRedirects, array( 'value' => '1', 'id' => 'redirs' ) );
00617 $redirectLabel = Xml::label( wfMsg( 'powersearch-redir' ), 'redirs' );
00618 $searchField = Xml::inputLabel( wfMsg('powersearch-field'), 'search', 'powerSearchText', 50, $term,
00619 array( 'type' => 'text') );
00620 $searchButton = Xml::submitButton( wfMsg( 'powersearch' ), array( 'name' => 'fulltext' )) . "\n";
00621 $searchTitle = SpecialPage::getTitleFor( 'Search' );
00622
00623 $redirectText = '';
00624
00625 if( $this->searchEngine->acceptListRedirects() ) {
00626 $redirectText = "<p>". $redirect . " " . $redirectLabel ."</p>";
00627 }
00628
00629 $out = Xml::openElement( 'form', array( 'id' => 'powersearch', 'method' => 'get', 'action' => $wgScript ) ) .
00630 Xml::hidden( 'title', $searchTitle->getPrefixedText() ) . "\n" .
00631 "<p>" .
00632 wfMsgExt( 'powersearch-ns', array( 'parseinline' ) ) .
00633 "</p>\n" .
00634 '<input type="hidden" name="advanced" value="'.$this->searchAdvanced."\"/>\n".
00635 $tables .
00636 "<hr style=\"clear: both;\" />\n".
00637 $redirectText ."\n".
00638 "<div style=\"padding-top:2px;padding-bottom:2px;\">".
00639 $searchField .
00640 " " .
00641 Xml::hidden( 'fulltext', 'Advanced search' ) . "\n" .
00642 $searchButton .
00643 "</div>".
00644 "</form>";
00645 $t = Title::newFromText( $term );
00646
00647
00648
00649 return Xml::openElement( 'fieldset', array('id' => 'mw-searchoptions','style' => 'margin:0em;') ) .
00650 Xml::element( 'legend', null, wfMsg('powersearch-legend') ) .
00651 $this->formHeader($term) . $out . $this->didYouMeanHtml .
00652 Xml::closeElement( 'fieldset' );
00653 }
00654
00655 protected function searchFocus() {
00656 global $wgJsMimeType;
00657 return "<script type=\"$wgJsMimeType\">" .
00658 "hookEvent(\"load\", function() {" .
00659 "document.getElementById('searchText').focus();" .
00660 "});" .
00661 "</script>";
00662 }
00663
00664 protected function powerSearchFocus() {
00665 global $wgJsMimeType;
00666 return "<script type=\"$wgJsMimeType\">" .
00667 "hookEvent(\"load\", function() {" .
00668 "document.getElementById('powerSearchText').focus();" .
00669 "});" .
00670 "</script>";
00671 }
00672
00673 protected function formHeader( $term ) {
00674 global $wgContLang, $wgCanonicalNamespaceNames, $wgLang;
00675
00676 $sep = ' ';
00677 $out = Xml::openElement('div', array( 'style' => 'padding-bottom:0.5em;' ) );
00678
00679 $bareterm = $term;
00680 if( $this->startsWithImage( $term ) )
00681 $bareterm = substr( $term, strpos( $term, ':' ) + 1 );
00682
00683 $nsAllSet = array_keys( SearchEngine::searchableNamespaces() );
00684
00685
00686 $m = wfMsg( 'searchprofile-articles' );
00687 $tt = wfMsg( 'searchprofile-articles-tooltip',
00688 $wgLang->commaList( SearchEngine::namespacesAsText( SearchEngine::defaultNamespaces() ) ) );
00689 if( $this->active == 'default' ) {
00690 $out .= Xml::element( 'strong', array( 'title'=>$tt ), $m );
00691 } else {
00692 $out .= $this->makeSearchLink( $bareterm, SearchEngine::defaultNamespaces(), $m, $tt );
00693 }
00694 $out .= $sep;
00695
00696 $m = wfMsg( 'searchprofile-images' );
00697 $tt = wfMsg( 'searchprofile-images-tooltip' );
00698 if( $this->active == 'images' ) {
00699 $out .= Xml::element( 'strong', array( 'title'=>$tt ), $m );
00700 } else {
00701 $imageTextForm = $wgContLang->getFormattedNsText(NS_FILE).':'.$bareterm;
00702 $out .= $this->makeSearchLink( $imageTextForm, array( NS_FILE ) , $m, $tt );
00703 }
00704 $out .= $sep;
00705
00706 $m = wfMsg( 'searchprofile-project' );
00707 $tt = wfMsg( 'searchprofile-project-tooltip',
00708 $wgLang->commaList( SearchEngine::namespacesAsText( SearchEngine::projectNamespaces() ) ) );
00709 if( $this->active == 'project' ) {
00710 $out .= Xml::element( 'strong', array( 'title'=>$tt ), $m );
00711 } else {
00712 $out .= $this->makeSearchLink( $bareterm, SearchEngine::projectNamespaces(), $m, $tt );
00713 }
00714 $out .= $sep;
00715
00716 $m = wfMsg( 'searchprofile-everything' );
00717 $tt = wfMsg( 'searchprofile-everything-tooltip' );
00718 if( $this->active == 'all' ) {
00719 $out .= Xml::element( 'strong', array( 'title'=>$tt ), $m );
00720 } else {
00721 $out .= $this->makeSearchLink( $bareterm, $nsAllSet, $m, $tt );
00722 }
00723 $out .= $sep;
00724
00725 $m = wfMsg( 'searchprofile-advanced' );
00726 $tt = wfMsg( 'searchprofile-advanced-tooltip' );
00727 if( $this->active == 'advanced' ) {
00728 $out .= Xml::element( 'strong', array( 'title'=>$tt ), $m );
00729 } else {
00730 $out .= $this->makeSearchLink( $bareterm, $this->namespaces, $m, $tt, array( 'advanced' => '1' ) );
00731 }
00732 $out .= Xml::closeElement('div') ;
00733
00734 return $out;
00735 }
00736
00737 protected function shortDialog( $term ) {
00738 global $wgScript;
00739 $searchTitle = SpecialPage::getTitleFor( 'Search' );
00740 $searchable = SearchEngine::searchableNamespaces();
00741 $out = Xml::openElement( 'form', array( 'id' => 'search', 'method' => 'get', 'action' => $wgScript ) );
00742 $out .= Xml::hidden( 'title', $searchTitle->getPrefixedText() ) . "\n";
00743
00744 if( $this->active == 'advanced' ) {
00745 $active = array();
00746 foreach( $this->namespaces as $ns ) {
00747 $active[$ns] = $searchable[$ns];
00748 }
00749 $out .= wfMsgExt( 'powersearch-ns', array( 'parseinline' ) ) . "<br/>\n";
00750 $out .= $this->namespaceTables( $active, 1 )."<br/>\n";
00751
00752 } else {
00753 foreach( $this->namespaces as $ns ) {
00754 $out .= Xml::hidden( "ns{$ns}", '1' );
00755 }
00756 }
00757
00758 $out .= Xml::hidden( "redirs", (int)$this->searchRedirects );
00759
00760 $out .= Xml::input( 'search', 50, $term, array( 'type' => 'text', 'id' => 'searchText' ) ) . "\n";
00761 $out .= Xml::hidden( 'fulltext', 'Search' );
00762 $out .= Xml::submitButton( wfMsg( 'searchbutton' ), array( 'name' => 'fulltext' ) );
00763 $out .= ' (' . wfMsgExt('searchmenu-help',array('parseinline') ) . ')';
00764 $out .= Xml::closeElement( 'form' );
00765
00766 $t = Title::newFromText( $term );
00767
00768
00769
00770 return Xml::openElement( 'fieldset', array('id' => 'mw-searchoptions','style' => 'margin:0em;') ) .
00771 Xml::element( 'legend', null, wfMsg('searchmenu-legend') ) .
00772 $this->formHeader($term) . $out . $this->didYouMeanHtml .
00773 Xml::closeElement( 'fieldset' );
00774 }
00775
00777 protected function makeSearchLink( $term, $namespaces, $label, $tooltip, $params=array() ) {
00778 $opt = $params;
00779 foreach( $namespaces as $n ) {
00780 $opt['ns' . $n] = 1;
00781 }
00782 $opt['redirs'] = $this->searchRedirects ? 1 : 0;
00783
00784 $st = SpecialPage::getTitleFor( 'Search' );
00785 $stParams = wfArrayToCGI( array( 'search' => $term, 'fulltext' => wfMsg( 'search' ) ), $opt );
00786
00787 return Xml::element( 'a',
00788 array( 'href'=> $st->getLocalURL( $stParams ), 'title' => $tooltip ),
00789 $label );
00790 }
00791
00793 protected function startsWithImage( $term ) {
00794 global $wgContLang;
00795
00796 $p = explode( ':', $term );
00797 if( count( $p ) > 1 ) {
00798 return $wgContLang->getNsIndex( $p[0] ) == NS_FILE;
00799 }
00800 return false;
00801 }
00802
00803 protected function namespaceTables( $namespaces, $rowsPerTable = 3 ) {
00804 global $wgContLang;
00805
00806
00807 $rows = array();
00808 $tables = "";
00809 foreach( $namespaces as $ns => $name ) {
00810 $subj = MWNamespace::getSubject( $ns );
00811 if( !array_key_exists( $subj, $rows ) ) {
00812 $rows[$subj] = "";
00813 }
00814 $name = str_replace( '_', ' ', $name );
00815 if( '' == $name ) {
00816 $name = wfMsg( 'blanknamespace' );
00817 }
00818 $rows[$subj] .= Xml::openElement( 'td', array( 'style' => 'white-space: nowrap' ) ) .
00819 Xml::checkLabel( $name, "ns{$ns}", "mw-search-ns{$ns}", in_array( $ns, $this->namespaces ) ) .
00820 Xml::closeElement( 'td' ) . "\n";
00821 }
00822 $rows = array_values( $rows );
00823 $numRows = count( $rows );
00824
00825
00826
00827 $tableStyle = $wgContLang->isRTL() ?
00828 'float: right; margin: 0 0 0em 1em' : 'float: left; margin: 0 1em 0em 0';
00829
00830 for( $i = 0; $i < $numRows; $i += $rowsPerTable ) {
00831 $tables .= Xml::openElement( 'table', array( 'style' => $tableStyle ) );
00832 for( $j = $i; $j < $i + $rowsPerTable && $j < $numRows; $j++ ) {
00833 $tables .= "<tr>\n" . $rows[$j] . "</tr>";
00834 }
00835 $tables .= Xml::closeElement( 'table' ) . "\n";
00836 }
00837 return $tables;
00838 }
00839 }
00840
00845 class SpecialSearchOld {
00846
00855 function __construct( &$request, &$user ) {
00856 list( $this->limit, $this->offset ) = $request->getLimitOffset( 20, 'searchlimit' );
00857 $this->mPrefix = $request->getVal('prefix', '');
00858 $this->namespaces = $this->powerSearch( $request );
00859 if( empty( $this->namespaces ) ) {
00860 $this->namespaces = SearchEngine::userNamespaces( $user );
00861 }
00862
00863 $this->searchRedirects = $request->getcheck( 'redirs' ) ? true : false;
00864 $this->fulltext = $request->getVal('fulltext');
00865 }
00866
00872 function goResult( $term ) {
00873 global $wgOut;
00874 global $wgGoToEdit;
00875
00876 $this->setupPage( $term );
00877
00878 # Try to go to page as entered.
00879 $t = Title::newFromText( $term );
00880
00881 # If the string cannot be used to create a title
00882 if( is_null( $t ) ){
00883 return $this->showResults( $term );
00884 }
00885
00886 # If there's an exact or very near match, jump right there.
00887 $t = SearchEngine::getNearMatch( $term );
00888 if( !is_null( $t ) ) {
00889 $wgOut->redirect( $t->getFullURL() );
00890 return;
00891 }
00892
00893 # No match, generate an edit URL
00894 $t = Title::newFromText( $term );
00895 if( ! is_null( $t ) ) {
00896 wfRunHooks( 'SpecialSearchNogomatch', array( &$t ) );
00897 # If the feature is enabled, go straight to the edit page
00898 if ( $wgGoToEdit ) {
00899 $wgOut->redirect( $t->getFullURL( 'action=edit' ) );
00900 return;
00901 }
00902 }
00903
00904 $extra = $wgOut->parse( '=='.wfMsgNoTrans( 'notitlematches' )."==\n" );
00905 if( $t->quickUserCan( 'create' ) && $t->quickUserCan( 'edit' ) ) {
00906 $extra .= wfMsgExt( 'noexactmatch', 'parse', wfEscapeWikiText( $term ) );
00907 } else {
00908 $extra .= wfMsgExt( 'noexactmatch-nocreate', 'parse', wfEscapeWikiText( $term ) );
00909 }
00910
00911 $this->showResults( $term, $extra );
00912 }
00913
00918 public function showResults( $term, $extra = '' ) {
00919 wfProfileIn( __METHOD__ );
00920 global $wgOut, $wgUser;
00921 $sk = $wgUser->getSkin();
00922
00923 $search = SearchEngine::create();
00924 $search->setLimitOffset( $this->limit, $this->offset );
00925 $search->setNamespaces( $this->namespaces );
00926 $search->showRedirects = $this->searchRedirects;
00927 $search->prefix = $this->mPrefix;
00928 $term = $search->transformSearchTerm($term);
00929
00930 $this->setupPage( $term );
00931
00932 $rewritten = $search->replacePrefixes($term);
00933 $titleMatches = $search->searchTitle( $rewritten );
00934 $textMatches = $search->searchText( $rewritten );
00935
00936
00937 if($textMatches && $textMatches->hasSuggestion()){
00938 $st = SpecialPage::getTitleFor( 'Search' );
00939
00940 # mirror Go/Search behaviour of original request
00941 $didYouMeanParams = array( 'search' => $textMatches->getSuggestionQuery() );
00942 if($this->fulltext != NULL)
00943 $didYouMeanParams['fulltext'] = $this->fulltext;
00944 $stParams = wfArrayToCGI(
00945 $didYouMeanParams,
00946 $this->powerSearchOptions()
00947 );
00948
00949 $suggestLink = $sk->makeKnownLinkObj( $st,
00950 $textMatches->getSuggestionSnippet(),
00951 $stParams );
00952
00953 $wgOut->addHTML('<div class="searchdidyoumean">'.wfMsg('search-suggest',$suggestLink).'</div>');
00954 }
00955
00956 $wgOut->addHTML( $extra );
00957
00958 $wgOut->wrapWikiMsg( "<div class='mw-searchresult'>\n$1</div>", 'searchresulttext' );
00959
00960 if( '' === trim( $term ) ) {
00961
00962 $wgOut->setSubtitle( '' );
00963 $wgOut->addHTML( $this->powerSearchBox( $term ) );
00964 $wgOut->addHTML( $this->powerSearchFocus() );
00965 wfProfileOut( __METHOD__ );
00966 return;
00967 }
00968
00969 global $wgDisableTextSearch;
00970 if ( $wgDisableTextSearch ) {
00971 global $wgSearchForwardUrl;
00972 if( $wgSearchForwardUrl ) {
00973 $url = str_replace( '$1', urlencode( $term ), $wgSearchForwardUrl );
00974 $wgOut->redirect( $url );
00975 wfProfileOut( __METHOD__ );
00976 return;
00977 }
00978 global $wgInputEncoding;
00979 $wgOut->addHTML(
00980 Xml::openElement( 'fieldset' ) .
00981 Xml::element( 'legend', null, wfMsg( 'search-external' ) ) .
00982 Xml::element( 'p', array( 'class' => 'mw-searchdisabled' ), wfMsg( 'searchdisabled' ) ) .
00983 wfMsg( 'googlesearch',
00984 htmlspecialchars( $term ),
00985 htmlspecialchars( $wgInputEncoding ),
00986 htmlspecialchars( wfMsg( 'searchbutton' ) )
00987 ) .
00988 Xml::closeElement( 'fieldset' )
00989 );
00990 wfProfileOut( __METHOD__ );
00991 return;
00992 }
00993
00994 $wgOut->addHTML( $this->shortDialog( $term ) );
00995
00996
00997 if ($titleMatches instanceof SearchResultTooMany) {
00998 $wgOut->addWikiText( '==' . wfMsg( 'toomanymatches' ) . "==\n" );
00999 $wgOut->addHTML( $this->powerSearchBox( $term ) );
01000 $wgOut->addHTML( $this->powerSearchFocus() );
01001 wfProfileOut( __METHOD__ );
01002 return;
01003 }
01004
01005
01006 $num = ( $titleMatches ? $titleMatches->numRows() : 0 )
01007 + ( $textMatches ? $textMatches->numRows() : 0);
01008 $totalNum = 0;
01009 if($titleMatches && !is_null($titleMatches->getTotalHits()))
01010 $totalNum += $titleMatches->getTotalHits();
01011 if($textMatches && !is_null($textMatches->getTotalHits()))
01012 $totalNum += $textMatches->getTotalHits();
01013 if ( $num > 0 ) {
01014 if ( $totalNum > 0 ){
01015 $top = wfMsgExt('showingresultstotal', array( 'parseinline' ),
01016 $this->offset+1, $this->offset+$num, $totalNum, $num );
01017 } elseif ( $num >= $this->limit ) {
01018 $top = wfShowingResults( $this->offset, $this->limit );
01019 } else {
01020 $top = wfShowingResultsNum( $this->offset, $this->limit, $num );
01021 }
01022 $wgOut->addHTML( "<p class='mw-search-numberresults'>{$top}</p>\n" );
01023 }
01024
01025
01026 if( $num || $this->offset ) {
01027 $prevnext = wfViewPrevNext( $this->offset, $this->limit,
01028 SpecialPage::getTitleFor( 'Search' ),
01029 wfArrayToCGI(
01030 $this->powerSearchOptions(),
01031 array( 'search' => $term ) ),
01032 ($num < $this->limit) );
01033 $wgOut->addHTML( "<p class='mw-search-pager-top'>{$prevnext}</p>\n" );
01034 wfRunHooks( 'SpecialSearchResults', array( $term, &$titleMatches, &$textMatches ) );
01035 } else {
01036 wfRunHooks( 'SpecialSearchNoResults', array( $term ) );
01037 }
01038
01039 if( $titleMatches ) {
01040 if( $titleMatches->numRows() ) {
01041 $wgOut->wrapWikiMsg( "==$1==\n", 'titlematches' );
01042 $wgOut->addHTML( $this->showMatches( $titleMatches ) );
01043 }
01044 $titleMatches->free();
01045 }
01046
01047 if( $textMatches ) {
01048
01049 if( $textMatches->numRows() ) {
01050 if($titleMatches)
01051 $wgOut->wrapWikiMsg( "==$1==\n", 'textmatches' );
01052 else
01053 $wgOut->addHTML("<hr/>");
01054 } elseif( $num == 0 ) {
01055 # Don't show the 'no text matches' if we received title matches
01056 $wgOut->wrapWikiMsg( "==$1==\n", 'notextmatches' );
01057 }
01058
01059 if( $textMatches->hasInterwikiResults() )
01060 $wgOut->addHTML( $this->showInterwiki( $textMatches->getInterwikiResults(), $term ));
01061
01062 if( $textMatches->numRows() )
01063 $wgOut->addHTML( $this->showMatches( $textMatches ) );
01064
01065 $textMatches->free();
01066 }
01067
01068 if ( $num == 0 ) {
01069 $wgOut->addWikiMsg( 'nonefound' );
01070 }
01071 if( $num || $this->offset ) {
01072 $wgOut->addHTML( "<p class='mw-search-pager-bottom'>{$prevnext}</p>\n" );
01073 }
01074 $wgOut->addHTML( $this->powerSearchBox( $term ) );
01075 wfProfileOut( __METHOD__ );
01076 }
01077
01078 #------------------------------------------------------------------
01079 # Private methods below this line
01080
01084 function setupPage( $term ) {
01085 global $wgOut;
01086 if( !empty( $term ) ){
01087 $wgOut->setPageTitle( wfMsg( 'searchresults') );
01088 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'searchresults-title', $term) ) );
01089 }
01090 $subtitlemsg = ( Title::newFromText( $term ) ? 'searchsubtitle' : 'searchsubtitleinvalid' );
01091 $wgOut->setSubtitle( $wgOut->parse( wfMsg( $subtitlemsg, wfEscapeWikiText($term) ) ) );
01092 $wgOut->setArticleRelated( false );
01093 $wgOut->setRobotPolicy( 'noindex,nofollow' );
01094 }
01095
01104 function powerSearch( &$request ) {
01105 $arr = array();
01106 foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
01107 if( $request->getCheck( 'ns' . $ns ) ) {
01108 $arr[] = $ns;
01109 }
01110 }
01111 return $arr;
01112 }
01113
01119 function powerSearchOptions() {
01120 $opt = array();
01121 foreach( $this->namespaces as $n ) {
01122 $opt['ns' . $n] = 1;
01123 }
01124 $opt['redirs'] = $this->searchRedirects ? 1 : 0;
01125 return $opt;
01126 }
01127
01133 function showMatches( &$matches ) {
01134 wfProfileIn( __METHOD__ );
01135
01136 global $wgContLang;
01137 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
01138
01139 $out = "";
01140
01141 $infoLine = $matches->getInfo();
01142 if( !is_null($infoLine) )
01143 $out .= "\n<!-- {$infoLine} -->\n";
01144
01145
01146 $off = $this->offset + 1;
01147 $out .= "<ul class='mw-search-results'>\n";
01148
01149 while( $result = $matches->next() ) {
01150 $out .= $this->showHit( $result, $terms );
01151 }
01152 $out .= "</ul>\n";
01153
01154
01155 global $wgContLang;
01156 $out = $wgContLang->convert( $out );
01157 wfProfileOut( __METHOD__ );
01158 return $out;
01159 }
01160
01166 function showHit( $result, $terms ) {
01167 wfProfileIn( __METHOD__ );
01168 global $wgUser, $wgContLang, $wgLang;
01169
01170 if( $result->isBrokenTitle() ) {
01171 wfProfileOut( __METHOD__ );
01172 return "<!-- Broken link in search result -->\n";
01173 }
01174
01175 $t = $result->getTitle();
01176 $sk = $wgUser->getSkin();
01177
01178 $link = $sk->makeKnownLinkObj( $t, $result->getTitleSnippet($terms));
01179
01180
01181
01182
01183 if (!$t->userCanRead()) {
01184 wfProfileOut( __METHOD__ );
01185 return "<li>{$link}</li>\n";
01186 }
01187
01188
01189
01190
01191 if( $result->isMissingRevision() ) {
01192 wfProfileOut( __METHOD__ );
01193 return "<!-- missing page " .
01194 htmlspecialchars( $t->getPrefixedText() ) . "-->\n";
01195 }
01196
01197
01198 $redirectTitle = $result->getRedirectTitle();
01199 $redirectText = $result->getRedirectSnippet($terms);
01200 $sectionTitle = $result->getSectionTitle();
01201 $sectionText = $result->getSectionSnippet($terms);
01202 $redirect = '';
01203 if( !is_null($redirectTitle) )
01204 $redirect = "<span class='searchalttitle'>"
01205 .wfMsg('search-redirect',$sk->makeKnownLinkObj( $redirectTitle, $redirectText))
01206 ."</span>";
01207 $section = '';
01208 if( !is_null($sectionTitle) )
01209 $section = "<span class='searchalttitle'>"
01210 .wfMsg('search-section', $sk->makeKnownLinkObj( $sectionTitle, $sectionText))
01211 ."</span>";
01212
01213
01214 $extract = "<div class='searchresult'>".$result->getTextSnippet($terms)."</div>";
01215
01216
01217 if( is_null( $result->getScore() ) ) {
01218
01219 $score = '';
01220 } else {
01221 $percent = sprintf( '%2.1f', $result->getScore() * 100 );
01222 $score = wfMsg( 'search-result-score', $wgLang->formatNum( $percent ) )
01223 . ' - ';
01224 }
01225
01226
01227 $byteSize = $result->getByteSize();
01228 $wordCount = $result->getWordCount();
01229 $timestamp = $result->getTimestamp();
01230 $size = wfMsgExt( 'search-result-size', array( 'parsemag', 'escape' ),
01231 $sk->formatSize( $byteSize ),
01232 $wordCount );
01233 $date = $wgLang->timeanddate( $timestamp );
01234
01235
01236 $related = '';
01237 if( $result->hasRelated() ){
01238 $st = SpecialPage::getTitleFor( 'Search' );
01239 $stParams = wfArrayToCGI( $this->powerSearchOptions(),
01240 array('search' => wfMsgForContent('searchrelated').':'.$t->getPrefixedText(),
01241 'fulltext' => wfMsg('search') ));
01242
01243 $related = ' -- ' . $sk->makeKnownLinkObj( $st,
01244 wfMsg('search-relatedarticle'), $stParams );
01245 }
01246
01247
01248 if( $t->getNamespace() == NS_FILE ) {
01249 $img = wfFindFile( $t );
01250 if( $img ) {
01251 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
01252 if( $thumb ) {
01253 $desc = $img->getShortDesc();
01254 wfProfileOut( __METHOD__ );
01255
01256
01257
01258
01259 return "<li>" .
01260 '<table class="searchResultImage">' .
01261 '<tr>' .
01262 '<td width="120" align="center">' .
01263 $thumb->toHtml( array( 'desc-link' => true ) ) .
01264 '</td>' .
01265 '<td valign="top">' .
01266 $link .
01267 $extract .
01268 "<div class='mw-search-result-data'>{$score}{$desc} - {$date}{$related}</div>" .
01269 '</td>' .
01270 '</tr>' .
01271 '</table>' .
01272 "</li>\n";
01273 }
01274 }
01275 }
01276
01277 wfProfileOut( __METHOD__ );
01278 return "<li>{$link} {$redirect} {$section} {$extract}\n" .
01279 "<div class='mw-search-result-data'>{$score}{$size} - {$date}{$related}</div>" .
01280 "</li>\n";
01281
01282 }
01283
01289 function showInterwiki( &$matches, $query ) {
01290 wfProfileIn( __METHOD__ );
01291
01292 global $wgContLang;
01293 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
01294
01295 $out = "<div id='mw-search-interwiki'><div id='mw-search-interwiki-caption'>".wfMsg('search-interwiki-caption')."</div>\n";
01296 $off = $this->offset + 1;
01297 $out .= "<ul start='{$off}' class='mw-search-iwresults'>\n";
01298
01299
01300 $customCaptions = array();
01301 $customLines = explode("\n",wfMsg('search-interwiki-custom'));
01302 foreach($customLines as $line){
01303 $parts = explode(":",$line,2);
01304 if(count($parts) == 2)
01305 $customCaptions[$parts[0]] = $parts[1];
01306 }
01307
01308
01309 $prev = null;
01310 while( $result = $matches->next() ) {
01311 $out .= $this->showInterwikiHit( $result, $prev, $terms, $query, $customCaptions );
01312 $prev = $result->getInterwikiPrefix();
01313 }
01314
01315 $out .= "</ul></div>\n";
01316
01317
01318 global $wgContLang;
01319 $out = $wgContLang->convert( $out );
01320 wfProfileOut( __METHOD__ );
01321 return $out;
01322 }
01323
01333 function showInterwikiHit( $result, $lastInterwiki, $terms, $query, $customCaptions) {
01334 wfProfileIn( __METHOD__ );
01335 global $wgUser, $wgContLang, $wgLang;
01336
01337 if( $result->isBrokenTitle() ) {
01338 wfProfileOut( __METHOD__ );
01339 return "<!-- Broken link in search result -->\n";
01340 }
01341
01342 $t = $result->getTitle();
01343 $sk = $wgUser->getSkin();
01344
01345 $link = $sk->makeKnownLinkObj( $t, $result->getTitleSnippet($terms));
01346
01347
01348 $redirectTitle = $result->getRedirectTitle();
01349 $redirectText = $result->getRedirectSnippet($terms);
01350 $redirect = '';
01351 if( !is_null($redirectTitle) )
01352 $redirect = "<span class='searchalttitle'>"
01353 .wfMsg('search-redirect',$sk->makeKnownLinkObj( $redirectTitle, $redirectText))
01354 ."</span>";
01355
01356 $out = "";
01357
01358 if(is_null($lastInterwiki) || $lastInterwiki != $t->getInterwiki()){
01359 if( key_exists($t->getInterwiki(),$customCaptions) )
01360
01361 $caption = $customCaptions[$t->getInterwiki()];
01362 else{
01363
01364
01365 $parsed = parse_url($t->getFullURL());
01366 $caption = wfMsg('search-interwiki-default', $parsed['host']);
01367 }
01368
01369 $searchTitle = Title::newFromText($t->getInterwiki().":Special:Search");
01370 $searchLink = $sk->makeKnownLinkObj( $searchTitle, wfMsg('search-interwiki-more'),
01371 wfArrayToCGI(array('search' => $query, 'fulltext' => 'Search')));
01372 $out .= "</ul><div class='mw-search-interwiki-project'><span class='mw-search-interwiki-more'>{$searchLink}</span>{$caption}</div>\n<ul>";
01373 }
01374
01375 $out .= "<li>{$link} {$redirect}</li>\n";
01376 wfProfileOut( __METHOD__ );
01377 return $out;
01378 }
01379
01380
01386 function powerSearchBox( $term ) {
01387 global $wgScript, $wgContLang;
01388
01389 $namespaces = SearchEngine::searchableNamespaces();
01390
01391
01392
01393 $rows = array();
01394 foreach( $namespaces as $ns => $name ) {
01395 $subj = MWNamespace::getSubject( $ns );
01396 if( !array_key_exists( $subj, $rows ) ) {
01397 $rows[$subj] = "";
01398 }
01399 $name = str_replace( '_', ' ', $name );
01400 if( '' == $name ) {
01401 $name = wfMsg( 'blanknamespace' );
01402 }
01403 $rows[$subj] .= Xml::openElement( 'td', array( 'style' => 'white-space: nowrap' ) ) .
01404 Xml::checkLabel( $name, "ns{$ns}", "mw-search-ns{$ns}", in_array( $ns, $this->namespaces ) ) .
01405 Xml::closeElement( 'td' ) . "\n";
01406 }
01407 $rows = array_values( $rows );
01408 $numRows = count( $rows );
01409
01410
01411
01412 $rowsPerTable = 3;
01413
01414
01415 $tableStyle = ( $wgContLang->isRTL() ?
01416 'float: right; margin: 0 0 1em 1em' :
01417 'float: left; margin: 0 1em 1em 0' );
01418
01419 $tables = "";
01420 for( $i = 0; $i < $numRows; $i += $rowsPerTable ) {
01421 $tables .= Xml::openElement( 'table', array( 'style' => $tableStyle ) );
01422 for( $j = $i; $j < $i + $rowsPerTable && $j < $numRows; $j++ ) {
01423 $tables .= "<tr>\n" . $rows[$j] . "</tr>";
01424 }
01425 $tables .= Xml::closeElement( 'table' ) . "\n";
01426 }
01427
01428 $redirect = Xml::check( 'redirs', $this->searchRedirects, array( 'value' => '1', 'id' => 'redirs' ) );
01429 $redirectLabel = Xml::label( wfMsg( 'powersearch-redir' ), 'redirs' );
01430 $searchField = Xml::input( 'search', 50, $term, array( 'type' => 'text', 'id' => 'powerSearchText' ) );
01431 $searchButton = Xml::submitButton( wfMsg( 'powersearch' ), array( 'name' => 'fulltext' ) ) . "\n";
01432 $searchTitle = SpecialPage::getTitleFor( 'Search' );
01433 $searchHiddens = Xml::hidden( 'title', $searchTitle->getPrefixedText() ) . "\n";
01434 $searchHiddens .= Xml::hidden( 'fulltext', 'Advanced search' ) . "\n";
01435
01436 $out = Xml::openElement( 'form', array( 'id' => 'powersearch', 'method' => 'get', 'action' => $wgScript ) ) .
01437 Xml::fieldset( wfMsg( 'powersearch-legend' ),
01438 "<p>" .
01439 wfMsgExt( 'powersearch-ns', array( 'parseinline' ) ) .
01440 "</p>\n" .
01441 $tables .
01442 "<hr style=\"clear: both\" />\n" .
01443 "<p>" .
01444 $redirect . " " . $redirectLabel .
01445 "</p>\n" .
01446 wfMsgExt( 'powersearch-field', array( 'parseinline' ) ) .
01447 " " .
01448 $searchField .
01449 " " .
01450 $searchHiddens .
01451 $searchButton ) .
01452 "</form>";
01453
01454 return $out;
01455 }
01456
01457 function powerSearchFocus() {
01458 global $wgJsMimeType;
01459 return "<script type=\"$wgJsMimeType\">" .
01460 "hookEvent(\"load\", function(){" .
01461 "document.getElementById('powerSearchText').focus();" .
01462 "});" .
01463 "</script>";
01464 }
01465
01466 function shortDialog($term) {
01467 global $wgScript;
01468
01469 $out = Xml::openElement( 'form', array(
01470 'id' => 'search',
01471 'method' => 'get',
01472 'action' => $wgScript
01473 ));
01474 $searchTitle = SpecialPage::getTitleFor( 'Search' );
01475 $out .= Xml::input( 'search', 50, $term, array( 'type' => 'text', 'id' => 'searchText' ) ) . ' ';
01476 foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
01477 if( in_array( $ns, $this->namespaces ) ) {
01478 $out .= Xml::hidden( "ns{$ns}", '1' );
01479 }
01480 }
01481 $out .= Xml::hidden( 'title', $searchTitle->getPrefixedText() );
01482 $out .= Xml::hidden( 'fulltext', 'Search' );
01483 $out .= Xml::submitButton( wfMsg( 'searchbutton' ), array( 'name' => 'fulltext' ) );
01484 $out .= Xml::closeElement( 'form' );
01485
01486 return $out;
01487 }
01488 }