MediaWiki master
RenderedRevision.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Revision;
24
25use Content;
26use InvalidArgumentException;
27use LogicException;
34use Psr\Log\LoggerInterface;
35use Psr\Log\NullLogger;
36use Wikimedia\Assert\Assert;
37
46
48 private $revision;
49
53 private $options;
54
58 private $audience = RevisionRecord::FOR_PUBLIC;
59
63 private $performer = null;
64
69 private $revisionOutput = null;
70
75 private $slotsOutput = [];
76
81 private $combineOutput;
82
86 private $saveParseLogger;
87
91 private $contentRenderer;
92
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
138 public function setSaveParseLogger( LoggerInterface $saveParseLogger ) {
139 $this->saveParseLogger = $saveParseLogger;
140 }
141
145 public function isContentDeleted() {
146 return $this->revision->isDeleted( RevisionRecord::DELETED_TEXT );
147 }
148
152 public function getRevision() {
153 return $this->revision;
154 }
155
159 public function getOptions() {
160 return $this->options;
161 }
162
173 public function setRevisionParserOutput( ParserOutput $output ) {
174 $this->revisionOutput = $output;
175
176 // If there is only one slot, we assume that the combined output is identical
177 // with the main slot's output. This is intended to prevent a redundant re-parse of
178 // the content in case getSlotParserOutput( SlotRecord::MAIN ) is called, for instance
179 // from ContentHandler::getSecondaryDataUpdates.
180 if ( $this->revision->getSlotRoles() === [ SlotRecord::MAIN ] ) {
181 $this->slotsOutput[ SlotRecord::MAIN ] = $output;
182 }
183 }
184
193 public function getRevisionParserOutput( array $hints = [] ) {
194 $withHtml = $hints['generate-html'] ?? true;
195
196 if ( !$this->revisionOutput
197 || ( $withHtml && !$this->revisionOutput->hasText() )
198 ) {
199 $output = call_user_func( $this->combineOutput, $this, $hints );
200
201 Assert::postcondition(
202 $output instanceof ParserOutput,
203 'Callback did not return a ParserOutput object!'
204 );
205
206 $this->revisionOutput = $output;
207 }
208
209 return $this->revisionOutput;
210 }
211
223 public function getSlotParserOutput( $role, array $hints = [] ) {
224 $withHtml = $hints['generate-html'] ?? true;
225
226 if ( !isset( $this->slotsOutput[ $role ] )
227 || ( $withHtml && !$this->slotsOutput[ $role ]->hasText() )
228 ) {
229 $content = $this->revision->getContentOrThrow( $role, $this->audience, $this->performer );
230
231 // XXX: allow SlotRoleHandler to control the ParserOutput?
232 $output = $this->getSlotParserOutputUncached( $content, $withHtml );
233
234 if ( $withHtml && !$output->hasText() ) {
235 throw new LogicException(
236 'HTML generation was requested, but '
237 . get_class( $content )
238 . ' that passed to '
239 . 'ContentRenderer::getParserOutput() returns a ParserOutput with no text set.'
240 );
241 }
242
243 // Detach watcher, to ensure option use is not recorded in the wrong ParserOutput.
244 $this->options->registerWatcher( null );
245
246 $this->slotsOutput[ $role ] = $output;
247 }
248
249 return $this->slotsOutput[$role];
250 }
251
258 private function getSlotParserOutputUncached( Content $content, $withHtml ) {
259 return $this->contentRenderer->getParserOutput(
260 $content,
261 $this->revision->getPage(),
262 $this->revision,
263 $this->options,
264 $withHtml
265 );
266 }
267
277 public function updateRevision( RevisionRecord $rev ) {
278 if ( $rev->getId() === $this->revision->getId() ) {
279 return;
280 }
281
282 if ( $this->revision->getId() ) {
283 throw new LogicException( 'RenderedRevision already has a revision with ID '
284 . $this->revision->getId() . ', can\'t update to revision with ID ' . $rev->getId() );
285 }
286
287 if ( !$this->revision->getSlots()->hasSameContent( $rev->getSlots() ) ) {
288 throw new LogicException( 'Cannot update to a revision with different content!' );
289 }
290
291 $this->setRevisionInternal( $rev );
292
293 $this->pruneRevisionSensitiveOutput(
294 $this->revision->getPageId(),
295 $this->revision->getId(),
296 $this->revision->getTimestamp()
297 );
298 }
299
313 private function pruneRevisionSensitiveOutput(
314 $actualPageId,
315 $actualRevId,
316 $actualRevTimestamp
317 ) {
318 if ( $this->revisionOutput ) {
319 if ( $this->outputVariesOnRevisionMetaData(
320 $this->revisionOutput,
321 $actualPageId,
322 $actualRevId,
323 $actualRevTimestamp
324 ) ) {
325 $this->revisionOutput = null;
326 }
327 } else {
328 $this->saveParseLogger->debug( __METHOD__ . ": no prepared revision output" );
329 }
330
331 foreach ( $this->slotsOutput as $role => $output ) {
332 if ( $this->outputVariesOnRevisionMetaData(
333 $output,
334 $actualPageId,
335 $actualRevId,
336 $actualRevTimestamp
337 ) ) {
338 unset( $this->slotsOutput[$role] );
339 }
340 }
341 }
342
346 private function setRevisionInternal( RevisionRecord $revision ) {
347 $this->revision = $revision;
348
349 // Force the parser to use $this->revision to resolve magic words like {{REVISIONUSER}}
350 // if the revision is either known to be complete, or it doesn't have a revision ID set.
351 // If it's incomplete and we have a revision ID, the parser can do better by loading
352 // the revision from the database if needed to handle a magic word.
353 //
354 // The following considerations inform the logic described above:
355 //
356 // 1) If we have a saved revision already loaded, we want the parser to use it, instead of
357 // loading it again.
358 //
359 // 2) If the revision is a fake that wraps some kind of synthetic content, such as an
360 // error message from Article, it should be used directly and things like {{REVISIONUSER}}
361 // should not expected to work, since there may not even be an actual revision to
362 // refer to.
363 //
364 // 3) If the revision is a fake constructed around a page, a Content object, and
365 // a revision ID, to provide backwards compatibility to code that has access to those
366 // but not to a complete RevisionRecord for rendering, then we want the Parser to
367 // load the actual revision from the database when it encounters a magic word like
368 // {{REVISIONUSER}}, but we don't want to load that revision ahead of time just in case.
369 //
370 // 4) Previewing an edit to a template should use the submitted unsaved
371 // MutableRevisionRecord for self-transclusions in the template's documentation (see T7278).
372 // That revision would be complete except for the ID field.
373 //
374 // 5) Pre-save transform would provide a RevisionRecord that has all meta-data but is
375 // incomplete due to not yet having content set. However, since it doesn't have a revision
376 // ID either, the below code would still force it to be used, allowing
377 // {{subst::REVISIONUSER}} to function as expected.
378
379 if ( $this->revision->isReadyForInsertion() || !$this->revision->getId() ) {
380 $oldCallback = $this->options->getCurrentRevisionRecordCallback();
381 $this->options->setCurrentRevisionRecordCallback(
382 function ( PageReference $parserPage, $parser = null ) use ( $oldCallback ) {
383 if ( $this->revision->getPage()->isSamePageAs( $parserPage ) ) {
384 return $this->revision;
385 } else {
386 return call_user_func( $oldCallback, $parserPage, $parser );
387 }
388 }
389 );
390 }
391 }
392
406 private function outputVariesOnRevisionMetaData(
407 ParserOutput $parserOutput,
408 $actualPageId,
409 $actualRevId,
410 $actualRevTimestamp
411 ) {
412 $logger = $this->saveParseLogger;
413 $varyMsg = __METHOD__ . ": cannot use prepared output for '{title}'";
414 $context = [ 'title' => (string)$this->revision->getPage() ];
415
416 if ( $parserOutput->getOutputFlag( ParserOutputFlags::VARY_REVISION ) ) {
417 // If {{PAGEID}} resolved to 0, then that word need to resolve to the actual page ID
418 $logger->info( "$varyMsg (vary-revision)", $context );
419 return true;
420 } elseif (
421 $parserOutput->getOutputFlag( ParserOutputFlags::VARY_REVISION_ID )
422 && $actualRevId !== false
423 && ( $actualRevId === true || $parserOutput->getSpeculativeRevIdUsed() !== $actualRevId )
424 ) {
425 $logger->info( "$varyMsg (vary-revision-id and wrong ID)", $context );
426 return true;
427 } elseif (
428 $parserOutput->getOutputFlag( ParserOutputFlags::VARY_REVISION_TIMESTAMP )
429 && $actualRevTimestamp !== false
430 && ( $actualRevTimestamp === true ||
431 $parserOutput->getRevisionTimestampUsed() !== $actualRevTimestamp )
432 ) {
433 $logger->info( "$varyMsg (vary-revision-timestamp and wrong timestamp)", $context );
434 return true;
435 } elseif (
436 $parserOutput->getOutputFlag( ParserOutputFlags::VARY_PAGE_ID )
437 && $actualPageId !== false
438 && ( $actualPageId === true || $parserOutput->getSpeculativePageIdUsed() !== $actualPageId )
439 ) {
440 $logger->info( "$varyMsg (vary-page-id and wrong ID)", $context );
441 return true;
442 } elseif ( $parserOutput->getOutputFlag( ParserOutputFlags::VARY_REVISION_EXISTS ) ) {
443 // If {{REVISIONID}} resolved to '', it now needs to resolve to '-'.
444 // Note that edit stashing always uses '-', which can be used for both
445 // edit filter checks and canonical parser cache.
446 $logger->info( "$varyMsg (vary-revision-exists)", $context );
447 return true;
448 } elseif (
449 $parserOutput->getOutputFlag( ParserOutputFlags::VARY_REVISION_SHA1 ) &&
450 $parserOutput->getRevisionUsedSha1Base36() !== $this->revision->getSha1()
451 ) {
452 // If a self-transclusion used the proposed page text, it must match the final
453 // page content after PST transformations and automatically merged edit conflicts
454 $logger->info( "$varyMsg (vary-revision-sha1 with wrong SHA-1)", $context );
455 return true;
456 }
457
458 // NOTE: In the original fix for T135261, the output was discarded if ParserOutputFlags::VARY_USER was
459 // set for a null-edit. The reason was that the original rendering in that case was
460 // targeting the user making the null-edit, not the user who made the original edit,
461 // causing {{REVISIONUSER}} to return the wrong name.
462 // This case is now expected to be handled by the code in RevisionRenderer that
463 // constructs the ParserOptions: For a null-edit, setCurrentRevisionRecordCallback is
464 // called with the old, existing revision.
465 $logger->debug( __METHOD__ . ": reusing prepared output for '{title}'", $context );
466 return false;
467 }
468}
ParserOutput is a rendering of a Content object or a message.
RenderedRevision represents the rendered representation of a revision.
__construct(RevisionRecord $revision, ParserOptions $options, ContentRenderer $contentRenderer, callable $combineOutput, $audience=RevisionRecord::FOR_PUBLIC, Authority $performer=null)
setRevisionParserOutput(ParserOutput $output)
Sets a ParserOutput to be returned by getRevisionParserOutput().
updateRevision(RevisionRecord $rev)
Updates the RevisionRecord after the revision has been saved.
setSaveParseLogger(LoggerInterface $saveParseLogger)
getSlotParserOutput( $role, array $hints=[])
Page revision base class.
getSlots()
Returns the slots defined for this revision.
getId( $wikiId=self::LOCAL)
Get revision ID.
Set options of the Parser.
Base interface for representing page content.
Definition Content.php:37
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
This interface represents the authority associated with the current execution context,...
Definition Authority.php:37
A lazy provider of ParserOutput objects for a revision's individual slots.