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