Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
83.47% |
101 / 121 |
|
53.85% |
7 / 13 |
CRAP | |
0.00% |
0 / 1 |
RenderedRevision | |
83.47% |
101 / 121 |
|
53.85% |
7 / 13 |
61.29 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
setSaveParseLogger | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isContentDeleted | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getRevision | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getOptions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setRevisionParserOutput | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getRevisionParserOutput | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
getSlotParserOutput | |
60.00% |
9 / 15 |
|
0.00% |
0 / 1 |
8.30 | |||
getSlotParserOutputUncached | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
updateRevision | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
4.01 | |||
pruneRevisionSensitiveOutput | |
94.12% |
16 / 17 |
|
0.00% |
0 / 1 |
5.01 | |||
setRevisionInternal | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
4.02 | |||
outputVariesOnRevisionMetaData | |
67.74% |
21 / 31 |
|
0.00% |
0 / 1 |
26.70 |
1 | <?php |
2 | /** |
3 | * This file is part of MediaWiki. |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | * http://www.gnu.org/copyleft/gpl.html |
19 | * |
20 | * @file |
21 | */ |
22 | |
23 | namespace MediaWiki\Revision; |
24 | |
25 | use InvalidArgumentException; |
26 | use LogicException; |
27 | use MediaWiki\Content\Content; |
28 | use MediaWiki\Content\Renderer\ContentRenderer; |
29 | use MediaWiki\Page\PageReference; |
30 | use MediaWiki\Parser\ParserOptions; |
31 | use MediaWiki\Parser\ParserOutput; |
32 | use MediaWiki\Parser\ParserOutputFlags; |
33 | use MediaWiki\Permissions\Authority; |
34 | use Psr\Log\LoggerInterface; |
35 | use Psr\Log\NullLogger; |
36 | use Wikimedia\Assert\Assert; |
37 | |
38 | /** |
39 | * RenderedRevision represents the rendered representation of a revision. It acts as a lazy provider |
40 | * of ParserOutput objects for the revision's individual slots, as well as a combined ParserOutput |
41 | * of all slots. |
42 | * |
43 | * @since 1.32 |
44 | */ |
45 | class RenderedRevision implements SlotRenderingProvider { |
46 | |
47 | /** @var RevisionRecord */ |
48 | private $revision; |
49 | |
50 | /** |
51 | * @var ParserOptions |
52 | */ |
53 | private $options; |
54 | |
55 | /** |
56 | * @var int Audience to check when accessing content. |
57 | */ |
58 | private $audience = RevisionRecord::FOR_PUBLIC; |
59 | |
60 | /** |
61 | * @var Authority|null The user to use for audience checks during content access. |
62 | */ |
63 | private $performer = null; |
64 | |
65 | /** |
66 | * @var ParserOutput|null The combined ParserOutput for the revision, |
67 | * initialized lazily by getRevisionParserOutput(). |
68 | */ |
69 | private $revisionOutput = null; |
70 | |
71 | /** |
72 | * @var ParserOutput[] The ParserOutput for each slot, |
73 | * initialized lazily by getSlotParserOutput(). |
74 | */ |
75 | private $slotsOutput = []; |
76 | |
77 | /** |
78 | * @var callable Callback for combining slot output into revision output. |
79 | * Signature: function ( RenderedRevision $this, array $hints ): ParserOutput. |
80 | */ |
81 | private $combineOutput; |
82 | |
83 | /** |
84 | * @var LoggerInterface For profiling ParserOutput re-use. |
85 | */ |
86 | private $saveParseLogger; |
87 | |
88 | /** |
89 | * @var ContentRenderer Service to render content. |
90 | */ |
91 | private $contentRenderer; |
92 | |
93 | /** |
94 | * @note Application logic should not instantiate RenderedRevision instances directly, |
95 | * but should use a RevisionRenderer instead. |
96 | * |
97 | * @param RevisionRecord $revision The revision to render. The content for rendering will be |
98 | * taken from this RevisionRecord. However, if the RevisionRecord is not complete |
99 | * according isReadyForInsertion(), but a revision ID is known, the parser may load |
100 | * the revision from the database if it needs revision meta data to handle magic |
101 | * words like {{REVISIONUSER}}. |
102 | * @param ParserOptions $options |
103 | * @param ContentRenderer $contentRenderer |
104 | * @param callable $combineOutput Callback for combining slot output into revision output. |
105 | * Signature: function ( RenderedRevision $this, array $hints ): ParserOutput. |
106 | * @param int $audience Use RevisionRecord::FOR_PUBLIC, FOR_THIS_USER, or RAW. |
107 | * @param Authority|null $performer Required if $audience is FOR_THIS_USER. |
108 | */ |
109 | public function __construct( |
110 | RevisionRecord $revision, |
111 | ParserOptions $options, |
112 | ContentRenderer $contentRenderer, |
113 | callable $combineOutput, |
114 | $audience = RevisionRecord::FOR_PUBLIC, |
115 | ?Authority $performer = null |
116 | ) { |
117 | $this->options = $options; |
118 | |
119 | $this->setRevisionInternal( $revision ); |
120 | |
121 | $this->contentRenderer = $contentRenderer; |
122 | $this->combineOutput = $combineOutput; |
123 | $this->saveParseLogger = new NullLogger(); |
124 | |
125 | if ( $audience === RevisionRecord::FOR_THIS_USER && !$performer ) { |
126 | throw new InvalidArgumentException( |
127 | 'User must be specified when setting audience to FOR_THIS_USER' |
128 | ); |
129 | } |
130 | |
131 | $this->audience = $audience; |
132 | $this->performer = $performer; |
133 | } |
134 | |
135 | public function setSaveParseLogger( LoggerInterface $saveParseLogger ) { |
136 | $this->saveParseLogger = $saveParseLogger; |
137 | } |
138 | |
139 | /** |
140 | * @return bool Whether the revision's content has been hidden from unprivileged users. |
141 | */ |
142 | public function isContentDeleted() { |
143 | return $this->revision->isDeleted( RevisionRecord::DELETED_TEXT ); |
144 | } |
145 | |
146 | /** |
147 | * @return RevisionRecord |
148 | */ |
149 | public function getRevision() { |
150 | return $this->revision; |
151 | } |
152 | |
153 | /** |
154 | * @return ParserOptions |
155 | */ |
156 | public function getOptions() { |
157 | return $this->options; |
158 | } |
159 | |
160 | /** |
161 | * Sets a ParserOutput to be returned by getRevisionParserOutput(). |
162 | * |
163 | * @note For internal use by RevisionRenderer only! This method may be modified |
164 | * or removed without notice per the deprecation policy. |
165 | * |
166 | * @internal |
167 | * |
168 | * @param ParserOutput $output |
169 | */ |
170 | public function setRevisionParserOutput( ParserOutput $output ) { |
171 | $this->revisionOutput = $output; |
172 | |
173 | // If there is only one slot, we assume that the combined output is identical |
174 | // with the main slot's output. This is intended to prevent a redundant re-parse of |
175 | // the content in case getSlotParserOutput( SlotRecord::MAIN ) is called, for instance |
176 | // from ContentHandler::getSecondaryDataUpdates. |
177 | if ( $this->revision->getSlotRoles() === [ SlotRecord::MAIN ] ) { |
178 | $this->slotsOutput[ SlotRecord::MAIN ] = $output; |
179 | } |
180 | } |
181 | |
182 | /** |
183 | * @param array $hints Hints given as an associative array. Known keys: |
184 | * - 'generate-html' => bool: Whether the caller is interested in output HTML (as opposed |
185 | * to just meta-data). Default is to generate HTML. |
186 | * @phan-param array{generate-html?:bool} $hints |
187 | * |
188 | * @return ParserOutput |
189 | */ |
190 | public function getRevisionParserOutput( array $hints = [] ) { |
191 | $withHtml = $hints['generate-html'] ?? true; |
192 | |
193 | if ( !$this->revisionOutput |
194 | || ( $withHtml && !$this->revisionOutput->hasText() ) |
195 | ) { |
196 | $output = ( $this->combineOutput )( $this, $hints ); |
197 | |
198 | Assert::postcondition( |
199 | $output instanceof ParserOutput, |
200 | 'Callback did not return a ParserOutput object!' |
201 | ); |
202 | |
203 | $this->revisionOutput = $output; |
204 | } |
205 | |
206 | return $this->revisionOutput; |
207 | } |
208 | |
209 | /** |
210 | * @param string $role |
211 | * @param array $hints Hints given as an associative array. Known keys: |
212 | * - 'generate-html' => bool: Whether the caller is interested in output HTML (as opposed |
213 | * to just meta-data). Default is to generate HTML. |
214 | * - 'previous-output' => ?ParserOutput: An optional "previously parsed" |
215 | * version of this slot; used to allow Parsoid selective updates. |
216 | * @phan-param array{generate-html?:bool,previous-output?:?ParserOutput} $hints |
217 | * |
218 | * @throws SuppressedDataException if the content is not accessible for the audience |
219 | * specified in the constructor. |
220 | * @throws BadRevisionException |
221 | * @throws RevisionAccessException |
222 | * @return ParserOutput |
223 | */ |
224 | public function getSlotParserOutput( $role, array $hints = [] ) { |
225 | $withHtml = $hints['generate-html'] ?? true; |
226 | |
227 | if ( !isset( $this->slotsOutput[ $role ] ) |
228 | || ( $withHtml && !$this->slotsOutput[ $role ]->hasText() ) |
229 | ) { |
230 | $content = $this->revision->getContentOrThrow( $role, $this->audience, $this->performer ); |
231 | |
232 | // XXX: allow SlotRoleHandler to control the ParserOutput? |
233 | $output = $this->getSlotParserOutputUncached( $content, $hints ); |
234 | |
235 | if ( $withHtml && !$output->hasText() ) { |
236 | throw new LogicException( |
237 | 'HTML generation was requested, but ' |
238 | . get_class( $content ) |
239 | . ' that passed to ' |
240 | . 'ContentRenderer::getParserOutput() returns a ParserOutput with no text set.' |
241 | ); |
242 | } |
243 | |
244 | // Detach watcher, to ensure option use is not recorded in the wrong ParserOutput. |
245 | $this->options->registerWatcher( null ); |
246 | |
247 | $this->slotsOutput[ $role ] = $output; |
248 | } |
249 | |
250 | return $this->slotsOutput[$role]; |
251 | } |
252 | |
253 | /** |
254 | * @note This method exists to make duplicate parses easier to see during profiling |
255 | * @param Content $content |
256 | * @param array{generate-html?:bool,previous-output?:?ParserOutput} $hints |
257 | * @return ParserOutput |
258 | */ |
259 | private function getSlotParserOutputUncached( Content $content, array $hints ): ParserOutput { |
260 | return $this->contentRenderer->getParserOutput( |
261 | $content, |
262 | $this->revision->getPage(), |
263 | $this->revision, |
264 | $this->options, |
265 | $hints |
266 | ); |
267 | } |
268 | |
269 | /** |
270 | * Updates the RevisionRecord after the revision has been saved. This can be used to discard |
271 | * and cached ParserOutput so parser functions like {{REVISIONTIMESTAMP}} or {{REVISIONID}} |
272 | * are re-evaluated. |
273 | * |
274 | * @note There should be no need to call this for null-edits. |
275 | * |
276 | * @param RevisionRecord $rev |
277 | */ |
278 | public function updateRevision( RevisionRecord $rev ) { |
279 | if ( $rev->getId() === $this->revision->getId() ) { |
280 | return; |
281 | } |
282 | |
283 | if ( $this->revision->getId() ) { |
284 | throw new LogicException( 'RenderedRevision already has a revision with ID ' |
285 | . $this->revision->getId() . ', can\'t update to revision with ID ' . $rev->getId() ); |
286 | } |
287 | |
288 | if ( !$this->revision->getSlots()->hasSameContent( $rev->getSlots() ) ) { |
289 | throw new LogicException( 'Cannot update to a revision with different content!' ); |
290 | } |
291 | |
292 | $this->setRevisionInternal( $rev ); |
293 | |
294 | $this->pruneRevisionSensitiveOutput( |
295 | $this->revision->getPageId(), |
296 | $this->revision->getId(), |
297 | $this->revision->getTimestamp() |
298 | ); |
299 | } |
300 | |
301 | /** |
302 | * Prune any output that depends on the revision ID. |
303 | * |
304 | * @param int|bool $actualPageId The actual page id, to check the used speculative page ID |
305 | * against; false, to not purge on vary-page-id; true, to purge on vary-page-id |
306 | * unconditionally. |
307 | * @param int|bool $actualRevId The actual rev id, to check the used speculative rev ID |
308 | * against,; false, to not purge on vary-revision-id; true, to purge on |
309 | * vary-revision-id unconditionally. |
310 | * @param string|bool $actualRevTimestamp The actual rev timestamp, to check against the |
311 | * parser output revision timestamp; false, to not purge on vary-revision-timestamp; |
312 | * true, to purge on vary-revision-timestamp unconditionally. |
313 | */ |
314 | private function pruneRevisionSensitiveOutput( |
315 | $actualPageId, |
316 | $actualRevId, |
317 | $actualRevTimestamp |
318 | ) { |
319 | if ( $this->revisionOutput ) { |
320 | if ( $this->outputVariesOnRevisionMetaData( |
321 | $this->revisionOutput, |
322 | $actualPageId, |
323 | $actualRevId, |
324 | $actualRevTimestamp |
325 | ) ) { |
326 | $this->revisionOutput = null; |
327 | } |
328 | } else { |
329 | $this->saveParseLogger->debug( __METHOD__ . ": no prepared revision output" ); |
330 | } |
331 | |
332 | foreach ( $this->slotsOutput as $role => $output ) { |
333 | if ( $this->outputVariesOnRevisionMetaData( |
334 | $output, |
335 | $actualPageId, |
336 | $actualRevId, |
337 | $actualRevTimestamp |
338 | ) ) { |
339 | unset( $this->slotsOutput[$role] ); |
340 | } |
341 | } |
342 | } |
343 | |
344 | private function setRevisionInternal( RevisionRecord $revision ) { |
345 | $this->revision = $revision; |
346 | |
347 | // Force the parser to use $this->revision to resolve magic words like {{REVISIONUSER}} |
348 | // if the revision is either known to be complete, or it doesn't have a revision ID set. |
349 | // If it's incomplete and we have a revision ID, the parser can do better by loading |
350 | // the revision from the database if needed to handle a magic word. |
351 | // |
352 | // The following considerations inform the logic described above: |
353 | // |
354 | // 1) If we have a saved revision already loaded, we want the parser to use it, instead of |
355 | // loading it again. |
356 | // |
357 | // 2) If the revision is a fake that wraps some kind of synthetic content, such as an |
358 | // error message from Article, it should be used directly and things like {{REVISIONUSER}} |
359 | // should not expected to work, since there may not even be an actual revision to |
360 | // refer to. |
361 | // |
362 | // 3) If the revision is a fake constructed around a page, a Content object, and |
363 | // a revision ID, to provide backwards compatibility to code that has access to those |
364 | // but not to a complete RevisionRecord for rendering, then we want the Parser to |
365 | // load the actual revision from the database when it encounters a magic word like |
366 | // {{REVISIONUSER}}, but we don't want to load that revision ahead of time just in case. |
367 | // |
368 | // 4) Previewing an edit to a template should use the submitted unsaved |
369 | // MutableRevisionRecord for self-transclusions in the template's documentation (see T7278). |
370 | // That revision would be complete except for the ID field. |
371 | // |
372 | // 5) Pre-save transform would provide a RevisionRecord that has all meta-data but is |
373 | // incomplete due to not yet having content set. However, since it doesn't have a revision |
374 | // ID either, the below code would still force it to be used, allowing |
375 | // {{subst::REVISIONUSER}} to function as expected. |
376 | |
377 | if ( $this->revision->isReadyForInsertion() || !$this->revision->getId() ) { |
378 | $oldCallback = $this->options->getCurrentRevisionRecordCallback(); |
379 | $this->options->setCurrentRevisionRecordCallback( |
380 | function ( PageReference $parserPage, $parser = null ) use ( $oldCallback ) { |
381 | if ( $this->revision->getPage()->isSamePageAs( $parserPage ) ) { |
382 | return $this->revision; |
383 | } else { |
384 | return $oldCallback( $parserPage, $parser ); |
385 | } |
386 | } |
387 | ); |
388 | } |
389 | } |
390 | |
391 | /** |
392 | * @param ParserOutput $parserOutput |
393 | * @param int|bool $actualPageId The actual page id, to check the used speculative page ID |
394 | * against; false, to not purge on vary-page-id; true, to purge on vary-page-id |
395 | * unconditionally. |
396 | * @param int|bool $actualRevId The actual rev id, to check the used speculative rev ID |
397 | * against,; false, to not purge on vary-revision-id; true, to purge on |
398 | * vary-revision-id unconditionally. |
399 | * @param string|bool $actualRevTimestamp The actual rev timestamp, to check against the |
400 | * parser output revision timestamp; false, to not purge on vary-revision-timestamp; |
401 | * true, to purge on vary-revision-timestamp unconditionally. |
402 | * @return bool |
403 | */ |
404 | private function outputVariesOnRevisionMetaData( |
405 | ParserOutput $parserOutput, |
406 | $actualPageId, |
407 | $actualRevId, |
408 | $actualRevTimestamp |
409 | ) { |
410 | $logger = $this->saveParseLogger; |
411 | $varyMsg = __METHOD__ . ": cannot use prepared output for '{title}'"; |
412 | $context = [ 'title' => (string)$this->revision->getPage() ]; |
413 | |
414 | if ( $parserOutput->getOutputFlag( ParserOutputFlags::VARY_REVISION ) ) { |
415 | // If {{PAGEID}} resolved to 0, then that word need to resolve to the actual page ID |
416 | $logger->info( "$varyMsg (vary-revision)", $context ); |
417 | return true; |
418 | } elseif ( |
419 | $parserOutput->getOutputFlag( ParserOutputFlags::VARY_REVISION_ID ) |
420 | && $actualRevId !== false |
421 | && ( $actualRevId === true || $parserOutput->getSpeculativeRevIdUsed() !== $actualRevId ) |
422 | ) { |
423 | $logger->info( "$varyMsg (vary-revision-id and wrong ID)", $context ); |
424 | return true; |
425 | } elseif ( |
426 | $parserOutput->getOutputFlag( ParserOutputFlags::VARY_REVISION_TIMESTAMP ) |
427 | && $actualRevTimestamp !== false |
428 | && ( $actualRevTimestamp === true || |
429 | $parserOutput->getRevisionTimestampUsed() !== $actualRevTimestamp ) |
430 | ) { |
431 | $logger->info( "$varyMsg (vary-revision-timestamp and wrong timestamp)", $context ); |
432 | return true; |
433 | } elseif ( |
434 | $parserOutput->getOutputFlag( ParserOutputFlags::VARY_PAGE_ID ) |
435 | && $actualPageId !== false |
436 | && ( $actualPageId === true || $parserOutput->getSpeculativePageIdUsed() !== $actualPageId ) |
437 | ) { |
438 | $logger->info( "$varyMsg (vary-page-id and wrong ID)", $context ); |
439 | return true; |
440 | } elseif ( $parserOutput->getOutputFlag( ParserOutputFlags::VARY_REVISION_EXISTS ) ) { |
441 | // If {{REVISIONID}} resolved to '', it now needs to resolve to '-'. |
442 | // Note that edit stashing always uses '-', which can be used for both |
443 | // edit filter checks and canonical parser cache. |
444 | $logger->info( "$varyMsg (vary-revision-exists)", $context ); |
445 | return true; |
446 | } elseif ( |
447 | $parserOutput->getOutputFlag( ParserOutputFlags::VARY_REVISION_SHA1 ) && |
448 | $parserOutput->getRevisionUsedSha1Base36() !== $this->revision->getSha1() |
449 | ) { |
450 | // If a self-transclusion used the proposed page text, it must match the final |
451 | // page content after PST transformations and automatically merged edit conflicts |
452 | $logger->info( "$varyMsg (vary-revision-sha1 with wrong SHA-1)", $context ); |
453 | return true; |
454 | } |
455 | |
456 | // NOTE: In the original fix for T135261, the output was discarded if ParserOutputFlags::VARY_USER was |
457 | // set for a null-edit. The reason was that the original rendering in that case was |
458 | // targeting the user making the null-edit, not the user who made the original edit, |
459 | // causing {{REVISIONUSER}} to return the wrong name. |
460 | // This case is now expected to be handled by the code in RevisionRenderer that |
461 | // constructs the ParserOptions: For a null-edit, setCurrentRevisionRecordCallback is |
462 | // called with the old, existing revision. |
463 | $logger->debug( __METHOD__ . ": reusing prepared output for '{title}'", $context ); |
464 | return false; |
465 | } |
466 | } |