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' );
86 # Extract manually requested namespaces
87 $nslist = $this->powerSearch( $request );
88 $this->profile
= $profile = $request->getVal( 'profile', null );
89 $profiles = $this->getSearchProfiles();
90 if ( $profile === null) {
91 // BC with old request format
92 $this->profile
= 'advanced';
93 if ( count( $nslist ) ) {
94 foreach( $profiles as $key => $data ) {
95 if ( $nslist === $data['namespaces'] && $key !== 'advanced') {
96 $this->profile
= $key;
99 $this->namespaces
= $nslist;
101 $this->namespaces
= SearchEngine
::userNamespaces( $user );
103 } elseif ( $profile === 'advanced' ) {
104 $this->namespaces
= $nslist;
106 if ( isset( $profiles[$profile]['namespaces'] ) ) {
107 $this->namespaces
= $profiles[$profile]['namespaces'];
109 // Unknown profile requested
110 $this->profile
= 'default';
111 $this->namespaces
= $profiles['default']['namespaces'];
115 // Redirects defaults to true, but we don't know whether it was ticked of or just missing
116 $default = $request->getBool( 'profile' ) ?
0 : 1;
117 $this->searchRedirects
= $request->getBool( 'redirs', $default ) ?
1 : 0;
118 $this->sk
= $user->getSkin();
119 $this->didYouMeanHtml
= ''; # html of did you mean... link
120 $this->fulltext
= $request->getVal('fulltext');
124 * If an exact title match can be found, jump straight ahead to it.
126 * @param $term String
128 public function goResult( $term ) {
130 $this->setupPage( $term );
131 # Try to go to page as entered.
132 $t = Title
::newFromText( $term );
133 # If the string cannot be used to create a title
134 if( is_null( $t ) ) {
135 return $this->showResults( $term );
137 # If there's an exact or very near match, jump right there.
138 $t = SearchEngine
::getNearMatch( $term );
140 if ( !wfRunHooks( 'SpecialSearchGo', array( &$t, &$term ) ) ) {
141 # Hook requested termination
145 if( !is_null( $t ) ) {
146 $wgOut->redirect( $t->getFullURL() );
149 # No match, generate an edit URL
150 $t = Title
::newFromText( $term );
151 if( !is_null( $t ) ) {
153 wfRunHooks( 'SpecialSearchNogomatch', array( &$t ) );
154 wfDebugLog( 'nogomatch', $t->getText(), false );
156 # If the feature is enabled, go straight to the edit page
158 $wgOut->redirect( $t->getFullURL( array( 'action' => 'edit' ) ) );
162 return $this->showResults( $term );
166 * @param $term String
168 public function showResults( $term ) {
169 global $wgOut, $wgUser, $wgDisableTextSearch, $wgContLang, $wgScript;
170 wfProfileIn( __METHOD__
);
172 $sk = $wgUser->getSkin();
174 $search = $this->getSearchEngine();
175 $search->setLimitOffset( $this->limit
, $this->offset
);
176 $search->setNamespaces( $this->namespaces
);
177 $search->showRedirects
= $this->searchRedirects
; // BC
178 $search->setFeatureData( 'list-redirects', $this->searchRedirects
);
179 $term = $search->transformSearchTerm($term);
181 wfRunHooks( 'SpecialSearchSetupEngine', array( $this, $this->profile
, $search ) );
183 $this->setupPage( $term );
185 if( $wgDisableTextSearch ) {
186 global $wgSearchForwardUrl;
187 if( $wgSearchForwardUrl ) {
188 $url = str_replace( '$1', urlencode( $term ), $wgSearchForwardUrl );
189 $wgOut->redirect( $url );
190 wfProfileOut( __METHOD__
);
193 global $wgInputEncoding;
195 Xml
::openElement( 'fieldset' ) .
196 Xml
::element( 'legend', null, wfMsg( 'search-external' ) ) .
197 Xml
::element( 'p', array( 'class' => 'mw-searchdisabled' ), wfMsg( 'searchdisabled' ) ) .
198 wfMsg( 'googlesearch',
199 htmlspecialchars( $term ),
200 htmlspecialchars( $wgInputEncoding ),
201 htmlspecialchars( wfMsg( 'searchbutton' ) )
203 Xml
::closeElement( 'fieldset' )
205 wfProfileOut( __METHOD__
);
209 $t = Title
::newFromText( $term );
211 // fetch search results
212 $rewritten = $search->replacePrefixes($term);
214 $titleMatches = $search->searchTitle( $rewritten );
215 if( !($titleMatches instanceof SearchResultTooMany
))
216 $textMatches = $search->searchText( $rewritten );
218 // did you mean... suggestions
219 if( $textMatches && $textMatches->hasSuggestion() ) {
220 $st = SpecialPage
::getTitleFor( 'Search' );
222 # mirror Go/Search behaviour of original request ..
223 $didYouMeanParams = array( 'search' => $textMatches->getSuggestionQuery() );
225 if($this->fulltext
!= null)
226 $didYouMeanParams['fulltext'] = $this->fulltext
;
228 $stParams = array_merge(
230 $this->powerSearchOptions()
233 $suggestionSnippet = $textMatches->getSuggestionSnippet();
235 if( $suggestionSnippet == '' )
236 $suggestionSnippet = null;
238 $suggestLink = $sk->linkKnown(
245 $this->didYouMeanHtml
= '<div class="searchdidyoumean">'.wfMsg('search-suggest',$suggestLink).'</div>';
247 // start rendering the page
252 'id' => ( $this->profile
=== 'advanced' ?
'powersearch' : 'search' ),
254 'action' => $wgScript
259 Xml
::openElement( 'table', array( 'id'=>'mw-search-top-table', 'border'=>0, 'cellpadding'=>0, 'cellspacing'=>0 ) ) .
260 Xml
::openElement( 'tr' ) .
261 Xml
::openElement( 'td' ) . "\n" .
262 $this->shortDialog( $term ) .
263 Xml
::closeElement('td') .
264 Xml
::closeElement('tr') .
265 Xml
::closeElement('table')
268 // Sometimes the search engine knows there are too many hits
269 if( $titleMatches instanceof SearchResultTooMany
) {
270 $wgOut->wrapWikiMsg( "==$1==\n", 'toomanymatches' );
271 wfProfileOut( __METHOD__
);
275 $filePrefix = $wgContLang->getFormattedNsText(NS_FILE
).':';
276 if( trim( $term ) === '' ||
$filePrefix === trim( $term ) ) {
277 $wgOut->addHTML( $this->formHeader( $term, 0, 0 ) );
278 $wgOut->addHtml( $this->getProfileForm( $this->profile
, $term ) );
279 $wgOut->addHTML( '</form>' );
280 // Empty query -- straight view of search form
281 wfProfileOut( __METHOD__
);
285 // Get number of results
286 $titleMatchesNum = $titleMatches ?
$titleMatches->numRows() : 0;
287 $textMatchesNum = $textMatches ?
$textMatches->numRows() : 0;
288 // Total initial query matches (possible false positives)
289 $num = $titleMatchesNum +
$textMatchesNum;
291 // Get total actual results (after second filtering, if any)
292 $numTitleMatches = $titleMatches && !is_null( $titleMatches->getTotalHits() ) ?
293 $titleMatches->getTotalHits() : $titleMatchesNum;
294 $numTextMatches = $textMatches && !is_null( $textMatches->getTotalHits() ) ?
295 $textMatches->getTotalHits() : $textMatchesNum;
297 // get total number of results if backend can calculate it
299 if($titleMatches && !is_null( $titleMatches->getTotalHits() ) )
300 $totalRes +
= $titleMatches->getTotalHits();
301 if($textMatches && !is_null( $textMatches->getTotalHits() ))
302 $totalRes +
= $textMatches->getTotalHits();
304 // show number of results and current offset
305 $wgOut->addHTML( $this->formHeader( $term, $num, $totalRes ) );
306 $wgOut->addHtml( $this->getProfileForm( $this->profile
, $term ) );
309 $wgOut->addHtml( Xml
::closeElement( 'form' ) );
310 $wgOut->addHtml( "<div class='searchresults'>" );
313 if( $num ||
$this->offset
) {
314 // Show the create link ahead
315 $this->showCreateLink( $t );
316 $prevnext = wfViewPrevNext( $this->offset
, $this->limit
,
317 SpecialPage
::getTitleFor( 'Search' ),
318 wfArrayToCGI( $this->powerSearchOptions(), array( 'search' => $term ) ),
319 max( $titleMatchesNum, $textMatchesNum ) < $this->limit
321 //$wgOut->addHTML( "<p class='mw-search-pager-top'>{$prevnext}</p>\n" );
322 wfRunHooks( 'SpecialSearchResults', array( $term, &$titleMatches, &$textMatches ) );
324 wfRunHooks( 'SpecialSearchNoResults', array( $term ) );
327 $wgOut->parserOptions()->setEditSection( false );
328 if( $titleMatches ) {
329 if( $numTitleMatches > 0 ) {
330 $wgOut->wrapWikiMsg( "==$1==\n", 'titlematches' );
331 $wgOut->addHTML( $this->showMatches( $titleMatches ) );
333 $titleMatches->free();
336 // output appropriate heading
337 if( $numTextMatches > 0 && $numTitleMatches > 0 ) {
338 // if no title matches the heading is redundant
339 $wgOut->wrapWikiMsg( "==$1==\n", 'textmatches' );
340 } elseif( $totalRes == 0 ) {
341 # Don't show the 'no text matches' if we received title matches
342 # $wgOut->wrapWikiMsg( "==$1==\n", 'notextmatches' );
344 // show interwiki results if any
345 if( $textMatches->hasInterwikiResults() ) {
346 $wgOut->addHTML( $this->showInterwiki( $textMatches->getInterwikiResults(), $term ) );
349 if( $numTextMatches > 0 ) {
350 $wgOut->addHTML( $this->showMatches( $textMatches ) );
353 $textMatches->free();
356 $wgOut->wrapWikiMsg( "<p class=\"mw-search-nonefound\">\n$1</p>", array( 'search-nonefound', wfEscapeWikiText( $term ) ) );
357 $this->showCreateLink( $t );
359 $wgOut->addHtml( "</div>" );
361 if( $num ||
$this->offset
) {
362 $wgOut->addHTML( "<p class='mw-search-pager-bottom'>{$prevnext}</p>\n" );
364 wfProfileOut( __METHOD__
);
367 protected function showCreateLink( $t ) {
370 // show direct page/create link if applicable
373 if( $t->isKnown() ) {
374 $messageName = 'searchmenu-exists';
375 } elseif( $t->userCan( 'create' ) ) {
376 $messageName = 'searchmenu-new';
378 $messageName = 'searchmenu-new-nocreate';
382 $wgOut->wrapWikiMsg( "<p class=\"mw-search-createlink\">\n$1</p>", array( $messageName, wfEscapeWikiText( $t->getPrefixedText() ) ) );
384 // preserve the paragraph for margins etc...
385 $wgOut->addHtml( '<p></p>' );
392 protected function setupPage( $term ) {
395 # Should advanced UI be used?
396 $this->searchAdvanced
= ($this->profile
=== 'advanced');
397 if( strval( $term ) !== '' ) {
398 $wgOut->setPageTitle( wfMsg( 'searchresults') );
399 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'searchresults-title', $term ) ) );
401 // add javascript specific to special:search
402 $wgOut->addModules( 'mediawiki.legacy.search' );
403 $wgOut->addModules( 'mediawiki.special.search' );
407 * Extract "power search" namespace settings from the request object,
408 * returning a list of index numbers to search.
410 * @param $request WebRequest
413 protected function powerSearch( &$request ) {
415 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
416 if( $request->getCheck( 'ns' . $ns ) ) {
425 * Reconstruct the 'power search' options for links
429 protected function powerSearchOptions() {
431 $opt['redirs'] = $this->searchRedirects ?
1 : 0;
432 if( $this->profile
!== 'advanced' ) {
433 $opt['profile'] = $this->profile
;
435 foreach( $this->namespaces
as $n ) {
443 * Show whole set of results
445 * @param $matches SearchResultSet
447 protected function showMatches( &$matches ) {
449 wfProfileIn( __METHOD__
);
451 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
454 $infoLine = $matches->getInfo();
455 if( !is_null($infoLine) ) {
456 $out .= "\n<!-- {$infoLine} -->\n";
458 $out .= "<ul class='mw-search-results'>\n";
459 while( $result = $matches->next() ) {
460 $out .= $this->showHit( $result, $terms );
464 // convert the whole thing to desired language variant
465 $out = $wgContLang->convert( $out );
466 wfProfileOut( __METHOD__
);
471 * Format a single hit result
473 * @param $result SearchResult
474 * @param $terms Array: terms to highlight
476 protected function showHit( $result, $terms ) {
477 global $wgLang, $wgUser;
478 wfProfileIn( __METHOD__
);
480 if( $result->isBrokenTitle() ) {
481 wfProfileOut( __METHOD__
);
482 return "<!-- Broken link in search result -->\n";
485 $sk = $wgUser->getSkin();
486 $t = $result->getTitle();
488 $titleSnippet = $result->getTitleSnippet($terms);
490 if( $titleSnippet == '' )
491 $titleSnippet = null;
495 wfRunHooks( 'ShowSearchHitTitle',
496 array( &$link_t, &$titleSnippet, $result, $terms, $this ) );
498 $link = $this->sk
->linkKnown(
503 //If page content is not readable, just return the title.
504 //This is not quite safe, but better than showing excerpts from non-readable pages
505 //Note that hiding the entry entirely would screw up paging.
506 if( !$t->userCanRead() ) {
507 wfProfileOut( __METHOD__
);
508 return "<li>{$link}</li>\n";
511 // If the page doesn't *exist*... our search index is out of date.
512 // The least confusing at this point is to drop the result.
513 // You may get less results, but... oh well. :P
514 if( $result->isMissingRevision() ) {
515 wfProfileOut( __METHOD__
);
516 return "<!-- missing page " . htmlspecialchars( $t->getPrefixedText() ) . "-->\n";
519 // format redirects / relevant sections
520 $redirectTitle = $result->getRedirectTitle();
521 $redirectText = $result->getRedirectSnippet($terms);
522 $sectionTitle = $result->getSectionTitle();
523 $sectionText = $result->getSectionSnippet($terms);
526 if( !is_null($redirectTitle) ) {
527 if( $redirectText == '' )
528 $redirectText = null;
530 $redirect = "<span class='searchalttitle'>" .
533 $this->sk
->linkKnown(
543 if( !is_null($sectionTitle) ) {
544 if( $sectionText == '' )
547 $section = "<span class='searchalttitle'>" .
549 'search-section', $this->sk
->linkKnown(
557 // format text extract
558 $extract = "<div class='searchresult'>".$result->getTextSnippet($terms)."</div>";
561 if( is_null( $result->getScore() ) ) {
562 // Search engine doesn't report scoring info
565 $percent = sprintf( '%2.1f', $result->getScore() * 100 );
566 $score = wfMsg( 'search-result-score', $wgLang->formatNum( $percent ) )
570 // format description
571 $byteSize = $result->getByteSize();
572 $wordCount = $result->getWordCount();
573 $timestamp = $result->getTimestamp();
575 'search-result-size',
576 array( 'parsemag', 'escape' ),
577 $this->sk
->formatSize( $byteSize ),
578 $wgLang->formatNum( $wordCount )
581 if( $t->getNamespace() == NS_CATEGORY
) {
582 $cat = Category
::newFromTitle( $t );
584 'search-result-category-size',
585 array( 'parsemag', 'escape' ),
586 $wgLang->formatNum( $cat->getPageCount() ),
587 $wgLang->formatNum( $cat->getSubcatCount() ),
588 $wgLang->formatNum( $cat->getFileCount() )
592 $date = $wgLang->timeanddate( $timestamp );
594 // link to related articles if supported
596 if( $result->hasRelated() ) {
597 $st = SpecialPage
::getTitleFor( 'Search' );
598 $stParams = array_merge(
599 $this->powerSearchOptions(),
601 'search' => wfMsgForContent( 'searchrelated' ) . ':' . $t->getPrefixedText(),
602 'fulltext' => wfMsg( 'search' )
606 $related = ' -- ' . $sk->linkKnown(
608 wfMsg('search-relatedarticle'),
614 // Include a thumbnail for media files...
615 if( $t->getNamespace() == NS_FILE
) {
616 $img = wfFindFile( $t );
618 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
620 $desc = wfMsg( 'parentheses', $img->getShortDesc() );
621 wfProfileOut( __METHOD__
);
622 // Float doesn't seem to interact well with the bullets.
623 // Table messes up vertical alignment of the bullets.
624 // Bullets are therefore disabled (didn't look great anyway).
626 '<table class="searchResultImage">' .
628 '<td width="120" align="center" valign="top">' .
629 $thumb->toHtml( array( 'desc-link' => true ) ) .
631 '<td valign="top">' .
634 "<div class='mw-search-result-data'>{$score}{$desc} - {$date}{$related}</div>" .
643 wfProfileOut( __METHOD__
);
644 return "<li><div class='mw-search-result-heading'>{$link} {$redirect} {$section}</div> {$extract}\n" .
645 "<div class='mw-search-result-data'>{$score}{$size} - {$date}{$related}</div>" .
651 * Show results from other wikis
653 * @param $matches SearchResultSet
654 * @param $query String
656 protected function showInterwiki( &$matches, $query ) {
658 wfProfileIn( __METHOD__
);
659 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
661 $out = "<div id='mw-search-interwiki'><div id='mw-search-interwiki-caption'>".
662 wfMsg('search-interwiki-caption')."</div>\n";
663 $out .= "<ul class='mw-search-iwresults'>\n";
665 // work out custom project captions
666 $customCaptions = array();
667 $customLines = explode("\n",wfMsg('search-interwiki-custom')); // format per line <iwprefix>:<caption>
668 foreach($customLines as $line) {
669 $parts = explode(":",$line,2);
670 if(count($parts) == 2) // validate line
671 $customCaptions[$parts[0]] = $parts[1];
675 while( $result = $matches->next() ) {
676 $out .= $this->showInterwikiHit( $result, $prev, $terms, $query, $customCaptions );
677 $prev = $result->getInterwikiPrefix();
679 // TODO: should support paging in a non-confusing way (not sure how though, maybe via ajax)..
680 $out .= "</ul></div>\n";
682 // convert the whole thing to desired language variant
683 $out = $wgContLang->convert( $out );
684 wfProfileOut( __METHOD__
);
689 * Show single interwiki link
691 * @param $result SearchResult
692 * @param $lastInterwiki String
693 * @param $terms Array
694 * @param $query String
695 * @param $customCaptions Array: iw prefix -> caption
697 protected function showInterwikiHit( $result, $lastInterwiki, $terms, $query, $customCaptions) {
698 wfProfileIn( __METHOD__
);
700 if( $result->isBrokenTitle() ) {
701 wfProfileOut( __METHOD__
);
702 return "<!-- Broken link in search result -->\n";
705 $t = $result->getTitle();
707 $titleSnippet = $result->getTitleSnippet($terms);
709 if( $titleSnippet == '' )
710 $titleSnippet = null;
712 $link = $this->sk
->linkKnown(
717 // format redirect if any
718 $redirectTitle = $result->getRedirectTitle();
719 $redirectText = $result->getRedirectSnippet($terms);
721 if( !is_null($redirectTitle) ) {
722 if( $redirectText == '' )
723 $redirectText = null;
725 $redirect = "<span class='searchalttitle'>" .
728 $this->sk
->linkKnown(
737 // display project name
738 if(is_null($lastInterwiki) ||
$lastInterwiki != $t->getInterwiki()) {
739 if( key_exists($t->getInterwiki(),$customCaptions) )
740 // captions from 'search-interwiki-custom'
741 $caption = $customCaptions[$t->getInterwiki()];
743 // default is to show the hostname of the other wiki which might suck
744 // if there are many wikis on one hostname
745 $parsed = parse_url($t->getFullURL());
746 $caption = wfMsg('search-interwiki-default', $parsed['host']);
748 // "more results" link (special page stuff could be localized, but we might not know target lang)
749 $searchTitle = Title
::newFromText($t->getInterwiki().":Special:Search");
750 $searchLink = $this->sk
->linkKnown(
752 wfMsg('search-interwiki-more'),
756 'fulltext' => 'Search'
759 $out .= "</ul><div class='mw-search-interwiki-project'><span class='mw-search-interwiki-more'>
760 {$searchLink}</span>{$caption}</div>\n<ul>";
763 $out .= "<li>{$link} {$redirect}</li>\n";
764 wfProfileOut( __METHOD__
);
768 protected function getProfileForm( $profile, $term ) {
771 $opts['redirs'] = $this->searchRedirects
;
772 $opts['profile'] = $this->profile
;
774 if ( $profile === 'advanced' ) {
775 return $this->powerSearchBox( $term, $opts );
778 wfRunHooks( 'SpecialSearchProfileForm', array( $this, &$form, $profile, $term, $opts ) );
784 * Generates the power search box at [[Special:Search]]
786 * @param $term String: search term
787 * @return String: HTML form
789 protected function powerSearchBox( $term, $opts ) {
790 // Groups namespaces into rows according to subject
792 foreach( SearchEngine
::searchableNamespaces() as $namespace => $name ) {
793 $subject = MWNamespace
::getSubject( $namespace );
794 if( !array_key_exists( $subject, $rows ) ) {
795 $rows[$subject] = "";
797 $name = str_replace( '_', ' ', $name );
799 $name = wfMsg( 'blanknamespace' );
803 'td', array( 'style' => 'white-space: nowrap' )
808 "mw-search-ns{$namespace}",
809 in_array( $namespace, $this->namespaces
)
811 Xml
::closeElement( 'td' );
813 $rows = array_values( $rows );
814 $numRows = count( $rows );
816 // Lays out namespaces in multiple floating two-column tables so they'll
817 // be arranged nicely while still accommodating different screen widths
818 $namespaceTables = '';
819 for( $i = 0; $i < $numRows; $i +
= 4 ) {
820 $namespaceTables .= Xml
::openElement(
822 array( 'cellpadding' => 0, 'cellspacing' => 0, 'border' => 0 )
824 for( $j = $i; $j < $i +
4 && $j < $numRows; $j++
) {
825 $namespaceTables .= Xml
::tags( 'tr', null, $rows[$j] );
827 $namespaceTables .= Xml
::closeElement( 'table' );
829 // Show redirects check only if backend supports it
831 if( $this->getSearchEngine()->supports( 'list-redirects' ) ) {
833 Xml
::checkLabel( wfMsg( 'powersearch-redir' ), 'redirs', 'redirs', $this->searchRedirects
);
837 unset( $opts['redirs'] );
838 foreach( $opts as $key => $value ) {
839 $hidden .= Html
::hidden( $key, $value );
841 // Return final output
845 array( 'id' => 'mw-searchoptions', 'style' => 'margin:0em;' )
847 Xml
::element( 'legend', null, wfMsg('powersearch-legend') ) .
848 Xml
::tags( 'h4', null, wfMsgExt( 'powersearch-ns', array( 'parseinline' ) ) ) .
851 array( 'id' => 'mw-search-togglebox' ),
852 Xml
::label( wfMsg( 'powersearch-togglelabel' ), 'mw-search-togglelabel' ) .
857 'id' => 'mw-search-toggleall',
858 'onclick' => 'mwToggleSearchCheckboxes("all");',
859 'value' => wfMsg( 'powersearch-toggleall' )
866 'id' => 'mw-search-togglenone',
867 'onclick' => 'mwToggleSearchCheckboxes("none");',
868 'value' => wfMsg( 'powersearch-togglenone' )
872 Xml
::element( 'div', array( 'class' => 'divider' ), '', false ) .
874 Xml
::element( 'div', array( 'class' => 'divider' ), '', false ) .
875 $redirects . $hidden .
876 Xml
::closeElement( 'fieldset' );
879 protected function getSearchProfiles() {
880 // Builds list of Search Types (profiles)
881 $nsAllSet = array_keys( SearchEngine
::searchableNamespaces() );
885 'message' => 'searchprofile-articles',
886 'tooltip' => 'searchprofile-articles-tooltip',
887 'namespaces' => SearchEngine
::defaultNamespaces(),
888 'namespace-messages' => SearchEngine
::namespacesAsText(
889 SearchEngine
::defaultNamespaces()
893 'message' => 'searchprofile-images',
894 'tooltip' => 'searchprofile-images-tooltip',
895 'namespaces' => array( NS_FILE
),
898 'message' => 'searchprofile-project',
899 'tooltip' => 'searchprofile-project-tooltip',
900 'namespaces' => SearchEngine
::helpNamespaces(),
901 'namespace-messages' => SearchEngine
::namespacesAsText(
902 SearchEngine
::helpNamespaces()
906 'message' => 'searchprofile-everything',
907 'tooltip' => 'searchprofile-everything-tooltip',
908 'namespaces' => $nsAllSet,
911 'message' => 'searchprofile-advanced',
912 'tooltip' => 'searchprofile-advanced-tooltip',
913 'namespaces' => self
::NAMESPACES_CURRENT
,
917 wfRunHooks( 'SpecialSearchProfiles', array( &$profiles ) );
919 foreach( $profiles as &$data ) {
920 if ( !is_array( $data['namespaces'] ) ) continue;
921 sort( $data['namespaces'] );
927 protected function formHeader( $term, $resultsShown, $totalNum ) {
930 $out = Xml
::openElement('div', array( 'class' => 'mw-search-formheader' ) );
933 if( $this->startsWithImage( $term ) ) {
935 $bareterm = substr( $term, strpos( $term, ':' ) +
1 );
938 $profiles = $this->getSearchProfiles();
940 // Outputs XML for Search Types
941 $out .= Xml
::openElement( 'div', array( 'class' => 'search-types' ) );
942 $out .= Xml
::openElement( 'ul' );
943 foreach ( $profiles as $id => $profile ) {
944 if ( !isset( $profile['parameters'] ) ) {
945 $profile['parameters'] = array();
947 $profile['parameters']['profile'] = $id;
949 $tooltipParam = isset( $profile['namespace-messages'] ) ?
950 $wgLang->commaList( $profile['namespace-messages'] ) : null;
954 'class' => $this->profile
=== $id ?
'current' : 'normal'
956 $this->makeSearchLink(
959 wfMsg( $profile['message'] ),
960 wfMsg( $profile['tooltip'], $tooltipParam ),
961 $profile['parameters']
965 $out .= Xml
::closeElement( 'ul' );
966 $out .= Xml
::closeElement('div') ;
969 if ( $resultsShown > 0 ) {
970 if ( $totalNum > 0 ){
971 $top = wfMsgExt( 'showingresultsheader', array( 'parseinline' ),
972 $wgLang->formatNum( $this->offset +
1 ),
973 $wgLang->formatNum( $this->offset +
$resultsShown ),
974 $wgLang->formatNum( $totalNum ),
975 wfEscapeWikiText( $term ),
976 $wgLang->formatNum( $resultsShown )
978 } elseif ( $resultsShown >= $this->limit
) {
979 $top = wfShowingResults( $this->offset
, $this->limit
);
981 $top = wfShowingResultsNum( $this->offset
, $this->limit
, $resultsShown );
983 $out .= Xml
::tags( 'div', array( 'class' => 'results-info' ),
984 Xml
::tags( 'ul', null, Xml
::tags( 'li', null, $top ) )
988 $out .= Xml
::element( 'div', array( 'style' => 'clear:both' ), '', false );
989 $out .= Xml
::closeElement('div');
994 protected function shortDialog( $term ) {
995 $out = Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
997 $out .= Html
::input( 'search', $term, 'search', array(
998 'id' => $this->profile
=== 'advanced' ?
'powerSearchText' : 'searchText',
1002 $out .= Html
::hidden( 'fulltext', 'Search' ) . "\n";
1003 $out .= Xml
::submitButton( wfMsg( 'searchbutton' ) ) . "\n";
1004 return $out . $this->didYouMeanHtml
;
1008 * Make a search link with some target namespaces
1010 * @param $term String
1011 * @param $namespaces Array ignored
1012 * @param $label String: link's text
1013 * @param $tooltip String: link's tooltip
1014 * @param $params Array: query string parameters
1015 * @return String: HTML fragment
1017 protected function makeSearchLink( $term, $namespaces, $label, $tooltip, $params = array() ) {
1019 foreach( $namespaces as $n ) {
1020 $opt['ns' . $n] = 1;
1022 $opt['redirs'] = $this->searchRedirects
;
1024 $stParams = array_merge(
1027 'fulltext' => wfMsg( 'search' )
1032 return Xml
::element(
1035 'href' => $this->getTitle()->getLocalURL( $stParams ),
1036 'title' => $tooltip,
1037 'onmousedown' => 'mwSearchHeaderClick(this);',
1038 'onkeydown' => 'mwSearchHeaderClick(this);'),
1044 * Check if query starts with image: prefix
1046 * @param $term String: the string to check
1049 protected function startsWithImage( $term ) {
1052 $p = explode( ':', $term );
1053 if( count( $p ) > 1 ) {
1054 return $wgContLang->getNsIndex( $p[0] ) == NS_FILE
;
1060 * Check if query starts with all: prefix
1062 * @param $term String: the string to check
1065 protected function startsWithAll( $term ) {
1067 $allkeyword = wfMsgForContent('searchall');
1069 $p = explode( ':', $term );
1070 if( count( $p ) > 1 ) {
1071 return $p[0] == $allkeyword;
1079 public function getSearchEngine() {
1080 if ( $this->searchEngine
=== null ) {
1081 $this->searchEngine
= SearchEngine
::create();
1083 return $this->searchEngine
;