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();
51 * @var string The prefix url parameter. Set on the searcher and the
52 * is expected to treat it as prefix filter on titles.
59 protected $limit, $offset;
64 protected $namespaces;
71 const NAMESPACES_CURRENT
= 'sense';
73 public function __construct() {
74 parent
::__construct( 'Search' );
82 public function execute( $par ) {
84 $this->outputHeader();
85 $out = $this->getOutput();
86 $out->allowClickjacking();
87 $out->addModuleStyles( array(
88 'mediawiki.special', 'mediawiki.special.search', 'mediawiki.ui', 'mediawiki.ui.button',
92 // Strip underscores from title parameter; most of the time we'll want
93 // text form here. But don't strip underscores from actual text params!
94 $titleParam = str_replace( '_', ' ', $par );
96 $request = $this->getRequest();
98 // Fetch the search term
99 $search = str_replace( "\n", " ", $request->getText( 'search', $titleParam ) );
102 if ( !is_null( $request->getVal( 'nsRemember' ) ) ) {
103 $this->saveNamespaces();
104 // Remove the token from the URL to prevent the user from inadvertently
105 // exposing it (e.g. by pasting it into a public wiki page) or undoing
106 // later settings changes (e.g. by reloading the page).
107 $query = $request->getValues();
108 unset( $query['title'], $query['nsRemember'] );
109 $out->redirect( $this->getPageTitle()->getFullURL( $query ) );
113 $this->searchEngineType
= $request->getVal( 'srbackend' );
115 if ( $request->getVal( 'fulltext' )
116 ||
!is_null( $request->getVal( 'offset' ) )
118 $this->showResults( $search );
120 $this->goResult( $search );
125 * Set up basic search parameters from the request and user settings.
127 * @see tests/phpunit/includes/specials/SpecialSearchTest.php
129 public function load() {
130 $request = $this->getRequest();
131 list( $this->limit
, $this->offset
) = $request->getLimitOffset( 20, '' );
132 $this->mPrefix
= $request->getVal( 'prefix', '' );
134 $user = $this->getUser();
136 # Extract manually requested namespaces
137 $nslist = $this->powerSearch( $request );
138 if ( !count( $nslist ) ) {
139 # Fallback to user preference
140 $nslist = SearchEngine
::userNamespaces( $user );
144 if ( !count( $nslist ) ) {
145 $profile = 'default';
148 $profile = $request->getVal( 'profile', $profile );
149 $profiles = $this->getSearchProfiles();
150 if ( $profile === null ) {
151 // BC with old request format
152 $profile = 'advanced';
153 foreach ( $profiles as $key => $data ) {
154 if ( $nslist === $data['namespaces'] && $key !== 'advanced' ) {
158 $this->namespaces
= $nslist;
159 } elseif ( $profile === 'advanced' ) {
160 $this->namespaces
= $nslist;
162 if ( isset( $profiles[$profile]['namespaces'] ) ) {
163 $this->namespaces
= $profiles[$profile]['namespaces'];
165 // Unknown profile requested
166 $profile = 'default';
167 $this->namespaces
= $profiles['default']['namespaces'];
171 $this->fulltext
= $request->getVal( 'fulltext' );
172 $this->profile
= $profile;
176 * If an exact title match can be found, jump straight ahead to it.
178 * @param string $term
180 public function goResult( $term ) {
181 $this->setupPage( $term );
182 # Try to go to page as entered.
183 $title = Title
::newFromText( $term );
184 # If the string cannot be used to create a title
185 if ( is_null( $title ) ) {
186 $this->showResults( $term );
190 # If there's an exact or very near match, jump right there.
191 $title = SearchEngine
::getNearMatch( $term );
193 if ( !is_null( $title ) ) {
194 $this->getOutput()->redirect( $title->getFullURL() );
198 # No match, generate an edit URL
199 $title = Title
::newFromText( $term );
200 if ( !is_null( $title ) ) {
201 Hooks
::run( 'SpecialSearchNogomatch', array( &$title ) );
203 $this->showResults( $term );
207 * @param string $term
209 public function showResults( $term ) {
212 $search = $this->getSearchEngine();
213 $search->setLimitOffset( $this->limit
, $this->offset
);
214 $search->setNamespaces( $this->namespaces
);
215 $search->prefix
= $this->mPrefix
;
216 $term = $search->transformSearchTerm( $term );
217 $didYouMeanHtml = '';
219 Hooks
::run( 'SpecialSearchSetupEngine', array( $this, $this->profile
, $search ) );
221 $this->setupPage( $term );
223 $out = $this->getOutput();
225 if ( $this->getConfig()->get( 'DisableTextSearch' ) ) {
226 $searchFowardUrl = $this->getConfig()->get( 'SearchForwardUrl' );
227 if ( $searchFowardUrl ) {
228 $url = str_replace( '$1', urlencode( $term ), $searchFowardUrl );
229 $out->redirect( $url );
232 Xml
::openElement( 'fieldset' ) .
233 Xml
::element( 'legend', null, $this->msg( 'search-external' )->text() ) .
236 array( 'class' => 'mw-searchdisabled' ),
237 $this->msg( 'searchdisabled' )->text()
239 $this->msg( 'googlesearch' )->rawParams(
240 htmlspecialchars( $term ),
242 $this->msg( 'searchbutton' )->escaped()
244 Xml
::closeElement( 'fieldset' )
251 $title = Title
::newFromText( $term );
252 $showSuggestion = $title === null ||
!$title->isKnown();
253 $search->setShowSuggestion( $showSuggestion );
255 // fetch search results
256 $rewritten = $search->replacePrefixes( $term );
258 $titleMatches = $search->searchTitle( $rewritten );
259 $textMatches = $search->searchText( $rewritten );
262 if ( $textMatches instanceof Status
) {
263 $textStatus = $textMatches;
267 // did you mean... suggestions
268 if ( $showSuggestion && $textMatches && !$textStatus && $textMatches->hasSuggestion() ) {
269 # mirror Go/Search behavior of original request ..
270 $didYouMeanParams = array( 'search' => $textMatches->getSuggestionQuery() );
272 if ( $this->fulltext
!= null ) {
273 $didYouMeanParams['fulltext'] = $this->fulltext
;
276 $stParams = array_merge(
278 $this->powerSearchOptions()
281 $suggestionSnippet = $textMatches->getSuggestionSnippet();
283 if ( $suggestionSnippet == '' ) {
284 $suggestionSnippet = null;
287 $suggestLink = Linker
::linkKnown(
288 $this->getPageTitle(),
294 # html of did you mean... search suggestion link
296 Xml
::openElement( 'div', array( 'class' => 'searchdidyoumean' ) ) .
297 $this->msg( 'search-suggest' )->rawParams( $suggestLink )->text() .
298 Xml
::closeElement( 'div' );
301 if ( !Hooks
::run( 'SpecialSearchResultsPrepend', array( $this, $out, $term ) ) ) {
302 # Hook requested termination
306 // start rendering the page
311 'id' => ( $this->isPowerSearch() ?
'powersearch' : 'search' ),
313 'action' => wfScript(),
318 // Get number of results
319 $titleMatchesNum = $textMatchesNum = $numTitleMatches = $numTextMatches = 0;
320 if ( $titleMatches ) {
321 $titleMatchesNum = $titleMatches->numRows();
322 $numTitleMatches = $titleMatches->getTotalHits();
324 if ( $textMatches ) {
325 $textMatchesNum = $textMatches->numRows();
326 $numTextMatches = $textMatches->getTotalHits();
328 $num = $titleMatchesNum +
$textMatchesNum;
329 $totalRes = $numTitleMatches +
$numTextMatches;
332 # This is an awful awful ID name. It's not a table, but we
333 # named it poorly from when this was a table so now we're
335 Xml
::openElement( 'div', array( 'id' => 'mw-search-top-table' ) ) .
336 $this->shortDialog( $term, $num, $totalRes ) .
337 Xml
::closeElement( 'div' ) .
338 $this->searchProfileTabs( $term ) .
339 $this->searchOptions( $term ) .
340 Xml
::closeElement( 'form' ) .
344 $filePrefix = $wgContLang->getFormattedNsText( NS_FILE
) . ':';
345 if ( trim( $term ) === '' ||
$filePrefix === trim( $term ) ) {
346 // Empty query -- straight view of search form
350 $out->addHtml( "<div class='searchresults'>" );
354 if ( $num ||
$this->offset
) {
355 // Show the create link ahead
356 $this->showCreateLink( $title, $num, $titleMatches, $textMatches );
357 if ( $totalRes > $this->limit ||
$this->offset
) {
358 if ( $this->searchEngineType
!== null ) {
359 $this->setExtraParam( 'srbackend', $this->searchEngineType
);
361 $prevnext = $this->getLanguage()->viewPrevNext(
362 $this->getPageTitle(),
365 $this->powerSearchOptions() +
array( 'search' => $term ),
366 $this->limit +
$this->offset
>= $totalRes
370 Hooks
::run( 'SpecialSearchResults', array( $term, &$titleMatches, &$textMatches ) );
372 $out->parserOptions()->setEditSection( false );
373 if ( $titleMatches ) {
374 if ( $numTitleMatches > 0 ) {
375 $out->wrapWikiMsg( "==$1==\n", 'titlematches' );
376 $out->addHTML( $this->showMatches( $titleMatches ) );
378 $titleMatches->free();
380 if ( $textMatches && !$textStatus ) {
381 // output appropriate heading
382 if ( $numTextMatches > 0 && $numTitleMatches > 0 ) {
383 // if no title matches the heading is redundant
384 $out->wrapWikiMsg( "==$1==\n", 'textmatches' );
387 // show interwiki results if any
388 if ( $textMatches->hasInterwikiResults() ) {
389 $out->addHTML( $this->showInterwiki( $textMatches->getInterwikiResults(), $term ) );
392 if ( $numTextMatches > 0 ) {
393 $out->addHTML( $this->showMatches( $textMatches ) );
396 $textMatches->free();
400 $out->addHTML( '<div class="error">' .
401 $textStatus->getMessage( 'search-error' ) . '</div>' );
403 $out->wrapWikiMsg( "<p class=\"mw-search-nonefound\">\n$1</p>",
404 array( 'search-nonefound', wfEscapeWikiText( $term ) ) );
405 $this->showCreateLink( $title, $num, $titleMatches, $textMatches );
408 $out->addHtml( "</div>" );
411 $out->addHTML( "<p class='mw-search-pager-bottom'>{$prevnext}</p>\n" );
414 Hooks
::run( 'SpecialSearchResultsAppend', array( $this, $out ) );
419 * @param Title $title
420 * @param int $num The number of search results found
421 * @param null|SearchResultSet $titleMatches Results from title search
422 * @param null|SearchResultSet $textMatches Results from text search
424 protected function showCreateLink( $title, $num, $titleMatches, $textMatches ) {
425 // show direct page/create link if applicable
427 // Check DBkey !== '' in case of fragment link only.
428 if ( is_null( $title ) ||
$title->getDBkey() === ''
429 ||
( $titleMatches !== null && $titleMatches->searchContainedSyntax() )
430 ||
( $textMatches !== null && $textMatches->searchContainedSyntax() )
433 // preserve the paragraph for margins etc...
434 $this->getOutput()->addHtml( '<p></p>' );
439 $messageName = 'searchmenu-new-nocreate';
440 $linkClass = 'mw-search-createlink';
442 if ( !$title->isExternal() ) {
443 if ( $title->isKnown() ) {
444 $messageName = 'searchmenu-exists';
445 $linkClass = 'mw-search-exists';
446 } elseif ( $title->quickUserCan( 'create', $this->getUser() ) ) {
447 $messageName = 'searchmenu-new';
453 wfEscapeWikiText( $title->getPrefixedText() ),
454 Message
::numParam( $num )
456 Hooks
::run( 'SpecialSearchCreateLink', array( $title, &$params ) );
458 // Extensions using the hook might still return an empty $messageName
459 if ( $messageName ) {
460 $this->getOutput()->wrapWikiMsg( "<p class=\"$linkClass\">\n$1</p>", $params );
462 // preserve the paragraph for margins etc...
463 $this->getOutput()->addHtml( '<p></p>' );
468 * @param string $term
470 protected function setupPage( $term ) {
471 $out = $this->getOutput();
472 if ( strval( $term ) !== '' ) {
473 $out->setPageTitle( $this->msg( 'searchresults' ) );
474 $out->setHTMLTitle( $this->msg( 'pagetitle' )
475 ->rawParams( $this->msg( 'searchresults-title' )->rawParams( $term )->text() )
476 ->inContentLanguage()->text()
479 // add javascript specific to special:search
480 $out->addModules( 'mediawiki.special.search' );
484 * Return true if current search is a power (advanced) search
488 protected function isPowerSearch() {
489 return $this->profile
=== 'advanced';
493 * Extract "power search" namespace settings from the request object,
494 * returning a list of index numbers to search.
496 * @param WebRequest $request
499 protected function powerSearch( &$request ) {
501 foreach ( SearchEngine
::searchableNamespaces() as $ns => $name ) {
502 if ( $request->getCheck( 'ns' . $ns ) ) {
511 * Reconstruct the 'power search' options for links
515 protected function powerSearchOptions() {
517 if ( !$this->isPowerSearch() ) {
518 $opt['profile'] = $this->profile
;
520 foreach ( $this->namespaces
as $n ) {
525 return $opt +
$this->extraParams
;
529 * Save namespace preferences when we're supposed to
531 * @return bool Whether we wrote something
533 protected function saveNamespaces() {
534 $user = $this->getUser();
535 $request = $this->getRequest();
537 if ( $user->isLoggedIn() &&
538 $user->matchEditToken(
539 $request->getVal( 'nsRemember' ),
544 // Reset namespace preferences: namespaces are not searched
545 // when they're not mentioned in the URL parameters.
546 foreach ( MWNamespace
::getValidNamespaces() as $n ) {
547 $user->setOption( 'searchNs' . $n, false );
549 // The request parameters include all the namespaces to be searched.
550 // Even if they're the same as an existing profile, they're not eaten.
551 foreach ( $this->namespaces
as $n ) {
552 $user->setOption( 'searchNs' . $n, true );
555 $user->saveSettings();
563 * Show whole set of results
565 * @param SearchResultSet $matches
569 protected function showMatches( &$matches ) {
572 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
574 $out = "<ul class='mw-search-results'>\n";
575 $result = $matches->next();
577 $out .= $this->showHit( $result, $terms );
578 $result = $matches->next();
582 // convert the whole thing to desired language variant
583 $out = $wgContLang->convert( $out );
589 * Format a single hit result
591 * @param SearchResult $result
592 * @param array $terms Terms to highlight
596 protected function showHit( $result, $terms ) {
598 if ( $result->isBrokenTitle() ) {
602 $title = $result->getTitle();
604 $titleSnippet = $result->getTitleSnippet();
606 if ( $titleSnippet == '' ) {
607 $titleSnippet = null;
610 $link_t = clone $title;
612 Hooks
::run( 'ShowSearchHitTitle',
613 array( &$link_t, &$titleSnippet, $result, $terms, $this ) );
615 $link = Linker
::linkKnown(
620 //If page content is not readable, just return the title.
621 //This is not quite safe, but better than showing excerpts from non-readable pages
622 //Note that hiding the entry entirely would screw up paging.
623 if ( !$title->userCan( 'read', $this->getUser() ) ) {
624 return "<li>{$link}</li>\n";
627 // If the page doesn't *exist*... our search index is out of date.
628 // The least confusing at this point is to drop the result.
629 // You may get less results, but... oh well. :P
630 if ( $result->isMissingRevision() ) {
634 // format redirects / relevant sections
635 $redirectTitle = $result->getRedirectTitle();
636 $redirectText = $result->getRedirectSnippet();
637 $sectionTitle = $result->getSectionTitle();
638 $sectionText = $result->getSectionSnippet();
639 $categorySnippet = $result->getCategorySnippet();
642 if ( !is_null( $redirectTitle ) ) {
643 if ( $redirectText == '' ) {
644 $redirectText = null;
647 $redirect = "<span class='searchalttitle'>" .
648 $this->msg( 'search-redirect' )->rawParams(
649 Linker
::linkKnown( $redirectTitle, $redirectText ) )->text() .
654 if ( !is_null( $sectionTitle ) ) {
655 if ( $sectionText == '' ) {
659 $section = "<span class='searchalttitle'>" .
660 $this->msg( 'search-section' )->rawParams(
661 Linker
::linkKnown( $sectionTitle, $sectionText ) )->text() .
666 if ( $categorySnippet ) {
667 $category = "<span class='searchalttitle'>" .
668 $this->msg( 'search-category' )->rawParams( $categorySnippet )->text() .
672 // format text extract
673 $extract = "<div class='searchresult'>" . $result->getTextSnippet( $terms ) . "</div>";
675 $lang = $this->getLanguage();
677 // format description
678 $byteSize = $result->getByteSize();
679 $wordCount = $result->getWordCount();
680 $timestamp = $result->getTimestamp();
681 $size = $this->msg( 'search-result-size', $lang->formatSize( $byteSize ) )
682 ->numParams( $wordCount )->escaped();
684 if ( $title->getNamespace() == NS_CATEGORY
) {
685 $cat = Category
::newFromTitle( $title );
686 $size = $this->msg( 'search-result-category-size' )
687 ->numParams( $cat->getPageCount(), $cat->getSubcatCount(), $cat->getFileCount() )
691 $date = $lang->userTimeAndDate( $timestamp, $this->getUser() );
694 // Include a thumbnail for media files...
695 if ( $title->getNamespace() == NS_FILE
) {
696 $img = $result->getFile();
697 $img = $img ?
: wfFindFile( $title );
698 if ( $result->isFileMatch() ) {
699 $fileMatch = "<span class='searchalttitle'>" .
700 $this->msg( 'search-file-match' )->escaped() . "</span>";
703 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
705 $desc = $this->msg( 'parentheses' )->rawParams( $img->getShortDesc() )->escaped();
706 // Float doesn't seem to interact well with the bullets.
707 // Table messes up vertical alignment of the bullets.
708 // Bullets are therefore disabled (didn't look great anyway).
710 '<table class="searchResultImage">' .
712 '<td style="width: 120px; text-align: center; vertical-align: top;">' .
713 $thumb->toHtml( array( 'desc-link' => true ) ) .
715 '<td style="vertical-align: top;">' .
716 "{$link} {$redirect} {$category} {$section} {$fileMatch}" .
718 "<div class='mw-search-result-data'>{$desc} - {$date}</div>" .
730 if ( Hooks
::run( 'ShowSearchHit', array(
731 $this, $result, $terms,
732 &$link, &$redirect, &$section, &$extract,
733 &$score, &$size, &$date, &$related,
736 $html = "<li><div class='mw-search-result-heading'>" .
737 "{$link} {$redirect} {$category} {$section} {$fileMatch}</div> {$extract}\n" .
738 "<div class='mw-search-result-data'>{$size} - {$date}</div>" .
746 * Show results from other wikis
748 * @param SearchResultSet|array $matches
749 * @param string $query
753 protected function showInterwiki( $matches, $query ) {
756 $out = "<div id='mw-search-interwiki'><div id='mw-search-interwiki-caption'>" .
757 $this->msg( 'search-interwiki-caption' )->text() . "</div>\n";
758 $out .= "<ul class='mw-search-iwresults'>\n";
760 // work out custom project captions
761 $customCaptions = array();
762 // format per line <iwprefix>:<caption>
763 $customLines = explode( "\n", $this->msg( 'search-interwiki-custom' )->text() );
764 foreach ( $customLines as $line ) {
765 $parts = explode( ":", $line, 2 );
766 if ( count( $parts ) == 2 ) { // validate line
767 $customCaptions[$parts[0]] = $parts[1];
771 if ( !is_array( $matches ) ) {
772 $matches = array( $matches );
775 foreach ( $matches as $set ) {
777 $result = $set->next();
779 $out .= $this->showInterwikiHit( $result, $prev, $query, $customCaptions );
780 $prev = $result->getInterwikiPrefix();
781 $result = $set->next();
785 // @todo Should support paging in a non-confusing way (not sure how though, maybe via ajax)..
786 $out .= "</ul></div>\n";
788 // convert the whole thing to desired language variant
789 $out = $wgContLang->convert( $out );
795 * Show single interwiki link
797 * @param SearchResult $result
798 * @param string $lastInterwiki
799 * @param string $query
800 * @param array $customCaptions Interwiki prefix -> caption
804 protected function showInterwikiHit( $result, $lastInterwiki, $query, $customCaptions ) {
806 if ( $result->isBrokenTitle() ) {
810 $title = $result->getTitle();
812 $titleSnippet = $result->getTitleSnippet();
814 if ( $titleSnippet == '' ) {
815 $titleSnippet = null;
818 $link = Linker
::linkKnown(
823 // format redirect if any
824 $redirectTitle = $result->getRedirectTitle();
825 $redirectText = $result->getRedirectSnippet();
827 if ( !is_null( $redirectTitle ) ) {
828 if ( $redirectText == '' ) {
829 $redirectText = null;
832 $redirect = "<span class='searchalttitle'>" .
833 $this->msg( 'search-redirect' )->rawParams(
834 Linker
::linkKnown( $redirectTitle, $redirectText ) )->text() .
839 // display project name
840 if ( is_null( $lastInterwiki ) ||
$lastInterwiki != $title->getInterwiki() ) {
841 if ( array_key_exists( $title->getInterwiki(), $customCaptions ) ) {
842 // captions from 'search-interwiki-custom'
843 $caption = $customCaptions[$title->getInterwiki()];
845 // default is to show the hostname of the other wiki which might suck
846 // if there are many wikis on one hostname
847 $parsed = wfParseUrl( $title->getFullURL() );
848 $caption = $this->msg( 'search-interwiki-default', $parsed['host'] )->text();
850 // "more results" link (special page stuff could be localized, but we might not know target lang)
851 $searchTitle = Title
::newFromText( $title->getInterwiki() . ":Special:Search" );
852 $searchLink = Linker
::linkKnown(
854 $this->msg( 'search-interwiki-more' )->text(),
858 'fulltext' => 'Search'
861 $out .= "</ul><div class='mw-search-interwiki-project'><span class='mw-search-interwiki-more'>
862 {$searchLink}</span>{$caption}</div>\n<ul>";
865 $out .= "<li>{$link} {$redirect}</li>\n";
871 * Generates the power search box at [[Special:Search]]
873 * @param string $term Search term
875 * @return string HTML form
877 protected function powerSearchBox( $term, $opts ) {
880 // Groups namespaces into rows according to subject
882 foreach ( SearchEngine
::searchableNamespaces() as $namespace => $name ) {
883 $subject = MWNamespace
::getSubject( $namespace );
884 if ( !array_key_exists( $subject, $rows ) ) {
885 $rows[$subject] = "";
888 $name = $wgContLang->getConverter()->convertNamespace( $namespace );
890 $name = $this->msg( 'blanknamespace' )->text();
894 Xml
::openElement( 'td' ) .
898 "mw-search-ns{$namespace}",
899 in_array( $namespace, $this->namespaces
)
901 Xml
::closeElement( 'td' );
904 $rows = array_values( $rows );
905 $numRows = count( $rows );
907 // Lays out namespaces in multiple floating two-column tables so they'll
908 // be arranged nicely while still accommodating different screen widths
909 $namespaceTables = '';
910 for ( $i = 0; $i < $numRows; $i +
= 4 ) {
911 $namespaceTables .= Xml
::openElement( 'table' );
913 for ( $j = $i; $j < $i +
4 && $j < $numRows; $j++
) {
914 $namespaceTables .= Xml
::tags( 'tr', null, $rows[$j] );
917 $namespaceTables .= Xml
::closeElement( 'table' );
920 $showSections = array( 'namespaceTables' => $namespaceTables );
922 Hooks
::run( 'SpecialSearchPowerBox', array( &$showSections, $term, $opts ) );
925 foreach ( $opts as $key => $value ) {
926 $hidden .= Html
::hidden( $key, $value );
929 # Stuff to feed saveNamespaces()
931 $user = $this->getUser();
932 if ( $user->isLoggedIn() ) {
933 $remember .= Xml
::checkLabel(
934 $this->msg( 'powersearch-remember' )->text(),
936 'mw-search-powersearch-remember',
938 // The token goes here rather than in a hidden field so it
939 // is only sent when necessary (not every form submission).
940 array( 'value' => $user->getEditToken(
947 // Return final output
948 return Xml
::openElement( 'fieldset', array( 'id' => 'mw-searchoptions' ) ) .
949 Xml
::element( 'legend', null, $this->msg( 'powersearch-legend' )->text() ) .
950 Xml
::tags( 'h4', null, $this->msg( 'powersearch-ns' )->parse() ) .
951 Xml
::element( 'div', array( 'id' => 'mw-search-togglebox' ), '', false ) .
952 Xml
::element( 'div', array( 'class' => 'divider' ), '', false ) .
953 implode( Xml
::element( 'div', array( 'class' => 'divider' ), '', false ), $showSections ) .
955 Xml
::element( 'div', array( 'class' => 'divider' ), '', false ) .
957 Xml
::closeElement( 'fieldset' );
963 protected function getSearchProfiles() {
964 // Builds list of Search Types (profiles)
965 $nsAllSet = array_keys( SearchEngine
::searchableNamespaces() );
969 'message' => 'searchprofile-articles',
970 'tooltip' => 'searchprofile-articles-tooltip',
971 'namespaces' => SearchEngine
::defaultNamespaces(),
972 'namespace-messages' => SearchEngine
::namespacesAsText(
973 SearchEngine
::defaultNamespaces()
977 'message' => 'searchprofile-images',
978 'tooltip' => 'searchprofile-images-tooltip',
979 'namespaces' => array( NS_FILE
),
982 'message' => 'searchprofile-everything',
983 'tooltip' => 'searchprofile-everything-tooltip',
984 'namespaces' => $nsAllSet,
987 'message' => 'searchprofile-advanced',
988 'tooltip' => 'searchprofile-advanced-tooltip',
989 'namespaces' => self
::NAMESPACES_CURRENT
,
993 Hooks
::run( 'SpecialSearchProfiles', array( &$profiles ) );
995 foreach ( $profiles as &$data ) {
996 if ( !is_array( $data['namespaces'] ) ) {
999 sort( $data['namespaces'] );
1006 * @param string $term
1009 protected function searchProfileTabs( $term ) {
1010 $out = Xml
::openElement( 'div', array( 'class' => 'mw-search-profile-tabs' ) );
1013 if ( $this->startsWithImage( $term ) ) {
1015 $bareterm = substr( $term, strpos( $term, ':' ) +
1 );
1018 $profiles = $this->getSearchProfiles();
1019 $lang = $this->getLanguage();
1021 // Outputs XML for Search Types
1022 $out .= Xml
::openElement( 'div', array( 'class' => 'search-types' ) );
1023 $out .= Xml
::openElement( 'ul' );
1024 foreach ( $profiles as $id => $profile ) {
1025 if ( !isset( $profile['parameters'] ) ) {
1026 $profile['parameters'] = array();
1028 $profile['parameters']['profile'] = $id;
1030 $tooltipParam = isset( $profile['namespace-messages'] ) ?
1031 $lang->commaList( $profile['namespace-messages'] ) : null;
1035 'class' => $this->profile
=== $id ?
'current' : 'normal'
1037 $this->makeSearchLink(
1040 $this->msg( $profile['message'] )->text(),
1041 $this->msg( $profile['tooltip'], $tooltipParam )->text(),
1042 $profile['parameters']
1046 $out .= Xml
::closeElement( 'ul' );
1047 $out .= Xml
::closeElement( 'div' );
1048 $out .= Xml
::element( 'div', array( 'style' => 'clear:both' ), '', false );
1049 $out .= Xml
::closeElement( 'div' );
1055 * @param string $term Search term
1058 protected function searchOptions( $term ) {
1061 $opts['profile'] = $this->profile
;
1063 if ( $this->isPowerSearch() ) {
1064 $out .= $this->powerSearchBox( $term, $opts );
1067 Hooks
::run( 'SpecialSearchProfileForm', array( $this, &$form, $this->profile
, $term, $opts ) );
1075 * @param string $term
1076 * @param int $resultsShown
1077 * @param int $totalNum
1080 protected function shortDialog( $term, $resultsShown, $totalNum ) {
1081 $out = Html
::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
1082 $out .= Html
::hidden( 'profile', $this->profile
) . "\n";
1084 $out .= Html
::input( 'search', $term, 'search', array(
1085 'id' => $this->isPowerSearch() ?
'powerSearchText' : 'searchText',
1087 'autofocus' => trim( $term ) === '',
1088 'class' => 'mw-ui-input mw-ui-input-inline',
1090 $out .= Html
::hidden( 'fulltext', 'Search' ) . "\n";
1091 $out .= Html
::submitButton(
1092 $this->msg( 'searchbutton' )->text(),
1093 array( 'class' => 'mw-ui-button mw-ui-progressive' ),
1094 array( 'mw-ui-progressive' )
1098 if ( $totalNum > 0 && $this->offset
< $totalNum ) {
1099 $top = $this->msg( 'search-showingresults' )
1100 ->numParams( $this->offset +
1, $this->offset +
$resultsShown, $totalNum )
1101 ->numParams( $resultsShown )
1103 $out .= Xml
::tags( 'div', array( 'class' => 'results-info' ), $top ) .
1104 Xml
::element( 'div', array( 'style' => 'clear:both' ), '', false );
1111 * Make a search link with some target namespaces
1113 * @param string $term
1114 * @param array $namespaces Ignored
1115 * @param string $label Link's text
1116 * @param string $tooltip Link's tooltip
1117 * @param array $params Query string parameters
1118 * @return string HTML fragment
1120 protected function makeSearchLink( $term, $namespaces, $label, $tooltip, $params = array() ) {
1122 foreach ( $namespaces as $n ) {
1123 $opt['ns' . $n] = 1;
1126 $stParams = array_merge(
1129 'fulltext' => $this->msg( 'search' )->text()
1134 return Xml
::element(
1137 'href' => $this->getPageTitle()->getLocalURL( $stParams ),
1145 * Check if query starts with image: prefix
1147 * @param string $term The string to check
1150 protected function startsWithImage( $term ) {
1153 $parts = explode( ':', $term );
1154 if ( count( $parts ) > 1 ) {
1155 return $wgContLang->getNsIndex( $parts[0] ) == NS_FILE
;
1162 * Check if query starts with all: prefix
1164 * @param string $term The string to check
1167 protected function startsWithAll( $term ) {
1169 $allkeyword = $this->msg( 'searchall' )->inContentLanguage()->text();
1171 $parts = explode( ':', $term );
1172 if ( count( $parts ) > 1 ) {
1173 return $parts[0] == $allkeyword;
1182 * @return SearchEngine
1184 public function getSearchEngine() {
1185 if ( $this->searchEngine
=== null ) {
1186 $this->searchEngine
= $this->searchEngineType ?
1187 SearchEngine
::create( $this->searchEngineType
) : SearchEngine
::create();
1190 return $this->searchEngine
;
1194 * Current search profile.
1195 * @return null|string
1197 function getProfile() {
1198 return $this->profile
;
1202 * Current namespaces.
1205 function getNamespaces() {
1206 return $this->namespaces
;
1210 * Users of hook SpecialSearchSetupEngine can use this to
1211 * add more params to links to not lose selection when
1212 * user navigates search results.
1215 * @param string $key
1216 * @param mixed $value
1218 public function setExtraParam( $key, $value ) {
1219 $this->extraParams
[$key] = $value;
1222 protected function getGroupName() {