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;
37 const NAMESPACES_CURRENT
= 'sense';
39 public function __construct() {
40 parent
::__construct( 'Search' );
46 * @param $par String or null
48 public function execute( $par ) {
49 global $wgRequest, $wgUser, $wgOut;
52 $this->outputHeader();
53 $wgOut->allowClickjacking();
54 $wgOut->addModuleStyles( 'mediawiki.special' );
56 // Strip underscores from title parameter; most of the time we'll want
57 // text form here. But don't strip underscores from actual text params!
58 $titleParam = str_replace( '_', ' ', $par );
60 // Fetch the search term
61 $search = str_replace( "\n", " ", $wgRequest->getText( 'search', $titleParam ) );
63 $this->load( $wgRequest, $wgUser );
65 if ( $wgRequest->getVal( 'fulltext' )
66 ||
!is_null( $wgRequest->getVal( 'offset' ) )
67 ||
!is_null( $wgRequest->getVal( 'searchx' ) ) )
69 $this->showResults( $search );
71 $this->goResult( $search );
76 * Set up basic search parameters from the request and user settings.
77 * Typically you'll pass $wgRequest and $wgUser.
79 * @param $request WebRequest
82 public function load( &$request, &$user ) {
83 list( $this->limit
, $this->offset
) = $request->getLimitOffset( 20, 'searchlimit' );
84 $this->mPrefix
= $request->getVal('prefix', '');
87 # Extract manually requested namespaces
88 $nslist = $this->powerSearch( $request );
89 $this->profile
= $profile = $request->getVal( 'profile', null );
90 $profiles = $this->getSearchProfiles();
91 if ( $profile === null) {
92 // BC with old request format
93 $this->profile
= 'advanced';
94 if ( count( $nslist ) ) {
95 foreach( $profiles as $key => $data ) {
96 if ( $nslist === $data['namespaces'] && $key !== 'advanced') {
97 $this->profile
= $key;
100 $this->namespaces
= $nslist;
102 $this->namespaces
= SearchEngine
::userNamespaces( $user );
104 } elseif ( $profile === 'advanced' ) {
105 $this->namespaces
= $nslist;
107 if ( isset( $profiles[$profile]['namespaces'] ) ) {
108 $this->namespaces
= $profiles[$profile]['namespaces'];
110 // Unknown profile requested
111 $this->profile
= 'default';
112 $this->namespaces
= $profiles['default']['namespaces'];
116 // Redirects defaults to true, but we don't know whether it was ticked of or just missing
117 $default = $request->getBool( 'profile' ) ?
0 : 1;
118 $this->searchRedirects
= $request->getBool( 'redirs', $default ) ?
1 : 0;
119 $this->sk
= $user->getSkin();
120 $this->didYouMeanHtml
= ''; # html of did you mean... link
121 $this->fulltext
= $request->getVal('fulltext');
125 * If an exact title match can be found, jump straight ahead to it.
127 * @param $term String
129 public function goResult( $term ) {
131 $this->setupPage( $term );
132 # Try to go to page as entered.
133 $t = Title
::newFromText( $term );
134 # If the string cannot be used to create a title
135 if( is_null( $t ) ) {
136 return $this->showResults( $term );
138 # If there's an exact or very near match, jump right there.
139 $t = SearchEngine
::getNearMatch( $term );
141 if ( !wfRunHooks( 'SpecialSearchGo', array( &$t, &$term ) ) ) {
142 # Hook requested termination
146 if( !is_null( $t ) ) {
147 $wgOut->redirect( $t->getFullURL() );
150 # No match, generate an edit URL
151 $t = Title
::newFromText( $term );
152 if( !is_null( $t ) ) {
154 wfRunHooks( 'SpecialSearchNogomatch', array( &$t ) );
155 wfDebugLog( 'nogomatch', $t->getText(), false );
157 # If the feature is enabled, go straight to the edit page
159 $wgOut->redirect( $t->getFullURL( array( 'action' => 'edit' ) ) );
163 return $this->showResults( $term );
167 * @param $term String
169 public function showResults( $term ) {
170 global $wgOut, $wgUser, $wgDisableTextSearch, $wgContLang, $wgScript;
171 wfProfileIn( __METHOD__
);
173 $sk = $wgUser->getSkin();
175 $search = $this->getSearchEngine();
176 $search->setLimitOffset( $this->limit
, $this->offset
);
177 $search->setNamespaces( $this->namespaces
);
178 $search->showRedirects
= $this->searchRedirects
; // BC
179 $search->setFeatureData( 'list-redirects', $this->searchRedirects
);
180 $search->prefix
= $this->mPrefix
;
181 $term = $search->transformSearchTerm($term);
183 wfRunHooks( 'SpecialSearchSetupEngine', array( $this, $this->profile
, $search ) );
185 $this->setupPage( $term );
187 if( $wgDisableTextSearch ) {
188 global $wgSearchForwardUrl;
189 if( $wgSearchForwardUrl ) {
190 $url = str_replace( '$1', urlencode( $term ), $wgSearchForwardUrl );
191 $wgOut->redirect( $url );
192 wfProfileOut( __METHOD__
);
195 global $wgInputEncoding;
197 Xml
::openElement( 'fieldset' ) .
198 Xml
::element( 'legend', null, wfMsg( 'search-external' ) ) .
199 Xml
::element( 'p', array( 'class' => 'mw-searchdisabled' ), wfMsg( 'searchdisabled' ) ) .
200 wfMsg( 'googlesearch',
201 htmlspecialchars( $term ),
202 htmlspecialchars( $wgInputEncoding ),
203 htmlspecialchars( wfMsg( 'searchbutton' ) )
205 Xml
::closeElement( 'fieldset' )
207 wfProfileOut( __METHOD__
);
211 $t = Title
::newFromText( $term );
213 // fetch search results
214 $rewritten = $search->replacePrefixes($term);
216 $titleMatches = $search->searchTitle( $rewritten );
217 if( !($titleMatches instanceof SearchResultTooMany
))
218 $textMatches = $search->searchText( $rewritten );
220 // did you mean... suggestions
221 if( $textMatches && $textMatches->hasSuggestion() ) {
222 $st = SpecialPage
::getTitleFor( 'Search' );
224 # mirror Go/Search behaviour of original request ..
225 $didYouMeanParams = array( 'search' => $textMatches->getSuggestionQuery() );
227 if($this->fulltext
!= null)
228 $didYouMeanParams['fulltext'] = $this->fulltext
;
230 $stParams = array_merge(
232 $this->powerSearchOptions()
235 $suggestionSnippet = $textMatches->getSuggestionSnippet();
237 if( $suggestionSnippet == '' )
238 $suggestionSnippet = null;
240 $suggestLink = $sk->linkKnown(
247 $this->didYouMeanHtml
= '<div class="searchdidyoumean">'.wfMsg('search-suggest',$suggestLink).'</div>';
249 // start rendering the page
254 'id' => ( $this->profile
=== 'advanced' ?
'powersearch' : 'search' ),
256 'action' => $wgScript
261 Xml
::openElement( 'table', array( 'id'=>'mw-search-top-table', 'border'=>0, 'cellpadding'=>0, 'cellspacing'=>0 ) ) .
262 Xml
::openElement( 'tr' ) .
263 Xml
::openElement( 'td' ) . "\n" .
264 $this->shortDialog( $term ) .
265 Xml
::closeElement('td') .
266 Xml
::closeElement('tr') .
267 Xml
::closeElement('table')
270 // Sometimes the search engine knows there are too many hits
271 if( $titleMatches instanceof SearchResultTooMany
) {
272 $wgOut->wrapWikiMsg( "==$1==\n", 'toomanymatches' );
273 wfProfileOut( __METHOD__
);
277 $filePrefix = $wgContLang->getFormattedNsText(NS_FILE
).':';
278 if( trim( $term ) === '' ||
$filePrefix === trim( $term ) ) {
279 $wgOut->addHTML( $this->formHeader( $term, 0, 0 ) );
280 $wgOut->addHtml( $this->getProfileForm( $this->profile
, $term ) );
281 $wgOut->addHTML( '</form>' );
282 // Empty query -- straight view of search form
283 wfProfileOut( __METHOD__
);
287 // Get number of results
288 $titleMatchesNum = $titleMatches ?
$titleMatches->numRows() : 0;
289 $textMatchesNum = $textMatches ?
$textMatches->numRows() : 0;
290 // Total initial query matches (possible false positives)
291 $num = $titleMatchesNum +
$textMatchesNum;
293 // Get total actual results (after second filtering, if any)
294 $numTitleMatches = $titleMatches && !is_null( $titleMatches->getTotalHits() ) ?
295 $titleMatches->getTotalHits() : $titleMatchesNum;
296 $numTextMatches = $textMatches && !is_null( $textMatches->getTotalHits() ) ?
297 $textMatches->getTotalHits() : $textMatchesNum;
299 // get total number of results if backend can calculate it
301 if($titleMatches && !is_null( $titleMatches->getTotalHits() ) )
302 $totalRes +
= $titleMatches->getTotalHits();
303 if($textMatches && !is_null( $textMatches->getTotalHits() ))
304 $totalRes +
= $textMatches->getTotalHits();
306 // show number of results and current offset
307 $wgOut->addHTML( $this->formHeader( $term, $num, $totalRes ) );
308 $wgOut->addHtml( $this->getProfileForm( $this->profile
, $term ) );
311 $wgOut->addHtml( Xml
::closeElement( 'form' ) );
312 $wgOut->addHtml( "<div class='searchresults'>" );
315 if( $num ||
$this->offset
) {
316 // Show the create link ahead
317 $this->showCreateLink( $t );
318 $prevnext = wfViewPrevNext( $this->offset
, $this->limit
,
319 SpecialPage
::getTitleFor( 'Search' ),
320 wfArrayToCGI( $this->powerSearchOptions(), array( 'search' => $term ) ),
321 max( $titleMatchesNum, $textMatchesNum ) < $this->limit
323 //$wgOut->addHTML( "<p class='mw-search-pager-top'>{$prevnext}</p>\n" );
324 wfRunHooks( 'SpecialSearchResults', array( $term, &$titleMatches, &$textMatches ) );
326 wfRunHooks( 'SpecialSearchNoResults', array( $term ) );
329 $wgOut->parserOptions()->setEditSection( false );
330 if( $titleMatches ) {
331 if( $numTitleMatches > 0 ) {
332 $wgOut->wrapWikiMsg( "==$1==\n", 'titlematches' );
333 $wgOut->addHTML( $this->showMatches( $titleMatches ) );
335 $titleMatches->free();
338 // output appropriate heading
339 if( $numTextMatches > 0 && $numTitleMatches > 0 ) {
340 // if no title matches the heading is redundant
341 $wgOut->wrapWikiMsg( "==$1==\n", 'textmatches' );
342 } elseif( $totalRes == 0 ) {
343 # Don't show the 'no text matches' if we received title matches
344 # $wgOut->wrapWikiMsg( "==$1==\n", 'notextmatches' );
346 // show interwiki results if any
347 if( $textMatches->hasInterwikiResults() ) {
348 $wgOut->addHTML( $this->showInterwiki( $textMatches->getInterwikiResults(), $term ) );
351 if( $numTextMatches > 0 ) {
352 $wgOut->addHTML( $this->showMatches( $textMatches ) );
355 $textMatches->free();
358 $wgOut->wrapWikiMsg( "<p class=\"mw-search-nonefound\">\n$1</p>", array( 'search-nonefound', wfEscapeWikiText( $term ) ) );
359 $this->showCreateLink( $t );
361 $wgOut->addHtml( "</div>" );
363 if( $num ||
$this->offset
) {
364 $wgOut->addHTML( "<p class='mw-search-pager-bottom'>{$prevnext}</p>\n" );
366 wfProfileOut( __METHOD__
);
369 protected function showCreateLink( $t ) {
372 // show direct page/create link if applicable
375 if( $t->isKnown() ) {
376 $messageName = 'searchmenu-exists';
377 } elseif( $t->userCan( 'create' ) ) {
378 $messageName = 'searchmenu-new';
380 $messageName = 'searchmenu-new-nocreate';
384 $wgOut->wrapWikiMsg( "<p class=\"mw-search-createlink\">\n$1</p>", array( $messageName, wfEscapeWikiText( $t->getPrefixedText() ) ) );
386 // preserve the paragraph for margins etc...
387 $wgOut->addHtml( '<p></p>' );
394 protected function setupPage( $term ) {
397 # Should advanced UI be used?
398 $this->searchAdvanced
= ($this->profile
=== 'advanced');
399 if( strval( $term ) !== '' ) {
400 $wgOut->setPageTitle( wfMsg( 'searchresults') );
401 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'searchresults-title', $term ) ) );
403 // add javascript specific to special:search
404 $wgOut->addModules( 'mediawiki.legacy.search' );
405 $wgOut->addModules( 'mediawiki.special.search' );
409 * Extract "power search" namespace settings from the request object,
410 * returning a list of index numbers to search.
412 * @param $request WebRequest
415 protected function powerSearch( &$request ) {
417 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
418 if( $request->getCheck( 'ns' . $ns ) ) {
427 * Reconstruct the 'power search' options for links
431 protected function powerSearchOptions() {
433 foreach( $this->namespaces
as $n ) {
436 $opt['redirs'] = $this->searchRedirects ?
1 : 0;
437 if( $this->profile
) {
438 $opt['profile'] = $this->profile
;
444 * Show whole set of results
446 * @param $matches SearchResultSet
448 protected function showMatches( &$matches ) {
450 wfProfileIn( __METHOD__
);
452 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
455 $infoLine = $matches->getInfo();
456 if( !is_null($infoLine) ) {
457 $out .= "\n<!-- {$infoLine} -->\n";
459 $out .= "<ul class='mw-search-results'>\n";
460 while( $result = $matches->next() ) {
461 $out .= $this->showHit( $result, $terms );
465 // convert the whole thing to desired language variant
466 $out = $wgContLang->convert( $out );
467 wfProfileOut( __METHOD__
);
472 * Format a single hit result
474 * @param $result SearchResult
475 * @param $terms Array: terms to highlight
477 protected function showHit( $result, $terms ) {
478 global $wgLang, $wgUser;
479 wfProfileIn( __METHOD__
);
481 if( $result->isBrokenTitle() ) {
482 wfProfileOut( __METHOD__
);
483 return "<!-- Broken link in search result -->\n";
486 $sk = $wgUser->getSkin();
487 $t = $result->getTitle();
489 $titleSnippet = $result->getTitleSnippet($terms);
491 if( $titleSnippet == '' )
492 $titleSnippet = null;
496 wfRunHooks( 'ShowSearchHitTitle',
497 array( &$link_t, &$titleSnippet, $result, $terms, $this ) );
499 $link = $this->sk
->linkKnown(
504 //If page content is not readable, just return the title.
505 //This is not quite safe, but better than showing excerpts from non-readable pages
506 //Note that hiding the entry entirely would screw up paging.
507 if( !$t->userCanRead() ) {
508 wfProfileOut( __METHOD__
);
509 return "<li>{$link}</li>\n";
512 // If the page doesn't *exist*... our search index is out of date.
513 // The least confusing at this point is to drop the result.
514 // You may get less results, but... oh well. :P
515 if( $result->isMissingRevision() ) {
516 wfProfileOut( __METHOD__
);
517 return "<!-- missing page " . htmlspecialchars( $t->getPrefixedText() ) . "-->\n";
520 // format redirects / relevant sections
521 $redirectTitle = $result->getRedirectTitle();
522 $redirectText = $result->getRedirectSnippet($terms);
523 $sectionTitle = $result->getSectionTitle();
524 $sectionText = $result->getSectionSnippet($terms);
527 if( !is_null($redirectTitle) ) {
528 if( $redirectText == '' )
529 $redirectText = null;
531 $redirect = "<span class='searchalttitle'>" .
534 $this->sk
->linkKnown(
544 if( !is_null($sectionTitle) ) {
545 if( $sectionText == '' )
548 $section = "<span class='searchalttitle'>" .
550 'search-section', $this->sk
->linkKnown(
558 // format text extract
559 $extract = "<div class='searchresult'>".$result->getTextSnippet($terms)."</div>";
562 if( is_null( $result->getScore() ) ) {
563 // Search engine doesn't report scoring info
566 $percent = sprintf( '%2.1f', $result->getScore() * 100 );
567 $score = wfMsg( 'search-result-score', $wgLang->formatNum( $percent ) )
571 // format description
572 $byteSize = $result->getByteSize();
573 $wordCount = $result->getWordCount();
574 $timestamp = $result->getTimestamp();
576 'search-result-size',
577 array( 'parsemag', 'escape' ),
578 $this->sk
->formatSize( $byteSize ),
579 $wgLang->formatNum( $wordCount )
582 if( $t->getNamespace() == NS_CATEGORY
) {
583 $cat = Category
::newFromTitle( $t );
585 'search-result-category-size',
586 array( 'parsemag', 'escape' ),
587 $wgLang->formatNum( $cat->getPageCount() ),
588 $wgLang->formatNum( $cat->getSubcatCount() ),
589 $wgLang->formatNum( $cat->getFileCount() )
593 $date = $wgLang->timeanddate( $timestamp );
595 // link to related articles if supported
597 if( $result->hasRelated() ) {
598 $st = SpecialPage
::getTitleFor( 'Search' );
599 $stParams = array_merge(
600 $this->powerSearchOptions(),
602 'search' => wfMsgForContent( 'searchrelated' ) . ':' . $t->getPrefixedText(),
603 'fulltext' => wfMsg( 'search' )
607 $related = ' -- ' . $sk->linkKnown(
609 wfMsg('search-relatedarticle'),
615 // Include a thumbnail for media files...
616 if( $t->getNamespace() == NS_FILE
) {
617 $img = wfFindFile( $t );
619 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
621 $desc = wfMsg( 'parentheses', $img->getShortDesc() );
622 wfProfileOut( __METHOD__
);
623 // Float doesn't seem to interact well with the bullets.
624 // Table messes up vertical alignment of the bullets.
625 // Bullets are therefore disabled (didn't look great anyway).
627 '<table class="searchResultImage">' .
629 '<td width="120" align="center" valign="top">' .
630 $thumb->toHtml( array( 'desc-link' => true ) ) .
632 '<td valign="top">' .
635 "<div class='mw-search-result-data'>{$score}{$desc} - {$date}{$related}</div>" .
644 wfProfileOut( __METHOD__
);
645 return "<li><div class='mw-search-result-heading'>{$link} {$redirect} {$section}</div> {$extract}\n" .
646 "<div class='mw-search-result-data'>{$score}{$size} - {$date}{$related}</div>" .
652 * Show results from other wikis
654 * @param $matches SearchResultSet
655 * @param $query String
657 protected function showInterwiki( &$matches, $query ) {
659 wfProfileIn( __METHOD__
);
660 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
662 $out = "<div id='mw-search-interwiki'><div id='mw-search-interwiki-caption'>".
663 wfMsg('search-interwiki-caption')."</div>\n";
664 $out .= "<ul class='mw-search-iwresults'>\n";
666 // work out custom project captions
667 $customCaptions = array();
668 $customLines = explode("\n",wfMsg('search-interwiki-custom')); // format per line <iwprefix>:<caption>
669 foreach($customLines as $line) {
670 $parts = explode(":",$line,2);
671 if(count($parts) == 2) // validate line
672 $customCaptions[$parts[0]] = $parts[1];
676 while( $result = $matches->next() ) {
677 $out .= $this->showInterwikiHit( $result, $prev, $terms, $query, $customCaptions );
678 $prev = $result->getInterwikiPrefix();
680 // TODO: should support paging in a non-confusing way (not sure how though, maybe via ajax)..
681 $out .= "</ul></div>\n";
683 // convert the whole thing to desired language variant
684 $out = $wgContLang->convert( $out );
685 wfProfileOut( __METHOD__
);
690 * Show single interwiki link
692 * @param $result SearchResult
693 * @param $lastInterwiki String
694 * @param $terms Array
695 * @param $query String
696 * @param $customCaptions Array: iw prefix -> caption
698 protected function showInterwikiHit( $result, $lastInterwiki, $terms, $query, $customCaptions) {
699 wfProfileIn( __METHOD__
);
701 if( $result->isBrokenTitle() ) {
702 wfProfileOut( __METHOD__
);
703 return "<!-- Broken link in search result -->\n";
706 $t = $result->getTitle();
708 $titleSnippet = $result->getTitleSnippet($terms);
710 if( $titleSnippet == '' )
711 $titleSnippet = null;
713 $link = $this->sk
->linkKnown(
718 // format redirect if any
719 $redirectTitle = $result->getRedirectTitle();
720 $redirectText = $result->getRedirectSnippet($terms);
722 if( !is_null($redirectTitle) ) {
723 if( $redirectText == '' )
724 $redirectText = null;
726 $redirect = "<span class='searchalttitle'>" .
729 $this->sk
->linkKnown(
738 // display project name
739 if(is_null($lastInterwiki) ||
$lastInterwiki != $t->getInterwiki()) {
740 if( key_exists($t->getInterwiki(),$customCaptions) )
741 // captions from 'search-interwiki-custom'
742 $caption = $customCaptions[$t->getInterwiki()];
744 // default is to show the hostname of the other wiki which might suck
745 // if there are many wikis on one hostname
746 $parsed = parse_url($t->getFullURL());
747 $caption = wfMsg('search-interwiki-default', $parsed['host']);
749 // "more results" link (special page stuff could be localized, but we might not know target lang)
750 $searchTitle = Title
::newFromText($t->getInterwiki().":Special:Search");
751 $searchLink = $this->sk
->linkKnown(
753 wfMsg('search-interwiki-more'),
757 'fulltext' => 'Search'
760 $out .= "</ul><div class='mw-search-interwiki-project'><span class='mw-search-interwiki-more'>
761 {$searchLink}</span>{$caption}</div>\n<ul>";
764 $out .= "<li>{$link} {$redirect}</li>\n";
765 wfProfileOut( __METHOD__
);
769 protected function getProfileForm( $profile, $term ) {
772 $opts['redirs'] = $this->searchRedirects
;
773 $opts['profile'] = $this->profile
;
775 if ( $profile === 'advanced' ) {
776 return $this->powerSearchBox( $term, $opts );
779 wfRunHooks( 'SpecialSearchProfileForm', array( $this, &$form, $profile, $term, $opts ) );
785 * Generates the power search box at [[Special:Search]]
787 * @param $term String: search term
788 * @return String: HTML form
790 protected function powerSearchBox( $term, $opts ) {
791 // Groups namespaces into rows according to subject
793 foreach( SearchEngine
::searchableNamespaces() as $namespace => $name ) {
794 $subject = MWNamespace
::getSubject( $namespace );
795 if( !array_key_exists( $subject, $rows ) ) {
796 $rows[$subject] = "";
798 $name = str_replace( '_', ' ', $name );
800 $name = wfMsg( 'blanknamespace' );
804 'td', array( 'style' => 'white-space: nowrap' )
809 "mw-search-ns{$namespace}",
810 in_array( $namespace, $this->namespaces
)
812 Xml
::closeElement( 'td' );
814 $rows = array_values( $rows );
815 $numRows = count( $rows );
817 // Lays out namespaces in multiple floating two-column tables so they'll
818 // be arranged nicely while still accommodating different screen widths
819 $namespaceTables = '';
820 for( $i = 0; $i < $numRows; $i +
= 4 ) {
821 $namespaceTables .= Xml
::openElement(
823 array( 'cellpadding' => 0, 'cellspacing' => 0, 'border' => 0 )
825 for( $j = $i; $j < $i +
4 && $j < $numRows; $j++
) {
826 $namespaceTables .= Xml
::tags( 'tr', null, $rows[$j] );
828 $namespaceTables .= Xml
::closeElement( 'table' );
830 // Show redirects check only if backend supports it
832 if( $this->getSearchEngine()->supports( 'list-redirects' ) ) {
834 Xml
::checkLabel( wfMsg( 'powersearch-redir' ), 'redirs', 'redirs', $this->searchRedirects
);
838 unset( $opts['redirs'] );
839 foreach( $opts as $key => $value ) {
840 $hidden .= Html
::hidden( $key, $value );
842 // Return final output
846 array( 'id' => 'mw-searchoptions', 'style' => 'margin:0em;' )
848 Xml
::element( 'legend', null, wfMsg('powersearch-legend') ) .
849 Xml
::tags( 'h4', null, wfMsgExt( 'powersearch-ns', array( 'parseinline' ) ) ) .
852 array( 'id' => 'mw-search-togglebox' ),
853 Xml
::label( wfMsg( 'powersearch-togglelabel' ), 'mw-search-togglelabel' ) .
858 'id' => 'mw-search-toggleall',
859 'onclick' => 'mwToggleSearchCheckboxes("all");',
860 'value' => wfMsg( 'powersearch-toggleall' )
867 'id' => 'mw-search-togglenone',
868 'onclick' => 'mwToggleSearchCheckboxes("none");',
869 'value' => wfMsg( 'powersearch-togglenone' )
873 Xml
::element( 'div', array( 'class' => 'divider' ), '', false ) .
875 Xml
::element( 'div', array( 'class' => 'divider' ), '', false ) .
876 $redirects . $hidden .
877 Xml
::closeElement( 'fieldset' );
880 protected function getSearchProfiles() {
881 // Builds list of Search Types (profiles)
882 $nsAllSet = array_keys( SearchEngine
::searchableNamespaces() );
886 'message' => 'searchprofile-articles',
887 'tooltip' => 'searchprofile-articles-tooltip',
888 'namespaces' => SearchEngine
::defaultNamespaces(),
889 'namespace-messages' => SearchEngine
::namespacesAsText(
890 SearchEngine
::defaultNamespaces()
894 'message' => 'searchprofile-images',
895 'tooltip' => 'searchprofile-images-tooltip',
896 'namespaces' => array( NS_FILE
),
899 'message' => 'searchprofile-project',
900 'tooltip' => 'searchprofile-project-tooltip',
901 'namespaces' => SearchEngine
::helpNamespaces(),
902 'namespace-messages' => SearchEngine
::namespacesAsText(
903 SearchEngine
::helpNamespaces()
907 'message' => 'searchprofile-everything',
908 'tooltip' => 'searchprofile-everything-tooltip',
909 'namespaces' => $nsAllSet,
912 'message' => 'searchprofile-advanced',
913 'tooltip' => 'searchprofile-advanced-tooltip',
914 'namespaces' => self
::NAMESPACES_CURRENT
,
918 wfRunHooks( 'SpecialSearchProfiles', array( &$profiles ) );
920 foreach( $profiles as &$data ) {
921 if ( !is_array( $data['namespaces'] ) ) continue;
922 sort( $data['namespaces'] );
928 protected function formHeader( $term, $resultsShown, $totalNum ) {
931 $out = Xml
::openElement('div', array( 'class' => 'mw-search-formheader' ) );
934 if( $this->startsWithImage( $term ) ) {
936 $bareterm = substr( $term, strpos( $term, ':' ) +
1 );
939 $profiles = $this->getSearchProfiles();
941 // Outputs XML for Search Types
942 $out .= Xml
::openElement( 'div', array( 'class' => 'search-types' ) );
943 $out .= Xml
::openElement( 'ul' );
944 foreach ( $profiles as $id => $profile ) {
945 if ( !isset( $profile['parameters'] ) ) {
946 $profile['parameters'] = array();
948 $profile['parameters']['profile'] = $id;
950 $tooltipParam = isset( $profile['namespace-messages'] ) ?
951 $wgLang->commaList( $profile['namespace-messages'] ) : null;
955 'class' => $this->profile
=== $id ?
'current' : 'normal'
957 $this->makeSearchLink(
960 wfMsg( $profile['message'] ),
961 wfMsg( $profile['tooltip'], $tooltipParam ),
962 $profile['parameters']
966 $out .= Xml
::closeElement( 'ul' );
967 $out .= Xml
::closeElement('div') ;
970 if ( $resultsShown > 0 ) {
971 if ( $totalNum > 0 ){
972 $top = wfMsgExt( 'showingresultsheader', array( 'parseinline' ),
973 $wgLang->formatNum( $this->offset +
1 ),
974 $wgLang->formatNum( $this->offset +
$resultsShown ),
975 $wgLang->formatNum( $totalNum ),
976 wfEscapeWikiText( $term ),
977 $wgLang->formatNum( $resultsShown )
979 } elseif ( $resultsShown >= $this->limit
) {
980 $top = wfShowingResults( $this->offset
, $this->limit
);
982 $top = wfShowingResultsNum( $this->offset
, $this->limit
, $resultsShown );
984 $out .= Xml
::tags( 'div', array( 'class' => 'results-info' ),
985 Xml
::tags( 'ul', null, Xml
::tags( 'li', null, $top ) )
989 $out .= Xml
::element( 'div', array( 'style' => 'clear:both' ), '', false );
990 $out .= Xml
::closeElement('div');
995 protected function shortDialog( $term ) {
996 $out = Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
998 $out .= Html
::input( 'search', $term, 'search', array(
999 'id' => $this->profile
=== 'advanced' ?
'powerSearchText' : 'searchText',
1003 $out .= Html
::hidden( 'fulltext', 'Search' ) . "\n";
1004 $out .= Xml
::submitButton( wfMsg( 'searchbutton' ) ) . "\n";
1005 return $out . $this->didYouMeanHtml
;
1009 * Make a search link with some target namespaces
1011 * @param $term String
1012 * @param $namespaces Array ignored
1013 * @param $label String: link's text
1014 * @param $tooltip String: link's tooltip
1015 * @param $params Array: query string parameters
1016 * @return String: HTML fragment
1018 protected function makeSearchLink( $term, $namespaces, $label, $tooltip, $params = array() ) {
1020 foreach( $namespaces as $n ) {
1021 $opt['ns' . $n] = 1;
1023 $opt['redirs'] = $this->searchRedirects
;
1025 $stParams = array_merge(
1028 'fulltext' => wfMsg( 'search' )
1033 return Xml
::element(
1036 'href' => $this->getTitle()->getLocalURL( $stParams ),
1037 'title' => $tooltip,
1038 'onmousedown' => 'mwSearchHeaderClick(this);',
1039 'onkeydown' => 'mwSearchHeaderClick(this);'),
1045 * Check if query starts with image: prefix
1047 * @param $term String: the string to check
1050 protected function startsWithImage( $term ) {
1053 $p = explode( ':', $term );
1054 if( count( $p ) > 1 ) {
1055 return $wgContLang->getNsIndex( $p[0] ) == NS_FILE
;
1061 * Check if query starts with all: prefix
1063 * @param $term String: the string to check
1066 protected function startsWithAll( $term ) {
1068 $allkeyword = wfMsgForContent('searchall');
1070 $p = explode( ':', $term );
1071 if( count( $p ) > 1 ) {
1072 return $p[0] == $allkeyword;
1080 public function getSearchEngine() {
1081 if ( $this->searchEngine
=== null ) {
1082 $this->searchEngine
= SearchEngine
::create();
1084 return $this->searchEngine
;