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
43 const NAMESPACES_CURRENT
= 'sense';
45 public function __construct() {
46 parent
::__construct( 'Search' );
52 * @param $par String or null
54 public function execute( $par ) {
55 global $wgRequest, $wgUser, $wgOut;
58 $this->outputHeader();
59 $wgOut->allowClickjacking();
60 $wgOut->addModuleStyles( 'mediawiki.special' );
62 // Strip underscores from title parameter; most of the time we'll want
63 // text form here. But don't strip underscores from actual text params!
64 $titleParam = str_replace( '_', ' ', $par );
66 // Fetch the search term
67 $search = str_replace( "\n", " ", $wgRequest->getText( 'search', $titleParam ) );
69 $this->load( $wgRequest, $wgUser );
71 if ( $wgRequest->getVal( 'fulltext' )
72 ||
!is_null( $wgRequest->getVal( 'offset' ) )
73 ||
!is_null( $wgRequest->getVal( 'searchx' ) ) )
75 $this->showResults( $search );
77 $this->goResult( $search );
82 * Set up basic search parameters from the request and user settings.
83 * Typically you'll pass $wgRequest and $wgUser.
85 * @param $request WebRequest
88 public function load( &$request, &$user ) {
89 list( $this->limit
, $this->offset
) = $request->getLimitOffset( 20, 'searchlimit' );
90 $this->mPrefix
= $request->getVal( 'prefix', '' );
93 # Extract manually requested namespaces
94 $nslist = $this->powerSearch( $request );
95 $this->profile
= $profile = $request->getVal( 'profile', null );
96 $profiles = $this->getSearchProfiles();
97 if ( $profile === null) {
98 // BC with old request format
99 $this->profile
= 'advanced';
100 if ( count( $nslist ) ) {
101 foreach( $profiles as $key => $data ) {
102 if ( $nslist === $data['namespaces'] && $key !== 'advanced') {
103 $this->profile
= $key;
106 $this->namespaces
= $nslist;
108 $this->namespaces
= SearchEngine
::userNamespaces( $user );
110 } elseif ( $profile === 'advanced' ) {
111 $this->namespaces
= $nslist;
113 if ( isset( $profiles[$profile]['namespaces'] ) ) {
114 $this->namespaces
= $profiles[$profile]['namespaces'];
116 // Unknown profile requested
117 $this->profile
= 'default';
118 $this->namespaces
= $profiles['default']['namespaces'];
122 // Redirects defaults to true, but we don't know whether it was ticked of or just missing
123 $default = $request->getBool( 'profile' ) ?
0 : 1;
124 $this->searchRedirects
= $request->getBool( 'redirs', $default ) ?
1 : 0;
125 $this->sk
= $this->getSkin();
126 $this->didYouMeanHtml
= ''; # html of did you mean... link
127 $this->fulltext
= $request->getVal('fulltext');
131 * If an exact title match can be found, jump straight ahead to it.
133 * @param $term String
135 public function goResult( $term ) {
137 $this->setupPage( $term );
138 # Try to go to page as entered.
139 $t = Title
::newFromText( $term );
140 # If the string cannot be used to create a title
141 if( is_null( $t ) ) {
142 return $this->showResults( $term );
144 # If there's an exact or very near match, jump right there.
145 $t = SearchEngine
::getNearMatch( $term );
147 if ( !wfRunHooks( 'SpecialSearchGo', array( &$t, &$term ) ) ) {
148 # Hook requested termination
152 if( !is_null( $t ) ) {
153 $wgOut->redirect( $t->getFullURL() );
156 # No match, generate an edit URL
157 $t = Title
::newFromText( $term );
158 if( !is_null( $t ) ) {
160 wfRunHooks( 'SpecialSearchNogomatch', array( &$t ) );
161 wfDebugLog( 'nogomatch', $t->getText(), false );
163 # If the feature is enabled, go straight to the edit page
165 $wgOut->redirect( $t->getFullURL( array( 'action' => 'edit' ) ) );
169 return $this->showResults( $term );
173 * @param $term String
175 public function showResults( $term ) {
176 global $wgOut, $wgDisableTextSearch, $wgContLang, $wgScript;
177 wfProfileIn( __METHOD__
);
179 $sk = $this->getSkin();
181 $search = $this->getSearchEngine();
182 $search->setLimitOffset( $this->limit
, $this->offset
);
183 $search->setNamespaces( $this->namespaces
);
184 $search->showRedirects
= $this->searchRedirects
; // BC
185 $search->setFeatureData( 'list-redirects', $this->searchRedirects
);
186 $search->prefix
= $this->mPrefix
;
187 $term = $search->transformSearchTerm($term);
189 wfRunHooks( 'SpecialSearchSetupEngine', array( $this, $this->profile
, $search ) );
191 $this->setupPage( $term );
193 if( $wgDisableTextSearch ) {
194 global $wgSearchForwardUrl;
195 if( $wgSearchForwardUrl ) {
196 $url = str_replace( '$1', urlencode( $term ), $wgSearchForwardUrl );
197 $wgOut->redirect( $url );
198 wfProfileOut( __METHOD__
);
202 Xml
::openElement( 'fieldset' ) .
203 Xml
::element( 'legend', null, wfMsg( 'search-external' ) ) .
204 Xml
::element( 'p', array( 'class' => 'mw-searchdisabled' ), wfMsg( 'searchdisabled' ) ) .
205 wfMsg( 'googlesearch',
206 htmlspecialchars( $term ),
207 htmlspecialchars( 'UTF-8' ),
208 htmlspecialchars( wfMsg( 'searchbutton' ) )
210 Xml
::closeElement( 'fieldset' )
212 wfProfileOut( __METHOD__
);
216 $t = Title
::newFromText( $term );
218 // fetch search results
219 $rewritten = $search->replacePrefixes($term);
221 $titleMatches = $search->searchTitle( $rewritten );
222 if( !($titleMatches instanceof SearchResultTooMany
))
223 $textMatches = $search->searchText( $rewritten );
225 // did you mean... suggestions
226 if( $textMatches && $textMatches->hasSuggestion() ) {
227 $st = SpecialPage
::getTitleFor( 'Search' );
229 # mirror Go/Search behaviour of original request ..
230 $didYouMeanParams = array( 'search' => $textMatches->getSuggestionQuery() );
232 if($this->fulltext
!= null)
233 $didYouMeanParams['fulltext'] = $this->fulltext
;
235 $stParams = array_merge(
237 $this->powerSearchOptions()
240 $suggestionSnippet = $textMatches->getSuggestionSnippet();
242 if( $suggestionSnippet == '' )
243 $suggestionSnippet = null;
245 $suggestLink = $sk->linkKnown(
252 $this->didYouMeanHtml
= '<div class="searchdidyoumean">'.wfMsg('search-suggest',$suggestLink).'</div>';
254 // start rendering the page
259 'id' => ( $this->profile
=== 'advanced' ?
'powersearch' : 'search' ),
261 'action' => $wgScript
266 Xml
::openElement( 'table', array( 'id'=>'mw-search-top-table', 'border'=>0, 'cellpadding'=>0, 'cellspacing'=>0 ) ) .
267 Xml
::openElement( 'tr' ) .
268 Xml
::openElement( 'td' ) . "\n" .
269 $this->shortDialog( $term ) .
270 Xml
::closeElement('td') .
271 Xml
::closeElement('tr') .
272 Xml
::closeElement('table')
275 // Sometimes the search engine knows there are too many hits
276 if( $titleMatches instanceof SearchResultTooMany
) {
277 $wgOut->wrapWikiMsg( "==$1==\n", 'toomanymatches' );
278 wfProfileOut( __METHOD__
);
282 $filePrefix = $wgContLang->getFormattedNsText(NS_FILE
).':';
283 if( trim( $term ) === '' ||
$filePrefix === trim( $term ) ) {
284 $wgOut->addHTML( $this->formHeader( $term, 0, 0 ) );
285 $wgOut->addHtml( $this->getProfileForm( $this->profile
, $term ) );
286 $wgOut->addHTML( '</form>' );
287 // Empty query -- straight view of search form
288 wfProfileOut( __METHOD__
);
292 // Get number of results
293 $titleMatchesNum = $titleMatches ?
$titleMatches->numRows() : 0;
294 $textMatchesNum = $textMatches ?
$textMatches->numRows() : 0;
295 // Total initial query matches (possible false positives)
296 $num = $titleMatchesNum +
$textMatchesNum;
298 // Get total actual results (after second filtering, if any)
299 $numTitleMatches = $titleMatches && !is_null( $titleMatches->getTotalHits() ) ?
300 $titleMatches->getTotalHits() : $titleMatchesNum;
301 $numTextMatches = $textMatches && !is_null( $textMatches->getTotalHits() ) ?
302 $textMatches->getTotalHits() : $textMatchesNum;
304 // get total number of results if backend can calculate it
306 if($titleMatches && !is_null( $titleMatches->getTotalHits() ) )
307 $totalRes +
= $titleMatches->getTotalHits();
308 if($textMatches && !is_null( $textMatches->getTotalHits() ))
309 $totalRes +
= $textMatches->getTotalHits();
311 // show number of results and current offset
312 $wgOut->addHTML( $this->formHeader( $term, $num, $totalRes ) );
313 $wgOut->addHtml( $this->getProfileForm( $this->profile
, $term ) );
316 $wgOut->addHtml( Xml
::closeElement( 'form' ) );
317 $wgOut->addHtml( "<div class='searchresults'>" );
320 if( $num ||
$this->offset
) {
321 // Show the create link ahead
322 $this->showCreateLink( $t );
323 $prevnext = wfViewPrevNext( $this->offset
, $this->limit
,
324 SpecialPage
::getTitleFor( 'Search' ),
325 wfArrayToCGI( $this->powerSearchOptions(), array( 'search' => $term ) ),
326 max( $titleMatchesNum, $textMatchesNum ) < $this->limit
328 //$wgOut->addHTML( "<p class='mw-search-pager-top'>{$prevnext}</p>\n" );
329 wfRunHooks( 'SpecialSearchResults', array( $term, &$titleMatches, &$textMatches ) );
331 wfRunHooks( 'SpecialSearchNoResults', array( $term ) );
334 $wgOut->parserOptions()->setEditSection( false );
335 if( $titleMatches ) {
336 if( $numTitleMatches > 0 ) {
337 $wgOut->wrapWikiMsg( "==$1==\n", 'titlematches' );
338 $wgOut->addHTML( $this->showMatches( $titleMatches ) );
340 $titleMatches->free();
343 // output appropriate heading
344 if( $numTextMatches > 0 && $numTitleMatches > 0 ) {
345 // if no title matches the heading is redundant
346 $wgOut->wrapWikiMsg( "==$1==\n", 'textmatches' );
347 } elseif( $totalRes == 0 ) {
348 # Don't show the 'no text matches' if we received title matches
349 # $wgOut->wrapWikiMsg( "==$1==\n", 'notextmatches' );
351 // show interwiki results if any
352 if( $textMatches->hasInterwikiResults() ) {
353 $wgOut->addHTML( $this->showInterwiki( $textMatches->getInterwikiResults(), $term ) );
356 if( $numTextMatches > 0 ) {
357 $wgOut->addHTML( $this->showMatches( $textMatches ) );
360 $textMatches->free();
363 $wgOut->wrapWikiMsg( "<p class=\"mw-search-nonefound\">\n$1</p>", array( 'search-nonefound', wfEscapeWikiText( $term ) ) );
364 $this->showCreateLink( $t );
366 $wgOut->addHtml( "</div>" );
368 if( $num ||
$this->offset
) {
369 $wgOut->addHTML( "<p class='mw-search-pager-bottom'>{$prevnext}</p>\n" );
371 wfProfileOut( __METHOD__
);
374 protected function showCreateLink( $t ) {
377 // show direct page/create link if applicable
378 // Check DBkey !== '' in case of fragment link only.
380 if( !is_null($t) && $t->getDBkey() !== '' ) {
381 if( $t->isKnown() ) {
382 $messageName = 'searchmenu-exists';
383 } elseif( $t->userCan( 'create' ) ) {
384 $messageName = 'searchmenu-new';
386 $messageName = 'searchmenu-new-nocreate';
390 $wgOut->wrapWikiMsg( "<p class=\"mw-search-createlink\">\n$1</p>", array( $messageName, wfEscapeWikiText( $t->getPrefixedText() ) ) );
392 // preserve the paragraph for margins etc...
393 $wgOut->addHtml( '<p></p>' );
400 protected function setupPage( $term ) {
403 # Should advanced UI be used?
404 $this->searchAdvanced
= ($this->profile
=== 'advanced');
405 if( strval( $term ) !== '' ) {
406 $wgOut->setPageTitle( wfMsg( 'searchresults') );
407 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'searchresults-title', $term ) ) );
409 // add javascript specific to special:search
410 $wgOut->addModules( 'mediawiki.special.search' );
414 * Extract "power search" namespace settings from the request object,
415 * returning a list of index numbers to search.
417 * @param $request WebRequest
420 protected function powerSearch( &$request ) {
422 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
423 if( $request->getCheck( 'ns' . $ns ) ) {
432 * Reconstruct the 'power search' options for links
436 protected function powerSearchOptions() {
438 $opt['redirs'] = $this->searchRedirects ?
1 : 0;
439 if( $this->profile
!== 'advanced' ) {
440 $opt['profile'] = $this->profile
;
442 foreach( $this->namespaces
as $n ) {
446 return $opt +
$this->extraParams
;
450 * Show whole set of results
452 * @param $matches SearchResultSet
454 protected function showMatches( &$matches ) {
456 wfProfileIn( __METHOD__
);
458 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
461 $infoLine = $matches->getInfo();
462 if( !is_null($infoLine) ) {
463 $out .= "\n<!-- {$infoLine} -->\n";
465 $out .= "<ul class='mw-search-results'>\n";
466 while( $result = $matches->next() ) {
467 $out .= $this->showHit( $result, $terms );
471 // convert the whole thing to desired language variant
472 $out = $wgContLang->convert( $out );
473 wfProfileOut( __METHOD__
);
478 * Format a single hit result
480 * @param $result SearchResult
481 * @param $terms Array: terms to highlight
483 protected function showHit( $result, $terms ) {
485 wfProfileIn( __METHOD__
);
487 if( $result->isBrokenTitle() ) {
488 wfProfileOut( __METHOD__
);
489 return "<!-- Broken link in search result -->\n";
492 $sk = $this->getSkin();
493 $t = $result->getTitle();
495 $titleSnippet = $result->getTitleSnippet($terms);
497 if( $titleSnippet == '' )
498 $titleSnippet = null;
502 wfRunHooks( 'ShowSearchHitTitle',
503 array( &$link_t, &$titleSnippet, $result, $terms, $this ) );
505 $link = $this->sk
->linkKnown(
510 //If page content is not readable, just return the title.
511 //This is not quite safe, but better than showing excerpts from non-readable pages
512 //Note that hiding the entry entirely would screw up paging.
513 if( !$t->userCanRead() ) {
514 wfProfileOut( __METHOD__
);
515 return "<li>{$link}</li>\n";
518 // If the page doesn't *exist*... our search index is out of date.
519 // The least confusing at this point is to drop the result.
520 // You may get less results, but... oh well. :P
521 if( $result->isMissingRevision() ) {
522 wfProfileOut( __METHOD__
);
523 return "<!-- missing page " . htmlspecialchars( $t->getPrefixedText() ) . "-->\n";
526 // format redirects / relevant sections
527 $redirectTitle = $result->getRedirectTitle();
528 $redirectText = $result->getRedirectSnippet($terms);
529 $sectionTitle = $result->getSectionTitle();
530 $sectionText = $result->getSectionSnippet($terms);
533 if( !is_null($redirectTitle) ) {
534 if( $redirectText == '' )
535 $redirectText = null;
537 $redirect = "<span class='searchalttitle'>" .
540 $this->sk
->linkKnown(
550 if( !is_null($sectionTitle) ) {
551 if( $sectionText == '' )
554 $section = "<span class='searchalttitle'>" .
556 'search-section', $this->sk
->linkKnown(
564 // format text extract
565 $extract = "<div class='searchresult'>".$result->getTextSnippet($terms)."</div>";
568 if( is_null( $result->getScore() ) ) {
569 // Search engine doesn't report scoring info
572 $percent = sprintf( '%2.1f', $result->getScore() * 100 );
573 $score = wfMsg( 'search-result-score', $wgLang->formatNum( $percent ) )
577 // format description
578 $byteSize = $result->getByteSize();
579 $wordCount = $result->getWordCount();
580 $timestamp = $result->getTimestamp();
582 'search-result-size',
583 array( 'parsemag', 'escape' ),
584 $wgLang->formatSize( $byteSize ),
585 $wgLang->formatNum( $wordCount )
588 if( $t->getNamespace() == NS_CATEGORY
) {
589 $cat = Category
::newFromTitle( $t );
591 'search-result-category-size',
592 array( 'parsemag', 'escape' ),
593 $wgLang->formatNum( $cat->getPageCount() ),
594 $wgLang->formatNum( $cat->getSubcatCount() ),
595 $wgLang->formatNum( $cat->getFileCount() )
599 $date = $wgLang->timeanddate( $timestamp );
601 // link to related articles if supported
603 if( $result->hasRelated() ) {
604 $st = SpecialPage
::getTitleFor( 'Search' );
605 $stParams = array_merge(
606 $this->powerSearchOptions(),
608 'search' => wfMsgForContent( 'searchrelated' ) . ':' . $t->getPrefixedText(),
609 'fulltext' => wfMsg( 'search' )
613 $related = ' -- ' . $sk->linkKnown(
615 wfMsg('search-relatedarticle'),
621 // Include a thumbnail for media files...
622 if( $t->getNamespace() == NS_FILE
) {
623 $img = wfFindFile( $t );
625 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
627 $desc = wfMsg( 'parentheses', $img->getShortDesc() );
628 wfProfileOut( __METHOD__
);
629 // Float doesn't seem to interact well with the bullets.
630 // Table messes up vertical alignment of the bullets.
631 // Bullets are therefore disabled (didn't look great anyway).
633 '<table class="searchResultImage">' .
635 '<td width="120" align="center" valign="top">' .
636 $thumb->toHtml( array( 'desc-link' => true ) ) .
638 '<td valign="top">' .
641 "<div class='mw-search-result-data'>{$score}{$desc} - {$date}{$related}</div>" .
650 wfProfileOut( __METHOD__
);
651 return "<li><div class='mw-search-result-heading'>{$link} {$redirect} {$section}</div> {$extract}\n" .
652 "<div class='mw-search-result-data'>{$score}{$size} - {$date}{$related}</div>" .
658 * Show results from other wikis
660 * @param $matches SearchResultSet
661 * @param $query String
663 protected function showInterwiki( &$matches, $query ) {
665 wfProfileIn( __METHOD__
);
666 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
668 $out = "<div id='mw-search-interwiki'><div id='mw-search-interwiki-caption'>".
669 wfMsg('search-interwiki-caption')."</div>\n";
670 $out .= "<ul class='mw-search-iwresults'>\n";
672 // work out custom project captions
673 $customCaptions = array();
674 $customLines = explode("\n",wfMsg('search-interwiki-custom')); // format per line <iwprefix>:<caption>
675 foreach($customLines as $line) {
676 $parts = explode(":",$line,2);
677 if(count($parts) == 2) // validate line
678 $customCaptions[$parts[0]] = $parts[1];
682 while( $result = $matches->next() ) {
683 $out .= $this->showInterwikiHit( $result, $prev, $terms, $query, $customCaptions );
684 $prev = $result->getInterwikiPrefix();
686 // TODO: should support paging in a non-confusing way (not sure how though, maybe via ajax)..
687 $out .= "</ul></div>\n";
689 // convert the whole thing to desired language variant
690 $out = $wgContLang->convert( $out );
691 wfProfileOut( __METHOD__
);
696 * Show single interwiki link
698 * @param $result SearchResult
699 * @param $lastInterwiki String
700 * @param $terms Array
701 * @param $query String
702 * @param $customCaptions Array: iw prefix -> caption
704 protected function showInterwikiHit( $result, $lastInterwiki, $terms, $query, $customCaptions) {
705 wfProfileIn( __METHOD__
);
707 if( $result->isBrokenTitle() ) {
708 wfProfileOut( __METHOD__
);
709 return "<!-- Broken link in search result -->\n";
712 $t = $result->getTitle();
714 $titleSnippet = $result->getTitleSnippet($terms);
716 if( $titleSnippet == '' )
717 $titleSnippet = null;
719 $link = $this->sk
->linkKnown(
724 // format redirect if any
725 $redirectTitle = $result->getRedirectTitle();
726 $redirectText = $result->getRedirectSnippet($terms);
728 if( !is_null($redirectTitle) ) {
729 if( $redirectText == '' )
730 $redirectText = null;
732 $redirect = "<span class='searchalttitle'>" .
735 $this->sk
->linkKnown(
744 // display project name
745 if(is_null($lastInterwiki) ||
$lastInterwiki != $t->getInterwiki()) {
746 if( key_exists($t->getInterwiki(),$customCaptions) )
747 // captions from 'search-interwiki-custom'
748 $caption = $customCaptions[$t->getInterwiki()];
750 // default is to show the hostname of the other wiki which might suck
751 // if there are many wikis on one hostname
752 $parsed = parse_url($t->getFullURL());
753 $caption = wfMsg('search-interwiki-default', $parsed['host']);
755 // "more results" link (special page stuff could be localized, but we might not know target lang)
756 $searchTitle = Title
::newFromText($t->getInterwiki().":Special:Search");
757 $searchLink = $this->sk
->linkKnown(
759 wfMsg('search-interwiki-more'),
763 'fulltext' => 'Search'
766 $out .= "</ul><div class='mw-search-interwiki-project'><span class='mw-search-interwiki-more'>
767 {$searchLink}</span>{$caption}</div>\n<ul>";
770 $out .= "<li>{$link} {$redirect}</li>\n";
771 wfProfileOut( __METHOD__
);
775 protected function getProfileForm( $profile, $term ) {
778 $opts['redirs'] = $this->searchRedirects
;
779 $opts['profile'] = $this->profile
;
781 if ( $profile === 'advanced' ) {
782 return $this->powerSearchBox( $term, $opts );
785 wfRunHooks( 'SpecialSearchProfileForm', array( $this, &$form, $profile, $term, $opts ) );
791 * Generates the power search box at [[Special:Search]]
793 * @param $term String: search term
794 * @return String: HTML form
796 protected function powerSearchBox( $term, $opts ) {
797 // Groups namespaces into rows according to subject
799 foreach( SearchEngine
::searchableNamespaces() as $namespace => $name ) {
800 $subject = MWNamespace
::getSubject( $namespace );
801 if( !array_key_exists( $subject, $rows ) ) {
802 $rows[$subject] = "";
804 $name = str_replace( '_', ' ', $name );
806 $name = wfMsg( 'blanknamespace' );
810 'td', array( 'style' => 'white-space: nowrap' )
815 "mw-search-ns{$namespace}",
816 in_array( $namespace, $this->namespaces
)
818 Xml
::closeElement( 'td' );
820 $rows = array_values( $rows );
821 $numRows = count( $rows );
823 // Lays out namespaces in multiple floating two-column tables so they'll
824 // be arranged nicely while still accommodating different screen widths
825 $namespaceTables = '';
826 for( $i = 0; $i < $numRows; $i +
= 4 ) {
827 $namespaceTables .= Xml
::openElement(
829 array( 'cellpadding' => 0, 'cellspacing' => 0, 'border' => 0 )
831 for( $j = $i; $j < $i +
4 && $j < $numRows; $j++
) {
832 $namespaceTables .= Xml
::tags( 'tr', null, $rows[$j] );
834 $namespaceTables .= Xml
::closeElement( 'table' );
836 // Show redirects check only if backend supports it
838 if( $this->getSearchEngine()->supports( 'list-redirects' ) ) {
840 Xml
::checkLabel( wfMsg( 'powersearch-redir' ), 'redirs', 'redirs', $this->searchRedirects
);
844 unset( $opts['redirs'] );
845 foreach( $opts as $key => $value ) {
846 $hidden .= Html
::hidden( $key, $value );
848 // Return final output
852 array( 'id' => 'mw-searchoptions', 'style' => 'margin:0em;' )
854 Xml
::element( 'legend', null, wfMsg('powersearch-legend') ) .
855 Xml
::tags( 'h4', null, wfMsgExt( 'powersearch-ns', array( 'parseinline' ) ) ) .
858 array( 'id' => 'mw-search-togglebox' ),
859 Xml
::label( wfMsg( 'powersearch-togglelabel' ), 'mw-search-togglelabel' ) .
864 'id' => 'mw-search-toggleall',
865 'value' => wfMsg( 'powersearch-toggleall' )
872 'id' => 'mw-search-togglenone',
873 'value' => wfMsg( 'powersearch-togglenone' )
877 Xml
::element( 'div', array( 'class' => 'divider' ), '', false ) .
879 Xml
::element( 'div', array( 'class' => 'divider' ), '', false ) .
880 $redirects . $hidden .
881 Xml
::closeElement( 'fieldset' );
884 protected function getSearchProfiles() {
885 // Builds list of Search Types (profiles)
886 $nsAllSet = array_keys( SearchEngine
::searchableNamespaces() );
890 'message' => 'searchprofile-articles',
891 'tooltip' => 'searchprofile-articles-tooltip',
892 'namespaces' => SearchEngine
::defaultNamespaces(),
893 'namespace-messages' => SearchEngine
::namespacesAsText(
894 SearchEngine
::defaultNamespaces()
898 'message' => 'searchprofile-images',
899 'tooltip' => 'searchprofile-images-tooltip',
900 'namespaces' => array( NS_FILE
),
903 'message' => 'searchprofile-project',
904 'tooltip' => 'searchprofile-project-tooltip',
905 'namespaces' => SearchEngine
::helpNamespaces(),
906 'namespace-messages' => SearchEngine
::namespacesAsText(
907 SearchEngine
::helpNamespaces()
911 'message' => 'searchprofile-everything',
912 'tooltip' => 'searchprofile-everything-tooltip',
913 'namespaces' => $nsAllSet,
916 'message' => 'searchprofile-advanced',
917 'tooltip' => 'searchprofile-advanced-tooltip',
918 'namespaces' => self
::NAMESPACES_CURRENT
,
922 wfRunHooks( 'SpecialSearchProfiles', array( &$profiles ) );
924 foreach( $profiles as &$data ) {
925 if ( !is_array( $data['namespaces'] ) ) continue;
926 sort( $data['namespaces'] );
932 protected function formHeader( $term, $resultsShown, $totalNum ) {
935 $out = Xml
::openElement('div', array( 'class' => 'mw-search-formheader' ) );
938 if( $this->startsWithImage( $term ) ) {
940 $bareterm = substr( $term, strpos( $term, ':' ) +
1 );
943 $profiles = $this->getSearchProfiles();
945 // Outputs XML for Search Types
946 $out .= Xml
::openElement( 'div', array( 'class' => 'search-types' ) );
947 $out .= Xml
::openElement( 'ul' );
948 foreach ( $profiles as $id => $profile ) {
949 if ( !isset( $profile['parameters'] ) ) {
950 $profile['parameters'] = array();
952 $profile['parameters']['profile'] = $id;
954 $tooltipParam = isset( $profile['namespace-messages'] ) ?
955 $wgLang->commaList( $profile['namespace-messages'] ) : null;
959 'class' => $this->profile
=== $id ?
'current' : 'normal'
961 $this->makeSearchLink(
964 wfMsg( $profile['message'] ),
965 wfMsg( $profile['tooltip'], $tooltipParam ),
966 $profile['parameters']
970 $out .= Xml
::closeElement( 'ul' );
971 $out .= Xml
::closeElement('div') ;
974 if ( $resultsShown > 0 ) {
975 if ( $totalNum > 0 ){
976 $top = wfMsgExt( 'showingresultsheader', array( 'parseinline' ),
977 $wgLang->formatNum( $this->offset +
1 ),
978 $wgLang->formatNum( $this->offset +
$resultsShown ),
979 $wgLang->formatNum( $totalNum ),
980 wfEscapeWikiText( $term ),
981 $wgLang->formatNum( $resultsShown )
983 } elseif ( $resultsShown >= $this->limit
) {
984 $top = wfShowingResults( $this->offset
, $this->limit
);
986 $top = wfMsgExt( 'showingresultsnum', array( 'parseinline' ),
987 $wgLang->formatNum( $this->limit
),
988 $wgLang->formatNum( $this->offset +
1 ),
989 $wgLang->formatNum( $resultsShown )
992 $out .= Xml
::tags( 'div', array( 'class' => 'results-info' ),
993 Xml
::tags( 'ul', null, Xml
::tags( 'li', null, $top ) )
997 $out .= Xml
::element( 'div', array( 'style' => 'clear:both' ), '', false );
998 $out .= Xml
::closeElement('div');
1003 protected function shortDialog( $term ) {
1004 $out = Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
1006 $out .= Html
::input( 'search', $term, 'search', array(
1007 'id' => $this->profile
=== 'advanced' ?
'powerSearchText' : 'searchText',
1011 $out .= Html
::hidden( 'fulltext', 'Search' ) . "\n";
1012 $out .= Xml
::submitButton( wfMsg( 'searchbutton' ) ) . "\n";
1013 return $out . $this->didYouMeanHtml
;
1017 * Make a search link with some target namespaces
1019 * @param $term String
1020 * @param $namespaces Array ignored
1021 * @param $label String: link's text
1022 * @param $tooltip String: link's tooltip
1023 * @param $params Array: query string parameters
1024 * @return String: HTML fragment
1026 protected function makeSearchLink( $term, $namespaces, $label, $tooltip, $params = array() ) {
1028 foreach( $namespaces as $n ) {
1029 $opt['ns' . $n] = 1;
1031 $opt['redirs'] = $this->searchRedirects
;
1033 $stParams = array_merge(
1036 'fulltext' => wfMsg( 'search' )
1041 return Xml
::element(
1044 'href' => $this->getTitle()->getLocalURL( $stParams ),
1045 'title' => $tooltip),
1051 * Check if query starts with image: prefix
1053 * @param $term String: the string to check
1056 protected function startsWithImage( $term ) {
1059 $p = explode( ':', $term );
1060 if( count( $p ) > 1 ) {
1061 return $wgContLang->getNsIndex( $p[0] ) == NS_FILE
;
1067 * Check if query starts with all: prefix
1069 * @param $term String: the string to check
1072 protected function startsWithAll( $term ) {
1074 $allkeyword = wfMsgForContent('searchall');
1076 $p = explode( ':', $term );
1077 if( count( $p ) > 1 ) {
1078 return $p[0] == $allkeyword;
1086 public function getSearchEngine() {
1087 if ( $this->searchEngine
=== null ) {
1088 $this->searchEngine
= SearchEngine
::create();
1090 return $this->searchEngine
;
1094 * Users of hook SpecialSearchSetupEngine can use this to
1095 * add more params to links to not lose selection when
1096 * user navigates search results.
1099 public function setExtraParam( $key, $value ) {
1100 $this->extraParams
[$key] = $value;