Merge "Remove parameter 'options' from hook 'SkinEditSectionLinks'"
[lhc/web/wiklou.git] / includes / Revision / RenderedRevision.php
index fa16c61..c4a0054 100644 (file)
@@ -24,8 +24,6 @@ namespace MediaWiki\Revision;
 
 use InvalidArgumentException;
 use LogicException;
-use MediaWiki\Storage\RevisionRecord;
-use MediaWiki\Storage\SuppressedDataException;
 use ParserOptions;
 use ParserOutput;
 use Psr\Log\LoggerInterface;
@@ -33,6 +31,7 @@ use Psr\Log\NullLogger;
 use Revision;
 use Title;
 use User;
+use Content;
 use Wikimedia\Assert\Assert;
 
 /**
@@ -160,6 +159,28 @@ class RenderedRevision implements SlotRenderingProvider {
                return $this->options;
        }
 
+       /**
+        * Sets a ParserOutput to be returned by getRevisionParserOutput().
+        *
+        * @note For internal use by RevisionRenderer only! This method may be modified
+        * or removed without notice per the deprecation policy.
+        *
+        * @internal
+        *
+        * @param ParserOutput $output
+        */
+       public function setRevisionParserOutput( ParserOutput $output ) {
+               $this->revisionOutput = $output;
+
+               // If there is only one slot, we assume that the combined output is identical
+               // with the main slot's output. This is intended to prevent a redundant re-parse of
+               // the content in case getSlotParserOutput( SlotRecord::MAIN ) is called, for instance
+               // from ContentHandler::getSecondaryDataUpdates.
+               if ( $this->revision->getSlotRoles() === [ SlotRecord::MAIN ] ) {
+                       $this->slotsOutput[ SlotRecord::MAIN ] = $output;
+               }
+       }
+
        /**
         * @param array $hints Hints given as an associative array. Known keys:
         *      - 'generate-html' => bool: Whether the caller is interested in output HTML (as opposed
@@ -209,12 +230,8 @@ class RenderedRevision implements SlotRenderingProvider {
                                        'Access to the content has been suppressed for this audience'
                                );
                        } else {
-                               $output = $content->getParserOutput(
-                                       $this->title,
-                                       $this->revision->getId(),
-                                       $this->options,
-                                       $withHtml
-                               );
+                               // XXX: allow SlotRoleHandler to control the ParserOutput?
+                               $output = $this->getSlotParserOutputUncached( $content, $withHtml );
 
                                if ( $withHtml && !$output->hasText() ) {
                                        throw new LogicException(
@@ -234,6 +251,21 @@ class RenderedRevision implements SlotRenderingProvider {
                return $this->slotsOutput[$role];
        }
 
+       /**
+        * @note This method exist to make duplicate parses easier to see during profiling
+        * @param Content $content
+        * @param bool $withHtml
+        * @return ParserOutput
+        */
+       private function getSlotParserOutputUncached( Content $content, $withHtml ) {
+               return $content->getParserOutput(
+                       $this->title,
+                       $this->revision->getId(),
+                       $this->options,
+                       $withHtml
+               );
+       }
+
        /**
         * Updates the RevisionRecord after the revision has been saved. This can be used to discard
         * and cached ParserOutput so parser functions like {{REVISIONTIMESTAMP}} or {{REVISIONID}}
@@ -348,9 +380,9 @@ class RenderedRevision implements SlotRenderingProvider {
                $method = __METHOD__;
 
                if ( $out->getFlag( 'vary-revision' ) ) {
-                       // XXX: Would be just keep the output if the speculative revision ID was correct,
-                       // but that can go wrong for some edge cases, like {{PAGEID}} during page creation.
-                       // For that specific case, it would perhaps nice to have a vary-page flag.
+                       // If {{PAGEID}} resolved to 0 or {{REVISIONTIMESTAMP}} used the current
+                       // timestamp rather than that of an actual revision, then those words need
+                       // to resolve to the actual page ID or revision timestamp, respectively.
                        $this->saveParseLogger->info(
                                "$method: Prepared output has vary-revision...\n"
                        );
@@ -363,6 +395,14 @@ class RenderedRevision implements SlotRenderingProvider {
                                "$method: Prepared output has vary-revision-id with wrong ID...\n"
                        );
                        return true;
+               } elseif ( $out->getFlag( 'vary-revision-exists' ) ) {
+                       // If {{REVISIONID}} resolved to '', it now needs to resolve to '-'.
+                       // Note that edit stashing always uses '-', which can be used for both
+                       // edit filter checks and canonical parser cache.
+                       $this->saveParseLogger->info(
+                               "$method: Prepared output has vary-revision-exists...\n"
+                       );
+                       return true;
                } else {
                        // NOTE: In the original fix for T135261, the output was discarded if 'vary-user' was
                        // set for a null-edit. The reason was that the original rendering in that case was