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 public function __construct() {
33 parent
::__construct( 'Search' );
39 * @param $par String or null
41 public function execute( $par ) {
42 global $wgRequest, $wgUser, $wgOut;
45 $this->outputHeader();
46 $wgOut->allowClickjacking();
48 // Strip underscores from title parameter; most of the time we'll want
49 // text form here. But don't strip underscores from actual text params!
50 $titleParam = str_replace( '_', ' ', $par );
52 // Fetch the search term
53 $search = str_replace( "\n", " ", $wgRequest->getText( 'search', $titleParam ) );
55 $this->load( $wgRequest, $wgUser );
57 if ( $wgRequest->getVal( 'fulltext' )
58 ||
!is_null( $wgRequest->getVal( 'offset' ) )
59 ||
!is_null( $wgRequest->getVal( 'searchx' ) ) )
61 $this->showResults( $search );
63 $this->goResult( $search );
68 * Set up basic search parameters from the request and user settings.
69 * Typically you'll pass $wgRequest and $wgUser.
71 * @param $request WebRequest
74 public function load( &$request, &$user ) {
75 list( $this->limit
, $this->offset
) = $request->getLimitOffset( 20, 'searchlimit' );
76 $this->mPrefix
= $request->getVal('prefix', '');
77 # Extract requested namespaces
78 $this->namespaces
= $this->powerSearch( $request );
79 if( empty( $this->namespaces
) ) {
80 $this->namespaces
= SearchEngine
::userNamespaces( $user );
82 $this->searchRedirects
= $request->getCheck( 'redirs' );
83 $this->searchAdvanced
= $request->getVal( 'advanced' );
84 $this->active
= 'advanced';
85 $this->sk
= $user->getSkin();
86 $this->didYouMeanHtml
= ''; # html of did you mean... link
87 $this->fulltext
= $request->getVal('fulltext');
91 * If an exact title match can be found, jump straight ahead to it.
95 public function goResult( $term ) {
97 $this->setupPage( $term );
98 # Try to go to page as entered.
99 $t = Title
::newFromText( $term );
100 # If the string cannot be used to create a title
101 if( is_null( $t ) ) {
102 return $this->showResults( $term );
104 # If there's an exact or very near match, jump right there.
105 $t = SearchEngine
::getNearMatch( $term );
107 if ( !wfRunHooks( 'SpecialSearchGo', array( &$t, &$term ) ) ) {
108 # Hook requested termination
112 if( !is_null( $t ) ) {
113 $wgOut->redirect( $t->getFullURL() );
116 # No match, generate an edit URL
117 $t = Title
::newFromText( $term );
118 if( !is_null( $t ) ) {
120 wfRunHooks( 'SpecialSearchNogomatch', array( &$t ) );
121 wfDebugLog( 'nogomatch', $t->getText(), false );
123 # If the feature is enabled, go straight to the edit page
125 $wgOut->redirect( $t->getFullURL( array( 'action' => 'edit' ) ) );
129 return $this->showResults( $term );
133 * @param $term String
135 public function showResults( $term ) {
136 global $wgOut, $wgUser, $wgDisableTextSearch, $wgContLang, $wgScript;
137 wfProfileIn( __METHOD__
);
139 $sk = $wgUser->getSkin();
141 $this->searchEngine
= SearchEngine
::create();
142 $search =& $this->searchEngine
;
143 $search->setLimitOffset( $this->limit
, $this->offset
);
144 $search->setNamespaces( $this->namespaces
);
145 $search->showRedirects
= $this->searchRedirects
;
146 $search->prefix
= $this->mPrefix
;
147 $term = $search->transformSearchTerm($term);
149 $this->setupPage( $term );
151 if( $wgDisableTextSearch ) {
152 global $wgSearchForwardUrl;
153 if( $wgSearchForwardUrl ) {
154 $url = str_replace( '$1', urlencode( $term ), $wgSearchForwardUrl );
155 $wgOut->redirect( $url );
156 wfProfileOut( __METHOD__
);
159 global $wgInputEncoding;
161 Xml
::openElement( 'fieldset' ) .
162 Xml
::element( 'legend', null, wfMsg( 'search-external' ) ) .
163 Xml
::element( 'p', array( 'class' => 'mw-searchdisabled' ), wfMsg( 'searchdisabled' ) ) .
164 wfMsg( 'googlesearch',
165 htmlspecialchars( $term ),
166 htmlspecialchars( $wgInputEncoding ),
167 htmlspecialchars( wfMsg( 'searchbutton' ) )
169 Xml
::closeElement( 'fieldset' )
171 wfProfileOut( __METHOD__
);
175 $t = Title
::newFromText( $term );
177 // fetch search results
178 $rewritten = $search->replacePrefixes($term);
180 $titleMatches = $search->searchTitle( $rewritten );
181 if( !($titleMatches instanceof SearchResultTooMany
))
182 $textMatches = $search->searchText( $rewritten );
184 // did you mean... suggestions
185 if( $textMatches && $textMatches->hasSuggestion() ) {
186 $st = SpecialPage
::getTitleFor( 'Search' );
188 # mirror Go/Search behaviour of original request ..
189 $didYouMeanParams = array( 'search' => $textMatches->getSuggestionQuery() );
191 if($this->fulltext
!= null)
192 $didYouMeanParams['fulltext'] = $this->fulltext
;
194 $stParams = array_merge(
196 $this->powerSearchOptions()
199 $suggestionSnippet = $textMatches->getSuggestionSnippet();
201 if( $suggestionSnippet == '' )
202 $suggestionSnippet = null;
204 $suggestLink = $sk->linkKnown(
211 $this->didYouMeanHtml
= '<div class="searchdidyoumean">'.wfMsg('search-suggest',$suggestLink).'</div>';
213 // start rendering the page
218 'id' => ( $this->searchAdvanced ?
'powersearch' : 'search' ),
220 'action' => $wgScript
225 Xml
::openElement( 'table', array( 'id'=>'mw-search-top-table', 'border'=>0, 'cellpadding'=>0, 'cellspacing'=>0 ) ) .
226 Xml
::openElement( 'tr' ) .
227 Xml
::openElement( 'td' ) . "\n" .
228 $this->shortDialog( $term ) .
229 Xml
::closeElement('td') .
230 Xml
::closeElement('tr') .
231 Xml
::closeElement('table')
234 // Sometimes the search engine knows there are too many hits
235 if( $titleMatches instanceof SearchResultTooMany
) {
236 $wgOut->addWikiText( '==' . wfMsg( 'toomanymatches' ) . "==\n" );
237 wfProfileOut( __METHOD__
);
241 $filePrefix = $wgContLang->getFormattedNsText(NS_FILE
).':';
242 if( trim( $term ) === '' ||
$filePrefix === trim( $term ) ) {
243 $wgOut->addHTML( $this->formHeader($term, 0, 0));
244 if( $this->searchAdvanced
) {
245 $wgOut->addHTML( $this->powerSearchBox( $term ) );
247 $wgOut->addHTML( '</form>' );
248 // Empty query -- straight view of search form
249 wfProfileOut( __METHOD__
);
253 // Get number of results
254 $titleMatchesNum = $titleMatches ?
$titleMatches->numRows() : 0;
255 $textMatchesNum = $textMatches ?
$textMatches->numRows() : 0;
256 // Total initial query matches (possible false positives)
257 $num = $titleMatchesNum +
$textMatchesNum;
259 // Get total actual results (after second filtering, if any)
260 $numTitleMatches = $titleMatches && !is_null( $titleMatches->getTotalHits() ) ?
261 $titleMatches->getTotalHits() : $titleMatchesNum;
262 $numTextMatches = $textMatches && !is_null( $textMatches->getTotalHits() ) ?
263 $textMatches->getTotalHits() : $textMatchesNum;
265 // get total number of results if backend can calculate it
267 if($titleMatches && !is_null( $titleMatches->getTotalHits() ) )
268 $totalRes +
= $titleMatches->getTotalHits();
269 if($textMatches && !is_null( $textMatches->getTotalHits() ))
270 $totalRes +
= $textMatches->getTotalHits();
272 // show number of results and current offset
273 $wgOut->addHTML( $this->formHeader($term, $num, $totalRes));
274 if( $this->searchAdvanced
) {
275 $wgOut->addHTML( $this->powerSearchBox( $term ) );
278 $wgOut->addHtml( Xml
::closeElement( 'form' ) );
279 $wgOut->addHtml( "<div class='searchresults'>" );
282 if( $num ||
$this->offset
) {
283 // Show the create link ahead
284 $this->showCreateLink( $t );
285 $prevnext = wfViewPrevNext( $this->offset
, $this->limit
,
286 SpecialPage
::getTitleFor( 'Search' ),
287 wfArrayToCGI( $this->powerSearchOptions(), array( 'search' => $term ) ),
288 max( $titleMatchesNum, $textMatchesNum ) < $this->limit
290 //$wgOut->addHTML( "<p class='mw-search-pager-top'>{$prevnext}</p>\n" );
291 wfRunHooks( 'SpecialSearchResults', array( $term, &$titleMatches, &$textMatches ) );
293 wfRunHooks( 'SpecialSearchNoResults', array( $term ) );
296 $wgOut->parserOptions()->setEditSection( false );
297 if( $titleMatches ) {
298 if( $numTitleMatches > 0 ) {
299 $wgOut->wrapWikiMsg( "==$1==\n", 'titlematches' );
300 $wgOut->addHTML( $this->showMatches( $titleMatches ) );
302 $titleMatches->free();
305 // output appropriate heading
306 if( $numTextMatches > 0 && $numTitleMatches > 0 ) {
307 // if no title matches the heading is redundant
308 $wgOut->wrapWikiMsg( "==$1==\n", 'textmatches' );
309 } elseif( $totalRes == 0 ) {
310 # Don't show the 'no text matches' if we received title matches
311 # $wgOut->wrapWikiMsg( "==$1==\n", 'notextmatches' );
313 // show interwiki results if any
314 if( $textMatches->hasInterwikiResults() ) {
315 $wgOut->addHTML( $this->showInterwiki( $textMatches->getInterwikiResults(), $term ) );
318 if( $numTextMatches > 0 ) {
319 $wgOut->addHTML( $this->showMatches( $textMatches ) );
322 $textMatches->free();
325 $wgOut->wrapWikiMsg( "<p class=\"mw-search-nonefound\">\n$1</p>", array( 'search-nonefound', wfEscapeWikiText( $term ) ) );
326 $this->showCreateLink( $t );
328 $wgOut->addHtml( "</div>" );
330 if( $num ||
$this->offset
) {
331 $wgOut->addHTML( "<p class='mw-search-pager-bottom'>{$prevnext}</p>\n" );
333 wfProfileOut( __METHOD__
);
336 protected function showCreateLink( $t ) {
339 // show direct page/create link if applicable
342 if( $t->isKnown() ) {
343 $messageName = 'searchmenu-exists';
344 } elseif( $t->userCan( 'create' ) ) {
345 $messageName = 'searchmenu-new';
347 $messageName = 'searchmenu-new-nocreate';
351 $wgOut->wrapWikiMsg( "<p class=\"mw-search-createlink\">\n$1</p>", array( $messageName, wfEscapeWikiText( $t->getPrefixedText() ) ) );
353 // preserve the paragraph for margins etc...
354 $wgOut->addHtml( '<p></p>' );
361 protected function setupPage( $term ) {
363 // Figure out the active search profile header
364 if( $this->searchAdvanced
) {
365 $this->active
= 'advanced';
367 $profiles = $this->getSearchProfiles();
369 foreach( $profiles as $key => $data ) {
370 if ( $this->namespaces
== $data['namespaces'] && $key != 'advanced')
371 $this->active
= $key;
375 # Should advanced UI be used?
376 $this->searchAdvanced
= ($this->active
=== 'advanced');
377 if( !empty( $term ) ) {
378 $wgOut->setPageTitle( wfMsg( 'searchresults') );
379 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'searchresults-title', $term ) ) );
381 // add javascript specific to special:search
382 $wgOut->addModules( 'mediawiki.legacy.search' );
383 $wgOut->addModules( 'mediawiki.special.search' );
387 * Extract "power search" namespace settings from the request object,
388 * returning a list of index numbers to search.
390 * @param $request WebRequest
393 protected function powerSearch( &$request ) {
395 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
396 if( $request->getCheck( 'ns' . $ns ) ) {
404 * Reconstruct the 'power search' options for links
408 protected function powerSearchOptions() {
410 foreach( $this->namespaces
as $n ) {
413 $opt['redirs'] = $this->searchRedirects ?
1 : 0;
414 if( $this->searchAdvanced
) {
415 $opt['advanced'] = $this->searchAdvanced
;
421 * Show whole set of results
423 * @param $matches SearchResultSet
425 protected function showMatches( &$matches ) {
427 wfProfileIn( __METHOD__
);
429 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
432 $infoLine = $matches->getInfo();
433 if( !is_null($infoLine) ) {
434 $out .= "\n<!-- {$infoLine} -->\n";
436 $out .= "<ul class='mw-search-results'>\n";
437 while( $result = $matches->next() ) {
438 $out .= $this->showHit( $result, $terms );
442 // convert the whole thing to desired language variant
443 $out = $wgContLang->convert( $out );
444 wfProfileOut( __METHOD__
);
449 * Format a single hit result
451 * @param $result SearchResult
452 * @param $terms Array: terms to highlight
454 protected function showHit( $result, $terms ) {
455 global $wgLang, $wgUser;
456 wfProfileIn( __METHOD__
);
458 if( $result->isBrokenTitle() ) {
459 wfProfileOut( __METHOD__
);
460 return "<!-- Broken link in search result -->\n";
463 $sk = $wgUser->getSkin();
464 $t = $result->getTitle();
466 $titleSnippet = $result->getTitleSnippet($terms);
468 if( $titleSnippet == '' )
469 $titleSnippet = null;
473 wfRunHooks( 'ShowSearchHitTitle',
474 array( &$link_t, &$titleSnippet, $result, $terms, $this ) );
476 $link = $this->sk
->linkKnown(
481 //If page content is not readable, just return the title.
482 //This is not quite safe, but better than showing excerpts from non-readable pages
483 //Note that hiding the entry entirely would screw up paging.
484 if( !$t->userCanRead() ) {
485 wfProfileOut( __METHOD__
);
486 return "<li>{$link}</li>\n";
489 // If the page doesn't *exist*... our search index is out of date.
490 // The least confusing at this point is to drop the result.
491 // You may get less results, but... oh well. :P
492 if( $result->isMissingRevision() ) {
493 wfProfileOut( __METHOD__
);
494 return "<!-- missing page " . htmlspecialchars( $t->getPrefixedText() ) . "-->\n";
497 // format redirects / relevant sections
498 $redirectTitle = $result->getRedirectTitle();
499 $redirectText = $result->getRedirectSnippet($terms);
500 $sectionTitle = $result->getSectionTitle();
501 $sectionText = $result->getSectionSnippet($terms);
504 if( !is_null($redirectTitle) ) {
505 if( $redirectText == '' )
506 $redirectText = null;
508 $redirect = "<span class='searchalttitle'>" .
511 $this->sk
->linkKnown(
522 if( !is_null($sectionTitle) ) {
523 if( $sectionText == '' )
526 $section = "<span class='searchalttitle'>" .
528 'search-section', $this->sk
->linkKnown(
536 // format text extract
537 $extract = "<div class='searchresult'>".$result->getTextSnippet($terms)."</div>";
540 if( is_null( $result->getScore() ) ) {
541 // Search engine doesn't report scoring info
544 $percent = sprintf( '%2.1f', $result->getScore() * 100 );
545 $score = wfMsg( 'search-result-score', $wgLang->formatNum( $percent ) )
549 // format description
550 $byteSize = $result->getByteSize();
551 $wordCount = $result->getWordCount();
552 $timestamp = $result->getTimestamp();
554 'search-result-size',
555 array( 'parsemag', 'escape' ),
556 $this->sk
->formatSize( $byteSize ),
557 $wgLang->formatNum( $wordCount )
560 if( $t->getNamespace() == NS_CATEGORY
) {
561 $cat = Category
::newFromTitle( $t );
563 'search-result-category-size',
564 array( 'parsemag', 'escape' ),
565 $wgLang->formatNum( $cat->getPageCount() ),
566 $wgLang->formatNum( $cat->getSubcatCount() ),
567 $wgLang->formatNum( $cat->getFileCount() )
571 $date = $wgLang->timeanddate( $timestamp );
573 // link to related articles if supported
575 if( $result->hasRelated() ) {
576 $st = SpecialPage
::getTitleFor( 'Search' );
577 $stParams = array_merge(
578 $this->powerSearchOptions(),
580 'search' => wfMsgForContent( 'searchrelated' ) . ':' . $t->getPrefixedText(),
581 'fulltext' => wfMsg( 'search' )
585 $related = ' -- ' . $sk->linkKnown(
587 wfMsg('search-relatedarticle'),
593 // Include a thumbnail for media files...
594 if( $t->getNamespace() == NS_FILE
) {
595 $img = wfFindFile( $t );
597 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
599 $desc = wfMsg( 'parentheses', $img->getShortDesc() );
600 wfProfileOut( __METHOD__
);
601 // Float doesn't seem to interact well with the bullets.
602 // Table messes up vertical alignment of the bullets.
603 // Bullets are therefore disabled (didn't look great anyway).
605 '<table class="searchResultImage">' .
607 '<td width="120" align="center" valign="top">' .
608 $thumb->toHtml( array( 'desc-link' => true ) ) .
610 '<td valign="top">' .
613 "<div class='mw-search-result-data'>{$score}{$desc} - {$date}{$related}</div>" .
622 wfProfileOut( __METHOD__
);
623 return "<li><div class='mw-search-result-heading'>{$link} {$redirect} {$section}</div> {$extract}\n" .
624 "<div class='mw-search-result-data'>{$score}{$size} - {$date}{$related}</div>" .
630 * Show results from other wikis
632 * @param $matches SearchResultSet
633 * @param $query String
635 protected function showInterwiki( &$matches, $query ) {
637 wfProfileIn( __METHOD__
);
638 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
640 $out = "<div id='mw-search-interwiki'><div id='mw-search-interwiki-caption'>".
641 wfMsg('search-interwiki-caption')."</div>\n";
642 $out .= "<ul class='mw-search-iwresults'>\n";
644 // work out custom project captions
645 $customCaptions = array();
646 $customLines = explode("\n",wfMsg('search-interwiki-custom')); // format per line <iwprefix>:<caption>
647 foreach($customLines as $line) {
648 $parts = explode(":",$line,2);
649 if(count($parts) == 2) // validate line
650 $customCaptions[$parts[0]] = $parts[1];
654 while( $result = $matches->next() ) {
655 $out .= $this->showInterwikiHit( $result, $prev, $terms, $query, $customCaptions );
656 $prev = $result->getInterwikiPrefix();
658 // TODO: should support paging in a non-confusing way (not sure how though, maybe via ajax)..
659 $out .= "</ul></div>\n";
661 // convert the whole thing to desired language variant
662 $out = $wgContLang->convert( $out );
663 wfProfileOut( __METHOD__
);
668 * Show single interwiki link
670 * @param $result SearchResult
671 * @param $lastInterwiki String
672 * @param $terms Array
673 * @param $query String
674 * @param $customCaptions Array: iw prefix -> caption
676 protected function showInterwikiHit( $result, $lastInterwiki, $terms, $query, $customCaptions) {
677 wfProfileIn( __METHOD__
);
679 if( $result->isBrokenTitle() ) {
680 wfProfileOut( __METHOD__
);
681 return "<!-- Broken link in search result -->\n";
684 $t = $result->getTitle();
686 $titleSnippet = $result->getTitleSnippet($terms);
688 if( $titleSnippet == '' )
689 $titleSnippet = null;
691 $link = $this->sk
->linkKnown(
696 // format redirect if any
697 $redirectTitle = $result->getRedirectTitle();
698 $redirectText = $result->getRedirectSnippet($terms);
700 if( !is_null($redirectTitle) ) {
701 if( $redirectText == '' )
702 $redirectText = null;
704 $redirect = "<span class='searchalttitle'>" .
707 $this->sk
->linkKnown(
716 // display project name
717 if(is_null($lastInterwiki) ||
$lastInterwiki != $t->getInterwiki()) {
718 if( key_exists($t->getInterwiki(),$customCaptions) )
719 // captions from 'search-interwiki-custom'
720 $caption = $customCaptions[$t->getInterwiki()];
722 // default is to show the hostname of the other wiki which might suck
723 // if there are many wikis on one hostname
724 $parsed = parse_url($t->getFullURL());
725 $caption = wfMsg('search-interwiki-default', $parsed['host']);
727 // "more results" link (special page stuff could be localized, but we might not know target lang)
728 $searchTitle = Title
::newFromText($t->getInterwiki().":Special:Search");
729 $searchLink = $this->sk
->linkKnown(
731 wfMsg('search-interwiki-more'),
735 'fulltext' => 'Search'
738 $out .= "</ul><div class='mw-search-interwiki-project'><span class='mw-search-interwiki-more'>
739 {$searchLink}</span>{$caption}</div>\n<ul>";
742 $out .= "<li>{$link} {$redirect}</li>\n";
743 wfProfileOut( __METHOD__
);
749 * Generates the power search box at bottom of [[Special:Search]]
751 * @param $term String: search term
752 * @return String: HTML form
754 protected function powerSearchBox( $term ) {
755 // Groups namespaces into rows according to subject
757 foreach( SearchEngine
::searchableNamespaces() as $namespace => $name ) {
758 $subject = MWNamespace
::getSubject( $namespace );
759 if( !array_key_exists( $subject, $rows ) ) {
760 $rows[$subject] = "";
762 $name = str_replace( '_', ' ', $name );
764 $name = wfMsg( 'blanknamespace' );
768 'td', array( 'style' => 'white-space: nowrap' )
773 "mw-search-ns{$namespace}",
774 in_array( $namespace, $this->namespaces
)
776 Xml
::closeElement( 'td' );
778 $rows = array_values( $rows );
779 $numRows = count( $rows );
781 // Lays out namespaces in multiple floating two-column tables so they'll
782 // be arranged nicely while still accommodating different screen widths
783 $namespaceTables = '';
784 for( $i = 0; $i < $numRows; $i +
= 4 ) {
785 $namespaceTables .= Xml
::openElement(
787 array( 'cellpadding' => 0, 'cellspacing' => 0, 'border' => 0 )
789 for( $j = $i; $j < $i +
4 && $j < $numRows; $j++
) {
790 $namespaceTables .= Xml
::tags( 'tr', null, $rows[$j] );
792 $namespaceTables .= Xml
::closeElement( 'table' );
794 // Show redirects check only if backend supports it
796 if( $this->searchEngine
->acceptListRedirects() ) {
799 'redirs', $this->searchRedirects
, array( 'value' => '1', 'id' => 'redirs' )
802 Xml
::label( wfMsg( 'powersearch-redir' ), 'redirs' );
804 // Return final output
808 array( 'id' => 'mw-searchoptions', 'style' => 'margin:0em;' )
810 Xml
::element( 'legend', null, wfMsg('powersearch-legend') ) .
811 Xml
::tags( 'h4', null, wfMsgExt( 'powersearch-ns', array( 'parseinline' ) ) ) .
814 array( 'id' => 'mw-search-togglebox' ),
815 Xml
::label( wfMsg( 'powersearch-togglelabel' ), 'mw-search-togglelabel' ) .
820 'id' => 'mw-search-toggleall',
821 'onclick' => 'mwToggleSearchCheckboxes("all");',
822 'value' => wfMsg( 'powersearch-toggleall' )
829 'id' => 'mw-search-togglenone',
830 'onclick' => 'mwToggleSearchCheckboxes("none");',
831 'value' => wfMsg( 'powersearch-togglenone' )
835 Xml
::element( 'div', array( 'class' => 'divider' ), '', false ) .
837 Xml
::element( 'div', array( 'class' => 'divider' ), '', false ) .
839 Html
::hidden( 'title', SpecialPage
::getTitleFor( 'Search' )->getPrefixedText() ) .
840 Html
::hidden( 'advanced', $this->searchAdvanced
) .
841 Html
::hidden( 'fulltext', 'Advanced search' ) .
842 Xml
::closeElement( 'fieldset' );
845 protected function getSearchProfiles() {
846 // Builds list of Search Types (profiles)
847 $nsAllSet = array_keys( SearchEngine
::searchableNamespaces() );
851 'message' => 'searchprofile-articles',
852 'tooltip' => 'searchprofile-articles-tooltip',
853 'namespaces' => SearchEngine
::defaultNamespaces(),
854 'namespace-messages' => SearchEngine
::namespacesAsText(
855 SearchEngine
::defaultNamespaces()
859 'message' => 'searchprofile-images',
860 'tooltip' => 'searchprofile-images-tooltip',
861 'namespaces' => array( NS_FILE
),
864 'message' => 'searchprofile-project',
865 'tooltip' => 'searchprofile-project-tooltip',
866 'namespaces' => SearchEngine
::helpNamespaces(),
867 'namespace-messages' => SearchEngine
::namespacesAsText(
868 SearchEngine
::helpNamespaces()
872 'message' => 'searchprofile-everything',
873 'tooltip' => 'searchprofile-everything-tooltip',
874 'namespaces' => $nsAllSet,
877 'message' => 'searchprofile-advanced',
878 'tooltip' => 'searchprofile-advanced-tooltip',
879 'namespaces' => $this->namespaces
,
880 'parameters' => array( 'advanced' => 1 ),
884 wfRunHooks( 'SpecialSearchProfiles', array( &$profiles ) );
886 foreach( $profiles as &$data ) {
887 sort($data['namespaces']);
893 protected function formHeader( $term, $resultsShown, $totalNum ) {
896 $out = Xml
::openElement('div', array( 'class' => 'mw-search-formheader' ) );
899 if( $this->startsWithImage( $term ) ) {
901 $bareterm = substr( $term, strpos( $term, ':' ) +
1 );
904 $profiles = $this->getSearchProfiles();
906 // Outputs XML for Search Types
907 $out .= Xml
::openElement( 'div', array( 'class' => 'search-types' ) );
908 $out .= Xml
::openElement( 'ul' );
909 foreach ( $profiles as $id => $profile ) {
910 $tooltipParam = isset( $profile['namespace-messages'] ) ?
911 $wgLang->commaList( $profile['namespace-messages'] ) : null;
915 'class' => $this->active
== $id ?
'current' : 'normal'
917 $this->makeSearchLink(
919 $profile['namespaces'],
920 wfMsg( $profile['message'] ),
921 wfMsg( $profile['tooltip'], $tooltipParam ),
922 isset( $profile['parameters'] ) ?
$profile['parameters'] : array()
926 $out .= Xml
::closeElement( 'ul' );
927 $out .= Xml
::closeElement('div') ;
930 if ( $resultsShown > 0 ) {
931 if ( $totalNum > 0 ){
932 $top = wfMsgExt( 'showingresultsheader', array( 'parseinline' ),
933 $wgLang->formatNum( $this->offset +
1 ),
934 $wgLang->formatNum( $this->offset +
$resultsShown ),
935 $wgLang->formatNum( $totalNum ),
936 wfEscapeWikiText( $term ),
937 $wgLang->formatNum( $resultsShown )
939 } elseif ( $resultsShown >= $this->limit
) {
940 $top = wfShowingResults( $this->offset
, $this->limit
);
942 $top = wfShowingResultsNum( $this->offset
, $this->limit
, $resultsShown );
944 $out .= Xml
::tags( 'div', array( 'class' => 'results-info' ),
945 Xml
::tags( 'ul', null, Xml
::tags( 'li', null, $top ) )
949 $out .= Xml
::element( 'div', array( 'style' => 'clear:both' ), '', false );
950 $out .= Xml
::closeElement('div');
952 // Adds hidden namespace fields
953 if ( !$this->searchAdvanced
) {
954 foreach( $this->namespaces
as $ns ) {
955 $out .= Html
::hidden( "ns{$ns}", '1' );
962 protected function shortDialog( $term ) {
963 $searchTitle = SpecialPage
::getTitleFor( 'Search' );
964 $out = Html
::hidden( 'title', $searchTitle->getPrefixedText() ) . "\n";
965 // Keep redirect setting
966 $out .= Html
::hidden( "redirs", (int)$this->searchRedirects
) . "\n";
968 $out .= Html
::input( 'search', $term, 'search', array(
969 'id' => $this->searchAdvanced ?
'powerSearchText' : 'searchText',
973 $out .= Html
::hidden( 'fulltext', 'Search' ) . "\n";
974 $out .= Xml
::submitButton( wfMsg( 'searchbutton' ) ) . "\n";
975 return $out . $this->didYouMeanHtml
;
979 * Make a search link with some target namespaces
981 * @param $term String
982 * @param $namespaces Array
983 * @param $label String: link's text
984 * @param $tooltip String: link's tooltip
985 * @param $params Array: query string parameters
986 * @return String: HTML fragment
988 protected function makeSearchLink( $term, $namespaces, $label, $tooltip, $params=array() ) {
990 foreach( $namespaces as $n ) {
993 $opt['redirs'] = $this->searchRedirects ?
1 : 0;
995 $st = SpecialPage
::getTitleFor( 'Search' );
996 $stParams = array_merge(
999 'fulltext' => wfMsg( 'search' )
1004 return Xml
::element(
1007 'href' => $st->getLocalURL( $stParams ),
1008 'title' => $tooltip,
1009 'onmousedown' => 'mwSearchHeaderClick(this);',
1010 'onkeydown' => 'mwSearchHeaderClick(this);'),
1016 * Check if query starts with image: prefix
1018 * @param $term String: the string to check
1021 protected function startsWithImage( $term ) {
1024 $p = explode( ':', $term );
1025 if( count( $p ) > 1 ) {
1026 return $wgContLang->getNsIndex( $p[0] ) == NS_FILE
;
1032 * Check if query starts with all: prefix
1034 * @param $term String: the string to check
1037 protected function startsWithAll( $term ) {
1039 $allkeyword = wfMsgForContent('searchall');
1041 $p = explode( ':', $term );
1042 if( count( $p ) > 1 ) {
1043 return $p[0] == $allkeyword;