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 * Current search profile. Search profile is just a name that identifies
33 * the active search tab on the search page (content, discussions...)
34 * For users tt replaces the set of enabled namespaces from the query
35 * string when applicable. Extensions can add new profiles with hooks
36 * with custom search options just for that profile.
41 /** @var SearchEngine Search engine */
42 protected $searchEngine;
44 /** @var string Search engine type, if not default */
45 protected $searchEngineType;
47 /** @var array For links */
48 protected $extraParams = array();
50 /** @var string No idea, apparently used by some other classes */
56 protected $limit, $offset;
61 protected $namespaces;
66 protected $didYouMeanHtml, $fulltext;
68 const NAMESPACES_CURRENT
= 'sense';
70 public function __construct() {
71 parent
::__construct( 'Search' );
79 public function execute( $par ) {
81 $this->outputHeader();
82 $out = $this->getOutput();
83 $out->allowClickjacking();
84 $out->addModuleStyles( array(
85 'mediawiki.special', 'mediawiki.special.search', 'mediawiki.ui', 'mediawiki.ui.button',
89 // Strip underscores from title parameter; most of the time we'll want
90 // text form here. But don't strip underscores from actual text params!
91 $titleParam = str_replace( '_', ' ', $par );
93 $request = $this->getRequest();
95 // Fetch the search term
96 $search = str_replace( "\n", " ", $request->getText( 'search', $titleParam ) );
99 if ( !is_null( $request->getVal( 'nsRemember' ) ) ) {
100 $this->saveNamespaces();
101 // Remove the token from the URL to prevent the user from inadvertently
102 // exposing it (e.g. by pasting it into a public wiki page) or undoing
103 // later settings changes (e.g. by reloading the page).
104 $query = $request->getValues();
105 unset( $query['title'], $query['nsRemember'] );
106 $out->redirect( $this->getPageTitle()->getFullURL( $query ) );
110 $this->searchEngineType
= $request->getVal( 'srbackend' );
112 if ( $request->getVal( 'fulltext' )
113 ||
!is_null( $request->getVal( 'offset' ) )
115 $this->showResults( $search );
117 $this->goResult( $search );
122 * Set up basic search parameters from the request and user settings.
124 * @see tests/phpunit/includes/specials/SpecialSearchTest.php
126 public function load() {
127 $request = $this->getRequest();
128 list( $this->limit
, $this->offset
) = $request->getLimitOffset( 20, '' );
129 $this->mPrefix
= $request->getVal( 'prefix', '' );
131 $user = $this->getUser();
133 # Extract manually requested namespaces
134 $nslist = $this->powerSearch( $request );
135 if ( !count( $nslist ) ) {
136 # Fallback to user preference
137 $nslist = SearchEngine
::userNamespaces( $user );
141 if ( !count( $nslist ) ) {
142 $profile = 'default';
145 $profile = $request->getVal( 'profile', $profile );
146 $profiles = $this->getSearchProfiles();
147 if ( $profile === null ) {
148 // BC with old request format
149 $profile = 'advanced';
150 foreach ( $profiles as $key => $data ) {
151 if ( $nslist === $data['namespaces'] && $key !== 'advanced' ) {
155 $this->namespaces
= $nslist;
156 } elseif ( $profile === 'advanced' ) {
157 $this->namespaces
= $nslist;
159 if ( isset( $profiles[$profile]['namespaces'] ) ) {
160 $this->namespaces
= $profiles[$profile]['namespaces'];
162 // Unknown profile requested
163 $profile = 'default';
164 $this->namespaces
= $profiles['default']['namespaces'];
168 $this->didYouMeanHtml
= ''; # html of did you mean... link
169 $this->fulltext
= $request->getVal( 'fulltext' );
170 $this->profile
= $profile;
174 * If an exact title match can be found, jump straight ahead to it.
176 * @param string $term
178 public function goResult( $term ) {
179 $this->setupPage( $term );
180 # Try to go to page as entered.
181 $title = Title
::newFromText( $term );
182 # If the string cannot be used to create a title
183 if ( is_null( $title ) ) {
184 $this->showResults( $term );
188 # If there's an exact or very near match, jump right there.
189 $title = SearchEngine
::getNearMatch( $term );
191 if ( !is_null( $title ) ) {
192 $this->getOutput()->redirect( $title->getFullURL() );
196 # No match, generate an edit URL
197 $title = Title
::newFromText( $term );
198 if ( !is_null( $title ) ) {
199 wfRunHooks( 'SpecialSearchNogomatch', array( &$title ) );
201 $this->showResults( $term );
205 * @param string $term
207 public function showResults( $term ) {
210 $profile = new ProfileSection( __METHOD__
);
211 $search = $this->getSearchEngine();
212 $search->setLimitOffset( $this->limit
, $this->offset
);
213 $search->setNamespaces( $this->namespaces
);
214 $search->prefix
= $this->mPrefix
;
215 $term = $search->transformSearchTerm( $term );
217 wfRunHooks( 'SpecialSearchSetupEngine', array( $this, $this->profile
, $search ) );
219 $this->setupPage( $term );
221 $out = $this->getOutput();
223 if ( $this->getConfig()->get( 'DisableTextSearch' ) ) {
224 $searchFowardUrl = $this->getConfig()->get( 'SearchForwardUrl' );
225 if ( $searchFowardUrl ) {
226 $url = str_replace( '$1', urlencode( $term ), $searchFowardUrl );
227 $out->redirect( $url );
230 Xml
::openElement( 'fieldset' ) .
231 Xml
::element( 'legend', null, $this->msg( 'search-external' )->text() ) .
234 array( 'class' => 'mw-searchdisabled' ),
235 $this->msg( 'searchdisabled' )->text()
237 $this->msg( 'googlesearch' )->rawParams(
238 htmlspecialchars( $term ),
240 $this->msg( 'searchbutton' )->escaped()
242 Xml
::closeElement( 'fieldset' )
249 $title = Title
::newFromText( $term );
250 $showSuggestion = $title === null ||
!$title->isKnown();
251 $search->setShowSuggestion( $showSuggestion );
253 // fetch search results
254 $rewritten = $search->replacePrefixes( $term );
256 $titleMatches = $search->searchTitle( $rewritten );
257 $textMatches = $search->searchText( $rewritten );
260 if ( $textMatches instanceof Status
) {
261 $textStatus = $textMatches;
265 // did you mean... suggestions
266 if ( $showSuggestion && $textMatches && !$textStatus && $textMatches->hasSuggestion() ) {
267 # mirror Go/Search behavior of original request ..
268 $didYouMeanParams = array( 'search' => $textMatches->getSuggestionQuery() );
270 if ( $this->fulltext
!= null ) {
271 $didYouMeanParams['fulltext'] = $this->fulltext
;
274 $stParams = array_merge(
276 $this->powerSearchOptions()
279 $suggestionSnippet = $textMatches->getSuggestionSnippet();
281 if ( $suggestionSnippet == '' ) {
282 $suggestionSnippet = null;
285 $suggestLink = Linker
::linkKnown(
286 $this->getPageTitle(),
292 $this->didYouMeanHtml
= '<div class="searchdidyoumean">'
293 . $this->msg( 'search-suggest' )->rawParams( $suggestLink )->text() . '</div>';
296 if ( !wfRunHooks( 'SpecialSearchResultsPrepend', array( $this, $out, $term ) ) ) {
297 # Hook requested termination
301 // start rendering the page
306 'id' => ( $this->profile
=== 'advanced' ?
'powersearch' : 'search' ),
308 'action' => wfScript(),
313 // Get number of results
314 $titleMatchesNum = $textMatchesNum = $numTitleMatches = $numTextMatches = 0;
315 if ( $titleMatches ) {
316 $titleMatchesNum = $titleMatches->numRows();
317 $numTitleMatches = $titleMatches->getTotalHits();
319 if ( $textMatches ) {
320 $textMatchesNum = $textMatches->numRows();
321 $numTextMatches = $textMatches->getTotalHits();
323 $num = $titleMatchesNum +
$textMatchesNum;
324 $totalRes = $numTitleMatches +
$numTextMatches;
327 # This is an awful awful ID name. It's not a table, but we
328 # named it poorly from when this was a table so now we're
330 Xml
::openElement( 'div', array( 'id' => 'mw-search-top-table' ) ) .
331 $this->shortDialog( $term, $num, $totalRes ) .
332 Xml
::closeElement( 'div' ) .
333 $this->formHeader( $term ) .
334 Xml
::closeElement( 'form' )
337 $filePrefix = $wgContLang->getFormattedNsText( NS_FILE
) . ':';
338 if ( trim( $term ) === '' ||
$filePrefix === trim( $term ) ) {
339 // Empty query -- straight view of search form
343 $out->addHtml( "<div class='searchresults'>" );
347 if ( $num ||
$this->offset
) {
348 // Show the create link ahead
349 $this->showCreateLink( $title, $num, $titleMatches, $textMatches );
350 if ( $totalRes > $this->limit ||
$this->offset
) {
351 if ( $this->searchEngineType
!== null ) {
352 $this->setExtraParam( 'srbackend', $this->searchEngineType
);
354 $prevnext = $this->getLanguage()->viewPrevNext(
355 $this->getPageTitle(),
358 $this->powerSearchOptions() +
array( 'search' => $term ),
359 $this->limit +
$this->offset
>= $totalRes
363 wfRunHooks( 'SpecialSearchResults', array( $term, &$titleMatches, &$textMatches ) );
365 $out->parserOptions()->setEditSection( false );
366 if ( $titleMatches ) {
367 if ( $numTitleMatches > 0 ) {
368 $out->wrapWikiMsg( "==$1==\n", 'titlematches' );
369 $out->addHTML( $this->showMatches( $titleMatches ) );
371 $titleMatches->free();
373 if ( $textMatches && !$textStatus ) {
374 // output appropriate heading
375 if ( $numTextMatches > 0 && $numTitleMatches > 0 ) {
376 // if no title matches the heading is redundant
377 $out->wrapWikiMsg( "==$1==\n", 'textmatches' );
380 // show interwiki results if any
381 if ( $textMatches->hasInterwikiResults() ) {
382 $out->addHTML( $this->showInterwiki( $textMatches->getInterwikiResults(), $term ) );
385 if ( $numTextMatches > 0 ) {
386 $out->addHTML( $this->showMatches( $textMatches ) );
389 $textMatches->free();
393 $out->addHTML( '<div class="error">' .
394 $textStatus->getMessage( 'search-error' ) . '</div>' );
396 $out->wrapWikiMsg( "<p class=\"mw-search-nonefound\">\n$1</p>",
397 array( 'search-nonefound', wfEscapeWikiText( $term ) ) );
398 $this->showCreateLink( $title, $num, $titleMatches, $textMatches );
401 $out->addHtml( "</div>" );
404 $out->addHTML( "<p class='mw-search-pager-bottom'>{$prevnext}</p>\n" );
409 * @param Title $title
410 * @param int $num The number of search results found
411 * @param null|SearchResultSet $titleMatches Results from title search
412 * @param null|SearchResultSet $textMatches Results from text search
414 protected function showCreateLink( $title, $num, $titleMatches, $textMatches ) {
415 // show direct page/create link if applicable
417 // Check DBkey !== '' in case of fragment link only.
418 if ( is_null( $title ) ||
$title->getDBkey() === ''
419 ||
( $titleMatches !== null && $titleMatches->searchContainedSyntax() )
420 ||
( $textMatches !== null && $textMatches->searchContainedSyntax() )
423 // preserve the paragraph for margins etc...
424 $this->getOutput()->addHtml( '<p></p>' );
429 $linkClass = 'mw-search-createlink';
430 if ( $title->isKnown() ) {
431 $messageName = 'searchmenu-exists';
432 $linkClass = 'mw-search-exists';
433 } elseif ( $title->quickUserCan( 'create', $this->getUser() ) ) {
434 $messageName = 'searchmenu-new';
436 $messageName = 'searchmenu-new-nocreate';
440 wfEscapeWikiText( $title->getPrefixedText() ),
441 Message
::numParam( $num )
443 wfRunHooks( 'SpecialSearchCreateLink', array( $title, &$params ) );
445 // Extensions using the hook might still return an empty $messageName
446 if ( $messageName ) {
447 $this->getOutput()->wrapWikiMsg( "<p class=\"$linkClass\">\n$1</p>", $params );
449 // preserve the paragraph for margins etc...
450 $this->getOutput()->addHtml( '<p></p>' );
455 * @param string $term
457 protected function setupPage( $term ) {
458 # Should advanced UI be used?
459 $this->searchAdvanced
= ( $this->profile
=== 'advanced' );
460 $out = $this->getOutput();
461 if ( strval( $term ) !== '' ) {
462 $out->setPageTitle( $this->msg( 'searchresults' ) );
463 $out->setHTMLTitle( $this->msg( 'pagetitle' )
464 ->rawParams( $this->msg( 'searchresults-title' )->rawParams( $term )->text() )
465 ->inContentLanguage()->text()
468 // add javascript specific to special:search
469 $out->addModules( 'mediawiki.special.search' );
473 * Extract "power search" namespace settings from the request object,
474 * returning a list of index numbers to search.
476 * @param WebRequest $request
479 protected function powerSearch( &$request ) {
481 foreach ( SearchEngine
::searchableNamespaces() as $ns => $name ) {
482 if ( $request->getCheck( 'ns' . $ns ) ) {
491 * Reconstruct the 'power search' options for links
495 protected function powerSearchOptions() {
497 if ( $this->profile
!== 'advanced' ) {
498 $opt['profile'] = $this->profile
;
500 foreach ( $this->namespaces
as $n ) {
505 return $opt +
$this->extraParams
;
509 * Save namespace preferences when we're supposed to
511 * @return bool Whether we wrote something
513 protected function saveNamespaces() {
514 $user = $this->getUser();
515 $request = $this->getRequest();
517 if ( $user->isLoggedIn() &&
518 $user->matchEditToken(
519 $request->getVal( 'nsRemember' ),
524 // Reset namespace preferences: namespaces are not searched
525 // when they're not mentioned in the URL parameters.
526 foreach ( MWNamespace
::getValidNamespaces() as $n ) {
527 $user->setOption( 'searchNs' . $n, false );
529 // The request parameters include all the namespaces to be searched.
530 // Even if they're the same as an existing profile, they're not eaten.
531 foreach ( $this->namespaces
as $n ) {
532 $user->setOption( 'searchNs' . $n, true );
535 $user->saveSettings();
543 * Show whole set of results
545 * @param SearchResultSet $matches
549 protected function showMatches( &$matches ) {
552 $profile = new ProfileSection( __METHOD__
);
553 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
555 $out = "<ul class='mw-search-results'>\n";
556 $result = $matches->next();
558 $out .= $this->showHit( $result, $terms );
559 $result = $matches->next();
563 // convert the whole thing to desired language variant
564 $out = $wgContLang->convert( $out );
570 * Format a single hit result
572 * @param SearchResult $result
573 * @param array $terms Terms to highlight
577 protected function showHit( $result, $terms ) {
578 $profile = new ProfileSection( __METHOD__
);
580 if ( $result->isBrokenTitle() ) {
584 $title = $result->getTitle();
586 $titleSnippet = $result->getTitleSnippet();
588 if ( $titleSnippet == '' ) {
589 $titleSnippet = null;
592 $link_t = clone $title;
594 wfRunHooks( 'ShowSearchHitTitle',
595 array( &$link_t, &$titleSnippet, $result, $terms, $this ) );
597 $link = Linker
::linkKnown(
602 //If page content is not readable, just return the title.
603 //This is not quite safe, but better than showing excerpts from non-readable pages
604 //Note that hiding the entry entirely would screw up paging.
605 if ( !$title->userCan( 'read', $this->getUser() ) ) {
606 return "<li>{$link}</li>\n";
609 // If the page doesn't *exist*... our search index is out of date.
610 // The least confusing at this point is to drop the result.
611 // You may get less results, but... oh well. :P
612 if ( $result->isMissingRevision() ) {
616 // format redirects / relevant sections
617 $redirectTitle = $result->getRedirectTitle();
618 $redirectText = $result->getRedirectSnippet();
619 $sectionTitle = $result->getSectionTitle();
620 $sectionText = $result->getSectionSnippet();
623 if ( !is_null( $redirectTitle ) ) {
624 if ( $redirectText == '' ) {
625 $redirectText = null;
628 $redirect = "<span class='searchalttitle'>" .
629 $this->msg( 'search-redirect' )->rawParams(
630 Linker
::linkKnown( $redirectTitle, $redirectText ) )->text() .
636 if ( !is_null( $sectionTitle ) ) {
637 if ( $sectionText == '' ) {
641 $section = "<span class='searchalttitle'>" .
642 $this->msg( 'search-section' )->rawParams(
643 Linker
::linkKnown( $sectionTitle, $sectionText ) )->text() .
647 // format text extract
648 $extract = "<div class='searchresult'>" . $result->getTextSnippet( $terms ) . "</div>";
650 $lang = $this->getLanguage();
652 // format description
653 $byteSize = $result->getByteSize();
654 $wordCount = $result->getWordCount();
655 $timestamp = $result->getTimestamp();
656 $size = $this->msg( 'search-result-size', $lang->formatSize( $byteSize ) )
657 ->numParams( $wordCount )->escaped();
659 if ( $title->getNamespace() == NS_CATEGORY
) {
660 $cat = Category
::newFromTitle( $title );
661 $size = $this->msg( 'search-result-category-size' )
662 ->numParams( $cat->getPageCount(), $cat->getSubcatCount(), $cat->getFileCount() )
666 $date = $lang->userTimeAndDate( $timestamp, $this->getUser() );
669 // Include a thumbnail for media files...
670 if ( $title->getNamespace() == NS_FILE
) {
671 $img = $result->getFile();
672 $img = $img ?
: wfFindFile( $title );
673 if ( $result->isFileMatch() ) {
674 $fileMatch = "<span class='searchalttitle'>" .
675 $this->msg( 'search-file-match' )->escaped() . "</span>";
678 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
680 $desc = $this->msg( 'parentheses' )->rawParams( $img->getShortDesc() )->escaped();
681 // Float doesn't seem to interact well with the bullets.
682 // Table messes up vertical alignment of the bullets.
683 // Bullets are therefore disabled (didn't look great anyway).
685 '<table class="searchResultImage">' .
687 '<td style="width: 120px; text-align: center; vertical-align: top;">' .
688 $thumb->toHtml( array( 'desc-link' => true ) ) .
690 '<td style="vertical-align: top;">' .
691 "{$link} {$redirect} {$section} {$fileMatch}" .
693 "<div class='mw-search-result-data'>{$desc} - {$date}</div>" .
705 if ( wfRunHooks( 'ShowSearchHit', array(
706 $this, $result, $terms,
707 &$link, &$redirect, &$section, &$extract,
708 &$score, &$size, &$date, &$related,
711 $html = "<li><div class='mw-search-result-heading'>" .
712 "{$link} {$redirect} {$section} {$fileMatch}</div> {$extract}\n" .
713 "<div class='mw-search-result-data'>{$size} - {$date}</div>" .
721 * Show results from other wikis
723 * @param SearchResultSet|array $matches
724 * @param string $query
728 protected function showInterwiki( $matches, $query ) {
730 $profile = new ProfileSection( __METHOD__
);
732 $out = "<div id='mw-search-interwiki'><div id='mw-search-interwiki-caption'>" .
733 $this->msg( 'search-interwiki-caption' )->text() . "</div>\n";
734 $out .= "<ul class='mw-search-iwresults'>\n";
736 // work out custom project captions
737 $customCaptions = array();
738 // format per line <iwprefix>:<caption>
739 $customLines = explode( "\n", $this->msg( 'search-interwiki-custom' )->text() );
740 foreach ( $customLines as $line ) {
741 $parts = explode( ":", $line, 2 );
742 if ( count( $parts ) == 2 ) { // validate line
743 $customCaptions[$parts[0]] = $parts[1];
747 if ( !is_array( $matches ) ) {
748 $matches = array( $matches );
751 foreach ( $matches as $set ) {
753 $result = $set->next();
755 $out .= $this->showInterwikiHit( $result, $prev, $query, $customCaptions );
756 $prev = $result->getInterwikiPrefix();
757 $result = $set->next();
761 // @todo Should support paging in a non-confusing way (not sure how though, maybe via ajax)..
762 $out .= "</ul></div>\n";
764 // convert the whole thing to desired language variant
765 $out = $wgContLang->convert( $out );
771 * Show single interwiki link
773 * @param SearchResult $result
774 * @param string $lastInterwiki
775 * @param string $query
776 * @param array $customCaptions Interwiki prefix -> caption
780 protected function showInterwikiHit( $result, $lastInterwiki, $query, $customCaptions ) {
781 $profile = new ProfileSection( __METHOD__
);
783 if ( $result->isBrokenTitle() ) {
787 $title = $result->getTitle();
789 $titleSnippet = $result->getTitleSnippet();
791 if ( $titleSnippet == '' ) {
792 $titleSnippet = null;
795 $link = Linker
::linkKnown(
800 // format redirect if any
801 $redirectTitle = $result->getRedirectTitle();
802 $redirectText = $result->getRedirectSnippet();
804 if ( !is_null( $redirectTitle ) ) {
805 if ( $redirectText == '' ) {
806 $redirectText = null;
809 $redirect = "<span class='searchalttitle'>" .
810 $this->msg( 'search-redirect' )->rawParams(
811 Linker
::linkKnown( $redirectTitle, $redirectText ) )->text() .
816 // display project name
817 if ( is_null( $lastInterwiki ) ||
$lastInterwiki != $title->getInterwiki() ) {
818 if ( array_key_exists( $title->getInterwiki(), $customCaptions ) ) {
819 // captions from 'search-interwiki-custom'
820 $caption = $customCaptions[$title->getInterwiki()];
822 // default is to show the hostname of the other wiki which might suck
823 // if there are many wikis on one hostname
824 $parsed = wfParseUrl( $title->getFullURL() );
825 $caption = $this->msg( 'search-interwiki-default', $parsed['host'] )->text();
827 // "more results" link (special page stuff could be localized, but we might not know target lang)
828 $searchTitle = Title
::newFromText( $title->getInterwiki() . ":Special:Search" );
829 $searchLink = Linker
::linkKnown(
831 $this->msg( 'search-interwiki-more' )->text(),
835 'fulltext' => 'Search'
838 $out .= "</ul><div class='mw-search-interwiki-project'><span class='mw-search-interwiki-more'>
839 {$searchLink}</span>{$caption}</div>\n<ul>";
842 $out .= "<li>{$link} {$redirect}</li>\n";
848 * Generates the power search box at [[Special:Search]]
850 * @param string $term Search term
852 * @return string HTML form
854 protected function powerSearchBox( $term, $opts ) {
857 // Groups namespaces into rows according to subject
859 foreach ( SearchEngine
::searchableNamespaces() as $namespace => $name ) {
860 $subject = MWNamespace
::getSubject( $namespace );
861 if ( !array_key_exists( $subject, $rows ) ) {
862 $rows[$subject] = "";
865 $name = $wgContLang->getConverter()->convertNamespace( $namespace );
867 $name = $this->msg( 'blanknamespace' )->text();
871 Xml
::openElement( 'td' ) .
875 "mw-search-ns{$namespace}",
876 in_array( $namespace, $this->namespaces
)
878 Xml
::closeElement( 'td' );
881 $rows = array_values( $rows );
882 $numRows = count( $rows );
884 // Lays out namespaces in multiple floating two-column tables so they'll
885 // be arranged nicely while still accommodating different screen widths
886 $namespaceTables = '';
887 for ( $i = 0; $i < $numRows; $i +
= 4 ) {
888 $namespaceTables .= Xml
::openElement(
890 array( 'cellpadding' => 0, 'cellspacing' => 0 )
893 for ( $j = $i; $j < $i +
4 && $j < $numRows; $j++
) {
894 $namespaceTables .= Xml
::tags( 'tr', null, $rows[$j] );
897 $namespaceTables .= Xml
::closeElement( 'table' );
900 $showSections = array( 'namespaceTables' => $namespaceTables );
902 wfRunHooks( 'SpecialSearchPowerBox', array( &$showSections, $term, $opts ) );
905 foreach ( $opts as $key => $value ) {
906 $hidden .= Html
::hidden( $key, $value );
909 # Stuff to feed saveNamespaces()
911 $user = $this->getUser();
912 if ( $user->isLoggedIn() ) {
913 $remember .= Xml
::checkLabel(
914 wfMessage( 'powersearch-remember' )->text(),
916 'mw-search-powersearch-remember',
918 // The token goes here rather than in a hidden field so it
919 // is only sent when necessary (not every form submission).
920 array( 'value' => $user->getEditToken(
927 // Return final output
928 return Xml
::openElement( 'fieldset', array( 'id' => 'mw-searchoptions' ) ) .
929 Xml
::element( 'legend', null, $this->msg( 'powersearch-legend' )->text() ) .
930 Xml
::tags( 'h4', null, $this->msg( 'powersearch-ns' )->parse() ) .
931 Xml
::element( 'div', array( 'id' => 'mw-search-togglebox' ), '', false ) .
932 Xml
::element( 'div', array( 'class' => 'divider' ), '', false ) .
933 implode( Xml
::element( 'div', array( 'class' => 'divider' ), '', false ), $showSections ) .
935 Xml
::element( 'div', array( 'class' => 'divider' ), '', false ) .
937 Xml
::closeElement( 'fieldset' );
943 protected function getSearchProfiles() {
944 // Builds list of Search Types (profiles)
945 $nsAllSet = array_keys( SearchEngine
::searchableNamespaces() );
949 'message' => 'searchprofile-articles',
950 'tooltip' => 'searchprofile-articles-tooltip',
951 'namespaces' => SearchEngine
::defaultNamespaces(),
952 'namespace-messages' => SearchEngine
::namespacesAsText(
953 SearchEngine
::defaultNamespaces()
957 'message' => 'searchprofile-images',
958 'tooltip' => 'searchprofile-images-tooltip',
959 'namespaces' => array( NS_FILE
),
962 'message' => 'searchprofile-everything',
963 'tooltip' => 'searchprofile-everything-tooltip',
964 'namespaces' => $nsAllSet,
967 'message' => 'searchprofile-advanced',
968 'tooltip' => 'searchprofile-advanced-tooltip',
969 'namespaces' => self
::NAMESPACES_CURRENT
,
973 wfRunHooks( 'SpecialSearchProfiles', array( &$profiles ) );
975 foreach ( $profiles as &$data ) {
976 if ( !is_array( $data['namespaces'] ) ) {
979 sort( $data['namespaces'] );
986 * @param string $term
989 protected function formHeader( $term ) {
990 $out = Xml
::openElement( 'div', array( 'class' => 'mw-search-formheader' ) );
993 if ( $this->startsWithImage( $term ) ) {
995 $bareterm = substr( $term, strpos( $term, ':' ) +
1 );
998 $profiles = $this->getSearchProfiles();
999 $lang = $this->getLanguage();
1001 // Outputs XML for Search Types
1002 $out .= Xml
::openElement( 'div', array( 'class' => 'search-types' ) );
1003 $out .= Xml
::openElement( 'ul' );
1004 foreach ( $profiles as $id => $profile ) {
1005 if ( !isset( $profile['parameters'] ) ) {
1006 $profile['parameters'] = array();
1008 $profile['parameters']['profile'] = $id;
1010 $tooltipParam = isset( $profile['namespace-messages'] ) ?
1011 $lang->commaList( $profile['namespace-messages'] ) : null;
1015 'class' => $this->profile
=== $id ?
'current' : 'normal'
1017 $this->makeSearchLink(
1020 $this->msg( $profile['message'] )->text(),
1021 $this->msg( $profile['tooltip'], $tooltipParam )->text(),
1022 $profile['parameters']
1026 $out .= Xml
::closeElement( 'ul' );
1027 $out .= Xml
::closeElement( 'div' );
1028 $out .= Xml
::element( 'div', array( 'style' => 'clear:both' ), '', false );
1029 $out .= Xml
::closeElement( 'div' );
1033 $opts['profile'] = $this->profile
;
1035 if ( $this->profile
=== 'advanced' ) {
1036 $out .= $this->powerSearchBox( $term, $opts );
1039 wfRunHooks( 'SpecialSearchProfileForm', array( $this, &$form, $this->profile
, $term, $opts ) );
1047 * @param string $term
1048 * @param int $resultsShown
1049 * @param int $totalNum
1052 protected function shortDialog( $term, $resultsShown, $totalNum ) {
1053 $out = Html
::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
1054 $out .= Html
::hidden( 'profile', $this->profile
) . "\n";
1056 $out .= Html
::input( 'search', $term, 'search', array(
1057 'id' => $this->profile
=== 'advanced' ?
'powerSearchText' : 'searchText',
1060 'class' => 'mw-ui-input mw-ui-input-inline',
1062 $out .= Html
::hidden( 'fulltext', 'Search' ) . "\n";
1063 $out .= Html
::submitButton(
1064 $this->msg( 'searchbutton' )->text(),
1065 array(), array( 'mw-ui-progressive' )
1069 if ( $totalNum > 0 && $this->offset
< $totalNum ) {
1070 $top = $this->msg( 'search-showingresults' )
1071 ->numParams( $this->offset +
1, $this->offset +
$resultsShown, $totalNum )
1072 ->numParams( $resultsShown )
1074 $out .= Xml
::tags( 'div', array( 'class' => 'results-info' ), $top ) .
1075 Xml
::element( 'div', array( 'style' => 'clear:both' ), '', false );
1078 return $out . $this->didYouMeanHtml
;
1082 * Make a search link with some target namespaces
1084 * @param string $term
1085 * @param array $namespaces Ignored
1086 * @param string $label Link's text
1087 * @param string $tooltip Link's tooltip
1088 * @param array $params Query string parameters
1089 * @return string HTML fragment
1091 protected function makeSearchLink( $term, $namespaces, $label, $tooltip, $params = array() ) {
1093 foreach ( $namespaces as $n ) {
1094 $opt['ns' . $n] = 1;
1097 $stParams = array_merge(
1100 'fulltext' => $this->msg( 'search' )->text()
1105 return Xml
::element(
1108 'href' => $this->getPageTitle()->getLocalURL( $stParams ),
1116 * Check if query starts with image: prefix
1118 * @param string $term The string to check
1121 protected function startsWithImage( $term ) {
1124 $parts = explode( ':', $term );
1125 if ( count( $parts ) > 1 ) {
1126 return $wgContLang->getNsIndex( $parts[0] ) == NS_FILE
;
1133 * Check if query starts with all: prefix
1135 * @param string $term The string to check
1138 protected function startsWithAll( $term ) {
1140 $allkeyword = $this->msg( 'searchall' )->inContentLanguage()->text();
1142 $parts = explode( ':', $term );
1143 if ( count( $parts ) > 1 ) {
1144 return $parts[0] == $allkeyword;
1153 * @return SearchEngine
1155 public function getSearchEngine() {
1156 if ( $this->searchEngine
=== null ) {
1157 $this->searchEngine
= $this->searchEngineType ?
1158 SearchEngine
::create( $this->searchEngineType
) : SearchEngine
::create();
1161 return $this->searchEngine
;
1165 * Current search profile.
1166 * @return null|string
1168 function getProfile() {
1169 return $this->profile
;
1173 * Current namespaces.
1176 function getNamespaces() {
1177 return $this->namespaces
;
1181 * Users of hook SpecialSearchSetupEngine can use this to
1182 * add more params to links to not lose selection when
1183 * user navigates search results.
1186 * @param string $key
1187 * @param mixed $value
1189 public function setExtraParam( $key, $value ) {
1190 $this->extraParams
[$key] = $value;
1193 protected function getGroupName() {