Merge "Use the WebRequest::getCheck() shortcut where possible"
[lhc/web/wiklou.git] / includes / specials / SpecialSearch.php
1 <?php
2 /**
3 * Implements Special:Search
4 *
5 * Copyright © 2004 Brion Vibber <brion@pobox.com>
6 *
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.
11 *
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.
16 *
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
21 *
22 * @file
23 * @ingroup SpecialPage
24 */
25
26 use MediaWiki\MediaWikiServices;
27 use MediaWiki\Widget\Search\BasicSearchResultSetWidget;
28 use MediaWiki\Widget\Search\FullSearchResultWidget;
29 use MediaWiki\Widget\Search\InterwikiSearchResultWidget;
30 use MediaWiki\Widget\Search\InterwikiSearchResultSetWidget;
31 use MediaWiki\Widget\Search\SimpleSearchResultWidget;
32 use MediaWiki\Widget\Search\SimpleSearchResultSetWidget;
33
34 /**
35 * implements Special:Search - Run text & title search and display the output
36 * @ingroup SpecialPage
37 */
38 class SpecialSearch extends SpecialPage {
39 /**
40 * Current search profile. Search profile is just a name that identifies
41 * the active search tab on the search page (content, discussions...)
42 * For users tt replaces the set of enabled namespaces from the query
43 * string when applicable. Extensions can add new profiles with hooks
44 * with custom search options just for that profile.
45 * @var null|string
46 */
47 protected $profile;
48
49 /** @var SearchEngine Search engine */
50 protected $searchEngine;
51
52 /** @var string Search engine type, if not default */
53 protected $searchEngineType;
54
55 /** @var array For links */
56 protected $extraParams = [];
57
58 /**
59 * @var string The prefix url parameter. Set on the searcher and the
60 * is expected to treat it as prefix filter on titles.
61 */
62 protected $mPrefix;
63
64 /**
65 * @var int
66 */
67 protected $limit, $offset;
68
69 /**
70 * @var array
71 */
72 protected $namespaces;
73
74 /**
75 * @var string
76 */
77 protected $fulltext;
78
79 /**
80 * @var string
81 */
82 protected $sort;
83
84 /**
85 * @var bool
86 */
87 protected $runSuggestion = true;
88
89 /**
90 * Search engine configurations.
91 * @var SearchEngineConfig
92 */
93 protected $searchConfig;
94
95 const NAMESPACES_CURRENT = 'sense';
96
97 public function __construct() {
98 parent::__construct( 'Search' );
99 $this->searchConfig = MediaWikiServices::getInstance()->getSearchEngineConfig();
100 }
101
102 /**
103 * Entry point
104 *
105 * @param string|null $par
106 */
107 public function execute( $par ) {
108 $request = $this->getRequest();
109 $out = $this->getOutput();
110
111 // Fetch the search term
112 $term = str_replace( "\n", " ", $request->getText( 'search' ) );
113
114 // Historically search terms have been accepted not only in the search query
115 // parameter, but also as part of the primary url. This can have PII implications
116 // in releasing page view data. As such issue a 301 redirect to the correct
117 // URL.
118 if ( $par !== null && $par !== '' && $term === '' ) {
119 $query = $request->getValues();
120 unset( $query['title'] );
121 // Strip underscores from title parameter; most of the time we'll want
122 // text form here. But don't strip underscores from actual text params!
123 $query['search'] = str_replace( '_', ' ', $par );
124 $out->redirect( $this->getPageTitle()->getFullURL( $query ), 301 );
125 return;
126 }
127
128 // Need to load selected namespaces before handling nsRemember
129 $this->load();
130 // TODO: This performs database actions on GET request, which is going to
131 // be a problem for our multi-datacenter work.
132 if ( $request->getCheck( 'nsRemember' ) ) {
133 $this->saveNamespaces();
134 // Remove the token from the URL to prevent the user from inadvertently
135 // exposing it (e.g. by pasting it into a public wiki page) or undoing
136 // later settings changes (e.g. by reloading the page).
137 $query = $request->getValues();
138 unset( $query['title'], $query['nsRemember'] );
139 $out->redirect( $this->getPageTitle()->getFullURL( $query ) );
140 return;
141 }
142
143 $this->searchEngineType = $request->getVal( 'srbackend' );
144 if ( !$request->getVal( 'fulltext' ) && !$request->getCheck( 'offset' ) ) {
145 $url = $this->goResult( $term );
146 if ( $url !== null ) {
147 // successful 'go'
148 $out->redirect( $url );
149 return;
150 }
151 // No match. If it could plausibly be a title
152 // run the No go match hook.
153 $title = Title::newFromText( $term );
154 if ( !is_null( $title ) ) {
155 Hooks::run( 'SpecialSearchNogomatch', [ &$title ] );
156 }
157 }
158
159 $this->setupPage( $term );
160
161 if ( $this->getConfig()->get( 'DisableTextSearch' ) ) {
162 $searchForwardUrl = $this->getConfig()->get( 'SearchForwardUrl' );
163 if ( $searchForwardUrl ) {
164 $url = str_replace( '$1', urlencode( $term ), $searchForwardUrl );
165 $out->redirect( $url );
166 } else {
167 $this->showGoogleSearch( $term );
168 }
169
170 return;
171 }
172
173 $this->showResults( $term );
174 }
175
176 /**
177 * Output a google search form if search is disabled
178 *
179 * @param string $term Search term
180 * @todo FIXME Maybe we should get rid of this raw html message at some future time
181 * @suppress SecurityCheck-XSS
182 */
183 private function showGoogleSearch( $term ) {
184 $this->getOutput()->addHTML(
185 "<fieldset>" .
186 "<legend>" .
187 $this->msg( 'search-external' )->escaped() .
188 "</legend>" .
189 "<p class='mw-searchdisabled'>" .
190 $this->msg( 'searchdisabled' )->escaped() .
191 "</p>" .
192 $this->msg( 'googlesearch' )->rawParams(
193 htmlspecialchars( $term ),
194 'UTF-8',
195 $this->msg( 'searchbutton' )->escaped()
196 )->text() .
197 "</fieldset>"
198 );
199 }
200
201 /**
202 * Set up basic search parameters from the request and user settings.
203 *
204 * @see tests/phpunit/includes/specials/SpecialSearchTest.php
205 */
206 public function load() {
207 $request = $this->getRequest();
208 list( $this->limit, $this->offset ) = $request->getLimitOffset( 20, '' );
209 $this->mPrefix = $request->getVal( 'prefix', '' );
210 if ( $this->mPrefix !== '' ) {
211 $this->setExtraParam( 'prefix', $this->mPrefix );
212 }
213
214 $this->sort = $request->getVal( 'sort', SearchEngine::DEFAULT_SORT );
215 if ( $this->sort !== SearchEngine::DEFAULT_SORT ) {
216 $this->setExtraParam( 'sort', $this->sort );
217 }
218
219 $user = $this->getUser();
220
221 # Extract manually requested namespaces
222 $nslist = $this->powerSearch( $request );
223 if ( $nslist === [] ) {
224 # Fallback to user preference
225 $nslist = $this->searchConfig->userNamespaces( $user );
226 }
227
228 $profile = null;
229 if ( $nslist === [] ) {
230 $profile = 'default';
231 }
232
233 $profile = $request->getVal( 'profile', $profile );
234 $profiles = $this->getSearchProfiles();
235 if ( $profile === null ) {
236 // BC with old request format
237 $profile = 'advanced';
238 foreach ( $profiles as $key => $data ) {
239 if ( $nslist === $data['namespaces'] && $key !== 'advanced' ) {
240 $profile = $key;
241 }
242 }
243 $this->namespaces = $nslist;
244 } elseif ( $profile === 'advanced' ) {
245 $this->namespaces = $nslist;
246 } else {
247 if ( isset( $profiles[$profile]['namespaces'] ) ) {
248 $this->namespaces = $profiles[$profile]['namespaces'];
249 } else {
250 // Unknown profile requested
251 $profile = 'default';
252 $this->namespaces = $profiles['default']['namespaces'];
253 }
254 }
255
256 $this->fulltext = $request->getVal( 'fulltext' );
257 $this->runSuggestion = (bool)$request->getVal( 'runsuggestion', true );
258 $this->profile = $profile;
259 }
260
261 /**
262 * If an exact title match can be found, jump straight ahead to it.
263 *
264 * @param string $term
265 * @return string|null The url to redirect to, or null if no redirect.
266 */
267 public function goResult( $term ) {
268 # If the string cannot be used to create a title
269 if ( is_null( Title::newFromText( $term ) ) ) {
270 return null;
271 }
272 # If there's an exact or very near match, jump right there.
273 $title = $this->getSearchEngine()
274 ->getNearMatcher( $this->getConfig() )->getNearMatch( $term );
275 if ( is_null( $title ) ) {
276 return null;
277 }
278 $url = null;
279 if ( !Hooks::run( 'SpecialSearchGoResult', [ $term, $title, &$url ] ) ) {
280 return null;
281 }
282
283 return $url ?? $title->getFullUrlForRedirect();
284 }
285
286 /**
287 * @param string $term
288 */
289 public function showResults( $term ) {
290 if ( $this->searchEngineType !== null ) {
291 $this->setExtraParam( 'srbackend', $this->searchEngineType );
292 }
293
294 $out = $this->getOutput();
295 $formWidget = new MediaWiki\Widget\Search\SearchFormWidget(
296 $this,
297 $this->searchConfig,
298 $this->getSearchProfiles()
299 );
300 $filePrefix = MediaWikiServices::getInstance()->getContentLanguage()->
301 getFormattedNsText( NS_FILE ) . ':';
302 if ( trim( $term ) === '' || $filePrefix === trim( $term ) ) {
303 // Empty query -- straight view of search form
304 if ( !Hooks::run( 'SpecialSearchResultsPrepend', [ $this, $out, $term ] ) ) {
305 # Hook requested termination
306 return;
307 }
308 $out->enableOOUI();
309 // The form also contains the 'Showing results 0 - 20 of 1234' so we can
310 // only do the form render here for the empty $term case. Rendering
311 // the form when a search is provided is repeated below.
312 $out->addHTML( $formWidget->render(
313 $this->profile, $term, 0, 0, $this->offset, $this->isPowerSearch()
314 ) );
315 return;
316 }
317
318 $search = $this->getSearchEngine();
319 $search->setFeatureData( 'rewrite', $this->runSuggestion );
320 $search->setLimitOffset( $this->limit, $this->offset );
321 $search->setNamespaces( $this->namespaces );
322 $search->setSort( $this->sort );
323 $search->prefix = $this->mPrefix;
324
325 Hooks::run( 'SpecialSearchSetupEngine', [ $this, $this->profile, $search ] );
326 if ( !Hooks::run( 'SpecialSearchResultsPrepend', [ $this, $out, $term ] ) ) {
327 # Hook requested termination
328 return;
329 }
330
331 $title = Title::newFromText( $term );
332 $showSuggestion = $title === null || !$title->isKnown();
333 $search->setShowSuggestion( $showSuggestion );
334
335 $rewritten = $search->transformSearchTerm( $term );
336 if ( $rewritten !== $term ) {
337 $term = $rewritten;
338 wfDeprecated( 'SearchEngine::transformSearchTerm() (overridden by ' .
339 get_class( $search ) . ')', '1.32' );
340 }
341
342 $rewritten = $search->replacePrefixes( $term );
343 if ( $rewritten !== $term ) {
344 wfDeprecated( 'SearchEngine::replacePrefixes() (overridden by ' .
345 get_class( $search ) . ')', '1.32' );
346 }
347
348 // fetch search results
349 $titleMatches = $search->searchTitle( $rewritten );
350 $textMatches = $search->searchText( $rewritten );
351
352 $textStatus = null;
353 if ( $textMatches instanceof Status ) {
354 $textStatus = $textMatches;
355 $textMatches = $textStatus->getValue();
356 }
357
358 // Get number of results
359 $titleMatchesNum = $textMatchesNum = $numTitleMatches = $numTextMatches = 0;
360 if ( $titleMatches ) {
361 $titleMatchesNum = $titleMatches->numRows();
362 $numTitleMatches = $titleMatches->getTotalHits();
363 }
364 if ( $textMatches ) {
365 $textMatchesNum = $textMatches->numRows();
366 $numTextMatches = $textMatches->getTotalHits();
367 if ( $textMatchesNum > 0 ) {
368 $search->augmentSearchResults( $textMatches );
369 }
370 }
371 $num = $titleMatchesNum + $textMatchesNum;
372 $totalRes = $numTitleMatches + $numTextMatches;
373
374 // start rendering the page
375 $out->enableOOUI();
376 $out->addHTML( $formWidget->render(
377 $this->profile, $term, $num, $totalRes, $this->offset, $this->isPowerSearch()
378 ) );
379
380 // did you mean... suggestions
381 if ( $textMatches ) {
382 $dymWidget = new MediaWiki\Widget\Search\DidYouMeanWidget( $this );
383 $out->addHTML( $dymWidget->render( $term, $textMatches ) );
384 }
385
386 $hasErrors = $textStatus && $textStatus->getErrors() !== [];
387 $hasOtherResults = $textMatches &&
388 $textMatches->hasInterwikiResults( SearchResultSet::INLINE_RESULTS );
389
390 if ( $textMatches && $textMatches->hasInterwikiResults( SearchResultSet::SECONDARY_RESULTS ) ) {
391 $out->addHTML( '<div class="searchresults mw-searchresults-has-iw">' );
392 } else {
393 $out->addHTML( '<div class="searchresults">' );
394 }
395
396 if ( $hasErrors ) {
397 list( $error, $warning ) = $textStatus->splitByErrorType();
398 if ( $error->getErrors() ) {
399 $out->addHTML( Html::errorBox(
400 $error->getHTML( 'search-error' )
401 ) );
402 }
403 if ( $warning->getErrors() ) {
404 $out->addHTML( Html::warningBox(
405 $warning->getHTML( 'search-warning' )
406 ) );
407 }
408 }
409
410 // Show the create link ahead
411 $this->showCreateLink( $title, $num, $titleMatches, $textMatches );
412
413 Hooks::run( 'SpecialSearchResults', [ $term, &$titleMatches, &$textMatches ] );
414
415 // If we have no results and have not already displayed an error message
416 if ( $num === 0 && !$hasErrors ) {
417 $out->wrapWikiMsg( "<p class=\"mw-search-nonefound\">\n$1</p>", [
418 $hasOtherResults ? 'search-nonefound-thiswiki' : 'search-nonefound',
419 wfEscapeWikiText( $term )
420 ] );
421 }
422
423 // Although $num might be 0 there can still be secondary or inline
424 // results to display.
425 $linkRenderer = $this->getLinkRenderer();
426 $mainResultWidget = new FullSearchResultWidget( $this, $linkRenderer );
427
428 // Default (null) on. Can be explicitly disabled.
429 if ( $search->getFeatureData( 'enable-new-crossproject-page' ) !== false ) {
430 $sidebarResultWidget = new InterwikiSearchResultWidget( $this, $linkRenderer );
431 $sidebarResultsWidget = new InterwikiSearchResultSetWidget(
432 $this,
433 $sidebarResultWidget,
434 $linkRenderer,
435 MediaWikiServices::getInstance()->getInterwikiLookup(),
436 $search->getFeatureData( 'show-multimedia-search-results' )
437 );
438 } else {
439 $sidebarResultWidget = new SimpleSearchResultWidget( $this, $linkRenderer );
440 $sidebarResultsWidget = new SimpleSearchResultSetWidget(
441 $this,
442 $sidebarResultWidget,
443 $linkRenderer,
444 MediaWikiServices::getInstance()->getInterwikiLookup()
445 );
446 }
447
448 $widget = new BasicSearchResultSetWidget( $this, $mainResultWidget, $sidebarResultsWidget );
449
450 $out->addHTML( $widget->render(
451 $term, $this->offset, $titleMatches, $textMatches
452 ) );
453
454 if ( $titleMatches ) {
455 $titleMatches->free();
456 }
457
458 if ( $textMatches ) {
459 $textMatches->free();
460 }
461
462 $out->addHTML( '<div class="mw-search-visualclear"></div>' );
463
464 // prev/next links
465 if ( $totalRes > $this->limit || $this->offset ) {
466 // Allow matches to define the correct offset, as interleaved
467 // AB testing may require a different next page offset.
468 if ( $textMatches && $textMatches->getOffset() !== null ) {
469 $offset = $textMatches->getOffset();
470 } else {
471 $offset = $this->offset;
472 }
473
474 $prevnext = $this->getLanguage()->viewPrevNext(
475 $this->getPageTitle(),
476 $offset,
477 $this->limit,
478 $this->powerSearchOptions() + [ 'search' => $term ],
479 $this->limit + $this->offset >= $totalRes
480 );
481 $out->addHTML( "<p class='mw-search-pager-bottom'>{$prevnext}</p>\n" );
482 }
483
484 // Close <div class='searchresults'>
485 $out->addHTML( "</div>" );
486
487 Hooks::run( 'SpecialSearchResultsAppend', [ $this, $out, $term ] );
488 }
489
490 /**
491 * @param Title $title
492 * @param int $num The number of search results found
493 * @param null|SearchResultSet $titleMatches Results from title search
494 * @param null|SearchResultSet $textMatches Results from text search
495 */
496 protected function showCreateLink( $title, $num, $titleMatches, $textMatches ) {
497 // show direct page/create link if applicable
498
499 // Check DBkey !== '' in case of fragment link only.
500 if ( is_null( $title ) || $title->getDBkey() === ''
501 || ( $titleMatches !== null && $titleMatches->searchContainedSyntax() )
502 || ( $textMatches !== null && $textMatches->searchContainedSyntax() )
503 ) {
504 // invalid title
505 // preserve the paragraph for margins etc...
506 $this->getOutput()->addHTML( '<p></p>' );
507
508 return;
509 }
510
511 $messageName = 'searchmenu-new-nocreate';
512 $linkClass = 'mw-search-createlink';
513
514 if ( !$title->isExternal() ) {
515 if ( $title->isKnown() ) {
516 $messageName = 'searchmenu-exists';
517 $linkClass = 'mw-search-exists';
518 } elseif ( ContentHandler::getForTitle( $title )->supportsDirectEditing()
519 && $title->quickUserCan( 'create', $this->getUser() )
520 ) {
521 $messageName = 'searchmenu-new';
522 }
523 }
524
525 $params = [
526 $messageName,
527 wfEscapeWikiText( $title->getPrefixedText() ),
528 Message::numParam( $num )
529 ];
530 Hooks::run( 'SpecialSearchCreateLink', [ $title, &$params ] );
531
532 // Extensions using the hook might still return an empty $messageName
533 if ( $messageName ) {
534 $this->getOutput()->wrapWikiMsg( "<p class=\"$linkClass\">\n$1</p>", $params );
535 } else {
536 // preserve the paragraph for margins etc...
537 $this->getOutput()->addHTML( '<p></p>' );
538 }
539 }
540
541 /**
542 * Sets up everything for the HTML output page including styles, javascript,
543 * page title, etc.
544 *
545 * @param string $term
546 */
547 protected function setupPage( $term ) {
548 $out = $this->getOutput();
549
550 $this->setHeaders();
551 $this->outputHeader();
552 // TODO: Is this true? The namespace remember uses a user token
553 // on save.
554 $out->allowClickjacking();
555 $this->addHelpLink( 'Help:Searching' );
556
557 if ( strval( $term ) !== '' ) {
558 $out->setPageTitle( $this->msg( 'searchresults' ) );
559 $out->setHTMLTitle( $this->msg( 'pagetitle' )
560 ->plaintextParams( $this->msg( 'searchresults-title' )->plaintextParams( $term )->text() )
561 ->inContentLanguage()->text()
562 );
563 }
564
565 if ( $this->mPrefix !== '' ) {
566 $subtitle = $this->msg( 'search-filter-title-prefix' )->plaintextParams( $this->mPrefix );
567 $params = $this->powerSearchOptions();
568 unset( $params['prefix'] );
569 $params += [
570 'search' => $term,
571 'fulltext' => 1,
572 ];
573
574 $subtitle .= ' (';
575 $subtitle .= Xml::element(
576 'a',
577 [
578 'href' => $this->getPageTitle()->getLocalURL( $params ),
579 'title' => $this->msg( 'search-filter-title-prefix-reset' )->text(),
580 ],
581 $this->msg( 'search-filter-title-prefix-reset' )->text()
582 );
583 $subtitle .= ')';
584 $out->setSubtitle( $subtitle );
585 }
586
587 $out->addJsConfigVars( [ 'searchTerm' => $term ] );
588 $out->addModules( 'mediawiki.special.search' );
589 $out->addModuleStyles( [
590 'mediawiki.special', 'mediawiki.special.search.styles', 'mediawiki.ui', 'mediawiki.ui.button',
591 'mediawiki.ui.input', 'mediawiki.widgets.SearchInputWidget.styles',
592 ] );
593 }
594
595 /**
596 * Return true if current search is a power (advanced) search
597 *
598 * @return bool
599 */
600 protected function isPowerSearch() {
601 return $this->profile === 'advanced';
602 }
603
604 /**
605 * Extract "power search" namespace settings from the request object,
606 * returning a list of index numbers to search.
607 *
608 * @param WebRequest &$request
609 * @return array
610 */
611 protected function powerSearch( &$request ) {
612 $arr = [];
613 foreach ( $this->searchConfig->searchableNamespaces() as $ns => $name ) {
614 if ( $request->getCheck( 'ns' . $ns ) ) {
615 $arr[] = $ns;
616 }
617 }
618
619 return $arr;
620 }
621
622 /**
623 * Reconstruct the 'power search' options for links
624 * TODO: Instead of exposing this publicly, could we instead expose
625 * a function for creating search links?
626 *
627 * @return array
628 */
629 public function powerSearchOptions() {
630 $opt = [];
631 if ( $this->isPowerSearch() ) {
632 foreach ( $this->namespaces as $n ) {
633 $opt['ns' . $n] = 1;
634 }
635 } else {
636 $opt['profile'] = $this->profile;
637 }
638
639 return $opt + $this->extraParams;
640 }
641
642 /**
643 * Save namespace preferences when we're supposed to
644 *
645 * @return bool Whether we wrote something
646 */
647 protected function saveNamespaces() {
648 $user = $this->getUser();
649 $request = $this->getRequest();
650
651 if ( $user->isLoggedIn() &&
652 $user->matchEditToken(
653 $request->getVal( 'nsRemember' ),
654 'searchnamespace',
655 $request
656 ) && !wfReadOnly()
657 ) {
658 // Reset namespace preferences: namespaces are not searched
659 // when they're not mentioned in the URL parameters.
660 foreach ( MWNamespace::getValidNamespaces() as $n ) {
661 $user->setOption( 'searchNs' . $n, false );
662 }
663 // The request parameters include all the namespaces to be searched.
664 // Even if they're the same as an existing profile, they're not eaten.
665 foreach ( $this->namespaces as $n ) {
666 $user->setOption( 'searchNs' . $n, true );
667 }
668
669 DeferredUpdates::addCallableUpdate( function () use ( $user ) {
670 $user->saveSettings();
671 } );
672
673 return true;
674 }
675
676 return false;
677 }
678
679 /**
680 * @return array
681 */
682 protected function getSearchProfiles() {
683 // Builds list of Search Types (profiles)
684 $nsAllSet = array_keys( $this->searchConfig->searchableNamespaces() );
685 $defaultNs = $this->searchConfig->defaultNamespaces();
686 $profiles = [
687 'default' => [
688 'message' => 'searchprofile-articles',
689 'tooltip' => 'searchprofile-articles-tooltip',
690 'namespaces' => $defaultNs,
691 'namespace-messages' => $this->searchConfig->namespacesAsText(
692 $defaultNs
693 ),
694 ],
695 'images' => [
696 'message' => 'searchprofile-images',
697 'tooltip' => 'searchprofile-images-tooltip',
698 'namespaces' => [ NS_FILE ],
699 ],
700 'all' => [
701 'message' => 'searchprofile-everything',
702 'tooltip' => 'searchprofile-everything-tooltip',
703 'namespaces' => $nsAllSet,
704 ],
705 'advanced' => [
706 'message' => 'searchprofile-advanced',
707 'tooltip' => 'searchprofile-advanced-tooltip',
708 'namespaces' => self::NAMESPACES_CURRENT,
709 ]
710 ];
711
712 Hooks::run( 'SpecialSearchProfiles', [ &$profiles ] );
713
714 foreach ( $profiles as &$data ) {
715 if ( !is_array( $data['namespaces'] ) ) {
716 continue;
717 }
718 sort( $data['namespaces'] );
719 }
720
721 return $profiles;
722 }
723
724 /**
725 * @since 1.18
726 *
727 * @return SearchEngine
728 */
729 public function getSearchEngine() {
730 if ( $this->searchEngine === null ) {
731 $services = MediaWikiServices::getInstance();
732 $this->searchEngine = $this->searchEngineType ?
733 $services->getSearchEngineFactory()->create( $this->searchEngineType ) :
734 $services->newSearchEngine();
735 }
736
737 return $this->searchEngine;
738 }
739
740 /**
741 * Current search profile.
742 * @return null|string
743 */
744 function getProfile() {
745 return $this->profile;
746 }
747
748 /**
749 * Current namespaces.
750 * @return array
751 */
752 function getNamespaces() {
753 return $this->namespaces;
754 }
755
756 /**
757 * Users of hook SpecialSearchSetupEngine can use this to
758 * add more params to links to not lose selection when
759 * user navigates search results.
760 * @since 1.18
761 *
762 * @param string $key
763 * @param mixed $value
764 */
765 public function setExtraParam( $key, $value ) {
766 $this->extraParams[$key] = $value;
767 }
768
769 /**
770 * The prefix value send to Special:Search using the 'prefix' URI param
771 * It means that the user is willing to search for pages whose titles start with
772 * this prefix value.
773 * (Used by the InputBox extension)
774 *
775 * @return string
776 */
777 public function getPrefix() {
778 return $this->mPrefix;
779 }
780
781 protected function getGroupName() {
782 return 'pages';
783 }
784 }