aboutsummaryrefslogtreecommitdiffstats
path: root/includes/search/searchwidgets/DidYouMeanWidget.php
blob: 05dc1caebaee9415ca5a0dcf62802827e906cd0b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php

namespace MediaWiki\Search\SearchWidgets;

use ISearchResultSet;
use MediaWiki\Html\Html;
use MediaWiki\Specials\SpecialSearch;

/**
 * Renders a suggested search for the user, or tells the user
 * a suggested search was run instead of the one provided.
 */
class DidYouMeanWidget {
	protected SpecialSearch $specialSearch;

	public function __construct( SpecialSearch $specialSearch ) {
		$this->specialSearch = $specialSearch;
	}

	/**
	 * @param string $term The user provided search term
	 * @param ISearchResultSet $resultSet
	 * @return string HTML
	 */
	public function render( $term, ISearchResultSet $resultSet ) {
		if ( $resultSet->hasRewrittenQuery() ) {
			$html = $this->rewrittenHtml( $term, $resultSet );
		} elseif ( $resultSet->hasSuggestion() ) {
			$html = $this->suggestionHtml( $resultSet );
		} else {
			return '';
		}

		return Html::rawElement( 'div', [ 'class' => 'searchdidyoumean' ], $html );
	}

	/**
	 * Generates HTML shown to user when their query has been internally
	 * rewritten, and the results of the rewritten query are being returned.
	 *
	 * @param string $term The users search input
	 * @param ISearchResultSet $resultSet The response to the search request
	 * @return string HTML Links the user to their original $term query, and the
	 *  one suggested by $resultSet
	 */
	protected function rewrittenHtml( $term, ISearchResultSet $resultSet ) {
		$params = [
			'search' => $resultSet->getQueryAfterRewrite(),
			// Don't magic this link into a 'go' link, it should always
			// show search results.
			'fulltext' => 1,
		];
		$stParams = array_merge( $params, $this->specialSearch->powerSearchOptions() );

		$linkRenderer = $this->specialSearch->getLinkRenderer();
		$snippet = $resultSet->getQueryAfterRewriteSnippet();
		if ( $snippet === '' || $snippet === null ) {
			// This should never happen. But if it did happen we would render
			// links as `Special:Search` which is even more useless. Since this
			// was only documented but not enforced previously emit a
			// deprecation warning and in the future we can simply fail on bad
			// inputs
			wfDeprecatedMsg(
				get_class( $resultSet ) . '::getQueryAfterRewriteSnippet returning empty snippet ' .
				'was deprecated in MediaWiki 1.35',
				'1.34', false, false
			);
			$snippet = $resultSet->getQueryAfterRewrite();
		}
		$rewritten = $linkRenderer->makeKnownLink(
			$this->specialSearch->getPageTitle(),
			$snippet,
			[ 'id' => 'mw-search-DYM-rewritten' ],
			$stParams
		);

		$original = $term;

		return $this->specialSearch->msg( 'search-rewritten' )
			->rawParams( $rewritten )
			->params( $original )
			->escaped();
	}

	/**
	 * Generates HTML shown to the user when we have a suggestion about
	 * a query that might give more/better results than their current
	 * query.
	 *
	 * @param ISearchResultSet $resultSet
	 * @return string HTML
	 */
	protected function suggestionHtml( ISearchResultSet $resultSet ) {
		$params = [
			'search' => $resultSet->getSuggestionQuery(),
			'fulltext' => 1,
		];
		$stParams = array_merge( $params, $this->specialSearch->powerSearchOptions() );

		$snippet = $resultSet->getSuggestionSnippet();
		if ( $snippet === '' || $snippet === null ) {
			// This should never happen. But if it did happen we would render
			// links as `Special:Search` which is even more useless. Since this
			// was only documented but not enforced previously emit a
			// deprecation warning and in the future we can simply fail on bad
			// inputs
			wfDeprecatedMsg(
				get_class( $resultSet ) . '::getSuggestionSnippet returning empty snippet ' .
				'was deprecated in MediaWiki 1.35',
				'1.34', false, false
			);
			$snippet = $resultSet->getSuggestionSnippet();
		}
		$suggest = $this->specialSearch->getLinkRenderer()->makeKnownLink(
			$this->specialSearch->getPageTitle(),
			$snippet,
			[ 'id' => 'mw-search-DYM-suggestion' ],
			$stParams
		);

		return $this->specialSearch->msg( 'search-suggest' )
			->rawParams( $suggest )->parse();
	}
}