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' );
370 * Extract "power search" namespace settings from the request object,
371 * returning a list of index numbers to search.
373 * @param WebRequest $request
376 protected function powerSearch( &$request ) {
378 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
379 if( $request->getCheck( 'ns' . $ns ) ) {
387 * Reconstruct the 'power search' options for links
390 protected function powerSearchOptions() {
392 foreach( $this->namespaces
as $n ) {
395 $opt['redirs'] = $this->searchRedirects ?
1 : 0;
396 if( $this->searchAdvanced
) {
397 $opt['advanced'] = $this->searchAdvanced
;
403 * Show whole set of results
405 * @param SearchResultSet $matches
407 protected function showMatches( &$matches ) {
409 wfProfileIn( __METHOD__
);
411 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
414 $infoLine = $matches->getInfo();
415 if( !is_null($infoLine) ) {
416 $out .= "\n<!-- {$infoLine} -->\n";
418 $off = $this->offset +
1;
419 $out .= "<ul class='mw-search-results'>\n";
420 while( $result = $matches->next() ) {
421 $out .= $this->showHit( $result, $terms );
425 // convert the whole thing to desired language variant
426 $out = $wgContLang->convert( $out );
427 wfProfileOut( __METHOD__
);
432 * Format a single hit result
433 * @param SearchResult $result
434 * @param array $terms terms to highlight
436 protected function showHit( $result, $terms ) {
437 global $wgContLang, $wgLang, $wgUser;
438 wfProfileIn( __METHOD__
);
440 if( $result->isBrokenTitle() ) {
441 wfProfileOut( __METHOD__
);
442 return "<!-- Broken link in search result -->\n";
445 $sk = $wgUser->getSkin();
446 $t = $result->getTitle();
448 $titleSnippet = $result->getTitleSnippet($terms);
450 if( $titleSnippet == '' )
451 $titleSnippet = null;
455 wfRunHooks( 'ShowSearchHitTitle',
456 array( &$link_t, &$titleSnippet, $result, $terms, $this ) );
458 $link = $this->sk
->linkKnown(
463 //If page content is not readable, just return the title.
464 //This is not quite safe, but better than showing excerpts from non-readable pages
465 //Note that hiding the entry entirely would screw up paging.
466 if( !$t->userCanRead() ) {
467 wfProfileOut( __METHOD__
);
468 return "<li>{$link}</li>\n";
471 // If the page doesn't *exist*... our search index is out of date.
472 // The least confusing at this point is to drop the result.
473 // You may get less results, but... oh well. :P
474 if( $result->isMissingRevision() ) {
475 wfProfileOut( __METHOD__
);
476 return "<!-- missing page " . htmlspecialchars( $t->getPrefixedText() ) . "-->\n";
479 // format redirects / relevant sections
480 $redirectTitle = $result->getRedirectTitle();
481 $redirectText = $result->getRedirectSnippet($terms);
482 $sectionTitle = $result->getSectionTitle();
483 $sectionText = $result->getSectionSnippet($terms);
486 if( !is_null($redirectTitle) ) {
487 if( $redirectText == '' )
488 $redirectText = null;
490 $redirect = "<span class='searchalttitle'>" .
493 $this->sk
->linkKnown(
504 if( !is_null($sectionTitle) ) {
505 if( $sectionText == '' )
508 $section = "<span class='searchalttitle'>" .
510 'search-section', $this->sk
->linkKnown(
518 // format text extract
519 $extract = "<div class='searchresult'>".$result->getTextSnippet($terms)."</div>";
522 if( is_null( $result->getScore() ) ) {
523 // Search engine doesn't report scoring info
526 $percent = sprintf( '%2.1f', $result->getScore() * 100 );
527 $score = wfMsg( 'search-result-score', $wgLang->formatNum( $percent ) )
531 // format description
532 $byteSize = $result->getByteSize();
533 $wordCount = $result->getWordCount();
534 $timestamp = $result->getTimestamp();
536 'search-result-size',
537 array( 'parsemag', 'escape' ),
538 $this->sk
->formatSize( $byteSize ),
539 $wgLang->formatNum( $wordCount )
541 $date = $wgLang->timeanddate( $timestamp );
543 // link to related articles if supported
545 if( $result->hasRelated() ) {
546 $st = SpecialPage
::getTitleFor( 'Search' );
547 $stParams = array_merge(
548 $this->powerSearchOptions(),
550 'search' => wfMsgForContent( 'searchrelated' ) . ':' . $t->getPrefixedText(),
551 'fulltext' => wfMsg( 'search' )
555 $related = ' -- ' . $sk->linkKnown(
557 wfMsg('search-relatedarticle'),
563 // Include a thumbnail for media files...
564 if( $t->getNamespace() == NS_FILE
) {
565 $img = wfFindFile( $t );
567 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
569 $desc = $img->getShortDesc();
570 wfProfileOut( __METHOD__
);
571 // Float doesn't seem to interact well with the bullets.
572 // Table messes up vertical alignment of the bullets.
573 // Bullets are therefore disabled (didn't look great anyway).
575 '<table class="searchResultImage">' .
577 '<td width="120" align="center" valign="top">' .
578 $thumb->toHtml( array( 'desc-link' => true ) ) .
580 '<td valign="top">' .
583 "<div class='mw-search-result-data'>{$score}{$desc} - {$date}{$related}</div>" .
592 wfProfileOut( __METHOD__
);
593 return "<li>{$link} {$redirect} {$section} {$extract}\n" .
594 "<div class='mw-search-result-data'>{$score}{$size} - {$date}{$related}</div>" .
600 * Show results from other wikis
602 * @param SearchResultSet $matches
604 protected function showInterwiki( &$matches, $query ) {
606 wfProfileIn( __METHOD__
);
607 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
609 $out = "<div id='mw-search-interwiki'><div id='mw-search-interwiki-caption'>".
610 wfMsg('search-interwiki-caption')."</div>\n";
611 $off = $this->offset +
1;
612 $out .= "<ul class='mw-search-iwresults'>\n";
614 // work out custom project captions
615 $customCaptions = array();
616 $customLines = explode("\n",wfMsg('search-interwiki-custom')); // format per line <iwprefix>:<caption>
617 foreach($customLines as $line) {
618 $parts = explode(":",$line,2);
619 if(count($parts) == 2) // validate line
620 $customCaptions[$parts[0]] = $parts[1];
624 while( $result = $matches->next() ) {
625 $out .= $this->showInterwikiHit( $result, $prev, $terms, $query, $customCaptions );
626 $prev = $result->getInterwikiPrefix();
628 // TODO: should support paging in a non-confusing way (not sure how though, maybe via ajax)..
629 $out .= "</ul></div>\n";
631 // convert the whole thing to desired language variant
632 $out = $wgContLang->convert( $out );
633 wfProfileOut( __METHOD__
);
638 * Show single interwiki link
640 * @param SearchResult $result
641 * @param string $lastInterwiki
642 * @param array $terms
643 * @param string $query
644 * @param array $customCaptions iw prefix -> caption
646 protected function showInterwikiHit( $result, $lastInterwiki, $terms, $query, $customCaptions) {
647 wfProfileIn( __METHOD__
);
648 global $wgContLang, $wgLang;
650 if( $result->isBrokenTitle() ) {
651 wfProfileOut( __METHOD__
);
652 return "<!-- Broken link in search result -->\n";
655 $t = $result->getTitle();
657 $titleSnippet = $result->getTitleSnippet($terms);
659 if( $titleSnippet == '' )
660 $titleSnippet = null;
662 $link = $this->sk
->linkKnown(
667 // format redirect if any
668 $redirectTitle = $result->getRedirectTitle();
669 $redirectText = $result->getRedirectSnippet($terms);
671 if( !is_null($redirectTitle) ) {
672 if( $redirectText == '' )
673 $redirectText = null;
675 $redirect = "<span class='searchalttitle'>" .
678 $this->sk
->linkKnown(
687 // display project name
688 if(is_null($lastInterwiki) ||
$lastInterwiki != $t->getInterwiki()) {
689 if( key_exists($t->getInterwiki(),$customCaptions) )
690 // captions from 'search-interwiki-custom'
691 $caption = $customCaptions[$t->getInterwiki()];
693 // default is to show the hostname of the other wiki which might suck
694 // if there are many wikis on one hostname
695 $parsed = parse_url($t->getFullURL());
696 $caption = wfMsg('search-interwiki-default', $parsed['host']);
698 // "more results" link (special page stuff could be localized, but we might not know target lang)
699 $searchTitle = Title
::newFromText($t->getInterwiki().":Special:Search");
700 $searchLink = $this->sk
->linkKnown(
702 wfMsg('search-interwiki-more'),
706 'fulltext' => 'Search'
709 $out .= "</ul><div class='mw-search-interwiki-project'><span class='mw-search-interwiki-more'>
710 {$searchLink}</span>{$caption}</div>\n<ul>";
713 $out .= "<li>{$link} {$redirect}</li>\n";
714 wfProfileOut( __METHOD__
);
720 * Generates the power search box at bottom of [[Special:Search]]
721 * @param $term string: search term
722 * @return $out string: HTML form
724 protected function powerSearchBox( $term ) {
725 global $wgScript, $wgContLang;
727 // Groups namespaces into rows according to subject
729 foreach( SearchEngine
::searchableNamespaces() as $namespace => $name ) {
730 $subject = MWNamespace
::getSubject( $namespace );
731 if( !array_key_exists( $subject, $rows ) ) {
732 $rows[$subject] = "";
734 $name = str_replace( '_', ' ', $name );
736 $name = wfMsg( 'blanknamespace' );
740 'td', array( 'style' => 'white-space: nowrap' )
745 "mw-search-ns{$namespace}",
746 in_array( $namespace, $this->namespaces
)
748 Xml
::closeElement( 'td' );
750 $rows = array_values( $rows );
751 $numRows = count( $rows );
753 // Lays out namespaces in multiple floating two-column tables so they'll
754 // be arranged nicely while still accommodating different screen widths
755 $namespaceTables = '';
756 for( $i = 0; $i < $numRows; $i +
= 4 ) {
757 $namespaceTables .= Xml
::openElement(
759 array( 'cellpadding' => 0, 'cellspacing' => 0, 'border' => 0 )
761 for( $j = $i; $j < $i +
4 && $j < $numRows; $j++
) {
762 $namespaceTables .= Xml
::tags( 'tr', null, $rows[$j] );
764 $namespaceTables .= Xml
::closeElement( 'table' );
766 // Show redirects check only if backend supports it
768 if( $this->searchEngine
->acceptListRedirects() ) {
771 'redirs', $this->searchRedirects
, array( 'value' => '1', 'id' => 'redirs' )
774 Xml
::label( wfMsg( 'powersearch-redir' ), 'redirs' );
776 // Return final output
780 array( 'id' => 'mw-searchoptions', 'style' => 'margin:0em;' )
782 Xml
::element( 'legend', null, wfMsg('powersearch-legend') ) .
783 Xml
::tags( 'h4', null, wfMsgExt( 'powersearch-ns', array( 'parseinline' ) ) ) .
786 array( 'id' => 'mw-search-togglebox' ),
787 Xml
::label( wfMsg( 'powersearch-togglelabel' ), 'mw-search-togglelabel' ) .
792 'id' => 'mw-search-toggleall',
793 'onclick' => 'mwToggleSearchCheckboxes("all");',
794 'value' => wfMsg( 'powersearch-toggleall' )
801 'id' => 'mw-search-togglenone',
802 'onclick' => 'mwToggleSearchCheckboxes("none");',
803 'value' => wfMsg( 'powersearch-togglenone' )
807 Xml
::element( 'div', array( 'class' => 'divider' ), '', false ) .
809 Xml
::element( 'div', array( 'class' => 'divider' ), '', false ) .
811 Xml
::hidden( 'title', SpecialPage
::getTitleFor( 'Search' )->getPrefixedText() ) .
812 Xml
::hidden( 'advanced', $this->searchAdvanced
) .
813 Xml
::hidden( 'fulltext', 'Advanced search' ) .
814 Xml
::closeElement( 'fieldset' );
817 protected function searchFocus() {
818 $id = $this->searchAdvanced ?
'powerSearchText' : 'searchText';
819 return Html
::inlineScript(
820 "hookEvent(\"load\", function() {" .
821 "document.getElementById('$id').focus();" .
825 protected function getSearchProfiles() {
826 // Builds list of Search Types (profiles)
827 $nsAllSet = array_keys( SearchEngine
::searchableNamespaces() );
831 'message' => 'searchprofile-articles',
832 'tooltip' => 'searchprofile-articles-tooltip',
833 'namespaces' => SearchEngine
::defaultNamespaces(),
834 'namespace-messages' => SearchEngine
::namespacesAsText(
835 SearchEngine
::defaultNamespaces()
839 'message' => 'searchprofile-images',
840 'tooltip' => 'searchprofile-images-tooltip',
841 'namespaces' => array( NS_FILE
),
844 'message' => 'searchprofile-project',
845 'tooltip' => 'searchprofile-project-tooltip',
846 'namespaces' => SearchEngine
::helpNamespaces(),
847 'namespace-messages' => SearchEngine
::namespacesAsText(
848 SearchEngine
::helpNamespaces()
852 'message' => 'searchprofile-everything',
853 'tooltip' => 'searchprofile-everything-tooltip',
854 'namespaces' => $nsAllSet,
857 'message' => 'searchprofile-advanced',
858 'tooltip' => 'searchprofile-advanced-tooltip',
859 'namespaces' => $this->namespaces
,
860 'parameters' => array( 'advanced' => 1 ),
864 wfRunHooks( 'SpecialSearchProfiles', array( &$profiles ) );
866 foreach( $profiles as $key => &$data ) {
867 sort($data['namespaces']);
873 protected function formHeader( $term, $resultsShown, $totalNum ) {
874 global $wgContLang, $wgLang;
876 $out = Xml
::openElement('div', array( 'class' => 'mw-search-formheader' ) );
879 if( $this->startsWithImage( $term ) ) {
881 $bareterm = substr( $term, strpos( $term, ':' ) +
1 );
885 $profiles = $this->getSearchProfiles();
887 // Outputs XML for Search Types
888 $out .= Xml
::openElement( 'div', array( 'class' => 'search-types' ) );
889 $out .= Xml
::openElement( 'ul' );
890 foreach ( $profiles as $id => $profile ) {
891 $tooltipParam = isset( $profile['namespace-messages'] ) ?
892 $wgLang->commaList( $profile['namespace-messages'] ) : null;
896 'class' => $this->active
== $id ?
'current' : 'normal'
898 $this->makeSearchLink(
900 $profile['namespaces'],
901 wfMsg( $profile['message'] ),
902 wfMsg( $profile['tooltip'], $tooltipParam ),
903 isset( $profile['parameters'] ) ?
$profile['parameters'] : array()
907 $out .= Xml
::closeElement( 'ul' );
908 $out .= Xml
::closeElement('div') ;
911 if ( $resultsShown > 0 ) {
912 if ( $totalNum > 0 ){
913 $top = wfMsgExt( 'showingresultsheader', array( 'parseinline' ),
914 $wgLang->formatNum( $this->offset +
1 ),
915 $wgLang->formatNum( $this->offset +
$resultsShown ),
916 $wgLang->formatNum( $totalNum ),
917 wfEscapeWikiText( $term ),
918 $wgLang->formatNum( $resultsShown )
920 } elseif ( $resultsShown >= $this->limit
) {
921 $top = wfShowingResults( $this->offset
, $this->limit
);
923 $top = wfShowingResultsNum( $this->offset
, $this->limit
, $resultsShown );
925 $out .= Xml
::tags( 'div', array( 'class' => 'results-info' ),
926 Xml
::tags( 'ul', null, Xml
::tags( 'li', null, $top ) )
930 $out .= Xml
::element( 'div', array( 'style' => 'clear:both' ), '', false );
931 $out .= Xml
::closeElement('div');
933 // Adds hidden namespace fields
934 if ( !$this->searchAdvanced
) {
935 foreach( $this->namespaces
as $ns ) {
936 $out .= Xml
::hidden( "ns{$ns}", '1' );
943 protected function shortDialog( $term ) {
944 $searchTitle = SpecialPage
::getTitleFor( 'Search' );
945 $searchable = SearchEngine
::searchableNamespaces();
946 $out = Html
::hidden( 'title', $searchTitle->getPrefixedText() ) . "\n";
947 // Keep redirect setting
948 $out .= Html
::hidden( "redirs", (int)$this->searchRedirects
) . "\n";
950 $out .= Html
::input( 'search', $term, 'search', array(
951 'id' => $this->searchAdvanced ?
'powerSearchText' : 'searchText',
955 $out .= Html
::hidden( 'fulltext', 'Search' ) . "\n";
956 $out .= Xml
::submitButton( wfMsg( 'searchbutton' ) ) . "\n";
957 return $out . $this->didYouMeanHtml
;
960 /** Make a search link with some target namespaces */
961 protected function makeSearchLink( $term, $namespaces, $label, $tooltip, $params=array() ) {
963 foreach( $namespaces as $n ) {
966 $opt['redirs'] = $this->searchRedirects ?
1 : 0;
968 $st = SpecialPage
::getTitleFor( 'Search' );
969 $stParams = array_merge(
972 'fulltext' => wfMsg( 'search' )
980 'href' => $st->getLocalURL( $stParams ),
982 'onmousedown' => 'mwSearchHeaderClick(this);',
983 'onkeydown' => 'mwSearchHeaderClick(this);'),
988 /** Check if query starts with image: prefix */
989 protected function startsWithImage( $term ) {
992 $p = explode( ':', $term );
993 if( count( $p ) > 1 ) {
994 return $wgContLang->getNsIndex( $p[0] ) == NS_FILE
;
999 /** Check if query starts with all: prefix */
1000 protected function startsWithAll( $term ) {
1002 $allkeyword = wfMsgForContent('searchall');
1004 $p = explode( ':', $term );
1005 if( count( $p ) > 1 ) {
1006 return $p[0] == $allkeyword;