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.
42 protected $searchEngine;
45 protected $extraParams = array();
47 /// No idea, apparently used by some other classes
53 protected $limit, $offset;
58 protected $namespaces;
63 protected $searchRedirects;
68 protected $didYouMeanHtml, $fulltext;
70 const NAMESPACES_CURRENT
= 'sense';
72 public function __construct() {
73 parent
::__construct( 'Search' );
79 * @param $par String or null
81 public function execute( $par ) {
83 $this->outputHeader();
84 $out = $this->getOutput();
85 $out->allowClickjacking();
86 $out->addModuleStyles( 'mediawiki.special' );
88 // Strip underscores from title parameter; most of the time we'll want
89 // text form here. But don't strip underscores from actual text params!
90 $titleParam = str_replace( '_', ' ', $par );
92 $request = $this->getRequest();
94 // Fetch the search term
95 $search = str_replace( "\n", " ", $request->getText( 'search', $titleParam ) );
99 if ( $request->getVal( 'fulltext' )
100 ||
!is_null( $request->getVal( 'offset' ) )
101 ||
!is_null( $request->getVal( 'searchx' ) ) )
103 $this->showResults( $search );
105 $this->goResult( $search );
110 * Set up basic search parameters from the request and user settings.
112 public function load() {
113 $request = $this->getRequest();
114 list( $this->limit
, $this->offset
) = $request->getLimitOffset( 20, 'searchlimit' );
115 $this->mPrefix
= $request->getVal( 'prefix', '' );
117 $user = $this->getUser();
118 # Extract manually requested namespaces
119 $nslist = $this->powerSearch( $request );
120 $this->profile
= $profile = $request->getVal( 'profile', null );
121 $profiles = $this->getSearchProfiles();
122 if ( $profile === null) {
123 // BC with old request format
124 $this->profile
= 'advanced';
125 if ( count( $nslist ) ) {
126 foreach( $profiles as $key => $data ) {
127 if ( $nslist === $data['namespaces'] && $key !== 'advanced') {
128 $this->profile
= $key;
131 $this->namespaces
= $nslist;
133 $this->namespaces
= SearchEngine
::userNamespaces( $user );
135 } elseif ( $profile === 'advanced' ) {
136 $this->namespaces
= $nslist;
138 if ( isset( $profiles[$profile]['namespaces'] ) ) {
139 $this->namespaces
= $profiles[$profile]['namespaces'];
141 // Unknown profile requested
142 $this->profile
= 'default';
143 $this->namespaces
= $profiles['default']['namespaces'];
147 // Redirects defaults to true, but we don't know whether it was ticked of or just missing
148 $default = $request->getBool( 'profile' ) ?
0 : 1;
149 $this->searchRedirects
= $request->getBool( 'redirs', $default ) ?
1 : 0;
150 $this->didYouMeanHtml
= ''; # html of did you mean... link
151 $this->fulltext
= $request->getVal('fulltext');
155 * If an exact title match can be found, jump straight ahead to it.
157 * @param $term String
159 public function goResult( $term ) {
160 $this->setupPage( $term );
161 # Try to go to page as entered.
162 $t = Title
::newFromText( $term );
163 # If the string cannot be used to create a title
164 if( is_null( $t ) ) {
165 return $this->showResults( $term );
167 # If there's an exact or very near match, jump right there.
168 $t = SearchEngine
::getNearMatch( $term );
170 if ( !wfRunHooks( 'SpecialSearchGo', array( &$t, &$term ) ) ) {
171 # Hook requested termination
175 if( !is_null( $t ) ) {
176 $this->getOutput()->redirect( $t->getFullURL() );
179 # No match, generate an edit URL
180 $t = Title
::newFromText( $term );
181 if( !is_null( $t ) ) {
183 wfRunHooks( 'SpecialSearchNogomatch', array( &$t ) );
184 wfDebugLog( 'nogomatch', $t->getText(), false );
186 # If the feature is enabled, go straight to the edit page
188 $this->getOutput()->redirect( $t->getFullURL( array( 'action' => 'edit' ) ) );
192 return $this->showResults( $term );
196 * @param $term String
198 public function showResults( $term ) {
199 global $wgDisableTextSearch, $wgSearchForwardUrl, $wgContLang, $wgScript;
200 wfProfileIn( __METHOD__
);
202 $search = $this->getSearchEngine();
203 $search->setLimitOffset( $this->limit
, $this->offset
);
204 $search->setNamespaces( $this->namespaces
);
205 $search->showRedirects
= $this->searchRedirects
; // BC
206 $search->setFeatureData( 'list-redirects', $this->searchRedirects
);
207 $search->prefix
= $this->mPrefix
;
208 $term = $search->transformSearchTerm($term);
210 wfRunHooks( 'SpecialSearchSetupEngine', array( $this, $this->profile
, $search ) );
212 $this->setupPage( $term );
214 $out = $this->getOutput();
216 if ( $wgDisableTextSearch ) {
217 if ( $wgSearchForwardUrl ) {
218 $url = str_replace( '$1', urlencode( $term ), $wgSearchForwardUrl );
219 $out->redirect( $url );
222 Xml
::openElement( 'fieldset' ) .
223 Xml
::element( 'legend', null, wfMsg( 'search-external' ) ) .
224 Xml
::element( 'p', array( 'class' => 'mw-searchdisabled' ), wfMsg( 'searchdisabled' ) ) .
225 wfMsg( 'googlesearch',
226 htmlspecialchars( $term ),
227 htmlspecialchars( 'UTF-8' ),
228 htmlspecialchars( wfMsg( 'searchbutton' ) )
230 Xml
::closeElement( 'fieldset' )
233 wfProfileOut( __METHOD__
);
237 $t = Title
::newFromText( $term );
239 // fetch search results
240 $rewritten = $search->replacePrefixes($term);
242 $titleMatches = $search->searchTitle( $rewritten );
243 if( !( $titleMatches instanceof SearchResultTooMany
) ) {
244 $textMatches = $search->searchText( $rewritten );
247 // did you mean... suggestions
248 if( $textMatches && $textMatches->hasSuggestion() ) {
249 $st = SpecialPage
::getTitleFor( 'Search' );
251 # mirror Go/Search behaviour of original request ..
252 $didYouMeanParams = array( 'search' => $textMatches->getSuggestionQuery() );
254 if( $this->fulltext
!= null ) {
255 $didYouMeanParams['fulltext'] = $this->fulltext
;
258 $stParams = array_merge(
260 $this->powerSearchOptions()
263 $suggestionSnippet = $textMatches->getSuggestionSnippet();
265 if( $suggestionSnippet == '' ) {
266 $suggestionSnippet = null;
269 $suggestLink = Linker
::linkKnown(
276 $this->didYouMeanHtml
= '<div class="searchdidyoumean">'.wfMsg('search-suggest',$suggestLink).'</div>';
278 // start rendering the page
283 'id' => ( $this->profile
=== 'advanced' ?
'powersearch' : 'search' ),
285 'action' => $wgScript
290 Xml
::openElement( 'table', array( 'id'=>'mw-search-top-table', 'border'=>0, 'cellpadding'=>0, 'cellspacing'=>0 ) ) .
291 Xml
::openElement( 'tr' ) .
292 Xml
::openElement( 'td' ) . "\n" .
293 $this->shortDialog( $term ) .
294 Xml
::closeElement('td') .
295 Xml
::closeElement('tr') .
296 Xml
::closeElement('table')
299 // Sometimes the search engine knows there are too many hits
300 if( $titleMatches instanceof SearchResultTooMany
) {
301 $out->wrapWikiMsg( "==$1==\n", 'toomanymatches' );
302 wfProfileOut( __METHOD__
);
306 $filePrefix = $wgContLang->getFormattedNsText(NS_FILE
).':';
307 if( trim( $term ) === '' ||
$filePrefix === trim( $term ) ) {
308 $out->addHTML( $this->formHeader( $term, 0, 0 ) );
309 $out->addHtml( $this->getProfileForm( $this->profile
, $term ) );
310 $out->addHTML( '</form>' );
311 // Empty query -- straight view of search form
312 wfProfileOut( __METHOD__
);
316 // Get number of results
317 $titleMatchesNum = $titleMatches ?
$titleMatches->numRows() : 0;
318 $textMatchesNum = $textMatches ?
$textMatches->numRows() : 0;
319 // Total initial query matches (possible false positives)
320 $num = $titleMatchesNum +
$textMatchesNum;
322 // Get total actual results (after second filtering, if any)
323 $numTitleMatches = $titleMatches && !is_null( $titleMatches->getTotalHits() ) ?
324 $titleMatches->getTotalHits() : $titleMatchesNum;
325 $numTextMatches = $textMatches && !is_null( $textMatches->getTotalHits() ) ?
326 $textMatches->getTotalHits() : $textMatchesNum;
328 // get total number of results if backend can calculate it
330 if($titleMatches && !is_null( $titleMatches->getTotalHits() ) )
331 $totalRes +
= $titleMatches->getTotalHits();
332 if($textMatches && !is_null( $textMatches->getTotalHits() ))
333 $totalRes +
= $textMatches->getTotalHits();
335 // show number of results and current offset
336 $out->addHTML( $this->formHeader( $term, $num, $totalRes ) );
337 $out->addHtml( $this->getProfileForm( $this->profile
, $term ) );
340 $out->addHtml( Xml
::closeElement( 'form' ) );
341 $out->addHtml( "<div class='searchresults'>" );
344 if( $num ||
$this->offset
) {
345 // Show the create link ahead
346 $this->showCreateLink( $t );
347 $prevnext = $this->getLang()->viewPrevNext( $this->getTitle(), $this->offset
, $this->limit
,
348 $this->powerSearchOptions() +
array( 'search' => $term ),
349 max( $titleMatchesNum, $textMatchesNum ) < $this->limit
351 //$out->addHTML( "<p class='mw-search-pager-top'>{$prevnext}</p>\n" );
352 wfRunHooks( 'SpecialSearchResults', array( $term, &$titleMatches, &$textMatches ) );
354 wfRunHooks( 'SpecialSearchNoResults', array( $term ) );
357 $out->parserOptions()->setEditSection( false );
358 if( $titleMatches ) {
359 if( $numTitleMatches > 0 ) {
360 $out->wrapWikiMsg( "==$1==\n", 'titlematches' );
361 $out->addHTML( $this->showMatches( $titleMatches ) );
363 $titleMatches->free();
366 // output appropriate heading
367 if( $numTextMatches > 0 && $numTitleMatches > 0 ) {
368 // if no title matches the heading is redundant
369 $out->wrapWikiMsg( "==$1==\n", 'textmatches' );
370 } elseif( $totalRes == 0 ) {
371 # Don't show the 'no text matches' if we received title matches
372 # $out->wrapWikiMsg( "==$1==\n", 'notextmatches' );
374 // show interwiki results if any
375 if( $textMatches->hasInterwikiResults() ) {
376 $out->addHTML( $this->showInterwiki( $textMatches->getInterwikiResults(), $term ) );
379 if( $numTextMatches > 0 ) {
380 $out->addHTML( $this->showMatches( $textMatches ) );
383 $textMatches->free();
386 $out->wrapWikiMsg( "<p class=\"mw-search-nonefound\">\n$1</p>", array( 'search-nonefound', wfEscapeWikiText( $term ) ) );
387 $this->showCreateLink( $t );
389 $out->addHtml( "</div>" );
391 if( $num ||
$this->offset
) {
392 $out->addHTML( "<p class='mw-search-pager-bottom'>{$prevnext}</p>\n" );
394 wfProfileOut( __METHOD__
);
400 protected function showCreateLink( $t ) {
401 // show direct page/create link if applicable
403 // Check DBkey !== '' in case of fragment link only.
404 if( is_null( $t ) ||
$t->getDBkey() === '' ) {
406 // preserve the paragraph for margins etc...
407 $this->getOutput()->addHtml( '<p></p>' );
411 if( $t->isKnown() ) {
412 $messageName = 'searchmenu-exists';
413 } elseif( $t->userCan( 'create' ) ) {
414 $messageName = 'searchmenu-new';
416 $messageName = 'searchmenu-new-nocreate';
418 $params = array( $messageName, wfEscapeWikiText( $t->getPrefixedText() ) );
419 wfRunHooks( 'SpecialSearchCreateLink', array( $t, &$params ) );
421 // Extensions using the hook might still return an empty $messageName
423 $this->getOutput()->wrapWikiMsg( "<p class=\"mw-search-createlink\">\n$1</p>", $params );
425 // preserve the paragraph for margins etc...
426 $this->getOutput()->addHtml( '<p></p>' );
431 * @param $term string
433 protected function setupPage( $term ) {
434 # Should advanced UI be used?
435 $this->searchAdvanced
= ($this->profile
=== 'advanced');
436 $out = $this->getOutput();
437 if( strval( $term ) !== '' ) {
438 $out->setPageTitle( wfMsg( 'searchresults') );
439 $out->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'searchresults-title', $term ) ) );
441 // add javascript specific to special:search
442 $out->addModules( 'mediawiki.special.search' );
446 * Extract "power search" namespace settings from the request object,
447 * returning a list of index numbers to search.
449 * @param $request WebRequest
452 protected function powerSearch( &$request ) {
454 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
455 if( $request->getCheck( 'ns' . $ns ) ) {
464 * Reconstruct the 'power search' options for links
468 protected function powerSearchOptions() {
470 $opt['redirs'] = $this->searchRedirects ?
1 : 0;
471 if( $this->profile
!== 'advanced' ) {
472 $opt['profile'] = $this->profile
;
474 foreach( $this->namespaces
as $n ) {
478 return $opt +
$this->extraParams
;
482 * Show whole set of results
484 * @param $matches SearchResultSet
488 protected function showMatches( &$matches ) {
490 wfProfileIn( __METHOD__
);
492 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
495 $infoLine = $matches->getInfo();
496 if( !is_null($infoLine) ) {
497 $out .= "\n<!-- {$infoLine} -->\n";
499 $out .= "<ul class='mw-search-results'>\n";
500 while( $result = $matches->next() ) {
501 $out .= $this->showHit( $result, $terms );
505 // convert the whole thing to desired language variant
506 $out = $wgContLang->convert( $out );
507 wfProfileOut( __METHOD__
);
512 * Format a single hit result
514 * @param $result SearchResult
515 * @param $terms Array: terms to highlight
519 protected function showHit( $result, $terms ) {
520 wfProfileIn( __METHOD__
);
522 if( $result->isBrokenTitle() ) {
523 wfProfileOut( __METHOD__
);
524 return "<!-- Broken link in search result -->\n";
527 $t = $result->getTitle();
529 $titleSnippet = $result->getTitleSnippet($terms);
531 if( $titleSnippet == '' )
532 $titleSnippet = null;
536 wfRunHooks( 'ShowSearchHitTitle',
537 array( &$link_t, &$titleSnippet, $result, $terms, $this ) );
539 $link = Linker
::linkKnown(
544 //If page content is not readable, just return the title.
545 //This is not quite safe, but better than showing excerpts from non-readable pages
546 //Note that hiding the entry entirely would screw up paging.
547 if( !$t->userCanRead() ) {
548 wfProfileOut( __METHOD__
);
549 return "<li>{$link}</li>\n";
552 // If the page doesn't *exist*... our search index is out of date.
553 // The least confusing at this point is to drop the result.
554 // You may get less results, but... oh well. :P
555 if( $result->isMissingRevision() ) {
556 wfProfileOut( __METHOD__
);
557 return "<!-- missing page " . htmlspecialchars( $t->getPrefixedText() ) . "-->\n";
560 // format redirects / relevant sections
561 $redirectTitle = $result->getRedirectTitle();
562 $redirectText = $result->getRedirectSnippet($terms);
563 $sectionTitle = $result->getSectionTitle();
564 $sectionText = $result->getSectionSnippet($terms);
567 if( !is_null($redirectTitle) ) {
568 if( $redirectText == '' )
569 $redirectText = null;
571 $redirect = "<span class='searchalttitle'>" .
584 if( !is_null($sectionTitle) ) {
585 if( $sectionText == '' )
588 $section = "<span class='searchalttitle'>" .
590 'search-section', Linker
::linkKnown(
598 // format text extract
599 $extract = "<div class='searchresult'>".$result->getTextSnippet($terms)."</div>";
601 $lang = $this->getLang();
604 if( is_null( $result->getScore() ) ) {
605 // Search engine doesn't report scoring info
608 $percent = sprintf( '%2.1f', $result->getScore() * 100 );
609 $score = wfMsg( 'search-result-score', $lang->formatNum( $percent ) )
613 // format description
614 $byteSize = $result->getByteSize();
615 $wordCount = $result->getWordCount();
616 $timestamp = $result->getTimestamp();
618 'search-result-size',
619 array( 'parsemag', 'escape' ),
620 $lang->formatSize( $byteSize ),
621 $lang->formatNum( $wordCount )
624 if( $t->getNamespace() == NS_CATEGORY
) {
625 $cat = Category
::newFromTitle( $t );
627 'search-result-category-size',
628 array( 'parsemag', 'escape' ),
629 $lang->formatNum( $cat->getPageCount() ),
630 $lang->formatNum( $cat->getSubcatCount() ),
631 $lang->formatNum( $cat->getFileCount() )
635 $date = $lang->timeanddate( $timestamp );
637 // link to related articles if supported
639 if( $result->hasRelated() ) {
640 $st = SpecialPage
::getTitleFor( 'Search' );
641 $stParams = array_merge(
642 $this->powerSearchOptions(),
644 'search' => wfMsgForContent( 'searchrelated' ) . ':' . $t->getPrefixedText(),
645 'fulltext' => wfMsg( 'search' )
649 $related = ' -- ' . Linker
::linkKnown(
651 wfMsg('search-relatedarticle'),
657 // Include a thumbnail for media files...
658 if( $t->getNamespace() == NS_FILE
) {
659 $img = wfFindFile( $t );
661 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
663 $desc = wfMsg( 'parentheses', $img->getShortDesc() );
664 wfProfileOut( __METHOD__
);
665 // Float doesn't seem to interact well with the bullets.
666 // Table messes up vertical alignment of the bullets.
667 // Bullets are therefore disabled (didn't look great anyway).
669 '<table class="searchResultImage">' .
671 '<td width="120" align="center" valign="top">' .
672 $thumb->toHtml( array( 'desc-link' => true ) ) .
674 '<td valign="top">' .
677 "<div class='mw-search-result-data'>{$score}{$desc} - {$date}{$related}</div>" .
686 wfProfileOut( __METHOD__
);
687 return "<li><div class='mw-search-result-heading'>{$link} {$redirect} {$section}</div> {$extract}\n" .
688 "<div class='mw-search-result-data'>{$score}{$size} - {$date}{$related}</div>" .
694 * Show results from other wikis
696 * @param $matches SearchResultSet
697 * @param $query String
701 protected function showInterwiki( &$matches, $query ) {
703 wfProfileIn( __METHOD__
);
704 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
706 $out = "<div id='mw-search-interwiki'><div id='mw-search-interwiki-caption'>".
707 wfMsg('search-interwiki-caption')."</div>\n";
708 $out .= "<ul class='mw-search-iwresults'>\n";
710 // work out custom project captions
711 $customCaptions = array();
712 $customLines = explode("\n",wfMsg('search-interwiki-custom')); // format per line <iwprefix>:<caption>
713 foreach($customLines as $line) {
714 $parts = explode(":",$line,2);
715 if(count($parts) == 2) // validate line
716 $customCaptions[$parts[0]] = $parts[1];
720 while( $result = $matches->next() ) {
721 $out .= $this->showInterwikiHit( $result, $prev, $terms, $query, $customCaptions );
722 $prev = $result->getInterwikiPrefix();
724 // TODO: should support paging in a non-confusing way (not sure how though, maybe via ajax)..
725 $out .= "</ul></div>\n";
727 // convert the whole thing to desired language variant
728 $out = $wgContLang->convert( $out );
729 wfProfileOut( __METHOD__
);
734 * Show single interwiki link
736 * @param $result SearchResult
737 * @param $lastInterwiki String
738 * @param $terms Array
739 * @param $query String
740 * @param $customCaptions Array: iw prefix -> caption
744 protected function showInterwikiHit( $result, $lastInterwiki, $terms, $query, $customCaptions) {
745 wfProfileIn( __METHOD__
);
747 if( $result->isBrokenTitle() ) {
748 wfProfileOut( __METHOD__
);
749 return "<!-- Broken link in search result -->\n";
752 $t = $result->getTitle();
754 $titleSnippet = $result->getTitleSnippet($terms);
756 if( $titleSnippet == '' )
757 $titleSnippet = null;
759 $link = Linker
::linkKnown(
764 // format redirect if any
765 $redirectTitle = $result->getRedirectTitle();
766 $redirectText = $result->getRedirectSnippet($terms);
768 if( !is_null($redirectTitle) ) {
769 if( $redirectText == '' )
770 $redirectText = null;
772 $redirect = "<span class='searchalttitle'>" .
784 // display project name
785 if(is_null($lastInterwiki) ||
$lastInterwiki != $t->getInterwiki()) {
786 if( array_key_exists($t->getInterwiki(),$customCaptions) ) {
787 // captions from 'search-interwiki-custom'
788 $caption = $customCaptions[$t->getInterwiki()];
790 // default is to show the hostname of the other wiki which might suck
791 // if there are many wikis on one hostname
792 $parsed = wfParseUrl( $t->getFullURL() );
793 $caption = wfMsg('search-interwiki-default', $parsed['host']);
795 // "more results" link (special page stuff could be localized, but we might not know target lang)
796 $searchTitle = Title
::newFromText($t->getInterwiki().":Special:Search");
797 $searchLink = Linker
::linkKnown(
799 wfMsg('search-interwiki-more'),
803 'fulltext' => 'Search'
806 $out .= "</ul><div class='mw-search-interwiki-project'><span class='mw-search-interwiki-more'>
807 {$searchLink}</span>{$caption}</div>\n<ul>";
810 $out .= "<li>{$link} {$redirect}</li>\n";
811 wfProfileOut( __METHOD__
);
820 protected function getProfileForm( $profile, $term ) {
823 $opts['redirs'] = $this->searchRedirects
;
824 $opts['profile'] = $this->profile
;
826 if ( $profile === 'advanced' ) {
827 return $this->powerSearchBox( $term, $opts );
830 wfRunHooks( 'SpecialSearchProfileForm', array( $this, &$form, $profile, $term, $opts ) );
836 * Generates the power search box at [[Special:Search]]
838 * @param $term String: search term
840 * @return String: HTML form
842 protected function powerSearchBox( $term, $opts ) {
843 // Groups namespaces into rows according to subject
845 foreach( SearchEngine
::searchableNamespaces() as $namespace => $name ) {
846 $subject = MWNamespace
::getSubject( $namespace );
847 if( !array_key_exists( $subject, $rows ) ) {
848 $rows[$subject] = "";
850 $name = str_replace( '_', ' ', $name );
852 $name = wfMsg( 'blanknamespace' );
856 'td', array( 'style' => 'white-space: nowrap' )
861 "mw-search-ns{$namespace}",
862 in_array( $namespace, $this->namespaces
)
864 Xml
::closeElement( 'td' );
866 $rows = array_values( $rows );
867 $numRows = count( $rows );
869 // Lays out namespaces in multiple floating two-column tables so they'll
870 // be arranged nicely while still accommodating different screen widths
871 $namespaceTables = '';
872 for( $i = 0; $i < $numRows; $i +
= 4 ) {
873 $namespaceTables .= Xml
::openElement(
875 array( 'cellpadding' => 0, 'cellspacing' => 0, 'border' => 0 )
877 for( $j = $i; $j < $i +
4 && $j < $numRows; $j++
) {
878 $namespaceTables .= Xml
::tags( 'tr', null, $rows[$j] );
880 $namespaceTables .= Xml
::closeElement( 'table' );
883 $showSections = array( 'namespaceTables' => $namespaceTables );
885 // Show redirects check only if backend supports it
886 if( $this->getSearchEngine()->supports( 'list-redirects' ) ) {
887 $showSections['redirects'] =
888 Xml
::checkLabel( wfMsg( 'powersearch-redir' ), 'redirs', 'redirs', $this->searchRedirects
);
891 wfRunHooks( 'SpecialSearchPowerBox', array( &$showSections, $term, $opts ) );
894 unset( $opts['redirs'] );
895 foreach( $opts as $key => $value ) {
896 $hidden .= Html
::hidden( $key, $value );
898 // Return final output
902 array( 'id' => 'mw-searchoptions', 'style' => 'margin:0em;' )
904 Xml
::element( 'legend', null, wfMsg('powersearch-legend') ) .
905 Xml
::tags( 'h4', null, wfMsgExt( 'powersearch-ns', array( 'parseinline' ) ) ) .
908 array( 'id' => 'mw-search-togglebox' ),
909 Xml
::label( wfMsg( 'powersearch-togglelabel' ), 'mw-search-togglelabel' ) .
914 'id' => 'mw-search-toggleall',
915 'value' => wfMsg( 'powersearch-toggleall' )
922 'id' => 'mw-search-togglenone',
923 'value' => wfMsg( 'powersearch-togglenone' )
927 Xml
::element( 'div', array( 'class' => 'divider' ), '', false ) .
928 implode( Xml
::element( 'div', array( 'class' => 'divider' ), '', false ), $showSections ) .
930 Xml
::closeElement( 'fieldset' );
936 protected function getSearchProfiles() {
937 // Builds list of Search Types (profiles)
938 $nsAllSet = array_keys( SearchEngine
::searchableNamespaces() );
942 'message' => 'searchprofile-articles',
943 'tooltip' => 'searchprofile-articles-tooltip',
944 'namespaces' => SearchEngine
::defaultNamespaces(),
945 'namespace-messages' => SearchEngine
::namespacesAsText(
946 SearchEngine
::defaultNamespaces()
950 'message' => 'searchprofile-images',
951 'tooltip' => 'searchprofile-images-tooltip',
952 'namespaces' => array( NS_FILE
),
955 'message' => 'searchprofile-project',
956 'tooltip' => 'searchprofile-project-tooltip',
957 'namespaces' => SearchEngine
::helpNamespaces(),
958 'namespace-messages' => SearchEngine
::namespacesAsText(
959 SearchEngine
::helpNamespaces()
963 'message' => 'searchprofile-everything',
964 'tooltip' => 'searchprofile-everything-tooltip',
965 'namespaces' => $nsAllSet,
968 'message' => 'searchprofile-advanced',
969 'tooltip' => 'searchprofile-advanced-tooltip',
970 'namespaces' => self
::NAMESPACES_CURRENT
,
974 wfRunHooks( 'SpecialSearchProfiles', array( &$profiles ) );
976 foreach( $profiles as &$data ) {
977 if ( !is_array( $data['namespaces'] ) ) continue;
978 sort( $data['namespaces'] );
986 * @param $resultsShown
990 protected function formHeader( $term, $resultsShown, $totalNum ) {
991 $out = Xml
::openElement('div', array( 'class' => 'mw-search-formheader' ) );
994 if( $this->startsWithImage( $term ) ) {
996 $bareterm = substr( $term, strpos( $term, ':' ) +
1 );
999 $profiles = $this->getSearchProfiles();
1000 $lang = $this->getLang();
1002 // Outputs XML for Search Types
1003 $out .= Xml
::openElement( 'div', array( 'class' => 'search-types' ) );
1004 $out .= Xml
::openElement( 'ul' );
1005 foreach ( $profiles as $id => $profile ) {
1006 if ( !isset( $profile['parameters'] ) ) {
1007 $profile['parameters'] = array();
1009 $profile['parameters']['profile'] = $id;
1011 $tooltipParam = isset( $profile['namespace-messages'] ) ?
1012 $lang->commaList( $profile['namespace-messages'] ) : null;
1016 'class' => $this->profile
=== $id ?
'current' : 'normal'
1018 $this->makeSearchLink(
1021 wfMsg( $profile['message'] ),
1022 wfMsg( $profile['tooltip'], $tooltipParam ),
1023 $profile['parameters']
1027 $out .= Xml
::closeElement( 'ul' );
1028 $out .= Xml
::closeElement('div') ;
1031 if ( $resultsShown > 0 ) {
1032 if ( $totalNum > 0 ){
1033 $top = wfMsgExt( 'showingresultsheader', array( 'parseinline' ),
1034 $lang->formatNum( $this->offset +
1 ),
1035 $lang->formatNum( $this->offset +
$resultsShown ),
1036 $lang->formatNum( $totalNum ),
1037 wfEscapeWikiText( $term ),
1038 $lang->formatNum( $resultsShown )
1040 } elseif ( $resultsShown >= $this->limit
) {
1041 $top = wfMsgExt( 'showingresults', array( 'parseinline' ),
1042 $lang->formatNum( $this->limit
),
1043 $lang->formatNum( $this->offset +
1 )
1046 $top = wfMsgExt( 'showingresultsnum', array( 'parseinline' ),
1047 $lang->formatNum( $this->limit
),
1048 $lang->formatNum( $this->offset +
1 ),
1049 $lang->formatNum( $resultsShown )
1052 $out .= Xml
::tags( 'div', array( 'class' => 'results-info' ),
1053 Xml
::tags( 'ul', null, Xml
::tags( 'li', null, $top ) )
1057 $out .= Xml
::element( 'div', array( 'style' => 'clear:both' ), '', false );
1058 $out .= Xml
::closeElement('div');
1064 * @param $term string
1067 protected function shortDialog( $term ) {
1068 $out = Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
1070 $out .= Html
::input( 'search', $term, 'search', array(
1071 'id' => $this->profile
=== 'advanced' ?
'powerSearchText' : 'searchText',
1075 $out .= Html
::hidden( 'fulltext', 'Search' ) . "\n";
1076 $out .= Xml
::submitButton( wfMsg( 'searchbutton' ) ) . "\n";
1077 return $out . $this->didYouMeanHtml
;
1081 * Make a search link with some target namespaces
1083 * @param $term String
1084 * @param $namespaces Array ignored
1085 * @param $label String: link's text
1086 * @param $tooltip String: link's tooltip
1087 * @param $params Array: query string parameters
1088 * @return String: HTML fragment
1090 protected function makeSearchLink( $term, $namespaces, $label, $tooltip, $params = array() ) {
1092 foreach( $namespaces as $n ) {
1093 $opt['ns' . $n] = 1;
1095 $opt['redirs'] = $this->searchRedirects
;
1097 $stParams = array_merge(
1100 'fulltext' => wfMsg( 'search' )
1105 return Xml
::element(
1108 'href' => $this->getTitle()->getLocalURL( $stParams ),
1109 'title' => $tooltip),
1115 * Check if query starts with image: prefix
1117 * @param $term String: the string to check
1120 protected function startsWithImage( $term ) {
1123 $p = explode( ':', $term );
1124 if( count( $p ) > 1 ) {
1125 return $wgContLang->getNsIndex( $p[0] ) == NS_FILE
;
1131 * Check if query starts with all: prefix
1133 * @param $term String: the string to check
1136 protected function startsWithAll( $term ) {
1138 $allkeyword = wfMsgForContent('searchall');
1140 $p = explode( ':', $term );
1141 if( count( $p ) > 1 ) {
1142 return $p[0] == $allkeyword;
1150 * @return SearchEngine
1152 public function getSearchEngine() {
1153 if ( $this->searchEngine
=== null ) {
1154 $this->searchEngine
= SearchEngine
::create();
1156 return $this->searchEngine
;
1160 * Users of hook SpecialSearchSetupEngine can use this to
1161 * add more params to links to not lose selection when
1162 * user navigates search results.
1168 public function setExtraParam( $key, $value ) {
1169 $this->extraParams
[$key] = $value;