Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 76 |
|
0.00% |
0 / 14 |
CRAP | |
0.00% |
0 / 1 |
| EntityResult | |
0.00% |
0 / 76 |
|
0.00% |
0 / 14 |
870 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
56 | |||
| getHighlightOrField | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
20 | |||
| getSourceField | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| processHighlighting | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getLabelData | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDescriptionData | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLabelHighlightedData | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDescriptionHighlightedData | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTitleSnippet | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTextSnippet | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getExtraDisplay | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getStatementCount | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getSitelinkCount | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getExtensionData | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | namespace Wikibase\Search\Elastic; |
| 3 | |
| 4 | use CirrusSearch\Search\Result; |
| 5 | use CirrusSearch\Searcher; |
| 6 | use Wikibase\Lib\TermLanguageFallbackChain; |
| 7 | use Wikibase\Repo\Search\ExtendedResult; |
| 8 | use Wikimedia\HtmlArmor\HtmlArmor; |
| 9 | |
| 10 | /** |
| 11 | * Single result for entity search. |
| 12 | */ |
| 13 | class EntityResult extends Result implements ExtendedResult { |
| 14 | /** |
| 15 | * Key which holds wikibase data for result extra data. |
| 16 | */ |
| 17 | private const WIKIBASE_EXTRA_DATA = 'wikibase'; |
| 18 | |
| 19 | /** |
| 20 | * Label data with highlighting. |
| 21 | * ['language' => LANG, 'value' => TEXT] |
| 22 | * @var string[] |
| 23 | */ |
| 24 | private $labelHighlightedData; |
| 25 | /** |
| 26 | * Raw label data from source. |
| 27 | * ['language' => LANG, 'value' => TEXT] |
| 28 | * @var string[] |
| 29 | */ |
| 30 | private $labelData; |
| 31 | /** |
| 32 | * Description data with highlighting. |
| 33 | * ['language' => LANG, 'value' => TEXT] |
| 34 | * @var string[] |
| 35 | */ |
| 36 | private $descriptionData; |
| 37 | /** |
| 38 | * Description data from source. |
| 39 | * ['language' => LANG, 'value' => TEXT] |
| 40 | * @var string[] |
| 41 | */ |
| 42 | private $descriptionHighlightedData; |
| 43 | |
| 44 | /** |
| 45 | * Did we capture actual match in display of |
| 46 | * label or description? |
| 47 | * @var bool |
| 48 | */ |
| 49 | private $haveMatch; |
| 50 | /** |
| 51 | * Extra display field for match. |
| 52 | * ['language' => LANG, 'value' => TEXT] |
| 53 | * @var array |
| 54 | */ |
| 55 | private $extraDisplay; |
| 56 | /** |
| 57 | * Original source data |
| 58 | * @var array |
| 59 | */ |
| 60 | private $sourceData; |
| 61 | |
| 62 | public function __construct( |
| 63 | string $displayLanguage, |
| 64 | TermLanguageFallbackChain $displayFallbackChain, |
| 65 | \Elastica\Result $result, |
| 66 | ) { |
| 67 | // Let Cirrus\Result class handle the boring stuff |
| 68 | parent::__construct( null, $result ); |
| 69 | // FIXME: null is not nice, but Result doesn't really need it... |
| 70 | // Think how to fix this. |
| 71 | |
| 72 | $this->sourceData = $result->getSource(); |
| 73 | $highlightData = $result->getHighlights(); |
| 74 | |
| 75 | // If our highlight hit is on alias, we have to put real label into the field, |
| 76 | // and alias in extra |
| 77 | $isAlias = false; |
| 78 | if ( !empty( $highlightData["labels.{$displayLanguage}.plain"] ) ) { |
| 79 | $hlLabel = $highlightData["labels.{$displayLanguage}.plain"][0]; |
| 80 | if ( preg_match( ElasticTermResult::HIGHLIGHT_PATTERN, $hlLabel, $match ) ) { |
| 81 | $isAlias = ( $hlLabel[0] !== '0' ); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | $this->labelData = $this->getSourceField( |
| 86 | 'labels', $displayLanguage, $displayFallbackChain, $this->sourceData ); |
| 87 | if ( $isAlias ) { |
| 88 | // We have matched alias for highlighting, so we will show regular label and |
| 89 | // alias as extra data |
| 90 | $this->labelHighlightedData = $this->labelData; |
| 91 | $this->extraDisplay = $this->getHighlightOrField( 'labels', $displayLanguage, |
| 92 | $displayFallbackChain, $highlightData, $this->sourceData, true ); |
| 93 | // Since isAlias can be true only if labels is in highlight data, the above will |
| 94 | // also set $this->haveMatch |
| 95 | } else { |
| 96 | $this->labelHighlightedData = $this->getHighlightOrField( 'labels', $displayLanguage, |
| 97 | $displayFallbackChain, $highlightData, $this->sourceData, true ); |
| 98 | } |
| 99 | |
| 100 | $this->descriptionHighlightedData = $this->getHighlightOrField( |
| 101 | 'descriptions', $displayLanguage, $displayFallbackChain, |
| 102 | $highlightData, $this->sourceData ); |
| 103 | $this->descriptionData = $this->getSourceField( |
| 104 | 'descriptions', $displayLanguage, $displayFallbackChain, $this->sourceData ); |
| 105 | |
| 106 | if ( !$this->haveMatch ) { |
| 107 | reset( $highlightData ); |
| 108 | $key = key( $highlightData ); |
| 109 | if ( $key && preg_match( '/^(\w+)\.([^.]+)\.plain$/', $key, $match ) ) { |
| 110 | $this->extraDisplay = [ |
| 111 | 'language' => $match[2], |
| 112 | // @phan-suppress-next-line PhanTypeArraySuspiciousNullable |
| 113 | 'value' => new HtmlArmor( $this->processHighlighting( $highlightData[$key][0], |
| 114 | $match[1] === 'labels' ) ) |
| 115 | ]; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Extract field value from highlighting or source data. |
| 122 | * @param string $field |
| 123 | * @param string $displayLanguage |
| 124 | * @param TermLanguageFallbackChain $displayFallbackChain |
| 125 | * @param array $highlightData |
| 126 | * @param array $sourceData |
| 127 | * @param bool $useOffsets Is highlighter using offsets option? |
| 128 | * @return array [ $language, $text ] or [null, null] if nothing found |
| 129 | */ |
| 130 | private function getHighlightOrField( $field, $displayLanguage, |
| 131 | TermLanguageFallbackChain $displayFallbackChain, |
| 132 | $highlightData, $sourceData, $useOffsets = false |
| 133 | ) { |
| 134 | // Try highlights first, if we have needed language there, use highlighted data |
| 135 | if ( !empty( $highlightData["{$field}.{$displayLanguage}.plain"] ) ) { |
| 136 | $this->haveMatch = true; |
| 137 | return [ |
| 138 | 'language' => $displayLanguage, |
| 139 | 'value' => new HtmlArmor( $this->processHighlighting( $highlightData["{$field}.{$displayLanguage}.plain"][0], |
| 140 | $useOffsets ) ) |
| 141 | ]; |
| 142 | } |
| 143 | // If that failed, try source data |
| 144 | $source = $this->getSourceField( $field, $displayLanguage, $displayFallbackChain, $sourceData ); |
| 145 | // But if we actually have highlight for this one, use it! |
| 146 | if ( $source && !empty( $highlightData["{$field}.{$source['language']}.plain"] ) ) { |
| 147 | $this->haveMatch = true; |
| 148 | return [ |
| 149 | 'language' => $source['language'], |
| 150 | // @phan-suppress-next-line PhanTypeArraySuspiciousNullable |
| 151 | 'value' => new HtmlArmor( $this->processHighlighting( $highlightData["{$field}.{$source['language']}.plain"][0], |
| 152 | $useOffsets ) ) |
| 153 | ]; |
| 154 | } |
| 155 | return $source; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Get data from source fields, using fallback chain if necessary. |
| 160 | * @param string $field Field in source data where we're looking. |
| 161 | * The field will contain subfield by language names. |
| 162 | * @param string $displayLanguage |
| 163 | * @param TermLanguageFallbackChain $displayFallbackChain |
| 164 | * @param array $sourceData The source data as returned by Elastic. |
| 165 | * @return array |
| 166 | */ |
| 167 | private function getSourceField( $field, $displayLanguage, |
| 168 | TermLanguageFallbackChain $displayFallbackChain, |
| 169 | $sourceData |
| 170 | ) { |
| 171 | $term = EntitySearchUtils::findTermForDisplay( $sourceData, $field, $displayFallbackChain ); |
| 172 | if ( $term ) { |
| 173 | return [ 'language' => $term->getLanguageCode(), 'value' => $term->getText() ]; |
| 174 | } |
| 175 | // OK, we don't have much here |
| 176 | return [ 'language' => $displayLanguage, 'value' => '' ]; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Process highlighted string from search results. |
| 181 | * @param string $snippet |
| 182 | * @param bool $useOffsets Is highlighter using offsets option? |
| 183 | * @return string Highlighted and HTML-encoded string |
| 184 | */ |
| 185 | private function processHighlighting( $snippet, $useOffsets = false ) { |
| 186 | if ( $useOffsets && preg_match( ElasticTermResult::HIGHLIGHT_PATTERN, $snippet, $match ) ) { |
| 187 | $snippet = $match[1]; |
| 188 | } |
| 189 | return strtr( htmlspecialchars( $snippet ), [ |
| 190 | Searcher::HIGHLIGHT_PRE_MARKER => Searcher::HIGHLIGHT_PRE, |
| 191 | Searcher::HIGHLIGHT_POST_MARKER => Searcher::HIGHLIGHT_POST |
| 192 | ] ); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * @return string[] ['language' => LANG, 'value' => TEXT] |
| 197 | */ |
| 198 | public function getLabelData() { |
| 199 | return $this->labelData; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @return string[] ['language' => LANG, 'value' => TEXT] |
| 204 | */ |
| 205 | public function getDescriptionData() { |
| 206 | return $this->descriptionData; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * @return string[] ['language' => LANG, 'value' => TEXT] |
| 211 | */ |
| 212 | public function getLabelHighlightedData() { |
| 213 | return $this->labelHighlightedData; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * @return string[] ['language' => LANG, 'value' => TEXT] |
| 218 | */ |
| 219 | public function getDescriptionHighlightedData() { |
| 220 | return $this->descriptionHighlightedData; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * @return string |
| 225 | */ |
| 226 | public function getTitleSnippet() { |
| 227 | return HtmlArmor::getHtml( $this->labelHighlightedData['value'] ); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * @param array $terms |
| 232 | * @return string |
| 233 | */ |
| 234 | public function getTextSnippet( $terms = [] ) { |
| 235 | return HtmlArmor::getHtml( $this->descriptionHighlightedData['value'] ); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * @return string[] ['language' => LANG, 'value' => TEXT] |
| 240 | */ |
| 241 | public function getExtraDisplay() { |
| 242 | return $this->extraDisplay; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Get number of statements |
| 247 | * @return int |
| 248 | */ |
| 249 | public function getStatementCount() { |
| 250 | if ( !isset( $this->sourceData['statement_count'] ) ) { |
| 251 | return 0; |
| 252 | } |
| 253 | return (int)$this->sourceData['statement_count']; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Get number of sitelinks |
| 258 | * @return int |
| 259 | */ |
| 260 | public function getSitelinkCount() { |
| 261 | if ( !isset( $this->sourceData['sitelink_count'] ) ) { |
| 262 | return 0; |
| 263 | } |
| 264 | return (int)$this->sourceData['sitelink_count']; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Augment extension data with extraDisplay data. |
| 269 | * @return array[] |
| 270 | */ |
| 271 | public function getExtensionData() { |
| 272 | $data = parent::getExtensionData(); |
| 273 | if ( $this->extraDisplay ) { |
| 274 | $data[self::WIKIBASE_EXTRA_DATA] = [ |
| 275 | 'extrasnippet' => HtmlArmor::getHtml( $this->extraDisplay['value'] ), |
| 276 | 'extrasnippet-language' => $this->extraDisplay['language'], |
| 277 | ]; |
| 278 | } |
| 279 | return $data; |
| 280 | } |
| 281 | |
| 282 | } |