| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Search\SearchWidgets; |
| 4 |
|
| 5 |
use ISearchResultSet; |
| 6 |
use MediaWiki\Html\Html; |
| 7 |
use MediaWiki\Specials\SpecialSearch; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class DidYouMeanWidget { |
| 14 |
|
| 15 |
protected $specialSearch; |
| 16 |
|
| 17 |
public function __construct( SpecialSearch $specialSearch ) { |
| 18 |
$this->specialSearch = $specialSearch; |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public function render( $term, ISearchResultSet $resultSet ) { |
| 27 |
if ( $resultSet->hasRewrittenQuery() ) { |
| 28 |
$html = $this->rewrittenHtml( $term, $resultSet ); |
| 29 |
} elseif ( $resultSet->hasSuggestion() ) { |
| 30 |
$html = $this->suggestionHtml( $resultSet ); |
| 31 |
} else { |
| 32 |
return ''; |
| 33 |
} |
| 34 |
|
| 35 |
return Html::rawElement( 'div', [ 'class' => 'searchdidyoumean' ], $html ); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
protected function rewrittenHtml( $term, ISearchResultSet $resultSet ) { |
| 48 |
$params = [ |
| 49 |
'search' => $resultSet->getQueryAfterRewrite(), |
| 50 |
|
| 51 |
|
| 52 |
'fulltext' => 1, |
| 53 |
]; |
| 54 |
$stParams = array_merge( $params, $this->specialSearch->powerSearchOptions() ); |
| 55 |
|
| 56 |
$linkRenderer = $this->specialSearch->getLinkRenderer(); |
| 57 |
$snippet = $resultSet->getQueryAfterRewriteSnippet(); |
| 58 |
if ( $snippet === '' || $snippet === null ) { |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
wfDeprecatedMsg( |
| 65 |
get_class( $resultSet ) . '::getQueryAfterRewriteSnippet returning empty snippet ' . |
| 66 |
'was deprecated in MediaWiki 1.35', |
| 67 |
'1.34', false, false |
| 68 |
); |
| 69 |
$snippet = $resultSet->getQueryAfterRewrite(); |
| 70 |
} |
| 71 |
$rewritten = $linkRenderer->makeKnownLink( |
| 72 |
$this->specialSearch->getPageTitle(), |
| 73 |
$snippet, |
| 74 |
[ 'id' => 'mw-search-DYM-rewritten' ], |
| 75 |
$stParams |
| 76 |
); |
| 77 |
|
| 78 |
$original = $term; |
| 79 |
|
| 80 |
return $this->specialSearch->msg( 'search-rewritten' ) |
| 81 |
->rawParams( $rewritten ) |
| 82 |
->params( $original ) |
| 83 |
->escaped(); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
protected function suggestionHtml( ISearchResultSet $resultSet ) { |
| 95 |
$params = [ |
| 96 |
'search' => $resultSet->getSuggestionQuery(), |
| 97 |
'fulltext' => 1, |
| 98 |
]; |
| 99 |
$stParams = array_merge( $params, $this->specialSearch->powerSearchOptions() ); |
| 100 |
|
| 101 |
$snippet = $resultSet->getSuggestionSnippet(); |
| 102 |
if ( $snippet === '' || $snippet === null ) { |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
wfDeprecatedMsg( |
| 109 |
get_class( $resultSet ) . '::getSuggestionSnippet returning empty snippet ' . |
| 110 |
'was deprecated in MediaWiki 1.35', |
| 111 |
'1.34', false, false |
| 112 |
); |
| 113 |
$snippet = $resultSet->getSuggestionSnippet(); |
| 114 |
} |
| 115 |
$suggest = $this->specialSearch->getLinkRenderer()->makeKnownLink( |
| 116 |
$this->specialSearch->getPageTitle(), |
| 117 |
$snippet, |
| 118 |
[ 'id' => 'mw-search-DYM-suggestion' ], |
| 119 |
$stParams |
| 120 |
); |
| 121 |
|
| 122 |
return $this->specialSearch->msg( 'search-suggest' ) |
| 123 |
->rawParams( $suggest )->parse(); |
| 124 |
} |
| 125 |
} |