Merge "[MCR] Introduce SlotRoleHandler and SlotRoleRegistry"
[lhc/web/wiklou.git] / includes / content / WikitextContent.php
index 5beef31..517d807 100644 (file)
@@ -25,6 +25,9 @@
  * @author Daniel Kinzler
  */
 
+use MediaWiki\Logger\LoggerFactory;
+use MediaWiki\MediaWikiServices;
+
 /**
  * Content object for wiki text pages.
  *
 class WikitextContent extends TextContent {
        private $redirectTargetAndText = null;
 
+       /**
+        * @var bool Tracks if the parser set the user-signature flag when creating this content, which
+        *   would make it expire faster in ApiStashEdit.
+        */
+       private $hadSignature = false;
+
+       /**
+        * @var array|null Stack trace of the previous parse
+        */
+       private $previousParseStackTrace = null;
+
        public function __construct( $text ) {
                parent::__construct( $text, CONTENT_MODEL_WIKITEXT );
        }
@@ -138,7 +152,17 @@ class WikitextContent extends TextContent {
                $text = $this->getNativeData();
                $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
 
-               return ( $text === $pst ) ? $this : new static( $pst );
+               if ( $text === $pst ) {
+                       return $this;
+               }
+
+               $ret = new static( $pst );
+
+               if ( $wgParser->getOutput()->getFlag( 'user-signature' ) ) {
+                       $ret->hadSignature = true;
+               }
+
+               return $ret;
        }
 
        /**
@@ -182,7 +206,7 @@ class WikitextContent extends TextContent {
                        return $this->redirectTargetAndText;
                }
 
-               $redir = MagicWord::get( 'redirect' );
+               $redir = MediaWikiServices::getInstance()->getMagicWordFactory()->get( 'redirect' );
                $text = ltrim( $this->getNativeData() );
                if ( $redir->matchStartAndRemove( $text ) ) {
                        // Extract the first link and see if it's usable
@@ -319,6 +343,28 @@ class WikitextContent extends TextContent {
        ) {
                global $wgParser;
 
+               $stackTrace = ( new RuntimeException() )->getTraceAsString();
+               if ( $this->previousParseStackTrace ) {
+                       // NOTE: there may be legitimate changes to re-parse the same WikiText content,
+                       // e.g. if predicted revision ID for the REVISIONID magic word mismatched.
+                       // But that should be rare.
+                       $logger = LoggerFactory::getInstance( 'DuplicateParse' );
+                       $logger->debug(
+                               __METHOD__ . ': Possibly redundant parse!',
+                               [
+                                       'title' => $title->getPrefixedDBkey(),
+                                       'rev' => $revId,
+                                       'options-hash' => $options->optionsHash(
+                                               ParserOptions::allCacheVaryingOptions(),
+                                               $title
+                                       ),
+                                       'trace' => $stackTrace,
+                                       'previous-trace' => $this->previousParseStackTrace,
+                               ]
+                       );
+               }
+               $this->previousParseStackTrace = $stackTrace;
+
                list( $redir, $text ) = $this->getRedirectTargetAndText();
                $output = $wgParser->parse( $text, $title, $options, true, true, $revId );
 
@@ -335,6 +381,11 @@ class WikitextContent extends TextContent {
                                $output->addModuleStyles( 'mediawiki.action.view.redirectPage' );
                        }
                }
+
+               // Pass along user-signature flag
+               if ( $this->hadSignature ) {
+                       $output->setFlag( 'user-signature' );
+               }
        }
 
        /**