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, $wgUseOldSearchUI;
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'>" );
262 // show direct page/create link
264 if( !$t->exists() ) {
265 $wgOut->addWikiMsg( 'searchmenu-new', wfEscapeWikiText( $t->getPrefixedText() ) );
267 $wgOut->addWikiMsg( 'searchmenu-exists', wfEscapeWikiText( $t->getPrefixedText() ) );
270 // preserve the paragraph for margins etc...
271 $wgOut->addHTML('<p></p>');
275 if( $num ||
$this->offset
) {
276 $prevnext = wfViewPrevNext( $this->offset
, $this->limit
,
277 SpecialPage
::getTitleFor( 'Search' ),
278 wfArrayToCGI( $this->powerSearchOptions(), array( 'search' => $term ) ),
279 max( $titleMatchesNum, $textMatchesNum ) < $this->limit
281 //$wgOut->addHTML( "<p class='mw-search-pager-top'>{$prevnext}</p>\n" );
282 wfRunHooks( 'SpecialSearchResults', array( $term, &$titleMatches, &$textMatches ) );
284 wfRunHooks( 'SpecialSearchNoResults', array( $term ) );
287 if( $titleMatches ) {
288 if( $numTitleMatches > 0 ) {
289 $wgOut->wrapWikiMsg( "==$1==\n", 'titlematches' );
290 $wgOut->addHTML( $this->showMatches( $titleMatches ) );
292 $titleMatches->free();
295 // output appropriate heading
296 if( $numTextMatches > 0 && $numTitleMatches > 0 ) {
297 // if no title matches the heading is redundant
298 $wgOut->wrapWikiMsg( "==$1==\n", 'textmatches' );
299 } elseif( $totalRes == 0 ) {
300 # Don't show the 'no text matches' if we received title matches
301 # $wgOut->wrapWikiMsg( "==$1==\n", 'notextmatches' );
303 // show interwiki results if any
304 if( $textMatches->hasInterwikiResults() ) {
305 $wgOut->addHTML( $this->showInterwiki( $textMatches->getInterwikiResults(), $term ) );
308 if( $numTextMatches > 0 ) {
309 $wgOut->addHTML( $this->showMatches( $textMatches ) );
312 $textMatches->free();
315 $wgOut->addWikiMsg( 'search-nonefound', wfEscapeWikiText( $term ) );
317 $wgOut->addHtml( "</div>" );
319 $wgOut->addHTML( $this->searchFocus() );
322 if( $num ||
$this->offset
) {
323 $wgOut->addHTML( "<p class='mw-search-pager-bottom'>{$prevnext}</p>\n" );
325 wfProfileOut( __METHOD__
);
331 protected function setupPage( $term ) {
333 // Figure out the active search profile header
334 $nsAllSet = array_keys( SearchEngine
::searchableNamespaces() );
335 if( $this->searchAdvanced
)
336 $this->active
= 'advanced';
337 else if( $this->namespaces
=== array(NS_FILE
) ||
$this->startsWithImage( $term ) )
338 $this->active
= 'images';
339 elseif( $this->namespaces
=== $nsAllSet ||
$this->startsWithAll( $term ) )
340 $this->active
= 'all';
341 elseif( $this->namespaces
=== SearchEngine
::defaultNamespaces() )
342 $this->active
= 'default';
343 elseif( $this->namespaces
=== SearchEngine
::helpNamespaces() )
344 $this->active
= 'help';
346 $this->active
= 'advanced';
347 # Should advanced UI be used?
348 $this->searchAdvanced
= ($this->active
=== 'advanced');
349 if( !empty( $term ) ) {
350 $wgOut->setPageTitle( wfMsg( 'searchresults') );
351 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'searchresults-title', $term ) ) );
353 $wgOut->setArticleRelated( false );
354 $wgOut->setRobotPolicy( 'noindex,nofollow' );
355 // add javascript specific to special:search
356 $wgOut->addScriptFile( 'search.js' );
360 * Extract "power search" namespace settings from the request object,
361 * returning a list of index numbers to search.
363 * @param WebRequest $request
366 protected function powerSearch( &$request ) {
368 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
369 if( $request->getCheck( 'ns' . $ns ) ) {
377 * Reconstruct the 'power search' options for links
380 protected function powerSearchOptions() {
382 foreach( $this->namespaces
as $n ) {
385 $opt['redirs'] = $this->searchRedirects ?
1 : 0;
386 if( $this->searchAdvanced
) {
387 $opt['advanced'] = $this->searchAdvanced
;
393 * Show whole set of results
395 * @param SearchResultSet $matches
397 protected function showMatches( &$matches ) {
399 wfProfileIn( __METHOD__
);
401 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
404 $infoLine = $matches->getInfo();
405 if( !is_null($infoLine) ) {
406 $out .= "\n<!-- {$infoLine} -->\n";
408 $off = $this->offset +
1;
409 $out .= "<ul class='mw-search-results'>\n";
410 while( $result = $matches->next() ) {
411 $out .= $this->showHit( $result, $terms );
415 // convert the whole thing to desired language variant
416 $out = $wgContLang->convert( $out );
417 wfProfileOut( __METHOD__
);
422 * Format a single hit result
423 * @param SearchResult $result
424 * @param array $terms terms to highlight
426 protected function showHit( $result, $terms ) {
427 global $wgContLang, $wgLang, $wgUser;
428 wfProfileIn( __METHOD__
);
430 if( $result->isBrokenTitle() ) {
431 wfProfileOut( __METHOD__
);
432 return "<!-- Broken link in search result -->\n";
435 $sk = $wgUser->getSkin();
436 $t = $result->getTitle();
438 $titleSnippet = $result->getTitleSnippet($terms);
440 if( $titleSnippet == '' )
441 $titleSnippet = null;
445 wfRunHooks( 'ShowSearchHitTitle',
446 array( &$link_t, &$titleSnippet, $result, $terms, $this ) );
448 $link = $this->sk
->linkKnown(
453 //If page content is not readable, just return the title.
454 //This is not quite safe, but better than showing excerpts from non-readable pages
455 //Note that hiding the entry entirely would screw up paging.
456 if( !$t->userCanRead() ) {
457 wfProfileOut( __METHOD__
);
458 return "<li>{$link}</li>\n";
461 // If the page doesn't *exist*... our search index is out of date.
462 // The least confusing at this point is to drop the result.
463 // You may get less results, but... oh well. :P
464 if( $result->isMissingRevision() ) {
465 wfProfileOut( __METHOD__
);
466 return "<!-- missing page " . htmlspecialchars( $t->getPrefixedText() ) . "-->\n";
469 // format redirects / relevant sections
470 $redirectTitle = $result->getRedirectTitle();
471 $redirectText = $result->getRedirectSnippet($terms);
472 $sectionTitle = $result->getSectionTitle();
473 $sectionText = $result->getSectionSnippet($terms);
476 if( !is_null($redirectTitle) ) {
477 if( $redirectText == '' )
478 $redirectText = null;
480 $redirect = "<span class='searchalttitle'>" .
483 $this->sk
->linkKnown(
494 if( !is_null($sectionTitle) ) {
495 if( $sectionText == '' )
498 $section = "<span class='searchalttitle'>" .
500 'search-section', $this->sk
->linkKnown(
508 // format text extract
509 $extract = "<div class='searchresult'>".$result->getTextSnippet($terms)."</div>";
512 if( is_null( $result->getScore() ) ) {
513 // Search engine doesn't report scoring info
516 $percent = sprintf( '%2.1f', $result->getScore() * 100 );
517 $score = wfMsg( 'search-result-score', $wgLang->formatNum( $percent ) )
521 // format description
522 $byteSize = $result->getByteSize();
523 $wordCount = $result->getWordCount();
524 $timestamp = $result->getTimestamp();
526 'search-result-size',
527 array( 'parsemag', 'escape' ),
528 $this->sk
->formatSize( $byteSize ),
531 $date = $wgLang->timeanddate( $timestamp );
533 // link to related articles if supported
535 if( $result->hasRelated() ) {
536 $st = SpecialPage
::getTitleFor( 'Search' );
537 $stParams = array_merge(
538 $this->powerSearchOptions(),
540 'search' => wfMsgForContent( 'searchrelated' ) . ':' . $t->getPrefixedText(),
541 'fulltext' => wfMsg( 'search' )
545 $related = ' -- ' . $sk->linkKnown(
547 wfMsg('search-relatedarticle'),
553 // Include a thumbnail for media files...
554 if( $t->getNamespace() == NS_FILE
) {
555 $img = wfFindFile( $t );
557 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
559 $desc = $img->getShortDesc();
560 wfProfileOut( __METHOD__
);
561 // Float doesn't seem to interact well with the bullets.
562 // Table messes up vertical alignment of the bullets.
563 // Bullets are therefore disabled (didn't look great anyway).
565 '<table class="searchResultImage">' .
567 '<td width="120" align="center" valign="top">' .
568 $thumb->toHtml( array( 'desc-link' => true ) ) .
570 '<td valign="top">' .
573 "<div class='mw-search-result-data'>{$score}{$desc} - {$date}{$related}</div>" .
582 wfProfileOut( __METHOD__
);
583 return "<li>{$link} {$redirect} {$section} {$extract}\n" .
584 "<div class='mw-search-result-data'>{$score}{$size} - {$date}{$related}</div>" .
590 * Show results from other wikis
592 * @param SearchResultSet $matches
594 protected function showInterwiki( &$matches, $query ) {
596 wfProfileIn( __METHOD__
);
597 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
599 $out = "<div id='mw-search-interwiki'><div id='mw-search-interwiki-caption'>".
600 wfMsg('search-interwiki-caption')."</div>\n";
601 $off = $this->offset +
1;
602 $out .= "<ul class='mw-search-iwresults'>\n";
604 // work out custom project captions
605 $customCaptions = array();
606 $customLines = explode("\n",wfMsg('search-interwiki-custom')); // format per line <iwprefix>:<caption>
607 foreach($customLines as $line) {
608 $parts = explode(":",$line,2);
609 if(count($parts) == 2) // validate line
610 $customCaptions[$parts[0]] = $parts[1];
614 while( $result = $matches->next() ) {
615 $out .= $this->showInterwikiHit( $result, $prev, $terms, $query, $customCaptions );
616 $prev = $result->getInterwikiPrefix();
618 // TODO: should support paging in a non-confusing way (not sure how though, maybe via ajax)..
619 $out .= "</ul></div>\n";
621 // convert the whole thing to desired language variant
622 $out = $wgContLang->convert( $out );
623 wfProfileOut( __METHOD__
);
628 * Show single interwiki link
630 * @param SearchResult $result
631 * @param string $lastInterwiki
632 * @param array $terms
633 * @param string $query
634 * @param array $customCaptions iw prefix -> caption
636 protected function showInterwikiHit( $result, $lastInterwiki, $terms, $query, $customCaptions) {
637 wfProfileIn( __METHOD__
);
638 global $wgContLang, $wgLang;
640 if( $result->isBrokenTitle() ) {
641 wfProfileOut( __METHOD__
);
642 return "<!-- Broken link in search result -->\n";
645 $t = $result->getTitle();
647 $titleSnippet = $result->getTitleSnippet($terms);
649 if( $titleSnippet == '' )
650 $titleSnippet = null;
652 $link = $this->sk
->linkKnown(
657 // format redirect if any
658 $redirectTitle = $result->getRedirectTitle();
659 $redirectText = $result->getRedirectSnippet($terms);
661 if( !is_null($redirectTitle) ) {
662 if( $redirectText == '' )
663 $redirectText = null;
665 $redirect = "<span class='searchalttitle'>" .
668 $this->sk
->linkKnown(
677 // display project name
678 if(is_null($lastInterwiki) ||
$lastInterwiki != $t->getInterwiki()) {
679 if( key_exists($t->getInterwiki(),$customCaptions) )
680 // captions from 'search-interwiki-custom'
681 $caption = $customCaptions[$t->getInterwiki()];
683 // default is to show the hostname of the other wiki which might suck
684 // if there are many wikis on one hostname
685 $parsed = parse_url($t->getFullURL());
686 $caption = wfMsg('search-interwiki-default', $parsed['host']);
688 // "more results" link (special page stuff could be localized, but we might not know target lang)
689 $searchTitle = Title
::newFromText($t->getInterwiki().":Special:Search");
690 $searchLink = $this->sk
->linkKnown(
692 wfMsg('search-interwiki-more'),
696 'fulltext' => 'Search'
699 $out .= "</ul><div class='mw-search-interwiki-project'><span class='mw-search-interwiki-more'>
700 {$searchLink}</span>{$caption}</div>\n<ul>";
703 $out .= "<li>{$link} {$redirect}</li>\n";
704 wfProfileOut( __METHOD__
);
710 * Generates the power search box at bottom of [[Special:Search]]
711 * @param $term string: search term
712 * @return $out string: HTML form
714 protected function powerSearchBox( $term ) {
715 global $wgScript, $wgContLang;
717 // Groups namespaces into rows according to subject
719 foreach( SearchEngine
::searchableNamespaces() as $namespace => $name ) {
720 $subject = MWNamespace
::getSubject( $namespace );
721 if( !array_key_exists( $subject, $rows ) ) {
722 $rows[$subject] = "";
724 $name = str_replace( '_', ' ', $name );
726 $name = wfMsg( 'blanknamespace' );
730 'td', array( 'style' => 'white-space: nowrap' )
735 "mw-search-ns{$namespace}",
736 in_array( $namespace, $this->namespaces
)
738 Xml
::closeElement( 'td' );
740 $rows = array_values( $rows );
741 $numRows = count( $rows );
743 // Lays out namespaces in multiple floating two-column tables so they'll
744 // be arranged nicely while still accommodating different screen widths
745 $namespaceTables = '';
746 for( $i = 0; $i < $numRows; $i +
= 4 ) {
747 $namespaceTables .= Xml
::openElement(
749 array( 'cellpadding' => 0, 'cellspacing' => 0, 'border' => 0 )
751 for( $j = $i; $j < $i +
4 && $j < $numRows; $j++
) {
752 $namespaceTables .= Xml
::tags( 'tr', null, $rows[$j] );
754 $namespaceTables .= Xml
::closeElement( 'table' );
756 // Show redirects check only if backend supports it
758 if( $this->searchEngine
->acceptListRedirects() ) {
761 'redirs', $this->searchRedirects
, array( 'value' => '1', 'id' => 'redirs' )
764 Xml
::label( wfMsg( 'powersearch-redir' ), 'redirs' );
766 // Return final output
770 array( 'id' => 'mw-searchoptions', 'style' => 'margin:0em;' )
772 Xml
::element( 'legend', null, wfMsg('powersearch-legend') ) .
773 Xml
::tags( 'h4', null, wfMsgExt( 'powersearch-ns', array( 'parseinline' ) ) ) .
776 array( 'id' => 'mw-search-togglebox' ),
777 Xml
::label( wfMsg( 'powersearch-togglelabel' ), 'mw-search-togglelabel' ) .
782 'id' => 'mw-search-toggleall',
783 'onclick' => 'mwToggleSearchCheckboxes("all");',
784 'value' => wfMsg( 'powersearch-toggleall' )
791 'id' => 'mw-search-togglenone',
792 'onclick' => 'mwToggleSearchCheckboxes("none");',
793 'value' => wfMsg( 'powersearch-togglenone'
798 Xml
::element( 'div', array( 'class' => 'divider' ), '', false ) .
800 Xml
::element( 'div', array( 'class' => 'divider' ), '', false ) .
802 Xml
::hidden( 'title', SpecialPage
::getTitleFor( 'Search' )->getPrefixedText() ) .
803 Xml
::hidden( 'advanced', $this->searchAdvanced
) .
804 Xml
::hidden( 'fulltext', 'Advanced search' ) .
805 Xml
::closeElement( 'fieldset' );
808 protected function searchFocus() {
809 return Html
::inlineScript(
810 "hookEvent(\"load\", function() {" .
811 "document.getElementById('searchText').focus();" .
815 protected function formHeader( $term, $resultsShown, $totalNum ) {
816 global $wgContLang, $wgCanonicalNamespaceNames, $wgLang;
818 $out = Xml
::openElement('div', array( 'class' => 'mw-search-formheader' ) );
821 if( $this->startsWithImage( $term ) ) {
823 $bareterm = substr( $term, strpos( $term, ':' ) +
1 );
825 $nsAllSet = array_keys( SearchEngine
::searchableNamespaces() );
827 // Builds list of Search Types (profiles)
830 'message' => 'searchprofile-articles',
831 'tooltip' => 'searchprofile-articles-tooltip',
832 'namespaces' => SearchEngine
::defaultNamespaces(),
833 'namespace-messages' => SearchEngine
::namespacesAsText(
834 SearchEngine
::defaultNamespaces()
838 'message' => 'searchprofile-images',
839 'tooltip' => 'searchprofile-images-tooltip',
840 'namespaces' => array( NS_FILE
),
843 'message' => 'searchprofile-project',
844 'tooltip' => 'searchprofile-project-tooltip',
845 'namespaces' => SearchEngine
::helpNamespaces(),
846 'namespace-messages' => SearchEngine
::namespacesAsText(
847 SearchEngine
::helpNamespaces()
851 'message' => 'searchprofile-everything',
852 'tooltip' => 'searchprofile-everything-tooltip',
853 'namespaces' => $nsAllSet,
856 'message' => 'searchprofile-advanced',
857 'tooltip' => 'searchprofile-advanced-tooltip',
858 'namespaces' => $this->namespaces
,
859 'parameters' => array( 'advanced' => 1 ),
862 // Outputs XML for Search Types
863 $out .= Xml
::openElement( 'div', array( 'class' => 'search-types' ) );
864 $out .= Xml
::openElement( 'ul' );
865 foreach ( $profiles as $id => $profile ) {
866 $tooltipParam = isset( $profile['namespace-messages'] ) ?
867 $wgLang->commaList( $profile['namespace-messages'] ) : null;
871 'class' => $this->active
== $id ?
'current' : 'normal'
873 $this->makeSearchLink(
875 $profile['namespaces'],
876 wfMsg( $profile['message'] ),
877 wfMsg( $profile['tooltip'], $tooltipParam ),
878 isset( $profile['parameters'] ) ?
$profile['parameters'] : array()
882 $out .= Xml
::closeElement( 'ul' );
883 $out .= Xml
::closeElement('div') ;
886 if ( $resultsShown > 0 ) {
887 if ( $totalNum > 0 ){
888 $top = wfMsgExt('showingresultsheader', array( 'parseinline' ),
889 $this->offset+
1, $this->offset+
$resultsShown, $totalNum, wfEscapeWikiText($term), $resultsShown );
890 } elseif ( $resultsShown >= $this->limit
) {
891 $top = wfShowingResults( $this->offset
, $this->limit
);
893 $top = wfShowingResultsNum( $this->offset
, $this->limit
, $resultsShown );
895 $out .= Xml
::tags( 'div', array( 'class' => 'results-info' ),
896 Xml
::tags( 'ul', null, Xml
::tags( 'li', null, $top ) )
900 $out .= Xml
::element( 'div', array( 'style' => 'clear:both' ), '', false );
901 $out .= Xml
::closeElement('div');
903 // Adds hidden namespace fields
904 if ( !$this->searchAdvanced
) {
905 foreach( $this->namespaces
as $ns ) {
906 $out .= Xml
::hidden( "ns{$ns}", '1' );
913 protected function shortDialog( $term ) {
914 $searchTitle = SpecialPage
::getTitleFor( 'Search' );
915 $searchable = SearchEngine
::searchableNamespaces();
916 $out = Xml
::hidden( 'title', $searchTitle->getPrefixedText() ) . "\n";
917 // Keep redirect setting
918 $out .= Xml
::hidden( "redirs", (int)$this->searchRedirects
);
920 $out .= Xml
::input( 'search', 50, $term, array( 'type' => 'text', 'id' => 'searchText' ) ) . "\n";
921 $out .= Xml
::hidden( 'fulltext', 'Search' );
922 $out .= Xml
::submitButton( wfMsg( 'searchbutton' ) );
923 return $out . $this->didYouMeanHtml
;
926 /** Make a search link with some target namespaces */
927 protected function makeSearchLink( $term, $namespaces, $label, $tooltip, $params=array() ) {
929 foreach( $namespaces as $n ) {
932 $opt['redirs'] = $this->searchRedirects ?
1 : 0;
934 $st = SpecialPage
::getTitleFor( 'Search' );
935 $stParams = array_merge(
938 'fulltext' => wfMsg( 'search' )
946 'href' => $st->getLocalURL( $stParams ),
948 'onmousedown' => 'mwSearchHeaderClick(this);',
949 'onkeydown' => 'mwSearchHeaderClick(this);'),
954 /** Check if query starts with image: prefix */
955 protected function startsWithImage( $term ) {
958 $p = explode( ':', $term );
959 if( count( $p ) > 1 ) {
960 return $wgContLang->getNsIndex( $p[0] ) == NS_FILE
;
965 /** Check if query starts with all: prefix */
966 protected function startsWithAll( $term ) {
968 $allkeyword = wfMsgForContent('searchall');
970 $p = explode( ':', $term );
971 if( count( $p ) > 1 ) {
972 return $p[0] == $allkeyword;