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