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" );
416 * @param Title $title
417 * @param int $num The number of search results found
418 * @param null|SearchResultSet $titleMatches Results from title search
419 * @param null|SearchResultSet $textMatches Results from text search
421 protected function showCreateLink( $title, $num, $titleMatches, $textMatches ) {
422 // show direct page/create link if applicable
424 // Check DBkey !== '' in case of fragment link only.
425 if ( is_null( $title ) ||
$title->getDBkey() === ''
426 ||
( $titleMatches !== null && $titleMatches->searchContainedSyntax() )
427 ||
( $textMatches !== null && $textMatches->searchContainedSyntax() )
430 // preserve the paragraph for margins etc...
431 $this->getOutput()->addHtml( '<p></p>' );
436 $messageName = 'searchmenu-new-nocreate';
437 $linkClass = 'mw-search-createlink';
439 if ( !$title->isExternal() ) {
440 if ( $title->isKnown() ) {
441 $messageName = 'searchmenu-exists';
442 $linkClass = 'mw-search-exists';
443 } elseif ( $title->quickUserCan( 'create', $this->getUser() ) ) {
444 $messageName = 'searchmenu-new';
450 wfEscapeWikiText( $title->getPrefixedText() ),
451 Message
::numParam( $num )
453 Hooks
::run( 'SpecialSearchCreateLink', array( $title, &$params ) );
455 // Extensions using the hook might still return an empty $messageName
456 if ( $messageName ) {
457 $this->getOutput()->wrapWikiMsg( "<p class=\"$linkClass\">\n$1</p>", $params );
459 // preserve the paragraph for margins etc...
460 $this->getOutput()->addHtml( '<p></p>' );
465 * @param string $term
467 protected function setupPage( $term ) {
468 $out = $this->getOutput();
469 if ( strval( $term ) !== '' ) {
470 $out->setPageTitle( $this->msg( 'searchresults' ) );
471 $out->setHTMLTitle( $this->msg( 'pagetitle' )
472 ->rawParams( $this->msg( 'searchresults-title' )->rawParams( $term )->text() )
473 ->inContentLanguage()->text()
476 // add javascript specific to special:search
477 $out->addModules( 'mediawiki.special.search' );
481 * Return true if current search is a power (advanced) search
485 protected function isPowerSearch() {
486 return $this->profile
=== 'advanced';
490 * Extract "power search" namespace settings from the request object,
491 * returning a list of index numbers to search.
493 * @param WebRequest $request
496 protected function powerSearch( &$request ) {
498 foreach ( SearchEngine
::searchableNamespaces() as $ns => $name ) {
499 if ( $request->getCheck( 'ns' . $ns ) ) {
508 * Reconstruct the 'power search' options for links
512 protected function powerSearchOptions() {
514 if ( !$this->isPowerSearch() ) {
515 $opt['profile'] = $this->profile
;
517 foreach ( $this->namespaces
as $n ) {
522 return $opt +
$this->extraParams
;
526 * Save namespace preferences when we're supposed to
528 * @return bool Whether we wrote something
530 protected function saveNamespaces() {
531 $user = $this->getUser();
532 $request = $this->getRequest();
534 if ( $user->isLoggedIn() &&
535 $user->matchEditToken(
536 $request->getVal( 'nsRemember' ),
541 // Reset namespace preferences: namespaces are not searched
542 // when they're not mentioned in the URL parameters.
543 foreach ( MWNamespace
::getValidNamespaces() as $n ) {
544 $user->setOption( 'searchNs' . $n, false );
546 // The request parameters include all the namespaces to be searched.
547 // Even if they're the same as an existing profile, they're not eaten.
548 foreach ( $this->namespaces
as $n ) {
549 $user->setOption( 'searchNs' . $n, true );
552 $user->saveSettings();
560 * Show whole set of results
562 * @param SearchResultSet $matches
566 protected function showMatches( &$matches ) {
569 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
571 $out = "<ul class='mw-search-results'>\n";
572 $result = $matches->next();
574 $out .= $this->showHit( $result, $terms );
575 $result = $matches->next();
579 // convert the whole thing to desired language variant
580 $out = $wgContLang->convert( $out );
586 * Format a single hit result
588 * @param SearchResult $result
589 * @param array $terms Terms to highlight
593 protected function showHit( $result, $terms ) {
595 if ( $result->isBrokenTitle() ) {
599 $title = $result->getTitle();
601 $titleSnippet = $result->getTitleSnippet();
603 if ( $titleSnippet == '' ) {
604 $titleSnippet = null;
607 $link_t = clone $title;
609 Hooks
::run( 'ShowSearchHitTitle',
610 array( &$link_t, &$titleSnippet, $result, $terms, $this ) );
612 $link = Linker
::linkKnown(
617 //If page content is not readable, just return the title.
618 //This is not quite safe, but better than showing excerpts from non-readable pages
619 //Note that hiding the entry entirely would screw up paging.
620 if ( !$title->userCan( 'read', $this->getUser() ) ) {
621 return "<li>{$link}</li>\n";
624 // If the page doesn't *exist*... our search index is out of date.
625 // The least confusing at this point is to drop the result.
626 // You may get less results, but... oh well. :P
627 if ( $result->isMissingRevision() ) {
631 // format redirects / relevant sections
632 $redirectTitle = $result->getRedirectTitle();
633 $redirectText = $result->getRedirectSnippet();
634 $sectionTitle = $result->getSectionTitle();
635 $sectionText = $result->getSectionSnippet();
636 $categorySnippet = $result->getCategorySnippet();
639 if ( !is_null( $redirectTitle ) ) {
640 if ( $redirectText == '' ) {
641 $redirectText = null;
644 $redirect = "<span class='searchalttitle'>" .
645 $this->msg( 'search-redirect' )->rawParams(
646 Linker
::linkKnown( $redirectTitle, $redirectText ) )->text() .
651 if ( !is_null( $sectionTitle ) ) {
652 if ( $sectionText == '' ) {
656 $section = "<span class='searchalttitle'>" .
657 $this->msg( 'search-section' )->rawParams(
658 Linker
::linkKnown( $sectionTitle, $sectionText ) )->text() .
663 if ( $categorySnippet ) {
664 $category = "<span class='searchalttitle'>" .
665 $this->msg( 'search-category' )->rawParams( $categorySnippet )->text() .
669 // format text extract
670 $extract = "<div class='searchresult'>" . $result->getTextSnippet( $terms ) . "</div>";
672 $lang = $this->getLanguage();
674 // format description
675 $byteSize = $result->getByteSize();
676 $wordCount = $result->getWordCount();
677 $timestamp = $result->getTimestamp();
678 $size = $this->msg( 'search-result-size', $lang->formatSize( $byteSize ) )
679 ->numParams( $wordCount )->escaped();
681 if ( $title->getNamespace() == NS_CATEGORY
) {
682 $cat = Category
::newFromTitle( $title );
683 $size = $this->msg( 'search-result-category-size' )
684 ->numParams( $cat->getPageCount(), $cat->getSubcatCount(), $cat->getFileCount() )
688 $date = $lang->userTimeAndDate( $timestamp, $this->getUser() );
691 // Include a thumbnail for media files...
692 if ( $title->getNamespace() == NS_FILE
) {
693 $img = $result->getFile();
694 $img = $img ?
: wfFindFile( $title );
695 if ( $result->isFileMatch() ) {
696 $fileMatch = "<span class='searchalttitle'>" .
697 $this->msg( 'search-file-match' )->escaped() . "</span>";
700 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
702 $desc = $this->msg( 'parentheses' )->rawParams( $img->getShortDesc() )->escaped();
703 // Float doesn't seem to interact well with the bullets.
704 // Table messes up vertical alignment of the bullets.
705 // Bullets are therefore disabled (didn't look great anyway).
707 '<table class="searchResultImage">' .
709 '<td style="width: 120px; text-align: center; vertical-align: top;">' .
710 $thumb->toHtml( array( 'desc-link' => true ) ) .
712 '<td style="vertical-align: top;">' .
713 "{$link} {$redirect} {$category} {$section} {$fileMatch}" .
715 "<div class='mw-search-result-data'>{$desc} - {$date}</div>" .
727 if ( Hooks
::run( 'ShowSearchHit', array(
728 $this, $result, $terms,
729 &$link, &$redirect, &$section, &$extract,
730 &$score, &$size, &$date, &$related,
733 $html = "<li><div class='mw-search-result-heading'>" .
734 "{$link} {$redirect} {$category} {$section} {$fileMatch}</div> {$extract}\n" .
735 "<div class='mw-search-result-data'>{$size} - {$date}</div>" .
743 * Show results from other wikis
745 * @param SearchResultSet|array $matches
746 * @param string $query
750 protected function showInterwiki( $matches, $query ) {
753 $out = "<div id='mw-search-interwiki'><div id='mw-search-interwiki-caption'>" .
754 $this->msg( 'search-interwiki-caption' )->text() . "</div>\n";
755 $out .= "<ul class='mw-search-iwresults'>\n";
757 // work out custom project captions
758 $customCaptions = array();
759 // format per line <iwprefix>:<caption>
760 $customLines = explode( "\n", $this->msg( 'search-interwiki-custom' )->text() );
761 foreach ( $customLines as $line ) {
762 $parts = explode( ":", $line, 2 );
763 if ( count( $parts ) == 2 ) { // validate line
764 $customCaptions[$parts[0]] = $parts[1];
768 if ( !is_array( $matches ) ) {
769 $matches = array( $matches );
772 foreach ( $matches as $set ) {
774 $result = $set->next();
776 $out .= $this->showInterwikiHit( $result, $prev, $query, $customCaptions );
777 $prev = $result->getInterwikiPrefix();
778 $result = $set->next();
782 // @todo Should support paging in a non-confusing way (not sure how though, maybe via ajax)..
783 $out .= "</ul></div>\n";
785 // convert the whole thing to desired language variant
786 $out = $wgContLang->convert( $out );
792 * Show single interwiki link
794 * @param SearchResult $result
795 * @param string $lastInterwiki
796 * @param string $query
797 * @param array $customCaptions Interwiki prefix -> caption
801 protected function showInterwikiHit( $result, $lastInterwiki, $query, $customCaptions ) {
803 if ( $result->isBrokenTitle() ) {
807 $title = $result->getTitle();
809 $titleSnippet = $result->getTitleSnippet();
811 if ( $titleSnippet == '' ) {
812 $titleSnippet = null;
815 $link = Linker
::linkKnown(
820 // format redirect if any
821 $redirectTitle = $result->getRedirectTitle();
822 $redirectText = $result->getRedirectSnippet();
824 if ( !is_null( $redirectTitle ) ) {
825 if ( $redirectText == '' ) {
826 $redirectText = null;
829 $redirect = "<span class='searchalttitle'>" .
830 $this->msg( 'search-redirect' )->rawParams(
831 Linker
::linkKnown( $redirectTitle, $redirectText ) )->text() .
836 // display project name
837 if ( is_null( $lastInterwiki ) ||
$lastInterwiki != $title->getInterwiki() ) {
838 if ( array_key_exists( $title->getInterwiki(), $customCaptions ) ) {
839 // captions from 'search-interwiki-custom'
840 $caption = $customCaptions[$title->getInterwiki()];
842 // default is to show the hostname of the other wiki which might suck
843 // if there are many wikis on one hostname
844 $parsed = wfParseUrl( $title->getFullURL() );
845 $caption = $this->msg( 'search-interwiki-default', $parsed['host'] )->text();
847 // "more results" link (special page stuff could be localized, but we might not know target lang)
848 $searchTitle = Title
::newFromText( $title->getInterwiki() . ":Special:Search" );
849 $searchLink = Linker
::linkKnown(
851 $this->msg( 'search-interwiki-more' )->text(),
855 'fulltext' => 'Search'
858 $out .= "</ul><div class='mw-search-interwiki-project'><span class='mw-search-interwiki-more'>
859 {$searchLink}</span>{$caption}</div>\n<ul>";
862 $out .= "<li>{$link} {$redirect}</li>\n";
868 * Generates the power search box at [[Special:Search]]
870 * @param string $term Search term
872 * @return string HTML form
874 protected function powerSearchBox( $term, $opts ) {
877 // Groups namespaces into rows according to subject
879 foreach ( SearchEngine
::searchableNamespaces() as $namespace => $name ) {
880 $subject = MWNamespace
::getSubject( $namespace );
881 if ( !array_key_exists( $subject, $rows ) ) {
882 $rows[$subject] = "";
885 $name = $wgContLang->getConverter()->convertNamespace( $namespace );
887 $name = $this->msg( 'blanknamespace' )->text();
891 Xml
::openElement( 'td' ) .
895 "mw-search-ns{$namespace}",
896 in_array( $namespace, $this->namespaces
)
898 Xml
::closeElement( 'td' );
901 $rows = array_values( $rows );
902 $numRows = count( $rows );
904 // Lays out namespaces in multiple floating two-column tables so they'll
905 // be arranged nicely while still accommodating different screen widths
906 $namespaceTables = '';
907 for ( $i = 0; $i < $numRows; $i +
= 4 ) {
908 $namespaceTables .= Xml
::openElement( 'table' );
910 for ( $j = $i; $j < $i +
4 && $j < $numRows; $j++
) {
911 $namespaceTables .= Xml
::tags( 'tr', null, $rows[$j] );
914 $namespaceTables .= Xml
::closeElement( 'table' );
917 $showSections = array( 'namespaceTables' => $namespaceTables );
919 Hooks
::run( 'SpecialSearchPowerBox', array( &$showSections, $term, $opts ) );
922 foreach ( $opts as $key => $value ) {
923 $hidden .= Html
::hidden( $key, $value );
926 # Stuff to feed saveNamespaces()
928 $user = $this->getUser();
929 if ( $user->isLoggedIn() ) {
930 $remember .= Xml
::checkLabel(
931 $this->msg( 'powersearch-remember' )->text(),
933 'mw-search-powersearch-remember',
935 // The token goes here rather than in a hidden field so it
936 // is only sent when necessary (not every form submission).
937 array( 'value' => $user->getEditToken(
944 // Return final output
945 return Xml
::openElement( 'fieldset', array( 'id' => 'mw-searchoptions' ) ) .
946 Xml
::element( 'legend', null, $this->msg( 'powersearch-legend' )->text() ) .
947 Xml
::tags( 'h4', null, $this->msg( 'powersearch-ns' )->parse() ) .
948 Xml
::element( 'div', array( 'id' => 'mw-search-togglebox' ), '', false ) .
949 Xml
::element( 'div', array( 'class' => 'divider' ), '', false ) .
950 implode( Xml
::element( 'div', array( 'class' => 'divider' ), '', false ), $showSections ) .
952 Xml
::element( 'div', array( 'class' => 'divider' ), '', false ) .
954 Xml
::closeElement( 'fieldset' );
960 protected function getSearchProfiles() {
961 // Builds list of Search Types (profiles)
962 $nsAllSet = array_keys( SearchEngine
::searchableNamespaces() );
966 'message' => 'searchprofile-articles',
967 'tooltip' => 'searchprofile-articles-tooltip',
968 'namespaces' => SearchEngine
::defaultNamespaces(),
969 'namespace-messages' => SearchEngine
::namespacesAsText(
970 SearchEngine
::defaultNamespaces()
974 'message' => 'searchprofile-images',
975 'tooltip' => 'searchprofile-images-tooltip',
976 'namespaces' => array( NS_FILE
),
979 'message' => 'searchprofile-everything',
980 'tooltip' => 'searchprofile-everything-tooltip',
981 'namespaces' => $nsAllSet,
984 'message' => 'searchprofile-advanced',
985 'tooltip' => 'searchprofile-advanced-tooltip',
986 'namespaces' => self
::NAMESPACES_CURRENT
,
990 Hooks
::run( 'SpecialSearchProfiles', array( &$profiles ) );
992 foreach ( $profiles as &$data ) {
993 if ( !is_array( $data['namespaces'] ) ) {
996 sort( $data['namespaces'] );
1003 * @param string $term
1006 protected function searchProfileTabs( $term ) {
1007 $out = Xml
::openElement( 'div', array( 'class' => 'mw-search-profile-tabs' ) );
1010 if ( $this->startsWithImage( $term ) ) {
1012 $bareterm = substr( $term, strpos( $term, ':' ) +
1 );
1015 $profiles = $this->getSearchProfiles();
1016 $lang = $this->getLanguage();
1018 // Outputs XML for Search Types
1019 $out .= Xml
::openElement( 'div', array( 'class' => 'search-types' ) );
1020 $out .= Xml
::openElement( 'ul' );
1021 foreach ( $profiles as $id => $profile ) {
1022 if ( !isset( $profile['parameters'] ) ) {
1023 $profile['parameters'] = array();
1025 $profile['parameters']['profile'] = $id;
1027 $tooltipParam = isset( $profile['namespace-messages'] ) ?
1028 $lang->commaList( $profile['namespace-messages'] ) : null;
1032 'class' => $this->profile
=== $id ?
'current' : 'normal'
1034 $this->makeSearchLink(
1037 $this->msg( $profile['message'] )->text(),
1038 $this->msg( $profile['tooltip'], $tooltipParam )->text(),
1039 $profile['parameters']
1043 $out .= Xml
::closeElement( 'ul' );
1044 $out .= Xml
::closeElement( 'div' );
1045 $out .= Xml
::element( 'div', array( 'style' => 'clear:both' ), '', false );
1046 $out .= Xml
::closeElement( 'div' );
1052 * @param string $term Search term
1055 protected function searchOptions( $term ) {
1058 $opts['profile'] = $this->profile
;
1060 if ( $this->isPowerSearch() ) {
1061 $out .= $this->powerSearchBox( $term, $opts );
1064 Hooks
::run( 'SpecialSearchProfileForm', array( $this, &$form, $this->profile
, $term, $opts ) );
1072 * @param string $term
1073 * @param int $resultsShown
1074 * @param int $totalNum
1077 protected function shortDialog( $term, $resultsShown, $totalNum ) {
1078 $out = Html
::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
1079 $out .= Html
::hidden( 'profile', $this->profile
) . "\n";
1081 $out .= Html
::input( 'search', $term, 'search', array(
1082 'id' => $this->isPowerSearch() ?
'powerSearchText' : 'searchText',
1084 'autofocus' => trim( $term ) === '',
1085 'class' => 'mw-ui-input mw-ui-input-inline',
1087 $out .= Html
::hidden( 'fulltext', 'Search' ) . "\n";
1088 $out .= Html
::submitButton(
1089 $this->msg( 'searchbutton' )->text(),
1090 array( 'class' => 'mw-ui-button mw-ui-progressive' ),
1091 array( 'mw-ui-progressive' )
1095 if ( $totalNum > 0 && $this->offset
< $totalNum ) {
1096 $top = $this->msg( 'search-showingresults' )
1097 ->numParams( $this->offset +
1, $this->offset +
$resultsShown, $totalNum )
1098 ->numParams( $resultsShown )
1100 $out .= Xml
::tags( 'div', array( 'class' => 'results-info' ), $top ) .
1101 Xml
::element( 'div', array( 'style' => 'clear:both' ), '', false );
1108 * Make a search link with some target namespaces
1110 * @param string $term
1111 * @param array $namespaces Ignored
1112 * @param string $label Link's text
1113 * @param string $tooltip Link's tooltip
1114 * @param array $params Query string parameters
1115 * @return string HTML fragment
1117 protected function makeSearchLink( $term, $namespaces, $label, $tooltip, $params = array() ) {
1119 foreach ( $namespaces as $n ) {
1120 $opt['ns' . $n] = 1;
1123 $stParams = array_merge(
1126 'fulltext' => $this->msg( 'search' )->text()
1131 return Xml
::element(
1134 'href' => $this->getPageTitle()->getLocalURL( $stParams ),
1142 * Check if query starts with image: prefix
1144 * @param string $term The string to check
1147 protected function startsWithImage( $term ) {
1150 $parts = explode( ':', $term );
1151 if ( count( $parts ) > 1 ) {
1152 return $wgContLang->getNsIndex( $parts[0] ) == NS_FILE
;
1159 * Check if query starts with all: prefix
1161 * @param string $term The string to check
1164 protected function startsWithAll( $term ) {
1166 $allkeyword = $this->msg( 'searchall' )->inContentLanguage()->text();
1168 $parts = explode( ':', $term );
1169 if ( count( $parts ) > 1 ) {
1170 return $parts[0] == $allkeyword;
1179 * @return SearchEngine
1181 public function getSearchEngine() {
1182 if ( $this->searchEngine
=== null ) {
1183 $this->searchEngine
= $this->searchEngineType ?
1184 SearchEngine
::create( $this->searchEngineType
) : SearchEngine
::create();
1187 return $this->searchEngine
;
1191 * Current search profile.
1192 * @return null|string
1194 function getProfile() {
1195 return $this->profile
;
1199 * Current namespaces.
1202 function getNamespaces() {
1203 return $this->namespaces
;
1207 * Users of hook SpecialSearchSetupEngine can use this to
1208 * add more params to links to not lose selection when
1209 * user navigates search results.
1212 * @param string $key
1213 * @param mixed $value
1215 public function setExtraParam( $key, $value ) {
1216 $this->extraParams
[$key] = $value;
1219 protected function getGroupName() {