Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
91.78% |
67 / 73 |
|
60.00% |
6 / 10 |
CRAP | |
0.00% |
0 / 1 |
ParserOutputPageProperties | |
91.78% |
67 / 73 |
|
60.00% |
6 / 10 |
28.44 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
initialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
finishInitializeBatch | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
finalize | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
finalizeReal | |
92.86% |
26 / 28 |
|
0.00% |
0 / 1 |
4.01 | |||
extractDisplayTitle | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
10 | |||
isSameString | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
fixAndFlagInvalidUTF8InSource | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
truncateFileTextContent | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
4 | |||
truncateFileContent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace CirrusSearch\BuildDocument; |
4 | |
5 | use CirrusSearch\CirrusSearch; |
6 | use CirrusSearch\Search\CirrusIndexField; |
7 | use CirrusSearch\SearchConfig; |
8 | use Elastica\Document; |
9 | use MediaWiki\Logger\LoggerFactory; |
10 | use MediaWiki\MediaWikiServices; |
11 | use MediaWiki\Revision\RevisionRecord; |
12 | use MediaWiki\Title\Title; |
13 | use ParserOutput; |
14 | use Sanitizer; |
15 | use Wikimedia\LightweightObjectStore\ExpirationAwareness; |
16 | use WikiPage; |
17 | |
18 | /** |
19 | * Extract searchable properties from the MediaWiki ParserOutput |
20 | */ |
21 | class ParserOutputPageProperties implements PagePropertyBuilder { |
22 | /** @var SearchConfig */ |
23 | private $config; |
24 | |
25 | /** |
26 | * @param SearchConfig $config |
27 | */ |
28 | public function __construct( SearchConfig $config ) { |
29 | $this->config = $config; |
30 | } |
31 | |
32 | /** |
33 | * {@inheritDoc} |
34 | */ |
35 | public function initialize( Document $doc, WikiPage $page, RevisionRecord $revision ): void { |
36 | // NOOP |
37 | } |
38 | |
39 | /** |
40 | * {@inheritDoc} |
41 | */ |
42 | public function finishInitializeBatch(): void { |
43 | // NOOP |
44 | } |
45 | |
46 | /** |
47 | * {@inheritDoc} |
48 | */ |
49 | public function finalize( Document $doc, Title $title, RevisionRecord $revision ): void { |
50 | $page = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $title ); |
51 | $this->finalizeReal( $doc, $page, new CirrusSearch, $revision ); |
52 | } |
53 | |
54 | /** |
55 | * Visible for testing. Much simpler to test with all objects resolved. |
56 | * |
57 | * @param Document $doc Document to finalize |
58 | * @param WikiPage $page WikiPage to scope operation to |
59 | * @param CirrusSearch $engine SearchEngine implementation |
60 | * @param RevisionRecord $revision The page revision to use |
61 | * @throws BuildDocumentException |
62 | */ |
63 | public function finalizeReal( |
64 | Document $doc, |
65 | WikiPage $page, |
66 | CirrusSearch $engine, |
67 | RevisionRecord $revision |
68 | ): void { |
69 | $wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache(); |
70 | $cacheKey = $wanCache->makeKey( |
71 | 'CirrusSearchParserOutputPageProperties', |
72 | $page->getId(), |
73 | $revision->getId(), |
74 | $page->getTouched(), |
75 | 'v2' |
76 | ); |
77 | |
78 | $fieldContent = $wanCache->getWithSetCallback( |
79 | $cacheKey, |
80 | ExpirationAwareness::TTL_HOUR * 6, |
81 | function () use ( $page, $revision, $engine ) { |
82 | $contentHandler = $page->getContentHandler(); |
83 | // TODO: Should see if we can change content handler api to avoid |
84 | // the WikiPage god object, but currently parser cache is still |
85 | // tied to WikiPage as well. |
86 | $output = $contentHandler->getParserOutputForIndexing( $page, null, $revision ); |
87 | |
88 | if ( !$output ) { |
89 | throw new BuildDocumentException( "ParserOutput cannot be obtained." ); |
90 | } |
91 | |
92 | $fieldContent = $contentHandler->getDataForSearchIndex( $page, $output, $engine, $revision ); |
93 | $fieldContent['display_title'] = self::extractDisplayTitle( $page->getTitle(), $output ); |
94 | return self::fixAndFlagInvalidUTF8InSource( $fieldContent, $page->getId() ); |
95 | } |
96 | ); |
97 | $fieldContent = $this->truncateFileContent( $fieldContent ); |
98 | $fieldDefinitions = $engine->getSearchIndexFields(); |
99 | foreach ( $fieldContent as $field => $fieldData ) { |
100 | $doc->set( $field, $fieldData ); |
101 | if ( isset( $fieldDefinitions[$field] ) ) { |
102 | $hints = $fieldDefinitions[$field]->getEngineHints( $engine ); |
103 | CirrusIndexField::addIndexingHints( $doc, $field, $hints ); |
104 | } |
105 | } |
106 | } |
107 | |
108 | /** |
109 | * @param Title $title |
110 | * @param ParserOutput $output |
111 | * @return string|null |
112 | */ |
113 | private static function extractDisplayTitle( Title $title, ParserOutput $output ): ?string { |
114 | $titleText = $title->getText(); |
115 | $titlePrefixedText = $title->getPrefixedText(); |
116 | |
117 | $raw = $output->getDisplayTitle(); |
118 | if ( $raw === false ) { |
119 | return null; |
120 | } |
121 | $clean = Sanitizer::stripAllTags( $raw ); |
122 | // Only index display titles that differ from the normal title |
123 | if ( self::isSameString( $clean, $titleText ) || |
124 | self::isSameString( $clean, $titlePrefixedText ) |
125 | ) { |
126 | return null; |
127 | } |
128 | if ( $title->getNamespace() === 0 || strpos( $clean, ':' ) === false ) { |
129 | return $clean; |
130 | } |
131 | // There is no official way that namespaces work in display title, it |
132 | // is an arbitrary string. Even so some use cases, such as the |
133 | // Translate extension, will translate the namespace as well. Here |
134 | // `Help:foo` will have a display title of `Aide:bar`. If we were to |
135 | // simply index as is the autocomplete and near matcher would see |
136 | // Help:Aide:bar, which doesn't seem particularly useful. |
137 | // The strategy here is to see if the portion before the : is a valid namespace |
138 | // in either the language of the wiki or the language of the page. If it is |
139 | // then we strip it from the display title. |
140 | [ $maybeNs, $maybeDisplayTitle ] = explode( ':', $clean, 2 ); |
141 | $cleanTitle = Title::newFromText( $clean ); |
142 | if ( $cleanTitle === null ) { |
143 | // The title is invalid, we cannot extract the ns prefix |
144 | return $clean; |
145 | } |
146 | if ( $cleanTitle->getNamespace() == $title->getNamespace() ) { |
147 | // While it doesn't really matter, $cleanTitle->getText() may |
148 | // have had ucfirst() applied depending on settings so we |
149 | // return the unmodified $maybeDisplayTitle. |
150 | return $maybeDisplayTitle; |
151 | } |
152 | |
153 | $docLang = $title->getPageLanguage(); |
154 | $nsIndex = $docLang->getNsIndex( $maybeNs ); |
155 | if ( $nsIndex !== $title->getNamespace() ) { |
156 | // Valid namespace but not the same as the actual page. |
157 | // Keep the namespace in the display title. |
158 | return $clean; |
159 | } |
160 | |
161 | return self::isSameString( $maybeDisplayTitle, $titleText ) |
162 | ? null |
163 | : $maybeDisplayTitle; |
164 | } |
165 | |
166 | private static function isSameString( string $a, string $b ): bool { |
167 | $a = mb_strtolower( strtr( $a, '_', ' ' ) ); |
168 | $b = mb_strtolower( strtr( $b, '_', ' ' ) ); |
169 | return $a === $b; |
170 | } |
171 | |
172 | /** |
173 | * Find invalid UTF-8 sequence in the source text. |
174 | * Fix them and flag the doc with the CirrusSearchInvalidUTF8 template. |
175 | * |
176 | * Temporary solution to help investigate/fix T225200 |
177 | * |
178 | * Visible for testing only |
179 | * @param array $fieldDefinitions |
180 | * @param int $pageId |
181 | * @return array |
182 | */ |
183 | public static function fixAndFlagInvalidUTF8InSource( array $fieldDefinitions, int $pageId ): array { |
184 | if ( isset( $fieldDefinitions['source_text'] ) ) { |
185 | $fixedVersion = mb_convert_encoding( $fieldDefinitions['source_text'], 'UTF-8', 'UTF-8' ); |
186 | if ( $fixedVersion !== $fieldDefinitions['source_text'] ) { |
187 | LoggerFactory::getInstance( 'CirrusSearch' ) |
188 | ->warning( 'Fixing invalid UTF-8 sequences in source text for page id {page_id}', |
189 | [ 'page_id' => $pageId ] ); |
190 | $fieldDefinitions['source_text'] = $fixedVersion; |
191 | $fieldDefinitions['template'][] = Title::makeTitle( NS_TEMPLATE, 'CirrusSearchInvalidUTF8' )->getPrefixedText(); |
192 | } |
193 | } |
194 | return $fieldDefinitions; |
195 | } |
196 | |
197 | /** |
198 | * Visible for testing only |
199 | * @param int $maxLen |
200 | * @param array $fieldContent |
201 | * @return array |
202 | */ |
203 | public static function truncateFileTextContent( int $maxLen, array $fieldContent ): array { |
204 | if ( $maxLen >= 0 && isset( $fieldContent['file_text'] ) && strlen( $fieldContent['file_text'] ) > $maxLen ) { |
205 | $fieldContent['file_text'] = mb_strcut( $fieldContent['file_text'], 0, $maxLen ); |
206 | } |
207 | |
208 | return $fieldContent; |
209 | } |
210 | |
211 | /** |
212 | * @param array $fieldContent |
213 | * @return array |
214 | */ |
215 | private function truncateFileContent( array $fieldContent ): array { |
216 | return self::truncateFileTextContent( $this->config->get( 'CirrusSearchMaxFileTextLength' ) ?: -1, $fieldContent ); |
217 | } |
218 | } |