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
{
31 /// Current search profile
35 protected $searchEngine;
38 protected $extraParams = array();
40 /// No idea, apparently used by some other classes
46 protected $limit, $offset;
51 protected $namespaces;
56 protected $searchRedirects;
61 protected $didYouMeanHtml, $fulltext;
63 const NAMESPACES_CURRENT
= 'sense';
65 public function __construct() {
66 parent
::__construct( 'Search' );
72 * @param $par String or null
74 public function execute( $par ) {
76 $this->outputHeader();
77 $out = $this->getOutput();
78 $out->allowClickjacking();
79 $out->addModuleStyles( 'mediawiki.special' );
81 // Strip underscores from title parameter; most of the time we'll want
82 // text form here. But don't strip underscores from actual text params!
83 $titleParam = str_replace( '_', ' ', $par );
85 $request = $this->getRequest();
87 // Fetch the search term
88 $search = str_replace( "\n", " ", $request->getText( 'search', $titleParam ) );
92 if ( $request->getVal( 'fulltext' )
93 ||
!is_null( $request->getVal( 'offset' ) )
94 ||
!is_null( $request->getVal( 'searchx' ) ) )
96 $this->showResults( $search );
98 $this->goResult( $search );
103 * Set up basic search parameters from the request and user settings.
105 public function load() {
106 $request = $this->getRequest();
107 list( $this->limit
, $this->offset
) = $request->getLimitOffset( 20, 'searchlimit' );
108 $this->mPrefix
= $request->getVal( 'prefix', '' );
110 $user = $this->getUser();
111 # Extract manually requested namespaces
112 $nslist = $this->powerSearch( $request );
113 $this->profile
= $profile = $request->getVal( 'profile', null );
114 $profiles = $this->getSearchProfiles();
115 if ( $profile === null) {
116 // BC with old request format
117 $this->profile
= 'advanced';
118 if ( count( $nslist ) ) {
119 foreach( $profiles as $key => $data ) {
120 if ( $nslist === $data['namespaces'] && $key !== 'advanced') {
121 $this->profile
= $key;
124 $this->namespaces
= $nslist;
126 $this->namespaces
= SearchEngine
::userNamespaces( $user );
128 } elseif ( $profile === 'advanced' ) {
129 $this->namespaces
= $nslist;
131 if ( isset( $profiles[$profile]['namespaces'] ) ) {
132 $this->namespaces
= $profiles[$profile]['namespaces'];
134 // Unknown profile requested
135 $this->profile
= 'default';
136 $this->namespaces
= $profiles['default']['namespaces'];
140 // Redirects defaults to true, but we don't know whether it was ticked of or just missing
141 $default = $request->getBool( 'profile' ) ?
0 : 1;
142 $this->searchRedirects
= $request->getBool( 'redirs', $default ) ?
1 : 0;
143 $this->didYouMeanHtml
= ''; # html of did you mean... link
144 $this->fulltext
= $request->getVal('fulltext');
148 * If an exact title match can be found, jump straight ahead to it.
150 * @param $term String
152 public function goResult( $term ) {
153 $this->setupPage( $term );
154 # Try to go to page as entered.
155 $t = Title
::newFromText( $term );
156 # If the string cannot be used to create a title
157 if( is_null( $t ) ) {
158 return $this->showResults( $term );
160 # If there's an exact or very near match, jump right there.
161 $t = SearchEngine
::getNearMatch( $term );
163 if ( !wfRunHooks( 'SpecialSearchGo', array( &$t, &$term ) ) ) {
164 # Hook requested termination
168 if( !is_null( $t ) ) {
169 $this->getOutput()->redirect( $t->getFullURL() );
172 # No match, generate an edit URL
173 $t = Title
::newFromText( $term );
174 if( !is_null( $t ) ) {
176 wfRunHooks( 'SpecialSearchNogomatch', array( &$t ) );
177 wfDebugLog( 'nogomatch', $t->getText(), false );
179 # If the feature is enabled, go straight to the edit page
181 $this->getOutput()->redirect( $t->getFullURL( array( 'action' => 'edit' ) ) );
185 return $this->showResults( $term );
189 * @param $term String
191 public function showResults( $term ) {
192 global $wgDisableTextSearch, $wgSearchForwardUrl, $wgContLang, $wgScript;
193 wfProfileIn( __METHOD__
);
195 $search = $this->getSearchEngine();
196 $search->setLimitOffset( $this->limit
, $this->offset
);
197 $search->setNamespaces( $this->namespaces
);
198 $search->showRedirects
= $this->searchRedirects
; // BC
199 $search->setFeatureData( 'list-redirects', $this->searchRedirects
);
200 $search->prefix
= $this->mPrefix
;
201 $term = $search->transformSearchTerm($term);
203 wfRunHooks( 'SpecialSearchSetupEngine', array( $this, $this->profile
, $search ) );
205 $this->setupPage( $term );
207 $out = $this->getOutput();
209 if ( $wgDisableTextSearch ) {
210 if ( $wgSearchForwardUrl ) {
211 $url = str_replace( '$1', urlencode( $term ), $wgSearchForwardUrl );
212 $out->redirect( $url );
215 Xml
::openElement( 'fieldset' ) .
216 Xml
::element( 'legend', null, wfMsg( 'search-external' ) ) .
217 Xml
::element( 'p', array( 'class' => 'mw-searchdisabled' ), wfMsg( 'searchdisabled' ) ) .
218 wfMsg( 'googlesearch',
219 htmlspecialchars( $term ),
220 htmlspecialchars( 'UTF-8' ),
221 htmlspecialchars( wfMsg( 'searchbutton' ) )
223 Xml
::closeElement( 'fieldset' )
226 wfProfileOut( __METHOD__
);
230 $t = Title
::newFromText( $term );
232 // fetch search results
233 $rewritten = $search->replacePrefixes($term);
235 $titleMatches = $search->searchTitle( $rewritten );
236 if( !( $titleMatches instanceof SearchResultTooMany
) ) {
237 $textMatches = $search->searchText( $rewritten );
240 // did you mean... suggestions
241 if( $textMatches && $textMatches->hasSuggestion() ) {
242 $st = SpecialPage
::getTitleFor( 'Search' );
244 # mirror Go/Search behaviour of original request ..
245 $didYouMeanParams = array( 'search' => $textMatches->getSuggestionQuery() );
247 if( $this->fulltext
!= null ) {
248 $didYouMeanParams['fulltext'] = $this->fulltext
;
251 $stParams = array_merge(
253 $this->powerSearchOptions()
256 $suggestionSnippet = $textMatches->getSuggestionSnippet();
258 if( $suggestionSnippet == '' ) {
259 $suggestionSnippet = null;
262 $suggestLink = Linker
::linkKnown(
269 $this->didYouMeanHtml
= '<div class="searchdidyoumean">'.wfMsg('search-suggest',$suggestLink).'</div>';
271 // start rendering the page
276 'id' => ( $this->profile
=== 'advanced' ?
'powersearch' : 'search' ),
278 'action' => $wgScript
283 Xml
::openElement( 'table', array( 'id'=>'mw-search-top-table', 'border'=>0, 'cellpadding'=>0, 'cellspacing'=>0 ) ) .
284 Xml
::openElement( 'tr' ) .
285 Xml
::openElement( 'td' ) . "\n" .
286 $this->shortDialog( $term ) .
287 Xml
::closeElement('td') .
288 Xml
::closeElement('tr') .
289 Xml
::closeElement('table')
292 // Sometimes the search engine knows there are too many hits
293 if( $titleMatches instanceof SearchResultTooMany
) {
294 $out->wrapWikiMsg( "==$1==\n", 'toomanymatches' );
295 wfProfileOut( __METHOD__
);
299 $filePrefix = $wgContLang->getFormattedNsText(NS_FILE
).':';
300 if( trim( $term ) === '' ||
$filePrefix === trim( $term ) ) {
301 $out->addHTML( $this->formHeader( $term, 0, 0 ) );
302 $out->addHtml( $this->getProfileForm( $this->profile
, $term ) );
303 $out->addHTML( '</form>' );
304 // Empty query -- straight view of search form
305 wfProfileOut( __METHOD__
);
309 // Get number of results
310 $titleMatchesNum = $titleMatches ?
$titleMatches->numRows() : 0;
311 $textMatchesNum = $textMatches ?
$textMatches->numRows() : 0;
312 // Total initial query matches (possible false positives)
313 $num = $titleMatchesNum +
$textMatchesNum;
315 // Get total actual results (after second filtering, if any)
316 $numTitleMatches = $titleMatches && !is_null( $titleMatches->getTotalHits() ) ?
317 $titleMatches->getTotalHits() : $titleMatchesNum;
318 $numTextMatches = $textMatches && !is_null( $textMatches->getTotalHits() ) ?
319 $textMatches->getTotalHits() : $textMatchesNum;
321 // get total number of results if backend can calculate it
323 if($titleMatches && !is_null( $titleMatches->getTotalHits() ) )
324 $totalRes +
= $titleMatches->getTotalHits();
325 if($textMatches && !is_null( $textMatches->getTotalHits() ))
326 $totalRes +
= $textMatches->getTotalHits();
328 // show number of results and current offset
329 $out->addHTML( $this->formHeader( $term, $num, $totalRes ) );
330 $out->addHtml( $this->getProfileForm( $this->profile
, $term ) );
333 $out->addHtml( Xml
::closeElement( 'form' ) );
334 $out->addHtml( "<div class='searchresults'>" );
337 if( $num ||
$this->offset
) {
338 // Show the create link ahead
339 $this->showCreateLink( $t );
340 $prevnext = wfViewPrevNext( $this->offset
, $this->limit
,
341 SpecialPage
::getTitleFor( 'Search' ),
342 wfArrayToCGI( $this->powerSearchOptions(), array( 'search' => $term ) ),
343 max( $titleMatchesNum, $textMatchesNum ) < $this->limit
345 //$out->addHTML( "<p class='mw-search-pager-top'>{$prevnext}</p>\n" );
346 wfRunHooks( 'SpecialSearchResults', array( $term, &$titleMatches, &$textMatches ) );
348 wfRunHooks( 'SpecialSearchNoResults', array( $term ) );
351 $out->parserOptions()->setEditSection( false );
352 if( $titleMatches ) {
353 if( $numTitleMatches > 0 ) {
354 $out->wrapWikiMsg( "==$1==\n", 'titlematches' );
355 $out->addHTML( $this->showMatches( $titleMatches ) );
357 $titleMatches->free();
360 // output appropriate heading
361 if( $numTextMatches > 0 && $numTitleMatches > 0 ) {
362 // if no title matches the heading is redundant
363 $out->wrapWikiMsg( "==$1==\n", 'textmatches' );
364 } elseif( $totalRes == 0 ) {
365 # Don't show the 'no text matches' if we received title matches
366 # $out->wrapWikiMsg( "==$1==\n", 'notextmatches' );
368 // show interwiki results if any
369 if( $textMatches->hasInterwikiResults() ) {
370 $out->addHTML( $this->showInterwiki( $textMatches->getInterwikiResults(), $term ) );
373 if( $numTextMatches > 0 ) {
374 $out->addHTML( $this->showMatches( $textMatches ) );
377 $textMatches->free();
380 $out->wrapWikiMsg( "<p class=\"mw-search-nonefound\">\n$1</p>", array( 'search-nonefound', wfEscapeWikiText( $term ) ) );
381 $this->showCreateLink( $t );
383 $out->addHtml( "</div>" );
385 if( $num ||
$this->offset
) {
386 $out->addHTML( "<p class='mw-search-pager-bottom'>{$prevnext}</p>\n" );
388 wfProfileOut( __METHOD__
);
394 protected function showCreateLink( $t ) {
395 // show direct page/create link if applicable
396 // Check DBkey !== '' in case of fragment link only.
398 if( !is_null($t) && $t->getDBkey() !== '' ) {
399 if( $t->isKnown() ) {
400 $messageName = 'searchmenu-exists';
401 } elseif( $t->userCan( 'create' ) ) {
402 $messageName = 'searchmenu-new';
404 $messageName = 'searchmenu-new-nocreate';
408 $this->getOutput()->wrapWikiMsg( "<p class=\"mw-search-createlink\">\n$1</p>", array( $messageName, wfEscapeWikiText( $t->getPrefixedText() ) ) );
410 // preserve the paragraph for margins etc...
411 $this->getOutput()->addHtml( '<p></p>' );
416 * @param $term string
418 protected function setupPage( $term ) {
419 # Should advanced UI be used?
420 $this->searchAdvanced
= ($this->profile
=== 'advanced');
421 $out = $this->getOutput();
422 if( strval( $term ) !== '' ) {
423 $out->setPageTitle( wfMsg( 'searchresults') );
424 $out->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'searchresults-title', $term ) ) );
426 // add javascript specific to special:search
427 $out->addModules( 'mediawiki.special.search' );
431 * Extract "power search" namespace settings from the request object,
432 * returning a list of index numbers to search.
434 * @param $request WebRequest
437 protected function powerSearch( &$request ) {
439 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
440 if( $request->getCheck( 'ns' . $ns ) ) {
449 * Reconstruct the 'power search' options for links
453 protected function powerSearchOptions() {
455 $opt['redirs'] = $this->searchRedirects ?
1 : 0;
456 if( $this->profile
!== 'advanced' ) {
457 $opt['profile'] = $this->profile
;
459 foreach( $this->namespaces
as $n ) {
463 return $opt +
$this->extraParams
;
467 * Show whole set of results
469 * @param $matches SearchResultSet
473 protected function showMatches( &$matches ) {
475 wfProfileIn( __METHOD__
);
477 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
480 $infoLine = $matches->getInfo();
481 if( !is_null($infoLine) ) {
482 $out .= "\n<!-- {$infoLine} -->\n";
484 $out .= "<ul class='mw-search-results'>\n";
485 while( $result = $matches->next() ) {
486 $out .= $this->showHit( $result, $terms );
490 // convert the whole thing to desired language variant
491 $out = $wgContLang->convert( $out );
492 wfProfileOut( __METHOD__
);
497 * Format a single hit result
499 * @param $result SearchResult
500 * @param $terms Array: terms to highlight
504 protected function showHit( $result, $terms ) {
505 wfProfileIn( __METHOD__
);
507 if( $result->isBrokenTitle() ) {
508 wfProfileOut( __METHOD__
);
509 return "<!-- Broken link in search result -->\n";
512 $t = $result->getTitle();
514 $titleSnippet = $result->getTitleSnippet($terms);
516 if( $titleSnippet == '' )
517 $titleSnippet = null;
521 wfRunHooks( 'ShowSearchHitTitle',
522 array( &$link_t, &$titleSnippet, $result, $terms, $this ) );
524 $link = Linker
::linkKnown(
529 //If page content is not readable, just return the title.
530 //This is not quite safe, but better than showing excerpts from non-readable pages
531 //Note that hiding the entry entirely would screw up paging.
532 if( !$t->userCanRead() ) {
533 wfProfileOut( __METHOD__
);
534 return "<li>{$link}</li>\n";
537 // If the page doesn't *exist*... our search index is out of date.
538 // The least confusing at this point is to drop the result.
539 // You may get less results, but... oh well. :P
540 if( $result->isMissingRevision() ) {
541 wfProfileOut( __METHOD__
);
542 return "<!-- missing page " . htmlspecialchars( $t->getPrefixedText() ) . "-->\n";
545 // format redirects / relevant sections
546 $redirectTitle = $result->getRedirectTitle();
547 $redirectText = $result->getRedirectSnippet($terms);
548 $sectionTitle = $result->getSectionTitle();
549 $sectionText = $result->getSectionSnippet($terms);
552 if( !is_null($redirectTitle) ) {
553 if( $redirectText == '' )
554 $redirectText = null;
556 $redirect = "<span class='searchalttitle'>" .
569 if( !is_null($sectionTitle) ) {
570 if( $sectionText == '' )
573 $section = "<span class='searchalttitle'>" .
575 'search-section', Linker
::linkKnown(
583 // format text extract
584 $extract = "<div class='searchresult'>".$result->getTextSnippet($terms)."</div>";
586 $lang = $this->getLang();
589 if( is_null( $result->getScore() ) ) {
590 // Search engine doesn't report scoring info
593 $percent = sprintf( '%2.1f', $result->getScore() * 100 );
594 $score = wfMsg( 'search-result-score', $lang->formatNum( $percent ) )
598 // format description
599 $byteSize = $result->getByteSize();
600 $wordCount = $result->getWordCount();
601 $timestamp = $result->getTimestamp();
603 'search-result-size',
604 array( 'parsemag', 'escape' ),
605 $lang->formatSize( $byteSize ),
606 $lang->formatNum( $wordCount )
609 if( $t->getNamespace() == NS_CATEGORY
) {
610 $cat = Category
::newFromTitle( $t );
612 'search-result-category-size',
613 array( 'parsemag', 'escape' ),
614 $lang->formatNum( $cat->getPageCount() ),
615 $lang->formatNum( $cat->getSubcatCount() ),
616 $lang->formatNum( $cat->getFileCount() )
620 $date = $lang->timeanddate( $timestamp );
622 // link to related articles if supported
624 if( $result->hasRelated() ) {
625 $st = SpecialPage
::getTitleFor( 'Search' );
626 $stParams = array_merge(
627 $this->powerSearchOptions(),
629 'search' => wfMsgForContent( 'searchrelated' ) . ':' . $t->getPrefixedText(),
630 'fulltext' => wfMsg( 'search' )
634 $related = ' -- ' . Linker
::linkKnown(
636 wfMsg('search-relatedarticle'),
642 // Include a thumbnail for media files...
643 if( $t->getNamespace() == NS_FILE
) {
644 $img = wfFindFile( $t );
646 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
648 $desc = wfMsg( 'parentheses', $img->getShortDesc() );
649 wfProfileOut( __METHOD__
);
650 // Float doesn't seem to interact well with the bullets.
651 // Table messes up vertical alignment of the bullets.
652 // Bullets are therefore disabled (didn't look great anyway).
654 '<table class="searchResultImage">' .
656 '<td width="120" align="center" valign="top">' .
657 $thumb->toHtml( array( 'desc-link' => true ) ) .
659 '<td valign="top">' .
662 "<div class='mw-search-result-data'>{$score}{$desc} - {$date}{$related}</div>" .
671 wfProfileOut( __METHOD__
);
672 return "<li><div class='mw-search-result-heading'>{$link} {$redirect} {$section}</div> {$extract}\n" .
673 "<div class='mw-search-result-data'>{$score}{$size} - {$date}{$related}</div>" .
679 * Show results from other wikis
681 * @param $matches SearchResultSet
682 * @param $query String
686 protected function showInterwiki( &$matches, $query ) {
688 wfProfileIn( __METHOD__
);
689 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
691 $out = "<div id='mw-search-interwiki'><div id='mw-search-interwiki-caption'>".
692 wfMsg('search-interwiki-caption')."</div>\n";
693 $out .= "<ul class='mw-search-iwresults'>\n";
695 // work out custom project captions
696 $customCaptions = array();
697 $customLines = explode("\n",wfMsg('search-interwiki-custom')); // format per line <iwprefix>:<caption>
698 foreach($customLines as $line) {
699 $parts = explode(":",$line,2);
700 if(count($parts) == 2) // validate line
701 $customCaptions[$parts[0]] = $parts[1];
705 while( $result = $matches->next() ) {
706 $out .= $this->showInterwikiHit( $result, $prev, $terms, $query, $customCaptions );
707 $prev = $result->getInterwikiPrefix();
709 // TODO: should support paging in a non-confusing way (not sure how though, maybe via ajax)..
710 $out .= "</ul></div>\n";
712 // convert the whole thing to desired language variant
713 $out = $wgContLang->convert( $out );
714 wfProfileOut( __METHOD__
);
719 * Show single interwiki link
721 * @param $result SearchResult
722 * @param $lastInterwiki String
723 * @param $terms Array
724 * @param $query String
725 * @param $customCaptions Array: iw prefix -> caption
729 protected function showInterwikiHit( $result, $lastInterwiki, $terms, $query, $customCaptions) {
730 wfProfileIn( __METHOD__
);
732 if( $result->isBrokenTitle() ) {
733 wfProfileOut( __METHOD__
);
734 return "<!-- Broken link in search result -->\n";
737 $t = $result->getTitle();
739 $titleSnippet = $result->getTitleSnippet($terms);
741 if( $titleSnippet == '' )
742 $titleSnippet = null;
744 $link = Linker
::linkKnown(
749 // format redirect if any
750 $redirectTitle = $result->getRedirectTitle();
751 $redirectText = $result->getRedirectSnippet($terms);
753 if( !is_null($redirectTitle) ) {
754 if( $redirectText == '' )
755 $redirectText = null;
757 $redirect = "<span class='searchalttitle'>" .
769 // display project name
770 if(is_null($lastInterwiki) ||
$lastInterwiki != $t->getInterwiki()) {
771 if( key_exists($t->getInterwiki(),$customCaptions) ) {
772 // captions from 'search-interwiki-custom'
773 $caption = $customCaptions[$t->getInterwiki()];
775 // default is to show the hostname of the other wiki which might suck
776 // if there are many wikis on one hostname
777 $parsed = parse_url($t->getFullURL());
778 $caption = wfMsg('search-interwiki-default', $parsed['host']);
780 // "more results" link (special page stuff could be localized, but we might not know target lang)
781 $searchTitle = Title
::newFromText($t->getInterwiki().":Special:Search");
782 $searchLink = Linker
::linkKnown(
784 wfMsg('search-interwiki-more'),
788 'fulltext' => 'Search'
791 $out .= "</ul><div class='mw-search-interwiki-project'><span class='mw-search-interwiki-more'>
792 {$searchLink}</span>{$caption}</div>\n<ul>";
795 $out .= "<li>{$link} {$redirect}</li>\n";
796 wfProfileOut( __METHOD__
);
805 protected function getProfileForm( $profile, $term ) {
808 $opts['redirs'] = $this->searchRedirects
;
809 $opts['profile'] = $this->profile
;
811 if ( $profile === 'advanced' ) {
812 return $this->powerSearchBox( $term, $opts );
815 wfRunHooks( 'SpecialSearchProfileForm', array( $this, &$form, $profile, $term, $opts ) );
821 * Generates the power search box at [[Special:Search]]
823 * @param $term String: search term
825 * @return String: HTML form
827 protected function powerSearchBox( $term, $opts ) {
828 // Groups namespaces into rows according to subject
830 foreach( SearchEngine
::searchableNamespaces() as $namespace => $name ) {
831 $subject = MWNamespace
::getSubject( $namespace );
832 if( !array_key_exists( $subject, $rows ) ) {
833 $rows[$subject] = "";
835 $name = str_replace( '_', ' ', $name );
837 $name = wfMsg( 'blanknamespace' );
841 'td', array( 'style' => 'white-space: nowrap' )
846 "mw-search-ns{$namespace}",
847 in_array( $namespace, $this->namespaces
)
849 Xml
::closeElement( 'td' );
851 $rows = array_values( $rows );
852 $numRows = count( $rows );
854 // Lays out namespaces in multiple floating two-column tables so they'll
855 // be arranged nicely while still accommodating different screen widths
856 $namespaceTables = '';
857 for( $i = 0; $i < $numRows; $i +
= 4 ) {
858 $namespaceTables .= Xml
::openElement(
860 array( 'cellpadding' => 0, 'cellspacing' => 0, 'border' => 0 )
862 for( $j = $i; $j < $i +
4 && $j < $numRows; $j++
) {
863 $namespaceTables .= Xml
::tags( 'tr', null, $rows[$j] );
865 $namespaceTables .= Xml
::closeElement( 'table' );
867 // Show redirects check only if backend supports it
869 if( $this->getSearchEngine()->supports( 'list-redirects' ) ) {
871 Xml
::checkLabel( wfMsg( 'powersearch-redir' ), 'redirs', 'redirs', $this->searchRedirects
);
875 unset( $opts['redirs'] );
876 foreach( $opts as $key => $value ) {
877 $hidden .= Html
::hidden( $key, $value );
879 // Return final output
883 array( 'id' => 'mw-searchoptions', 'style' => 'margin:0em;' )
885 Xml
::element( 'legend', null, wfMsg('powersearch-legend') ) .
886 Xml
::tags( 'h4', null, wfMsgExt( 'powersearch-ns', array( 'parseinline' ) ) ) .
889 array( 'id' => 'mw-search-togglebox' ),
890 Xml
::label( wfMsg( 'powersearch-togglelabel' ), 'mw-search-togglelabel' ) .
895 'id' => 'mw-search-toggleall',
896 'value' => wfMsg( 'powersearch-toggleall' )
903 'id' => 'mw-search-togglenone',
904 'value' => wfMsg( 'powersearch-togglenone' )
908 Xml
::element( 'div', array( 'class' => 'divider' ), '', false ) .
910 Xml
::element( 'div', array( 'class' => 'divider' ), '', false ) .
911 $redirects . $hidden .
912 Xml
::closeElement( 'fieldset' );
918 protected function getSearchProfiles() {
919 // Builds list of Search Types (profiles)
920 $nsAllSet = array_keys( SearchEngine
::searchableNamespaces() );
924 'message' => 'searchprofile-articles',
925 'tooltip' => 'searchprofile-articles-tooltip',
926 'namespaces' => SearchEngine
::defaultNamespaces(),
927 'namespace-messages' => SearchEngine
::namespacesAsText(
928 SearchEngine
::defaultNamespaces()
932 'message' => 'searchprofile-images',
933 'tooltip' => 'searchprofile-images-tooltip',
934 'namespaces' => array( NS_FILE
),
937 'message' => 'searchprofile-project',
938 'tooltip' => 'searchprofile-project-tooltip',
939 'namespaces' => SearchEngine
::helpNamespaces(),
940 'namespace-messages' => SearchEngine
::namespacesAsText(
941 SearchEngine
::helpNamespaces()
945 'message' => 'searchprofile-everything',
946 'tooltip' => 'searchprofile-everything-tooltip',
947 'namespaces' => $nsAllSet,
950 'message' => 'searchprofile-advanced',
951 'tooltip' => 'searchprofile-advanced-tooltip',
952 'namespaces' => self
::NAMESPACES_CURRENT
,
956 wfRunHooks( 'SpecialSearchProfiles', array( &$profiles ) );
958 foreach( $profiles as &$data ) {
959 if ( !is_array( $data['namespaces'] ) ) continue;
960 sort( $data['namespaces'] );
968 * @param $resultsShown
972 protected function formHeader( $term, $resultsShown, $totalNum ) {
973 $out = Xml
::openElement('div', array( 'class' => 'mw-search-formheader' ) );
976 if( $this->startsWithImage( $term ) ) {
978 $bareterm = substr( $term, strpos( $term, ':' ) +
1 );
981 $profiles = $this->getSearchProfiles();
982 $lang = $this->getLang();
984 // Outputs XML for Search Types
985 $out .= Xml
::openElement( 'div', array( 'class' => 'search-types' ) );
986 $out .= Xml
::openElement( 'ul' );
987 foreach ( $profiles as $id => $profile ) {
988 if ( !isset( $profile['parameters'] ) ) {
989 $profile['parameters'] = array();
991 $profile['parameters']['profile'] = $id;
993 $tooltipParam = isset( $profile['namespace-messages'] ) ?
994 $lang->commaList( $profile['namespace-messages'] ) : null;
998 'class' => $this->profile
=== $id ?
'current' : 'normal'
1000 $this->makeSearchLink(
1003 wfMsg( $profile['message'] ),
1004 wfMsg( $profile['tooltip'], $tooltipParam ),
1005 $profile['parameters']
1009 $out .= Xml
::closeElement( 'ul' );
1010 $out .= Xml
::closeElement('div') ;
1013 if ( $resultsShown > 0 ) {
1014 if ( $totalNum > 0 ){
1015 $top = wfMsgExt( 'showingresultsheader', array( 'parseinline' ),
1016 $lang->formatNum( $this->offset +
1 ),
1017 $lang->formatNum( $this->offset +
$resultsShown ),
1018 $lang->formatNum( $totalNum ),
1019 wfEscapeWikiText( $term ),
1020 $lang->formatNum( $resultsShown )
1022 } elseif ( $resultsShown >= $this->limit
) {
1023 $top = wfShowingResults( $this->offset
, $this->limit
);
1025 $top = wfMsgExt( 'showingresultsnum', array( 'parseinline' ),
1026 $lang->formatNum( $this->limit
),
1027 $lang->formatNum( $this->offset +
1 ),
1028 $lang->formatNum( $resultsShown )
1031 $out .= Xml
::tags( 'div', array( 'class' => 'results-info' ),
1032 Xml
::tags( 'ul', null, Xml
::tags( 'li', null, $top ) )
1036 $out .= Xml
::element( 'div', array( 'style' => 'clear:both' ), '', false );
1037 $out .= Xml
::closeElement('div');
1043 * @param $term string
1046 protected function shortDialog( $term ) {
1047 $out = Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
1049 $out .= Html
::input( 'search', $term, 'search', array(
1050 'id' => $this->profile
=== 'advanced' ?
'powerSearchText' : 'searchText',
1054 $out .= Html
::hidden( 'fulltext', 'Search' ) . "\n";
1055 $out .= Xml
::submitButton( wfMsg( 'searchbutton' ) ) . "\n";
1056 return $out . $this->didYouMeanHtml
;
1060 * Make a search link with some target namespaces
1062 * @param $term String
1063 * @param $namespaces Array ignored
1064 * @param $label String: link's text
1065 * @param $tooltip String: link's tooltip
1066 * @param $params Array: query string parameters
1067 * @return String: HTML fragment
1069 protected function makeSearchLink( $term, $namespaces, $label, $tooltip, $params = array() ) {
1071 foreach( $namespaces as $n ) {
1072 $opt['ns' . $n] = 1;
1074 $opt['redirs'] = $this->searchRedirects
;
1076 $stParams = array_merge(
1079 'fulltext' => wfMsg( 'search' )
1084 return Xml
::element(
1087 'href' => $this->getTitle()->getLocalURL( $stParams ),
1088 'title' => $tooltip),
1094 * Check if query starts with image: prefix
1096 * @param $term String: the string to check
1099 protected function startsWithImage( $term ) {
1102 $p = explode( ':', $term );
1103 if( count( $p ) > 1 ) {
1104 return $wgContLang->getNsIndex( $p[0] ) == NS_FILE
;
1110 * Check if query starts with all: prefix
1112 * @param $term String: the string to check
1115 protected function startsWithAll( $term ) {
1117 $allkeyword = wfMsgForContent('searchall');
1119 $p = explode( ':', $term );
1120 if( count( $p ) > 1 ) {
1121 return $p[0] == $allkeyword;
1129 * @return SearchEngine
1131 public function getSearchEngine() {
1132 if ( $this->searchEngine
=== null ) {
1133 $this->searchEngine
= SearchEngine
::create();
1135 return $this->searchEngine
;
1139 * Users of hook SpecialSearchSetupEngine can use this to
1140 * add more params to links to not lose selection when
1141 * user navigates search results.
1147 public function setExtraParam( $key, $value ) {
1148 $this->extraParams
[$key] = $value;