MediaWiki  1.32.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 
169  public function getRevisionParserOutput( array $hints = [] ) {
170  $withHtml = $hints['generate-html'] ?? true;
171 
172  if ( !$this->revisionOutput
173  || ( $withHtml && !$this->revisionOutput->hasText() )
174  ) {
175  $output = call_user_func( $this->combineOutput, $this, $hints );
176 
177  Assert::postcondition(
178  $output instanceof ParserOutput,
179  'Callback did not return a ParserOutput object!'
180  );
181 
182  $this->revisionOutput = $output;
183  }
184 
185  return $this->revisionOutput;
186  }
187 
198  public function getSlotParserOutput( $role, array $hints = [] ) {
199  $withHtml = $hints['generate-html'] ?? true;
200 
201  if ( !isset( $this->slotsOutput[ $role ] )
202  || ( $withHtml && !$this->slotsOutput[ $role ]->hasText() )
203  ) {
204  $content = $this->revision->getContent( $role, $this->audience, $this->forUser );
205 
206  if ( !$content ) {
207  throw new SuppressedDataException(
208  'Access to the content has been suppressed for this audience'
209  );
210  } else {
211  $output = $this->getSlotParserOutputUncached( $content, $withHtml );
212 
213  if ( $withHtml && !$output->hasText() ) {
214  throw new LogicException(
215  'HTML generation was requested, but '
216  . get_class( $content )
217  . '::getParserOutput() returns a ParserOutput with no text set.'
218  );
219  }
220 
221  // Detach watcher, to ensure option use is not recorded in the wrong ParserOutput.
222  $this->options->registerWatcher( null );
223  }
224 
225  $this->slotsOutput[ $role ] = $output;
226  }
227 
228  return $this->slotsOutput[$role];
229  }
230 
237  private function getSlotParserOutputUncached( Content $content, $withHtml ) {
238  return $content->getParserOutput(
239  $this->title,
240  $this->revision->getId(),
242  $withHtml
243  );
244  }
245 
255  public function updateRevision( RevisionRecord $rev ) {
256  if ( $rev->getId() === $this->revision->getId() ) {
257  return;
258  }
259 
260  if ( $this->revision->getId() ) {
261  throw new LogicException( 'RenderedRevision already has a revision with ID '
262  . $this->revision->getId(), ', can\'t update to revision with ID ' . $rev->getId() );
263  }
264 
265  if ( !$this->revision->getSlots()->hasSameContent( $rev->getSlots() ) ) {
266  throw new LogicException( 'Cannot update to a revision with different content!' );
267  }
268 
269  $this->setRevisionInternal( $rev );
270 
271  $this->pruneRevisionSensitiveOutput( $this->revision->getId() );
272  }
273 
281  private function pruneRevisionSensitiveOutput( $actualRevId ) {
282  if ( $this->revisionOutput ) {
283  if ( $this->outputVariesOnRevisionMetaData( $this->revisionOutput, $actualRevId ) ) {
284  $this->revisionOutput = null;
285  }
286  } else {
287  $this->saveParseLogger->debug( __METHOD__ . ": no prepared revision output...\n" );
288  }
289 
290  foreach ( $this->slotsOutput as $role => $output ) {
291  if ( $this->outputVariesOnRevisionMetaData( $output, $actualRevId ) ) {
292  unset( $this->slotsOutput[$role] );
293  }
294  }
295  }
296 
301  $this->revision = $revision;
302 
303  // Force the parser to use $this->revision to resolve magic words like {{REVISIONUSER}}
304  // if the revision is either known to be complete, or it doesn't have a revision ID set.
305  // If it's incomplete and we have a revision ID, the parser can do better by loading
306  // the revision from the database if needed to handle a magic word.
307  //
308  // The following considerations inform the logic described above:
309  //
310  // 1) If we have a saved revision already loaded, we want the parser to use it, instead of
311  // loading it again.
312  //
313  // 2) If the revision is a fake that wraps some kind of synthetic content, such as an
314  // error message from Article, it should be used directly and things like {{REVISIONUSER}}
315  // should not expected to work, since there may not even be an actual revision to
316  // refer to.
317  //
318  // 3) If the revision is a fake constructed around a Title, a Content object, and
319  // a revision ID, to provide backwards compatibility to code that has access to those
320  // but not to a complete RevisionRecord for rendering, then we want the Parser to
321  // load the actual revision from the database when it encounters a magic word like
322  // {{REVISIONUSER}}, but we don't want to load that revision ahead of time just in case.
323  //
324  // 4) Previewing an edit to a template should use the submitted unsaved
325  // MutableRevisionRecord for self-transclusions in the template's documentation (see T7278).
326  // That revision would be complete except for the ID field.
327  //
328  // 5) Pre-save transform would provide a RevisionRecord that has all meta-data but is
329  // incomplete due to not yet having content set. However, since it doesn't have a revision
330  // ID either, the below code would still force it to be used, allowing
331  // {{subst::REVISIONUSER}} to function as expected.
332 
333  if ( $this->revision->isReadyForInsertion() || !$this->revision->getId() ) {
335  $oldCallback = $this->options->getCurrentRevisionCallback();
336  $this->options->setCurrentRevisionCallback(
337  function ( Title $parserTitle, $parser = false ) use ( $title, $oldCallback ) {
338  if ( $title->equals( $parserTitle ) ) {
339  $legacyRevision = new Revision( $this->revision );
340  return $legacyRevision;
341  } else {
342  return call_user_func( $oldCallback, $parserTitle, $parser );
343  }
344  }
345  );
346  }
347  }
348 
356  private function outputVariesOnRevisionMetaData( ParserOutput $out, $actualRevId ) {
357  $method = __METHOD__;
358 
359  if ( $out->getFlag( 'vary-revision' ) ) {
360  // XXX: Would be just keep the output if the speculative revision ID was correct,
361  // but that can go wrong for some edge cases, like {{PAGEID}} during page creation.
362  // For that specific case, it would perhaps nice to have a vary-page flag.
363  $this->saveParseLogger->info(
364  "$method: Prepared output has vary-revision...\n"
365  );
366  return true;
367  } elseif ( $out->getFlag( 'vary-revision-id' )
368  && $actualRevId !== false
369  && ( $actualRevId === true || $out->getSpeculativeRevIdUsed() !== $actualRevId )
370  ) {
371  $this->saveParseLogger->info(
372  "$method: Prepared output has vary-revision-id with wrong ID...\n"
373  );
374  return true;
375  } else {
376  // NOTE: In the original fix for T135261, the output was discarded if 'vary-user' was
377  // set for a null-edit. The reason was that the original rendering in that case was
378  // targeting the user making the null-edit, not the user who made the original edit,
379  // causing {{REVISIONUSER}} to return the wrong name.
380  // This case is now expected to be handled by the code in RevisionRenderer that
381  // constructs the ParserOptions: For a null-edit, setCurrentRevisionCallback is called
382  // with the old, existing revision.
383 
384  wfDebug( "$method: Keeping prepared output...\n" );
385  return false;
386  }
387  }
388 
389 }
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:169
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:281
Revision\RenderedRevision\outputVariesOnRevisionMetaData
outputVariesOnRevisionMetaData(ParserOutput $out, $actualRevId)
Definition: RenderedRevision.php:356
Revision\RenderedRevision\$options
ParserOptions $options
Definition: RenderedRevision.php:57
Revision\RenderedRevision\getSlotParserOutputUncached
getSlotParserOutputUncached(Content $content, $withHtml)
Definition: RenderedRevision.php:237
Revision\RenderedRevision\setSaveParseLogger
setSaveParseLogger(LoggerInterface $saveParseLogger)
Definition: RenderedRevision.php:137
Revision\RenderedRevision\setRevisionInternal
setRevisionInternal(RevisionRecord $revision)
Definition: RenderedRevision.php:300
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: IncompleteRevisionException.php:23
Revision\RenderedRevision\$saveParseLogger
LoggerInterface $saveParseLogger
For profiling ParserOutput re-use.
Definition: RenderedRevision.php:90
Revision
Definition: Revision.php:41
Revision\RenderedRevision\getSlotParserOutput
getSlotParserOutput( $role, array $hints=[])
Definition: RenderedRevision.php:198
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:1841
$output
$output
Definition: SyntaxHighlight.php:334
Title\equals
equals(Title $title)
Compare with another title.
Definition: Title.php:4644
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:988
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:239
Revision\RenderedRevision\$audience
int $audience
Audience to check when accessing content.
Definition: RenderedRevision.php:62
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:39
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:1808
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:47
Revision\RevisionRecord\FOR_THIS_USER
const FOR_THIS_USER
Definition: RevisionRecord.php:58
Revision\RenderedRevision\getOptions
getOptions()
Definition: RenderedRevision.php:158
$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 probably a stub 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:813
Revision\RenderedRevision\updateRevision
updateRevision(RevisionRecord $rev)
Updates the RevisionRecord after the revision has been saved.
Definition: RenderedRevision.php:255