d3e5938c8637f44b8ce50eedaf6ce76fb4cbc125
[lhc/web/wiklou.git] / includes / Revision / RenderedRevision.php
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 ParserOptions;
28 use ParserOutput;
29 use Psr\Log\LoggerInterface;
30 use Psr\Log\NullLogger;
31 use Revision;
32 use Title;
33 use User;
34 use Content;
35 use Wikimedia\Assert\Assert;
36
37 /**
38 * RenderedRevision represents the rendered representation of a revision. It acts as a lazy provider
39 * of ParserOutput objects for the revision's individual slots, as well as a combined ParserOutput
40 * of all slots.
41 *
42 * @since 1.32
43 */
44 class RenderedRevision implements SlotRenderingProvider {
45
46 /**
47 * @var Title
48 */
49 private $title;
50
51 /** @var RevisionRecord */
52 private $revision;
53
54 /**
55 * @var ParserOptions
56 */
57 private $options;
58
59 /**
60 * @var int Audience to check when accessing content.
61 */
62 private $audience = RevisionRecord::FOR_PUBLIC;
63
64 /**
65 * @var User|null The user to use for audience checks during content access.
66 */
67 private $forUser = null;
68
69 /**
70 * @var ParserOutput|null The combined ParserOutput for the revision,
71 * initialized lazily by getRevisionParserOutput().
72 */
73 private $revisionOutput = null;
74
75 /**
76 * @var ParserOutput[] The ParserOutput for each slot,
77 * initialized lazily by getSlotParserOutput().
78 */
79 private $slotsOutput = [];
80
81 /**
82 * @var callable Callback for combining slot output into revision output.
83 * Signature: function ( RenderedRevision $this ): ParserOutput.
84 */
85 private $combineOutput;
86
87 /**
88 * @var LoggerInterface For profiling ParserOutput re-use.
89 */
90 private $saveParseLogger;
91
92 /**
93 * @note Application logic should not instantiate RenderedRevision instances directly,
94 * but should use a RevisionRenderer instead.
95 *
96 * @param Title $title
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 callable $combineOutput Callback for combining slot output into revision output.
104 * Signature: function ( RenderedRevision $this ): ParserOutput.
105 * @param int $audience Use RevisionRecord::FOR_PUBLIC, FOR_THIS_USER, or RAW.
106 * @param User|null $forUser Required if $audience is FOR_THIS_USER.
107 */
108 public function __construct(
109 Title $title,
110 RevisionRecord $revision,
111 ParserOptions $options,
112 callable $combineOutput,
113 $audience = RevisionRecord::FOR_PUBLIC,
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
124 if ( $audience === RevisionRecord::FOR_THIS_USER && !$forUser ) {
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
134 /**
135 * @param LoggerInterface $saveParseLogger
136 */
137 public function setSaveParseLogger( LoggerInterface $saveParseLogger ) {
138 $this->saveParseLogger = $saveParseLogger;
139 }
140
141 /**
142 * @return bool Whether the revision's content has been hidden from unprivileged users.
143 */
144 public function isContentDeleted() {
145 return $this->revision->isDeleted( RevisionRecord::DELETED_TEXT );
146 }
147
148 /**
149 * @return RevisionRecord
150 */
151 public function getRevision() {
152 return $this->revision;
153 }
154
155 /**
156 * @return ParserOptions
157 */
158 public function getOptions() {
159 return $this->options;
160 }
161
162 /**
163 * Sets a ParserOutput to be returned by getRevisionParserOutput().
164 *
165 * @note For internal use by RevisionRenderer only! This method may be modified
166 * or removed without notice per the deprecation policy.
167 *
168 * @internal
169 *
170 * @param ParserOutput $output
171 */
172 public function setRevisionParserOutput( ParserOutput $output ) {
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
184 /**
185 * @param array $hints Hints given as an associative array. Known keys:
186 * - 'generate-html' => bool: Whether the caller is interested in output HTML (as opposed
187 * to just meta-data). Default is to generate HTML.
188 *
189 * @return ParserOutput
190 */
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
210 /**
211 * @param string $role
212 * @param array $hints Hints given as an associative array. Known keys:
213 * - 'generate-html' => bool: Whether the caller is interested in output HTML (as opposed
214 * to just meta-data). Default is to generate HTML.
215 *
216 * @throws SuppressedDataException if the content is not accessible for the audience
217 * specified in the constructor.
218 * @return ParserOutput
219 */
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
254 /**
255 * @note This method exist to make duplicate parses easier to see during profiling
256 * @param Content $content
257 * @param bool $withHtml
258 * @return ParserOutput
259 */
260 private function getSlotParserOutputUncached( Content $content, $withHtml ) {
261 return $content->getParserOutput(
262 $this->title,
263 $this->revision->getId(),
264 $this->options,
265 $withHtml
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( $this->revision->getId() );
295 }
296
297 /**
298 * Prune any output that depends on the revision ID.
299 *
300 * @param int|bool $actualRevId The actual rev id, to check the used speculative rev ID
301 * against, or false to not purge on vary-revision-id, or true to purge on
302 * vary-revision-id unconditionally.
303 */
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
320 /**
321 * @param RevisionRecord $revision
322 */
323 private function setRevisionInternal( RevisionRecord $revision ) {
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() ) {
357 $title = $this->title;
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
372 /**
373 * @param ParserOutput $out
374 * @param int|bool $actualRevId The actual rev id, to check the used speculative rev ID
375 * against, or false to not purge on vary-revision-id, or true to purge on
376 * vary-revision-id unconditionally.
377 * @return bool
378 */
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 } elseif ( $out->getFlag( 'vary-revision-exists' ) ) {
399 // If {{REVISIONID}} resolved to '', it now needs to resolve to '-'.
400 // Note that edit stashing always uses '-', which can be used for both
401 // edit filter checks and canonical parser cache.
402 $this->saveParseLogger->info(
403 "$method: Prepared output has vary-revision-exists...\n"
404 );
405 return true;
406 } else {
407 // NOTE: In the original fix for T135261, the output was discarded if 'vary-user' was
408 // set for a null-edit. The reason was that the original rendering in that case was
409 // targeting the user making the null-edit, not the user who made the original edit,
410 // causing {{REVISIONUSER}} to return the wrong name.
411 // This case is now expected to be handled by the code in RevisionRenderer that
412 // constructs the ParserOptions: For a null-edit, setCurrentRevisionCallback is called
413 // with the old, existing revision.
414
415 wfDebug( "$method: Keeping prepared output...\n" );
416 return false;
417 }
418 }
419
420 }