2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
21 * Run text & title search and display the output
23 * @ingroup SpecialPage
29 * @param $par String: (default '')
31 function wfSpecialSearch( $par = '' ) {
32 global $wgRequest, $wgUser;
33 // Strip underscores from title parameter; most of the time we'll want
34 // text form here. But don't strip underscores from actual text params!
35 $titleParam = str_replace( '_', ' ', $par );
36 // Fetch the search term
37 $search = str_replace( "\n", " ", $wgRequest->getText( 'search', $titleParam ) );
38 $searchPage = new SpecialSearch( $wgRequest, $wgUser );
39 if( $wgRequest->getVal( 'fulltext' )
40 || !is_null( $wgRequest->getVal( 'offset' ))
41 || !is_null( $wgRequest->getVal( 'searchx' )) )
43 $searchPage->showResults( $search );
45 $searchPage->goResult( $search );
50 * implements Special:Search - Run text & title search and display the output
51 * @ingroup SpecialPage
56 * Set up basic search parameters from the request and user settings.
57 * Typically you'll pass $wgRequest and $wgUser.
59 * @param WebRequest $request
63 function __construct( &$request, &$user ) {
64 list( $this->limit, $this->offset ) = $request->getLimitOffset( 20, 'searchlimit' );
65 $this->mPrefix = $request->getVal('prefix', '');
66 # Extract requested namespaces
67 $this->namespaces = $this->powerSearch( $request );
68 if( empty( $this->namespaces ) ) {
69 $this->namespaces = SearchEngine::userNamespaces( $user );
71 $this->searchRedirects = $request->getcheck( 'redirs' ) ? true : false;
72 $this->searchAdvanced = $request->getVal( 'advanced' );
73 $this->active = 'advanced';
74 $this->sk = $user->getSkin();
75 $this->didYouMeanHtml = ''; # html of did you mean... link
76 $this->fulltext = $request->getVal('fulltext');
80 * If an exact title match can be found, jump straight ahead to it.
83 public function goResult( $term ) {
85 $this->setupPage( $term );
86 # Try to go to page as entered.
87 $t = Title::newFromText( $term );
88 # If the string cannot be used to create a title
90 return $this->showResults( $term );
92 # If there's an exact or very near match, jump right there.
93 $t = SearchEngine::getNearMatch( $term );
94 if( !is_null( $t ) ) {
95 $wgOut->redirect( $t->getFullURL() );
98 # No match, generate an edit URL
99 $t = Title::newFromText( $term );
100 if( !is_null( $t ) ) {
102 wfRunHooks( 'SpecialSearchNogomatch', array( &$t ) );
103 # If the feature is enabled, go straight to the edit page
105 $wgOut->redirect( $t->getFullURL( array( 'action' => 'edit' ) ) );
109 return $this->showResults( $term );
113 * @param string $term
115 public function showResults( $term ) {
116 global $wgOut, $wgUser, $wgDisableTextSearch, $wgContLang, $wgScript;
117 wfProfileIn( __METHOD__ );
119 $sk = $wgUser->getSkin();
121 $this->searchEngine = SearchEngine::create();
122 $search =& $this->searchEngine;
123 $search->setLimitOffset( $this->limit, $this->offset );
124 $search->setNamespaces( $this->namespaces );
125 $search->showRedirects = $this->searchRedirects;
126 $search->prefix = $this->mPrefix;
127 $term = $search->transformSearchTerm($term);
129 $this->setupPage( $term );
131 if( $wgDisableTextSearch ) {
132 global $wgSearchForwardUrl;
133 if( $wgSearchForwardUrl ) {
134 $url = str_replace( '$1', urlencode( $term ), $wgSearchForwardUrl );
135 $wgOut->redirect( $url );
136 wfProfileOut( __METHOD__ );
139 global $wgInputEncoding;
141 Xml::openElement( 'fieldset' ) .
142 Xml::element( 'legend', null, wfMsg( 'search-external' ) ) .
143 Xml::element( 'p', array( 'class' => 'mw-searchdisabled' ), wfMsg( 'searchdisabled' ) ) .
144 wfMsg( 'googlesearch',
145 htmlspecialchars( $term ),
146 htmlspecialchars( $wgInputEncoding ),
147 htmlspecialchars( wfMsg( 'searchbutton' ) )
149 Xml::closeElement( 'fieldset' )
151 wfProfileOut( __METHOD__ );
155 $t = Title::newFromText( $term );
157 // fetch search results
158 $rewritten = $search->replacePrefixes($term);
160 $titleMatches = $search->searchTitle( $rewritten );
161 if( !($titleMatches instanceof SearchResultTooMany))
162 $textMatches = $search->searchText( $rewritten );
164 // did you mean... suggestions
165 if( $textMatches && $textMatches->hasSuggestion() ) {
166 $st = SpecialPage::getTitleFor( 'Search' );
168 # mirror Go/Search behaviour of original request ..
169 $didYouMeanParams = array( 'search' => $textMatches->getSuggestionQuery() );
171 if($this->fulltext != null)
172 $didYouMeanParams['fulltext'] = $this->fulltext;
174 $stParams = array_merge(
176 $this->powerSearchOptions()
179 $suggestionSnippet = $textMatches->getSuggestionSnippet();
181 if( $suggestionSnippet == '' )
182 $suggestionSnippet = null;
184 $suggestLink = $sk->linkKnown(
191 $this->didYouMeanHtml = '<div class="searchdidyoumean">'.wfMsg('search-suggest',$suggestLink).'</div>';
193 // start rendering the page
198 'id' => ( $this->searchAdvanced ? 'powersearch' : 'search' ),
200 'action' => $wgScript
205 Xml::openElement( 'table', array( 'id'=>'mw-search-top-table', 'border'=>0, 'cellpadding'=>0, 'cellspacing'=>0 ) ) .
206 Xml::openElement( 'tr' ) .
207 Xml::openElement( 'td' ) . "\n" .
208 $this->shortDialog( $term ) .
209 Xml::closeElement('td') .
210 Xml::closeElement('tr') .
211 Xml::closeElement('table')
214 // Sometimes the search engine knows there are too many hits
215 if( $titleMatches instanceof SearchResultTooMany ) {
216 $wgOut->addWikiText( '==' . wfMsg( 'toomanymatches' ) . "==\n" );
217 wfProfileOut( __METHOD__ );
221 $filePrefix = $wgContLang->getFormattedNsText(NS_FILE).':';
222 if( trim( $term ) === '' || $filePrefix === trim( $term ) ) {
223 $wgOut->addHTML( $this->searchFocus() );
224 $wgOut->addHTML( $this->formHeader($term, 0, 0));
225 if( $this->searchAdvanced ) {
226 $wgOut->addHTML( $this->powerSearchBox( $term ) );
228 $wgOut->addHTML( '</form>' );
229 // Empty query -- straight view of search form
230 wfProfileOut( __METHOD__ );
234 // Get number of results
235 $titleMatchesNum = $titleMatches ? $titleMatches->numRows() : 0;
236 $textMatchesNum = $textMatches ? $textMatches->numRows() : 0;
237 // Total initial query matches (possible false positives)
238 $num = $titleMatchesNum + $textMatchesNum;
240 // Get total actual results (after second filtering, if any)
241 $numTitleMatches = $titleMatches && !is_null( $titleMatches->getTotalHits() ) ?
242 $titleMatches->getTotalHits() : $titleMatchesNum;
243 $numTextMatches = $textMatches && !is_null( $textMatches->getTotalHits() ) ?
244 $textMatches->getTotalHits() : $textMatchesNum;
246 // get total number of results if backend can calculate it
248 if($titleMatches && !is_null( $titleMatches->getTotalHits() ) )
249 $totalRes += $titleMatches->getTotalHits();
250 if($textMatches && !is_null( $textMatches->getTotalHits() ))
251 $totalRes += $textMatches->getTotalHits();
253 // show number of results and current offset
254 $wgOut->addHTML( $this->formHeader($term, $num, $totalRes));
255 if( $this->searchAdvanced ) {
256 $wgOut->addHTML( $this->powerSearchBox( $term ) );
259 $wgOut->addHtml( Xml::closeElement( 'form' ) );
260 $wgOut->addHtml( "<div class='searchresults'>" );
263 if( $num || $this->offset ) {
264 // Show the create link ahead
265 $this->showCreateLink( $t );
266 $prevnext = wfViewPrevNext( $this->offset, $this->limit,
267 SpecialPage::getTitleFor( 'Search' ),
268 wfArrayToCGI( $this->powerSearchOptions(), array( 'search' => $term ) ),
269 max( $titleMatchesNum, $textMatchesNum ) < $this->limit
271 //$wgOut->addHTML( "<p class='mw-search-pager-top'>{$prevnext}</p>\n" );
272 wfRunHooks( 'SpecialSearchResults', array( $term, &$titleMatches, &$textMatches ) );
274 wfRunHooks( 'SpecialSearchNoResults', array( $term ) );
277 if( $titleMatches ) {
278 if( $numTitleMatches > 0 ) {
279 $wgOut->wrapWikiMsg( "==$1==\n", 'titlematches' );
280 $wgOut->addHTML( $this->showMatches( $titleMatches ) );
282 $titleMatches->free();
285 // output appropriate heading
286 if( $numTextMatches > 0 && $numTitleMatches > 0 ) {
287 // if no title matches the heading is redundant
288 $wgOut->wrapWikiMsg( "==$1==\n", 'textmatches' );
289 } elseif( $totalRes == 0 ) {
290 # Don't show the 'no text matches' if we received title matches
291 # $wgOut->wrapWikiMsg( "==$1==\n", 'notextmatches' );
293 // show interwiki results if any
294 if( $textMatches->hasInterwikiResults() ) {
295 $wgOut->addHTML( $this->showInterwiki( $textMatches->getInterwikiResults(), $term ) );
298 if( $numTextMatches > 0 ) {
299 $wgOut->addHTML( $this->showMatches( $textMatches ) );
302 $textMatches->free();
305 $wgOut->addWikiMsg( 'search-nonefound', wfEscapeWikiText( $term ) );
306 $this->showCreateLink( $t );
308 $wgOut->addHtml( "</div>" );
310 $wgOut->addHTML( $this->searchFocus() );
313 if( $num || $this->offset ) {
314 $wgOut->addHTML( "<p class='mw-search-pager-bottom'>{$prevnext}</p>\n" );
316 wfProfileOut( __METHOD__ );
319 protected function showCreateLink( $t ) {
322 // show direct page/create link if applicable
325 if( $t->isKnown() ) {
326 $messageName = 'searchmenu-exists';
327 } elseif( $t->userCan( 'create' ) ) {
328 $messageName = 'searchmenu-new';
332 $wgOut->addWikiMsg( $messageName, wfEscapeWikiText( $t->getPrefixedText() ) );
334 // preserve the paragraph for margins etc...
335 $wgOut->addHtml( '<p></p>' );
342 protected function setupPage( $term ) {
344 // Figure out the active search profile header
345 $nsAllSet = array_keys( SearchEngine::searchableNamespaces() );
346 if( $this->searchAdvanced )
347 $this->active = 'advanced';
349 $profiles = $this->getSearchProfiles();
351 foreach( $profiles as $key => $data ) {
352 if ( $this->namespaces == $data['namespaces'] && $key != 'advanced')
353 $this->active = $key;
357 # Should advanced UI be used?
358 $this->searchAdvanced = ($this->active === 'advanced');
359 if( !empty( $term ) ) {
360 $wgOut->setPageTitle( wfMsg( 'searchresults') );
361 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'searchresults-title', $term ) ) );
363 $wgOut->setArticleRelated( false );
364 $wgOut->setRobotPolicy( 'noindex,nofollow' );
365 // add javascript specific to special:search
366 $wgOut->addScriptFile( 'search.js' );
367 $wgOut->allowClickjacking();
371 * Extract "power search" namespace settings from the request object,
372 * returning a list of index numbers to search.
374 * @param WebRequest $request
377 protected function powerSearch( &$request ) {
379 foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
380 if( $request->getCheck( 'ns' . $ns ) ) {
388 * Reconstruct the 'power search' options for links
391 protected function powerSearchOptions() {
393 foreach( $this->namespaces as $n ) {
396 $opt['redirs'] = $this->searchRedirects ? 1 : 0;
397 if( $this->searchAdvanced ) {
398 $opt['advanced'] = $this->searchAdvanced;
404 * Show whole set of results
406 * @param SearchResultSet $matches
408 protected function showMatches( &$matches ) {
410 wfProfileIn( __METHOD__ );
412 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
415 $infoLine = $matches->getInfo();
416 if( !is_null($infoLine) ) {
417 $out .= "\n<!-- {$infoLine} -->\n";
419 $off = $this->offset + 1;
420 $out .= "<ul class='mw-search-results'>\n";
421 while( $result = $matches->next() ) {
422 $out .= $this->showHit( $result, $terms );
426 // convert the whole thing to desired language variant
427 $out = $wgContLang->convert( $out );
428 wfProfileOut( __METHOD__ );
433 * Format a single hit result
434 * @param SearchResult $result
435 * @param array $terms terms to highlight
437 protected function showHit( $result, $terms ) {
438 global $wgContLang, $wgLang, $wgUser;
439 wfProfileIn( __METHOD__ );
441 if( $result->isBrokenTitle() ) {
442 wfProfileOut( __METHOD__ );
443 return "<!-- Broken link in search result -->\n";
446 $sk = $wgUser->getSkin();
447 $t = $result->getTitle();
449 $titleSnippet = $result->getTitleSnippet($terms);
451 if( $titleSnippet == '' )
452 $titleSnippet = null;
456 wfRunHooks( 'ShowSearchHitTitle',
457 array( &$link_t, &$titleSnippet, $result, $terms, $this ) );
459 $link = $this->sk->linkKnown(
464 //If page content is not readable, just return the title.
465 //This is not quite safe, but better than showing excerpts from non-readable pages
466 //Note that hiding the entry entirely would screw up paging.
467 if( !$t->userCanRead() ) {
468 wfProfileOut( __METHOD__ );
469 return "<li>{$link}</li>\n";
472 // If the page doesn't *exist*... our search index is out of date.
473 // The least confusing at this point is to drop the result.
474 // You may get less results, but... oh well. :P
475 if( $result->isMissingRevision() ) {
476 wfProfileOut( __METHOD__ );
477 return "<!-- missing page " . htmlspecialchars( $t->getPrefixedText() ) . "-->\n";
480 // format redirects / relevant sections
481 $redirectTitle = $result->getRedirectTitle();
482 $redirectText = $result->getRedirectSnippet($terms);
483 $sectionTitle = $result->getSectionTitle();
484 $sectionText = $result->getSectionSnippet($terms);
487 if( !is_null($redirectTitle) ) {
488 if( $redirectText == '' )
489 $redirectText = null;
491 $redirect = "<span class='searchalttitle'>" .
494 $this->sk->linkKnown(
505 if( !is_null($sectionTitle) ) {
506 if( $sectionText == '' )
509 $section = "<span class='searchalttitle'>" .
511 'search-section', $this->sk->linkKnown(
519 // format text extract
520 $extract = "<div class='searchresult'>".$result->getTextSnippet($terms)."</div>";
523 if( is_null( $result->getScore() ) ) {
524 // Search engine doesn't report scoring info
527 $percent = sprintf( '%2.1f', $result->getScore() * 100 );
528 $score = wfMsg( 'search-result-score', $wgLang->formatNum( $percent ) )
532 // format description
533 $byteSize = $result->getByteSize();
534 $wordCount = $result->getWordCount();
535 $timestamp = $result->getTimestamp();
537 'search-result-size',
538 array( 'parsemag', 'escape' ),
539 $this->sk->formatSize( $byteSize ),
540 $wgLang->formatNum( $wordCount )
542 $date = $wgLang->timeanddate( $timestamp );
544 // link to related articles if supported
546 if( $result->hasRelated() ) {
547 $st = SpecialPage::getTitleFor( 'Search' );
548 $stParams = array_merge(
549 $this->powerSearchOptions(),
551 'search' => wfMsgForContent( 'searchrelated' ) . ':' . $t->getPrefixedText(),
552 'fulltext' => wfMsg( 'search' )
556 $related = ' -- ' . $sk->linkKnown(
558 wfMsg('search-relatedarticle'),
564 // Include a thumbnail for media files...
565 if( $t->getNamespace() == NS_FILE ) {
566 $img = wfFindFile( $t );
568 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
570 $desc = $img->getShortDesc();
571 wfProfileOut( __METHOD__ );
572 // Float doesn't seem to interact well with the bullets.
573 // Table messes up vertical alignment of the bullets.
574 // Bullets are therefore disabled (didn't look great anyway).
576 '<table class="searchResultImage">' .
578 '<td width="120" align="center" valign="top">' .
579 $thumb->toHtml( array( 'desc-link' => true ) ) .
581 '<td valign="top">' .
584 "<div class='mw-search-result-data'>{$score}{$desc} - {$date}{$related}</div>" .
593 wfProfileOut( __METHOD__ );
594 return "<li>{$link} {$redirect} {$section} {$extract}\n" .
595 "<div class='mw-search-result-data'>{$score}{$size} - {$date}{$related}</div>" .
601 * Show results from other wikis
603 * @param SearchResultSet $matches
605 protected function showInterwiki( &$matches, $query ) {
607 wfProfileIn( __METHOD__ );
608 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
610 $out = "<div id='mw-search-interwiki'><div id='mw-search-interwiki-caption'>".
611 wfMsg('search-interwiki-caption')."</div>\n";
612 $off = $this->offset + 1;
613 $out .= "<ul class='mw-search-iwresults'>\n";
615 // work out custom project captions
616 $customCaptions = array();
617 $customLines = explode("\n",wfMsg('search-interwiki-custom')); // format per line <iwprefix>:<caption>
618 foreach($customLines as $line) {
619 $parts = explode(":",$line,2);
620 if(count($parts) == 2) // validate line
621 $customCaptions[$parts[0]] = $parts[1];
625 while( $result = $matches->next() ) {
626 $out .= $this->showInterwikiHit( $result, $prev, $terms, $query, $customCaptions );
627 $prev = $result->getInterwikiPrefix();
629 // TODO: should support paging in a non-confusing way (not sure how though, maybe via ajax)..
630 $out .= "</ul></div>\n";
632 // convert the whole thing to desired language variant
633 $out = $wgContLang->convert( $out );
634 wfProfileOut( __METHOD__ );
639 * Show single interwiki link
641 * @param SearchResult $result
642 * @param string $lastInterwiki
643 * @param array $terms
644 * @param string $query
645 * @param array $customCaptions iw prefix -> caption
647 protected function showInterwikiHit( $result, $lastInterwiki, $terms, $query, $customCaptions) {
648 wfProfileIn( __METHOD__ );
649 global $wgContLang, $wgLang;
651 if( $result->isBrokenTitle() ) {
652 wfProfileOut( __METHOD__ );
653 return "<!-- Broken link in search result -->\n";
656 $t = $result->getTitle();
658 $titleSnippet = $result->getTitleSnippet($terms);
660 if( $titleSnippet == '' )
661 $titleSnippet = null;
663 $link = $this->sk->linkKnown(
668 // format redirect if any
669 $redirectTitle = $result->getRedirectTitle();
670 $redirectText = $result->getRedirectSnippet($terms);
672 if( !is_null($redirectTitle) ) {
673 if( $redirectText == '' )
674 $redirectText = null;
676 $redirect = "<span class='searchalttitle'>" .
679 $this->sk->linkKnown(
688 // display project name
689 if(is_null($lastInterwiki) || $lastInterwiki != $t->getInterwiki()) {
690 if( key_exists($t->getInterwiki(),$customCaptions) )
691 // captions from 'search-interwiki-custom'
692 $caption = $customCaptions[$t->getInterwiki()];
694 // default is to show the hostname of the other wiki which might suck
695 // if there are many wikis on one hostname
696 $parsed = parse_url($t->getFullURL());
697 $caption = wfMsg('search-interwiki-default', $parsed['host']);
699 // "more results" link (special page stuff could be localized, but we might not know target lang)
700 $searchTitle = Title::newFromText($t->getInterwiki().":Special:Search");
701 $searchLink = $this->sk->linkKnown(
703 wfMsg('search-interwiki-more'),
707 'fulltext' => 'Search'
710 $out .= "</ul><div class='mw-search-interwiki-project'><span class='mw-search-interwiki-more'>
711 {$searchLink}</span>{$caption}</div>\n<ul>";
714 $out .= "<li>{$link} {$redirect}</li>\n";
715 wfProfileOut( __METHOD__ );
721 * Generates the power search box at bottom of [[Special:Search]]
722 * @param $term string: search term
723 * @return $out string: HTML form
725 protected function powerSearchBox( $term ) {
726 global $wgScript, $wgContLang;
728 // Groups namespaces into rows according to subject
730 foreach( SearchEngine::searchableNamespaces() as $namespace => $name ) {
731 $subject = MWNamespace::getSubject( $namespace );
732 if( !array_key_exists( $subject, $rows ) ) {
733 $rows[$subject] = "";
735 $name = str_replace( '_', ' ', $name );
737 $name = wfMsg( 'blanknamespace' );
741 'td', array( 'style' => 'white-space: nowrap' )
746 "mw-search-ns{$namespace}",
747 in_array( $namespace, $this->namespaces )
749 Xml::closeElement( 'td' );
751 $rows = array_values( $rows );
752 $numRows = count( $rows );
754 // Lays out namespaces in multiple floating two-column tables so they'll
755 // be arranged nicely while still accommodating different screen widths
756 $namespaceTables = '';
757 for( $i = 0; $i < $numRows; $i += 4 ) {
758 $namespaceTables .= Xml::openElement(
760 array( 'cellpadding' => 0, 'cellspacing' => 0, 'border' => 0 )
762 for( $j = $i; $j < $i + 4 && $j < $numRows; $j++ ) {
763 $namespaceTables .= Xml::tags( 'tr', null, $rows[$j] );
765 $namespaceTables .= Xml::closeElement( 'table' );
767 // Show redirects check only if backend supports it
769 if( $this->searchEngine->acceptListRedirects() ) {
772 'redirs', $this->searchRedirects, array( 'value' => '1', 'id' => 'redirs' )
775 Xml::label( wfMsg( 'powersearch-redir' ), 'redirs' );
777 // Return final output
781 array( 'id' => 'mw-searchoptions', 'style' => 'margin:0em;' )
783 Xml::element( 'legend', null, wfMsg('powersearch-legend') ) .
784 Xml::tags( 'h4', null, wfMsgExt( 'powersearch-ns', array( 'parseinline' ) ) ) .
787 array( 'id' => 'mw-search-togglebox' ),
788 Xml::label( wfMsg( 'powersearch-togglelabel' ), 'mw-search-togglelabel' ) .
793 'id' => 'mw-search-toggleall',
794 'onclick' => 'mwToggleSearchCheckboxes("all");',
795 'value' => wfMsg( 'powersearch-toggleall' )
802 'id' => 'mw-search-togglenone',
803 'onclick' => 'mwToggleSearchCheckboxes("none");',
804 'value' => wfMsg( 'powersearch-togglenone' )
808 Xml::element( 'div', array( 'class' => 'divider' ), '', false ) .
810 Xml::element( 'div', array( 'class' => 'divider' ), '', false ) .
812 Xml::hidden( 'title', SpecialPage::getTitleFor( 'Search' )->getPrefixedText() ) .
813 Xml::hidden( 'advanced', $this->searchAdvanced ) .
814 Xml::hidden( 'fulltext', 'Advanced search' ) .
815 Xml::closeElement( 'fieldset' );
818 protected function searchFocus() {
819 $id = $this->searchAdvanced ? 'powerSearchText' : 'searchText';
820 return Html::inlineScript(
821 "hookEvent(\"load\", function() {" .
822 "document.getElementById('$id').focus();" .
826 protected function getSearchProfiles() {
827 // Builds list of Search Types (profiles)
828 $nsAllSet = array_keys( SearchEngine::searchableNamespaces() );
832 'message' => 'searchprofile-articles',
833 'tooltip' => 'searchprofile-articles-tooltip',
834 'namespaces' => SearchEngine::defaultNamespaces(),
835 'namespace-messages' => SearchEngine::namespacesAsText(
836 SearchEngine::defaultNamespaces()
840 'message' => 'searchprofile-images',
841 'tooltip' => 'searchprofile-images-tooltip',
842 'namespaces' => array( NS_FILE ),
845 'message' => 'searchprofile-project',
846 'tooltip' => 'searchprofile-project-tooltip',
847 'namespaces' => SearchEngine::helpNamespaces(),
848 'namespace-messages' => SearchEngine::namespacesAsText(
849 SearchEngine::helpNamespaces()
853 'message' => 'searchprofile-everything',
854 'tooltip' => 'searchprofile-everything-tooltip',
855 'namespaces' => $nsAllSet,
858 'message' => 'searchprofile-advanced',
859 'tooltip' => 'searchprofile-advanced-tooltip',
860 'namespaces' => $this->namespaces,
861 'parameters' => array( 'advanced' => 1 ),
865 wfRunHooks( 'SpecialSearchProfiles', array( &$profiles ) );
867 foreach( $profiles as $key => &$data ) {
868 sort($data['namespaces']);
874 protected function formHeader( $term, $resultsShown, $totalNum ) {
875 global $wgContLang, $wgLang;
877 $out = Xml::openElement('div', array( 'class' => 'mw-search-formheader' ) );
880 if( $this->startsWithImage( $term ) ) {
882 $bareterm = substr( $term, strpos( $term, ':' ) + 1 );
886 $profiles = $this->getSearchProfiles();
888 // Outputs XML for Search Types
889 $out .= Xml::openElement( 'div', array( 'class' => 'search-types' ) );
890 $out .= Xml::openElement( 'ul' );
891 foreach ( $profiles as $id => $profile ) {
892 $tooltipParam = isset( $profile['namespace-messages'] ) ?
893 $wgLang->commaList( $profile['namespace-messages'] ) : null;
897 'class' => $this->active == $id ? 'current' : 'normal'
899 $this->makeSearchLink(
901 $profile['namespaces'],
902 wfMsg( $profile['message'] ),
903 wfMsg( $profile['tooltip'], $tooltipParam ),
904 isset( $profile['parameters'] ) ? $profile['parameters'] : array()
908 $out .= Xml::closeElement( 'ul' );
909 $out .= Xml::closeElement('div') ;
912 if ( $resultsShown > 0 ) {
913 if ( $totalNum > 0 ){
914 $top = wfMsgExt( 'showingresultsheader', array( 'parseinline' ),
915 $wgLang->formatNum( $this->offset + 1 ),
916 $wgLang->formatNum( $this->offset + $resultsShown ),
917 $wgLang->formatNum( $totalNum ),
918 wfEscapeWikiText( $term ),
919 $wgLang->formatNum( $resultsShown )
921 } elseif ( $resultsShown >= $this->limit ) {
922 $top = wfShowingResults( $this->offset, $this->limit );
924 $top = wfShowingResultsNum( $this->offset, $this->limit, $resultsShown );
926 $out .= Xml::tags( 'div', array( 'class' => 'results-info' ),
927 Xml::tags( 'ul', null, Xml::tags( 'li', null, $top ) )
931 $out .= Xml::element( 'div', array( 'style' => 'clear:both' ), '', false );
932 $out .= Xml::closeElement('div');
934 // Adds hidden namespace fields
935 if ( !$this->searchAdvanced ) {
936 foreach( $this->namespaces as $ns ) {
937 $out .= Xml::hidden( "ns{$ns}", '1' );
944 protected function shortDialog( $term ) {
945 $searchTitle = SpecialPage::getTitleFor( 'Search' );
946 $searchable = SearchEngine::searchableNamespaces();
947 $out = Html::hidden( 'title', $searchTitle->getPrefixedText() ) . "\n";
948 // Keep redirect setting
949 $out .= Html::hidden( "redirs", (int)$this->searchRedirects ) . "\n";
951 $out .= Html::input( 'search', $term, 'search', array(
952 'id' => $this->searchAdvanced ? 'powerSearchText' : 'searchText',
956 $out .= Html::hidden( 'fulltext', 'Search' ) . "\n";
957 $out .= Xml::submitButton( wfMsg( 'searchbutton' ) ) . "\n";
958 return $out . $this->didYouMeanHtml;
961 /** Make a search link with some target namespaces */
962 protected function makeSearchLink( $term, $namespaces, $label, $tooltip, $params=array() ) {
964 foreach( $namespaces as $n ) {
967 $opt['redirs'] = $this->searchRedirects ? 1 : 0;
969 $st = SpecialPage::getTitleFor( 'Search' );
970 $stParams = array_merge(
973 'fulltext' => wfMsg( 'search' )
981 'href' => $st->getLocalURL( $stParams ),
983 'onmousedown' => 'mwSearchHeaderClick(this);',
984 'onkeydown' => 'mwSearchHeaderClick(this);'),
989 /** Check if query starts with image: prefix */
990 protected function startsWithImage( $term ) {
993 $p = explode( ':', $term );
994 if( count( $p ) > 1 ) {
995 return $wgContLang->getNsIndex( $p[0] ) == NS_FILE;
1000 /** Check if query starts with all: prefix */
1001 protected function startsWithAll( $term ) {
1003 $allkeyword = wfMsgForContent('searchall');
1005 $p = explode( ':', $term );
1006 if( count( $p ) > 1 ) {
1007 return $p[0] == $allkeyword;