MediaWiki  1.33.0
RenderedRevision.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\Revision;
24 
25 use InvalidArgumentException;
26 use LogicException;
29 use Psr\Log\LoggerInterface;
30 use Psr\Log\NullLogger;
32 use Title;
33 use User;
34 use Content;
35 use Wikimedia\Assert\Assert;
36 
45 
49  private $title;
50 
52  private $revision;
53 
57  private $options;
58 
63 
67  private $forUser = null;
68 
73  private $revisionOutput = null;
74 
79  private $slotsOutput = [];
80 
85  private $combineOutput;
86 
91 
108  public function __construct(
109  Title $title,
112  callable $combineOutput,
114  User $forUser = null
115  ) {
116  $this->title = $title;
117  $this->options = $options;
118 
119  $this->setRevisionInternal( $revision );
120 
121  $this->combineOutput = $combineOutput;
122  $this->saveParseLogger = new NullLogger();
123 
125  throw new InvalidArgumentException(
126  'User must be specified when setting audience to FOR_THIS_USER'
127  );
128  }
129 
130  $this->audience = $audience;
131  $this->forUser = $forUser;
132  }
133 
137  public function setSaveParseLogger( LoggerInterface $saveParseLogger ) {
138  $this->saveParseLogger = $saveParseLogger;
139  }
140 
144  public function isContentDeleted() {
145  return $this->revision->isDeleted( RevisionRecord::DELETED_TEXT );
146  }
147 
151  public function getRevision() {
152  return $this->revision;
153  }
154 
158  public function getOptions() {
159  return $this->options;
160  }
161 
173  $this->revisionOutput = $output;
174 
175  // If there is only one slot, we assume that the combined output is identical
176  // with the main slot's output. This is intended to prevent a redundant re-parse of
177  // the content in case getSlotParserOutput( SlotRecord::MAIN ) is called, for instance
178  // from ContentHandler::getSecondaryDataUpdates.
179  if ( $this->revision->getSlotRoles() === [ SlotRecord::MAIN ] ) {
180  $this->slotsOutput[ SlotRecord::MAIN ] = $output;
181  }
182  }
183 
191  public function getRevisionParserOutput( array $hints = [] ) {
192  $withHtml = $hints['generate-html'] ?? true;
193 
194  if ( !$this->revisionOutput
195  || ( $withHtml && !$this->revisionOutput->hasText() )
196  ) {
197  $output = call_user_func( $this->combineOutput, $this, $hints );
198 
199  Assert::postcondition(
200  $output instanceof ParserOutput,
201  'Callback did not return a ParserOutput object!'
202  );
203 
204  $this->revisionOutput = $output;
205  }
206 
207  return $this->revisionOutput;
208  }
209 
220  public function getSlotParserOutput( $role, array $hints = [] ) {
221  $withHtml = $hints['generate-html'] ?? true;
222 
223  if ( !isset( $this->slotsOutput[ $role ] )
224  || ( $withHtml && !$this->slotsOutput[ $role ]->hasText() )
225  ) {
226  $content = $this->revision->getContent( $role, $this->audience, $this->forUser );
227 
228  if ( !$content ) {
229  throw new SuppressedDataException(
230  'Access to the content has been suppressed for this audience'
231  );
232  } else {
233  // XXX: allow SlotRoleHandler to control the ParserOutput?
234  $output = $this->getSlotParserOutputUncached( $content, $withHtml );
235 
236  if ( $withHtml && !$output->hasText() ) {
237  throw new LogicException(
238  'HTML generation was requested, but '
239  . get_class( $content )
240  . '::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 
248  $this->slotsOutput[ $role ] = $output;
249  }
250 
251  return $this->slotsOutput[$role];
252  }
253 
260  private function getSlotParserOutputUncached( Content $content, $withHtml ) {
261  return $content->getParserOutput(
262  $this->title,
263  $this->revision->getId(),
265  $withHtml
266  );
267  }
268 
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( $this->revision->getId() );
295  }
296 
304  private function pruneRevisionSensitiveOutput( $actualRevId ) {
305  if ( $this->revisionOutput ) {
306  if ( $this->outputVariesOnRevisionMetaData( $this->revisionOutput, $actualRevId ) ) {
307  $this->revisionOutput = null;
308  }
309  } else {
310  $this->saveParseLogger->debug( __METHOD__ . ": no prepared revision output...\n" );
311  }
312 
313  foreach ( $this->slotsOutput as $role => $output ) {
314  if ( $this->outputVariesOnRevisionMetaData( $output, $actualRevId ) ) {
315  unset( $this->slotsOutput[$role] );
316  }
317  }
318  }
319 
324  $this->revision = $revision;
325 
326  // Force the parser to use $this->revision to resolve magic words like {{REVISIONUSER}}
327  // if the revision is either known to be complete, or it doesn't have a revision ID set.
328  // If it's incomplete and we have a revision ID, the parser can do better by loading
329  // the revision from the database if needed to handle a magic word.
330  //
331  // The following considerations inform the logic described above:
332  //
333  // 1) If we have a saved revision already loaded, we want the parser to use it, instead of
334  // loading it again.
335  //
336  // 2) If the revision is a fake that wraps some kind of synthetic content, such as an
337  // error message from Article, it should be used directly and things like {{REVISIONUSER}}
338  // should not expected to work, since there may not even be an actual revision to
339  // refer to.
340  //
341  // 3) If the revision is a fake constructed around a Title, a Content object, and
342  // a revision ID, to provide backwards compatibility to code that has access to those
343  // but not to a complete RevisionRecord for rendering, then we want the Parser to
344  // load the actual revision from the database when it encounters a magic word like
345  // {{REVISIONUSER}}, but we don't want to load that revision ahead of time just in case.
346  //
347  // 4) Previewing an edit to a template should use the submitted unsaved
348  // MutableRevisionRecord for self-transclusions in the template's documentation (see T7278).
349  // That revision would be complete except for the ID field.
350  //
351  // 5) Pre-save transform would provide a RevisionRecord that has all meta-data but is
352  // incomplete due to not yet having content set. However, since it doesn't have a revision
353  // ID either, the below code would still force it to be used, allowing
354  // {{subst::REVISIONUSER}} to function as expected.
355 
356  if ( $this->revision->isReadyForInsertion() || !$this->revision->getId() ) {
358  $oldCallback = $this->options->getCurrentRevisionCallback();
359  $this->options->setCurrentRevisionCallback(
360  function ( Title $parserTitle, $parser = false ) use ( $title, $oldCallback ) {
361  if ( $title->equals( $parserTitle ) ) {
362  $legacyRevision = new Revision( $this->revision );
363  return $legacyRevision;
364  } else {
365  return call_user_func( $oldCallback, $parserTitle, $parser );
366  }
367  }
368  );
369  }
370  }
371 
379  private function outputVariesOnRevisionMetaData( ParserOutput $out, $actualRevId ) {
380  $method = __METHOD__;
381 
382  if ( $out->getFlag( 'vary-revision' ) ) {
383  // XXX: Would be just keep the output if the speculative revision ID was correct,
384  // but that can go wrong for some edge cases, like {{PAGEID}} during page creation.
385  // For that specific case, it would perhaps nice to have a vary-page flag.
386  $this->saveParseLogger->info(
387  "$method: Prepared output has vary-revision...\n"
388  );
389  return true;
390  } elseif ( $out->getFlag( 'vary-revision-id' )
391  && $actualRevId !== false
392  && ( $actualRevId === true || $out->getSpeculativeRevIdUsed() !== $actualRevId )
393  ) {
394  $this->saveParseLogger->info(
395  "$method: Prepared output has vary-revision-id with wrong ID...\n"
396  );
397  return true;
398  } else {
399  // NOTE: In the original fix for T135261, the output was discarded if 'vary-user' was
400  // set for a null-edit. The reason was that the original rendering in that case was
401  // targeting the user making the null-edit, not the user who made the original edit,
402  // causing {{REVISIONUSER}} to return the wrong name.
403  // This case is now expected to be handled by the code in RevisionRenderer that
404  // constructs the ParserOptions: For a null-edit, setCurrentRevisionCallback is called
405  // with the old, existing revision.
406 
407  wfDebug( "$method: Keeping prepared output...\n" );
408  return false;
409  }
410  }
411 
412 }
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:42
Revision\RenderedRevision\getRevision
getRevision()
Definition: RenderedRevision.php:151
Revision\RenderedRevision\$revision
RevisionRecord $revision
Definition: RenderedRevision.php:52
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:45
ParserOutput
Definition: ParserOutput.php:25
Revision\RenderedRevision\$title
Title $title
Definition: RenderedRevision.php:49
Revision\RenderedRevision\getRevisionParserOutput
getRevisionParserOutput(array $hints=[])
Definition: RenderedRevision.php:191
Revision\SuppressedDataException
Exception raised in response to an audience check when attempting to access suppressed information wi...
Definition: SuppressedDataException.php:32
Revision\RenderedRevision\pruneRevisionSensitiveOutput
pruneRevisionSensitiveOutput( $actualRevId)
Prune any output that depends on the revision ID.
Definition: RenderedRevision.php:304
Revision\RenderedRevision\outputVariesOnRevisionMetaData
outputVariesOnRevisionMetaData(ParserOutput $out, $actualRevId)
Definition: RenderedRevision.php:379
Revision\RenderedRevision\$options
ParserOptions $options
Definition: RenderedRevision.php:57
Revision\RenderedRevision\getSlotParserOutputUncached
getSlotParserOutputUncached(Content $content, $withHtml)
Definition: RenderedRevision.php:260
$out
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition: hooks.txt:780
Revision\RenderedRevision\setSaveParseLogger
setSaveParseLogger(LoggerInterface $saveParseLogger)
Definition: RenderedRevision.php:137
Revision\RenderedRevision\setRevisionInternal
setRevisionInternal(RevisionRecord $revision)
Definition: RenderedRevision.php:323
User
User
Definition: All_system_messages.txt:425
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Revision\SlotRenderingProvider
A lazy provider of ParserOutput objects for a revision's individual slots.
Definition: SlotRenderingProvider.php:17
MediaWiki\Revision
Created by PhpStorm.
Definition: FallbackSlotRoleHandler.php:23
Revision\RenderedRevision\$saveParseLogger
LoggerInterface $saveParseLogger
For profiling ParserOutput re-use.
Definition: RenderedRevision.php:90
Revision
Definition: Revision.php:40
Revision\RenderedRevision\getSlotParserOutput
getSlotParserOutput( $role, array $hints=[])
Definition: RenderedRevision.php:220
Revision\RenderedRevision\$revisionOutput
ParserOutput null $revisionOutput
The combined ParserOutput for the revision, initialized lazily by getRevisionParserOutput().
Definition: RenderedRevision.php:73
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$parser
see documentation in includes Linker php for Linker::makeImageLink or false for current used if you return false $parser
Definition: hooks.txt:1802
$output
$output
Definition: SyntaxHighlight.php:334
Title\equals
equals(Title $title)
Compare with another title.
Definition: Title.php:4006
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:949
Revision\RevisionRecord\getId
getId()
Get revision ID.
Definition: RevisionRecord.php:273
Revision\RenderedRevision\$combineOutput
callable $combineOutput
Callback for combining slot output into revision output.
Definition: RenderedRevision.php:85
title
title
Definition: parserTests.txt:245
Revision\RenderedRevision\$audience
int $audience
Audience to check when accessing content.
Definition: RenderedRevision.php:62
Revision\SlotRecord\MAIN
const MAIN
Definition: SlotRecord.php:41
Revision\RevisionRecord\getSlots
getSlots()
Returns the slots defined for this revision.
Definition: RevisionRecord.php:227
Content
Base interface for content objects.
Definition: Content.php:34
Revision\RevisionRecord\FOR_PUBLIC
const FOR_PUBLIC
Definition: RevisionRecord.php:57
Revision\RenderedRevision\$forUser
User null $forUser
The user to use for audience checks during content access.
Definition: RenderedRevision.php:67
Revision\RenderedRevision\$slotsOutput
ParserOutput[] $slotsOutput
The ParserOutput for each slot, initialized lazily by getSlotParserOutput().
Definition: RenderedRevision.php:79
Title
Represents a title within MediaWiki.
Definition: Title.php:40
Revision\RenderedRevision\__construct
__construct(Title $title, RevisionRecord $revision, ParserOptions $options, callable $combineOutput, $audience=RevisionRecord::FOR_PUBLIC, User $forUser=null)
Definition: RenderedRevision.php:108
$rev
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition: hooks.txt:1769
Revision\RevisionRecord\DELETED_TEXT
const DELETED_TEXT
Definition: RevisionRecord.php:48
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
revision
In both all secondary updates will be triggered handle like object that caches derived data representing a revision
Definition: pageupdater.txt:78
$content
$content
Definition: pageupdater.txt:72
Revision\RenderedRevision
RenderedRevision represents the rendered representation of a revision.
Definition: RenderedRevision.php:44
Revision\RenderedRevision\isContentDeleted
isContentDeleted()
Definition: RenderedRevision.php:144
options
Using a hook running we can avoid having all this option specific stuff in our mainline code Using the function We ve cleaned up the code here by removing clumps of infrequently used code and moving them off somewhere else It s much easier for someone working with this code to see what s _really_ going and make changes or fix bugs In we can take all the code that deals with the little used title reversing options(say) and put it in one place. Instead of having little title-reversing if-blocks spread all over the codebase in showAnArticle
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:48
Revision\RevisionRecord\FOR_THIS_USER
const FOR_THIS_USER
Definition: RevisionRecord.php:58
Revision\RenderedRevision\setRevisionParserOutput
setRevisionParserOutput(ParserOutput $output)
Sets a ParserOutput to be returned by getRevisionParserOutput().
Definition: RenderedRevision.php:172
Revision\RenderedRevision\getOptions
getOptions()
Definition: RenderedRevision.php:158
Revision\RenderedRevision\updateRevision
updateRevision(RevisionRecord $rev)
Updates the RevisionRecord after the revision has been saved.
Definition: RenderedRevision.php:278