a4ab7a802748dcbf6e20e6cbc4cbd2d2ca8900b4
3 * Implements Special:Search
5 * Copyright © 2004 Brion Vibber <brion@pobox.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
23 * @ingroup SpecialPage
27 * implements Special:Search - Run text & title search and display the output
28 * @ingroup SpecialPage
30 class SpecialSearch
extends SpecialPage
{
32 * Current search profile. Search profile is just a name that identifies
33 * the active search tab on the search page (content, help, discussions...)
34 * For users tt replaces the set of enabled namespaces from the query
35 * string when applicable. Extensions can add new profiles with hooks
36 * with custom search options just for that profile.
40 function getProfile() { return $this->profile
; }
43 protected $searchEngine;
46 protected $extraParams = array();
48 /// No idea, apparently used by some other classes
54 protected $limit, $offset;
59 protected $namespaces;
60 function getNamespaces() { return $this->namespaces
; }
65 protected $searchRedirects;
70 protected $didYouMeanHtml, $fulltext;
72 const NAMESPACES_CURRENT
= 'sense';
74 public function __construct() {
75 parent
::__construct( 'Search' );
81 * @param string $par or null
83 public function execute( $par ) {
85 $this->outputHeader();
86 $out = $this->getOutput();
87 $out->allowClickjacking();
88 $out->addModuleStyles( 'mediawiki.special' );
90 // Strip underscores from title parameter; most of the time we'll want
91 // text form here. But don't strip underscores from actual text params!
92 $titleParam = str_replace( '_', ' ', $par );
94 $request = $this->getRequest();
96 // Fetch the search term
97 $search = str_replace( "\n", " ", $request->getText( 'search', $titleParam ) );
101 if ( $request->getVal( 'fulltext' )
102 ||
!is_null( $request->getVal( 'offset' ) )
103 ||
!is_null( $request->getVal( 'searchx' ) ) )
105 $this->showResults( $search );
107 $this->goResult( $search );
112 * Set up basic search parameters from the request and user settings.
114 * @see tests/phpunit/includes/specials/SpecialSearchTest.php
116 public function load() {
117 $request = $this->getRequest();
118 list( $this->limit
, $this->offset
) = $request->getLimitOffset( 20, 'searchlimit' );
119 $this->mPrefix
= $request->getVal( 'prefix', '' );
121 $user = $this->getUser();
123 # Extract manually requested namespaces
124 $nslist = $this->powerSearch( $request );
125 if ( !count( $nslist ) ) {
126 # Fallback to user preference
127 $nslist = SearchEngine
::userNamespaces( $user );
131 if ( !count( $nslist ) ) {
132 $profile = 'default';
135 $profile = $request->getVal( 'profile', $profile );
136 $profiles = $this->getSearchProfiles();
137 if ( $profile === null ) {
138 // BC with old request format
139 $profile = 'advanced';
140 foreach( $profiles as $key => $data ) {
141 if ( $nslist === $data['namespaces'] && $key !== 'advanced' ) {
145 $this->namespaces
= $nslist;
146 } elseif ( $profile === 'advanced' ) {
147 $this->namespaces
= $nslist;
149 if ( isset( $profiles[$profile]['namespaces'] ) ) {
150 $this->namespaces
= $profiles[$profile]['namespaces'];
152 // Unknown profile requested
153 $profile = 'default';
154 $this->namespaces
= $profiles['default']['namespaces'];
158 // Redirects defaults to true, but we don't know whether it was ticked of or just missing
159 $default = $request->getBool( 'profile' ) ?
0 : 1;
160 $this->searchRedirects
= $request->getBool( 'redirs', $default ) ?
1 : 0;
161 $this->didYouMeanHtml
= ''; # html of did you mean... link
162 $this->fulltext
= $request->getVal( 'fulltext' );
163 $this->profile
= $profile;
167 * If an exact title match can be found, jump straight ahead to it.
169 * @param $term String
171 public function goResult( $term ) {
172 $this->setupPage( $term );
173 # Try to go to page as entered.
174 $t = Title
::newFromText( $term );
175 # If the string cannot be used to create a title
176 if( is_null( $t ) ) {
177 $this->showResults( $term );
180 # If there's an exact or very near match, jump right there.
181 $t = SearchEngine
::getNearMatch( $term );
183 if ( !wfRunHooks( 'SpecialSearchGo', array( &$t, &$term ) ) ) {
184 # Hook requested termination
188 if( !is_null( $t ) ) {
189 $this->getOutput()->redirect( $t->getFullURL() );
192 # No match, generate an edit URL
193 $t = Title
::newFromText( $term );
194 if( !is_null( $t ) ) {
196 wfRunHooks( 'SpecialSearchNogomatch', array( &$t ) );
197 wfDebugLog( 'nogomatch', $t->getText(), false );
199 # If the feature is enabled, go straight to the edit page
201 $this->getOutput()->redirect( $t->getFullURL( array( 'action' => 'edit' ) ) );
205 $this->showResults( $term );
209 * @param $term String
211 public function showResults( $term ) {
212 global $wgDisableTextSearch, $wgSearchForwardUrl, $wgContLang, $wgScript;
213 wfProfileIn( __METHOD__
);
215 $search = $this->getSearchEngine();
216 $search->setLimitOffset( $this->limit
, $this->offset
);
217 $search->setNamespaces( $this->namespaces
);
218 $search->showRedirects
= $this->searchRedirects
; // BC
219 $search->setFeatureData( 'list-redirects', $this->searchRedirects
);
220 $search->prefix
= $this->mPrefix
;
221 $term = $search->transformSearchTerm( $term );
223 wfRunHooks( 'SpecialSearchSetupEngine', array( $this, $this->profile
, $search ) );
225 $this->setupPage( $term );
227 $out = $this->getOutput();
229 if ( $wgDisableTextSearch ) {
230 if ( $wgSearchForwardUrl ) {
231 $url = str_replace( '$1', urlencode( $term ), $wgSearchForwardUrl );
232 $out->redirect( $url );
235 Xml
::openElement( 'fieldset' ) .
236 Xml
::element( 'legend', null, $this->msg( 'search-external' )->text() ) .
237 Xml
::element( 'p', array( 'class' => 'mw-searchdisabled' ), $this->msg( 'searchdisabled' )->text() ) .
238 $this->msg( 'googlesearch' )->rawParams(
239 htmlspecialchars( $term ),
241 $this->msg( 'searchbutton' )->escaped()
243 Xml
::closeElement( 'fieldset' )
246 wfProfileOut( __METHOD__
);
250 $t = Title
::newFromText( $term );
252 // fetch search results
253 $rewritten = $search->replacePrefixes( $term );
255 $titleMatches = $search->searchTitle( $rewritten );
256 if( !( $titleMatches instanceof SearchResultTooMany
) ) {
257 $textMatches = $search->searchText( $rewritten );
261 if( $textMatches instanceof Status
) {
262 $textStatus = $textMatches;
266 // did you mean... suggestions
267 if( $textMatches && !$textStatus && $textMatches->hasSuggestion() ) {
268 $st = SpecialPage
::getTitleFor( 'Search' );
270 # mirror Go/Search behavior of original request ..
271 $didYouMeanParams = array( 'search' => $textMatches->getSuggestionQuery() );
273 if( $this->fulltext
!= null ) {
274 $didYouMeanParams['fulltext'] = $this->fulltext
;
277 $stParams = array_merge(
279 $this->powerSearchOptions()
282 $suggestionSnippet = $textMatches->getSuggestionSnippet();
284 if( $suggestionSnippet == '' ) {
285 $suggestionSnippet = null;
288 $suggestLink = Linker
::linkKnown(
295 $this->didYouMeanHtml
= '<div class="searchdidyoumean">' . $this->msg( 'search-suggest' )->rawParams( $suggestLink )->text() . '</div>';
298 if ( !wfRunHooks( 'SpecialSearchResultsPrepend', array( $this, $out, $term ) ) ) {
299 # Hook requested termination
300 wfProfileOut( __METHOD__
);
304 // start rendering the page
309 'id' => ( $this->profile
=== 'advanced' ?
'powersearch' : 'search' ),
311 'action' => $wgScript
316 Xml
::openElement( 'table', array( 'id' => 'mw-search-top-table', 'cellpadding' => 0, 'cellspacing' => 0 ) ) .
317 Xml
::openElement( 'tr' ) .
318 Xml
::openElement( 'td' ) . "\n" .
319 $this->shortDialog( $term ) .
320 Xml
::closeElement( 'td' ) .
321 Xml
::closeElement( 'tr' ) .
322 Xml
::closeElement( 'table' )
325 // Sometimes the search engine knows there are too many hits
326 if( $titleMatches instanceof SearchResultTooMany
) {
327 $out->wrapWikiMsg( "==$1==\n", 'toomanymatches' );
328 wfProfileOut( __METHOD__
);
332 $filePrefix = $wgContLang->getFormattedNsText( NS_FILE
) . ':';
333 if( trim( $term ) === '' ||
$filePrefix === trim( $term ) ) {
334 $out->addHTML( $this->formHeader( $term, 0, 0 ) );
335 $out->addHtml( $this->getProfileForm( $this->profile
, $term ) );
336 $out->addHTML( '</form>' );
337 // Empty query -- straight view of search form
338 wfProfileOut( __METHOD__
);
342 // Get number of results
343 $titleMatchesNum = $titleMatches ?
$titleMatches->numRows() : 0;
344 $textMatchesNum = $textMatches ?
$textMatches->numRows() : 0;
345 // Total initial query matches (possible false positives)
346 $num = $titleMatchesNum +
$textMatchesNum;
348 // Get total actual results (after second filtering, if any)
349 $numTitleMatches = $titleMatches && !is_null( $titleMatches->getTotalHits() ) ?
350 $titleMatches->getTotalHits() : $titleMatchesNum;
351 $numTextMatches = $textMatches && !is_null( $textMatches->getTotalHits() ) ?
352 $textMatches->getTotalHits() : $textMatchesNum;
354 // get total number of results if backend can calculate it
356 if( $titleMatches && !is_null( $titleMatches->getTotalHits() ) )
357 $totalRes +
= $titleMatches->getTotalHits();
358 if( $textMatches && !is_null( $textMatches->getTotalHits() ) )
359 $totalRes +
= $textMatches->getTotalHits();
361 // show number of results and current offset
362 $out->addHTML( $this->formHeader( $term, $num, $totalRes ) );
363 $out->addHtml( $this->getProfileForm( $this->profile
, $term ) );
365 $out->addHtml( Xml
::closeElement( 'form' ) );
366 $out->addHtml( "<div class='searchresults'>" );
369 if( $num ||
$this->offset
) {
370 // Show the create link ahead
371 $this->showCreateLink( $t );
372 $prevnext = $this->getLanguage()->viewPrevNext( $this->getTitle(), $this->offset
, $this->limit
,
373 $this->powerSearchOptions() +
array( 'search' => $term ),
374 max( $titleMatchesNum, $textMatchesNum ) < $this->limit
376 //$out->addHTML( "<p class='mw-search-pager-top'>{$prevnext}</p>\n" );
377 wfRunHooks( 'SpecialSearchResults', array( $term, &$titleMatches, &$textMatches ) );
379 wfRunHooks( 'SpecialSearchNoResults', array( $term ) );
382 $out->parserOptions()->setEditSection( false );
383 if( $titleMatches ) {
384 if( $numTitleMatches > 0 ) {
385 $out->wrapWikiMsg( "==$1==\n", 'titlematches' );
386 $out->addHTML( $this->showMatches( $titleMatches ) );
388 $titleMatches->free();
390 if( $textMatches && !$textStatus ) {
391 // output appropriate heading
392 if( $numTextMatches > 0 && $numTitleMatches > 0 ) {
393 // if no title matches the heading is redundant
394 $out->wrapWikiMsg( "==$1==\n", 'textmatches' );
395 } elseif( $totalRes == 0 ) {
396 # Don't show the 'no text matches' if we received title matches
397 # $out->wrapWikiMsg( "==$1==\n", 'notextmatches' );
399 // show interwiki results if any
400 if( $textMatches->hasInterwikiResults() ) {
401 $out->addHTML( $this->showInterwiki( $textMatches->getInterwikiResults(), $term ) );
404 if( $numTextMatches > 0 ) {
405 $out->addHTML( $this->showMatches( $textMatches ) );
408 $textMatches->free();
412 $out->addHTML( '<div class="error">' .
413 htmlspecialchars( $textStatus->getWikiText( 'search-error' ) ) . '</div>' );
415 $out->wrapWikiMsg( "<p class=\"mw-search-nonefound\">\n$1</p>",
416 array( 'search-nonefound', wfEscapeWikiText( $term ) ) );
417 $this->showCreateLink( $t );
420 $out->addHtml( "</div>" );
422 if( $num ||
$this->offset
) {
423 $out->addHTML( "<p class='mw-search-pager-bottom'>{$prevnext}</p>\n" );
425 wfRunHooks( 'SpecialSearchResultsAppend', array( $this, $out, $term ) );
426 wfProfileOut( __METHOD__
);
432 protected function showCreateLink( $t ) {
433 // show direct page/create link if applicable
435 // Check DBkey !== '' in case of fragment link only.
436 if( is_null( $t ) ||
$t->getDBkey() === '' ) {
438 // preserve the paragraph for margins etc...
439 $this->getOutput()->addHtml( '<p></p>' );
443 if( $t->isKnown() ) {
444 $messageName = 'searchmenu-exists';
445 } elseif( $t->userCan( 'create', $this->getUser() ) ) {
446 $messageName = 'searchmenu-new';
448 $messageName = 'searchmenu-new-nocreate';
450 $params = array( $messageName, wfEscapeWikiText( $t->getPrefixedText() ) );
451 wfRunHooks( 'SpecialSearchCreateLink', array( $t, &$params ) );
453 // Extensions using the hook might still return an empty $messageName
455 $this->getOutput()->wrapWikiMsg( "<p class=\"mw-search-createlink\">\n$1</p>", $params );
457 // preserve the paragraph for margins etc...
458 $this->getOutput()->addHtml( '<p></p>' );
463 * @param $term string
465 protected function setupPage( $term ) {
466 # Should advanced UI be used?
467 $this->searchAdvanced
= ($this->profile
=== 'advanced');
468 $out = $this->getOutput();
469 if( strval( $term ) !== '' ) {
470 $out->setPageTitle( $this->msg( 'searchresults' ) );
471 $out->setHTMLTitle( $this->msg( 'pagetitle' )->rawParams(
472 $this->msg( 'searchresults-title' )->rawParams( $term )->text()
475 // add javascript specific to special:search
476 $out->addModules( 'mediawiki.special.search' );
480 * Extract "power search" namespace settings from the request object,
481 * returning a list of index numbers to search.
483 * @param $request WebRequest
486 protected function powerSearch( &$request ) {
488 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
489 if( $request->getCheck( 'ns' . $ns ) ) {
498 * Reconstruct the 'power search' options for links
502 protected function powerSearchOptions() {
504 $opt['redirs'] = $this->searchRedirects ?
1 : 0;
505 if( $this->profile
!== 'advanced' ) {
506 $opt['profile'] = $this->profile
;
508 foreach( $this->namespaces
as $n ) {
512 return $opt +
$this->extraParams
;
516 * Show whole set of results
518 * @param $matches SearchResultSet
522 protected function showMatches( &$matches ) {
524 wfProfileIn( __METHOD__
);
526 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
529 $infoLine = $matches->getInfo();
530 if( !is_null( $infoLine ) ) {
531 $out .= "\n<!-- {$infoLine} -->\n";
533 $out .= "<ul class='mw-search-results'>\n";
534 $result = $matches->next();
536 $out .= $this->showHit( $result, $terms );
537 $result = $matches->next();
541 // convert the whole thing to desired language variant
542 $out = $wgContLang->convert( $out );
543 wfProfileOut( __METHOD__
);
548 * Format a single hit result
550 * @param $result SearchResult
551 * @param array $terms terms to highlight
555 protected function showHit( $result, $terms ) {
556 wfProfileIn( __METHOD__
);
558 if( $result->isBrokenTitle() ) {
559 wfProfileOut( __METHOD__
);
560 return "<!-- Broken link in search result -->\n";
563 $t = $result->getTitle();
565 $titleSnippet = $result->getTitleSnippet( $terms );
567 if( $titleSnippet == '' )
568 $titleSnippet = null;
572 wfRunHooks( 'ShowSearchHitTitle',
573 array( &$link_t, &$titleSnippet, $result, $terms, $this ) );
575 $link = Linker
::linkKnown(
580 //If page content is not readable, just return the title.
581 //This is not quite safe, but better than showing excerpts from non-readable pages
582 //Note that hiding the entry entirely would screw up paging.
583 if( !$t->userCan( 'read', $this->getUser() ) ) {
584 wfProfileOut( __METHOD__
);
585 return "<li>{$link}</li>\n";
588 // If the page doesn't *exist*... our search index is out of date.
589 // The least confusing at this point is to drop the result.
590 // You may get less results, but... oh well. :P
591 if( $result->isMissingRevision() ) {
592 wfProfileOut( __METHOD__
);
593 return "<!-- missing page " . htmlspecialchars( $t->getPrefixedText() ) . "-->\n";
596 // format redirects / relevant sections
597 $redirectTitle = $result->getRedirectTitle();
598 $redirectText = $result->getRedirectSnippet( $terms );
599 $sectionTitle = $result->getSectionTitle();
600 $sectionText = $result->getSectionSnippet( $terms );
603 if( !is_null( $redirectTitle ) ) {
604 if( $redirectText == '' )
605 $redirectText = null;
607 $redirect = "<span class='searchalttitle'>" .
608 $this->msg( 'search-redirect' )->rawParams(
609 Linker
::linkKnown( $redirectTitle, $redirectText ) )->text() .
615 if( !is_null( $sectionTitle ) ) {
616 if( $sectionText == '' )
619 $section = "<span class='searchalttitle'>" .
620 $this->msg( 'search-section' )->rawParams(
621 Linker
::linkKnown( $sectionTitle, $sectionText ) )->text() .
625 // format text extract
626 $extract = "<div class='searchresult'>" . $result->getTextSnippet( $terms ) . "</div>";
628 $lang = $this->getLanguage();
631 if( is_null( $result->getScore() ) ) {
632 // Search engine doesn't report scoring info
635 $percent = sprintf( '%2.1f', $result->getScore() * 100 );
636 $score = $this->msg( 'search-result-score' )->numParams( $percent )->text()
640 // format description
641 $byteSize = $result->getByteSize();
642 $wordCount = $result->getWordCount();
643 $timestamp = $result->getTimestamp();
644 $size = $this->msg( 'search-result-size', $lang->formatSize( $byteSize ) )
645 ->numParams( $wordCount )->escaped();
647 if( $t->getNamespace() == NS_CATEGORY
) {
648 $cat = Category
::newFromTitle( $t );
649 $size = $this->msg( 'search-result-category-size' )
650 ->numParams( $cat->getPageCount(), $cat->getSubcatCount(), $cat->getFileCount() )
654 $date = $lang->userTimeAndDate( $timestamp, $this->getUser() );
656 // link to related articles if supported
658 if( $result->hasRelated() ) {
659 $st = SpecialPage
::getTitleFor( 'Search' );
660 $stParams = array_merge(
661 $this->powerSearchOptions(),
663 'search' => $this->msg( 'searchrelated' )->inContentLanguage()->text() .
664 ':' . $t->getPrefixedText(),
665 'fulltext' => $this->msg( 'search' )->text()
669 $related = ' -- ' . Linker
::linkKnown(
671 $this->msg( 'search-relatedarticle' )->text(),
677 // Include a thumbnail for media files...
678 if( $t->getNamespace() == NS_FILE
) {
679 $img = wfFindFile( $t );
681 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
683 $desc = $this->msg( 'parentheses' )->rawParams( $img->getShortDesc() )->escaped();
684 wfProfileOut( __METHOD__
);
685 // Float doesn't seem to interact well with the bullets.
686 // Table messes up vertical alignment of the bullets.
687 // Bullets are therefore disabled (didn't look great anyway).
689 '<table class="searchResultImage">' .
691 '<td style="width: 120px; text-align: center; vertical-align: top;">' .
692 $thumb->toHtml( array( 'desc-link' => true ) ) .
694 '<td style="vertical-align: top;">' .
697 "<div class='mw-search-result-data'>{$score}{$desc} - {$date}{$related}</div>" .
708 if ( wfRunHooks( 'ShowSearchHit', array (
709 $this, $result, $terms,
710 &$link, &$redirect, &$section, &$extract,
711 &$score, &$size, &$date, &$related,
714 $html = "<li><div class='mw-search-result-heading'>{$link} {$redirect} {$section}</div> {$extract}\n" .
715 "<div class='mw-search-result-data'>{$score}{$size} - {$date}{$related}</div>" .
719 wfProfileOut( __METHOD__
);
724 * Show results from other wikis
726 * @param $matches SearchResultSet
727 * @param $query String
731 protected function showInterwiki( &$matches, $query ) {
733 wfProfileIn( __METHOD__
);
734 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
736 $out = "<div id='mw-search-interwiki'><div id='mw-search-interwiki-caption'>".
737 $this->msg( 'search-interwiki-caption' )->text() . "</div>\n";
738 $out .= "<ul class='mw-search-iwresults'>\n";
740 // work out custom project captions
741 $customCaptions = array();
742 $customLines = explode( "\n", $this->msg( 'search-interwiki-custom' )->text() ); // format per line <iwprefix>:<caption>
743 foreach( $customLines as $line ) {
744 $parts = explode( ":", $line, 2 );
745 if( count( $parts ) == 2 ) { // validate line
746 $customCaptions[$parts[0]] = $parts[1];
751 $result = $matches->next();
753 $out .= $this->showInterwikiHit( $result, $prev, $terms, $query, $customCaptions );
754 $prev = $result->getInterwikiPrefix();
755 $result = $matches->next();
757 // TODO: should support paging in a non-confusing way (not sure how though, maybe via ajax)..
758 $out .= "</ul></div>\n";
760 // convert the whole thing to desired language variant
761 $out = $wgContLang->convert( $out );
762 wfProfileOut( __METHOD__
);
767 * Show single interwiki link
769 * @param $result SearchResult
770 * @param $lastInterwiki String
771 * @param $terms Array
772 * @param $query String
773 * @param array $customCaptions iw prefix -> caption
777 protected function showInterwikiHit( $result, $lastInterwiki, $terms, $query, $customCaptions) {
778 wfProfileIn( __METHOD__
);
780 if( $result->isBrokenTitle() ) {
781 wfProfileOut( __METHOD__
);
782 return "<!-- Broken link in search result -->\n";
785 $t = $result->getTitle();
787 $titleSnippet = $result->getTitleSnippet( $terms );
789 if( $titleSnippet == '' )
790 $titleSnippet = null;
792 $link = Linker
::linkKnown(
797 // format redirect if any
798 $redirectTitle = $result->getRedirectTitle();
799 $redirectText = $result->getRedirectSnippet( $terms );
801 if( !is_null( $redirectTitle ) ) {
802 if( $redirectText == '' )
803 $redirectText = null;
805 $redirect = "<span class='searchalttitle'>" .
806 $this->msg( 'search-redirect' )->rawParams(
807 Linker
::linkKnown( $redirectTitle, $redirectText ) )->text() .
812 // display project name
813 if( is_null( $lastInterwiki ) ||
$lastInterwiki != $t->getInterwiki() ) {
814 if( array_key_exists( $t->getInterwiki(), $customCaptions ) ) {
815 // captions from 'search-interwiki-custom'
816 $caption = $customCaptions[$t->getInterwiki()];
818 // default is to show the hostname of the other wiki which might suck
819 // if there are many wikis on one hostname
820 $parsed = wfParseUrl( $t->getFullURL() );
821 $caption = $this->msg( 'search-interwiki-default', $parsed['host'] )->text();
823 // "more results" link (special page stuff could be localized, but we might not know target lang)
824 $searchTitle = Title
::newFromText( $t->getInterwiki() . ":Special:Search" );
825 $searchLink = Linker
::linkKnown(
827 $this->msg( 'search-interwiki-more' )->text(),
831 'fulltext' => 'Search'
834 $out .= "</ul><div class='mw-search-interwiki-project'><span class='mw-search-interwiki-more'>
835 {$searchLink}</span>{$caption}</div>\n<ul>";
838 $out .= "<li>{$link} {$redirect}</li>\n";
839 wfProfileOut( __METHOD__
);
848 protected function getProfileForm( $profile, $term ) {
851 $opts['redirs'] = $this->searchRedirects
;
852 $opts['profile'] = $this->profile
;
854 if ( $profile === 'advanced' ) {
855 return $this->powerSearchBox( $term, $opts );
858 wfRunHooks( 'SpecialSearchProfileForm', array( $this, &$form, $profile, $term, $opts ) );
864 * Generates the power search box at [[Special:Search]]
866 * @param string $term search term
868 * @return String: HTML form
870 protected function powerSearchBox( $term, $opts ) {
871 // Groups namespaces into rows according to subject
873 foreach( SearchEngine
::searchableNamespaces() as $namespace => $name ) {
874 $subject = MWNamespace
::getSubject( $namespace );
875 if( !array_key_exists( $subject, $rows ) ) {
876 $rows[$subject] = "";
878 $name = str_replace( '_', ' ', $name );
880 $name = $this->msg( 'blanknamespace' )->text();
884 'td', array( 'style' => 'white-space: nowrap' )
889 "mw-search-ns{$namespace}",
890 in_array( $namespace, $this->namespaces
)
892 Xml
::closeElement( 'td' );
894 $rows = array_values( $rows );
895 $numRows = count( $rows );
897 // Lays out namespaces in multiple floating two-column tables so they'll
898 // be arranged nicely while still accommodating different screen widths
899 $namespaceTables = '';
900 for( $i = 0; $i < $numRows; $i +
= 4 ) {
901 $namespaceTables .= Xml
::openElement(
903 array( 'cellpadding' => 0, 'cellspacing' => 0 )
905 for( $j = $i; $j < $i +
4 && $j < $numRows; $j++
) {
906 $namespaceTables .= Xml
::tags( 'tr', null, $rows[$j] );
908 $namespaceTables .= Xml
::closeElement( 'table' );
911 $showSections = array( 'namespaceTables' => $namespaceTables );
913 // Show redirects check only if backend supports it
914 if( $this->getSearchEngine()->supports( 'list-redirects' ) ) {
915 $showSections['redirects'] =
916 Xml
::checkLabel( $this->msg( 'powersearch-redir' )->text(), 'redirs', 'redirs', $this->searchRedirects
);
919 wfRunHooks( 'SpecialSearchPowerBox', array( &$showSections, $term, $opts ) );
922 unset( $opts['redirs'] );
923 foreach( $opts as $key => $value ) {
924 $hidden .= Html
::hidden( $key, $value );
926 // Return final output
927 return Xml
::openElement(
929 array( 'id' => 'mw-searchoptions', 'style' => 'margin:0em;' )
931 Xml
::element( 'legend', null, $this->msg( 'powersearch-legend' )->text() ) .
932 Xml
::tags( 'h4', null, $this->msg( 'powersearch-ns' )->parse() ) .
933 Html
::element( 'div', array( 'id' => 'mw-search-togglebox' ) ) .
934 Xml
::element( 'div', array( 'class' => 'divider' ), '', false ) .
935 implode( Xml
::element( 'div', array( 'class' => 'divider' ), '', false ), $showSections ) .
937 Xml
::closeElement( 'fieldset' );
943 protected function getSearchProfiles() {
944 // Builds list of Search Types (profiles)
945 $nsAllSet = array_keys( SearchEngine
::searchableNamespaces() );
949 'message' => 'searchprofile-articles',
950 'tooltip' => 'searchprofile-articles-tooltip',
951 'namespaces' => SearchEngine
::defaultNamespaces(),
952 'namespace-messages' => SearchEngine
::namespacesAsText(
953 SearchEngine
::defaultNamespaces()
957 'message' => 'searchprofile-images',
958 'tooltip' => 'searchprofile-images-tooltip',
959 'namespaces' => array( NS_FILE
),
962 'message' => 'searchprofile-project',
963 'tooltip' => 'searchprofile-project-tooltip',
964 'namespaces' => SearchEngine
::helpNamespaces(),
965 'namespace-messages' => SearchEngine
::namespacesAsText(
966 SearchEngine
::helpNamespaces()
970 'message' => 'searchprofile-everything',
971 'tooltip' => 'searchprofile-everything-tooltip',
972 'namespaces' => $nsAllSet,
975 'message' => 'searchprofile-advanced',
976 'tooltip' => 'searchprofile-advanced-tooltip',
977 'namespaces' => self
::NAMESPACES_CURRENT
,
981 wfRunHooks( 'SpecialSearchProfiles', array( &$profiles ) );
983 foreach( $profiles as &$data ) {
984 if ( !is_array( $data['namespaces'] ) ) continue;
985 sort( $data['namespaces'] );
993 * @param $resultsShown
997 protected function formHeader( $term, $resultsShown, $totalNum ) {
998 $out = Xml
::openElement( 'div', array( 'class' => 'mw-search-formheader' ) );
1001 if( $this->startsWithImage( $term ) ) {
1003 $bareterm = substr( $term, strpos( $term, ':' ) +
1 );
1006 $profiles = $this->getSearchProfiles();
1007 $lang = $this->getLanguage();
1009 // Outputs XML for Search Types
1010 $out .= Xml
::openElement( 'div', array( 'class' => 'search-types' ) );
1011 $out .= Xml
::openElement( 'ul' );
1012 foreach ( $profiles as $id => $profile ) {
1013 if ( !isset( $profile['parameters'] ) ) {
1014 $profile['parameters'] = array();
1016 $profile['parameters']['profile'] = $id;
1018 $tooltipParam = isset( $profile['namespace-messages'] ) ?
1019 $lang->commaList( $profile['namespace-messages'] ) : null;
1023 'class' => $this->profile
=== $id ?
'current' : 'normal'
1025 $this->makeSearchLink(
1028 $this->msg( $profile['message'] )->text(),
1029 $this->msg( $profile['tooltip'], $tooltipParam )->text(),
1030 $profile['parameters']
1034 $out .= Xml
::closeElement( 'ul' );
1035 $out .= Xml
::closeElement( 'div' );
1038 if ( $resultsShown > 0 ) {
1039 if ( $totalNum > 0 ) {
1040 $top = $this->msg( 'showingresultsheader' )
1041 ->numParams( $this->offset +
1, $this->offset +
$resultsShown, $totalNum )
1042 ->params( wfEscapeWikiText( $term ) )
1043 ->numParams( $resultsShown )
1045 } elseif ( $resultsShown >= $this->limit
) {
1046 $top = $this->msg( 'showingresults' )
1047 ->numParams( $this->limit
, $this->offset +
1 )
1050 $top = $this->msg( 'showingresultsnum' )
1051 ->numParams( $this->limit
, $this->offset +
1, $resultsShown )
1054 $out .= Xml
::tags( 'div', array( 'class' => 'results-info' ),
1055 Xml
::tags( 'ul', null, Xml
::tags( 'li', null, $top ) )
1059 $out .= Xml
::element( 'div', array( 'style' => 'clear:both' ), '', false );
1060 $out .= Xml
::closeElement( 'div' );
1066 * @param $term string
1069 protected function shortDialog( $term ) {
1070 $out = Html
::hidden( 'title', $this->getTitle()->getPrefixedText() );
1071 $out .= Html
::hidden( 'profile', $this->profile
) . "\n";
1073 $out .= Html
::input( 'search', $term, 'search', array(
1074 'id' => $this->profile
=== 'advanced' ?
'powerSearchText' : 'searchText',
1078 $out .= Html
::hidden( 'fulltext', 'Search' ) . "\n";
1079 $out .= Xml
::submitButton( $this->msg( 'searchbutton' )->text() ) . "\n";
1080 return $out . $this->didYouMeanHtml
;
1084 * Make a search link with some target namespaces
1086 * @param $term String
1087 * @param array $namespaces ignored
1088 * @param string $label link's text
1089 * @param string $tooltip link's tooltip
1090 * @param array $params query string parameters
1091 * @return String: HTML fragment
1093 protected function makeSearchLink( $term, $namespaces, $label, $tooltip, $params = array() ) {
1095 foreach( $namespaces as $n ) {
1096 $opt['ns' . $n] = 1;
1098 $opt['redirs'] = $this->searchRedirects
;
1100 $stParams = array_merge(
1103 'fulltext' => $this->msg( 'search' )->text()
1108 return Xml
::element(
1111 'href' => $this->getTitle()->getLocalURL( $stParams ),
1119 * Check if query starts with image: prefix
1121 * @param string $term the string to check
1124 protected function startsWithImage( $term ) {
1127 $p = explode( ':', $term );
1128 if( count( $p ) > 1 ) {
1129 return $wgContLang->getNsIndex( $p[0] ) == NS_FILE
;
1135 * Check if query starts with all: prefix
1137 * @param string $term the string to check
1140 protected function startsWithAll( $term ) {
1142 $allkeyword = $this->msg( 'searchall' )->inContentLanguage()->text();
1144 $p = explode( ':', $term );
1145 if( count( $p ) > 1 ) {
1146 return $p[0] == $allkeyword;
1154 * @return SearchEngine
1156 public function getSearchEngine() {
1157 if ( $this->searchEngine
=== null ) {
1158 $this->searchEngine
= SearchEngine
::create();
1160 return $this->searchEngine
;
1164 * Users of hook SpecialSearchSetupEngine can use this to
1165 * add more params to links to not lose selection when
1166 * user navigates search results.
1172 public function setExtraParam( $key, $value ) {
1173 $this->extraParams
[$key] = $value;
1176 protected function getGroupName() {