MediaWiki REL1_32
RenderedRevision.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Revision;
24
25use InvalidArgumentException;
26use LogicException;
28use ParserOutput;
29use Psr\Log\LoggerInterface;
30use Psr\Log\NullLogger;
31use Revision;
32use Title;
33use User;
34use Content;
35use 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
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
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(),
241 $this->options,
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() ) {
334 $title = $this->title;
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}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
RenderedRevision represents the rendered representation of a revision.
outputVariesOnRevisionMetaData(ParserOutput $out, $actualRevId)
LoggerInterface $saveParseLogger
For profiling ParserOutput re-use.
User null $forUser
The user to use for audience checks during content access.
__construct(Title $title, RevisionRecord $revision, ParserOptions $options, callable $combineOutput, $audience=RevisionRecord::FOR_PUBLIC, User $forUser=null)
ParserOutput null $revisionOutput
The combined ParserOutput for the revision, initialized lazily by getRevisionParserOutput().
updateRevision(RevisionRecord $rev)
Updates the RevisionRecord after the revision has been saved.
pruneRevisionSensitiveOutput( $actualRevId)
Prune any output that depends on the revision ID.
ParserOutput[] $slotsOutput
The ParserOutput for each slot, initialized lazily by getSlotParserOutput().
setRevisionInternal(RevisionRecord $revision)
setSaveParseLogger(LoggerInterface $saveParseLogger)
int $audience
Audience to check when accessing content.
callable $combineOutput
Callback for combining slot output into revision output.
getSlotParserOutputUncached(Content $content, $withHtml)
getSlotParserOutput( $role, array $hints=[])
Page revision base class.
Exception raised in response to an audience check when attempting to access suppressed information wi...
Set options of the Parser.
Represents a title within MediaWiki.
Definition Title.php:39
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:47
see documentation in includes Linker php for Linker::makeImageLink or false for current used if you return false $parser
Definition hooks.txt:1873
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
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:894
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place $output
Definition hooks.txt:2317
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:1818
Base interface for content objects.
Definition Content.php:34
A lazy provider of ParserOutput objects for a revision's individual slots.
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))
$content