Anomie has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/393261 )

Change subject: Use ParserOutput stateless transforms
......................................................................

Use ParserOutput stateless transforms

We still set the state in many cases for benefit of extensions, but all
calls within core should no longer be using non-default state.

Change-Id: I78b62ec33fcb8273acb9b3b4e9012215442be94c
Depends-On: I140ff32373430b61b92226689ef9b58cca317450
---
M includes/EditPage.php
M includes/Message.php
M includes/OutputPage.php
M includes/Status.php
M includes/api/ApiParse.php
M includes/content/WikiTextStructure.php
M includes/diff/DifferenceEngine.php
M includes/installer/Installer.php
M includes/page/Article.php
M includes/parser/ParserOutput.php
M includes/specials/SpecialRecentchanges.php
M includes/specials/SpecialUndelete.php
M tests/parser/ParserTestRunner.php
13 files changed, 80 insertions(+), 30 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/61/393261/1

diff --git a/includes/EditPage.php b/includes/EditPage.php
index ff224c5..bcaab3a 100644
--- a/includes/EditPage.php
+++ b/includes/EditPage.php
@@ -4012,7 +4012,10 @@
                $parserOutput->setEditSectionTokens( false ); // no section 
edit links
                return [
                        'parserOutput' => $parserOutput,
-                       'html' => $parserOutput->getText() ];
+                       'html' => $parserOutput->getText( [
+                               'enableSectionEditLinks' => false
+                       ] )
+               ];
        }
 
        /**
diff --git a/includes/Message.php b/includes/Message.php
index 3b2f3cc..16ae839 100644
--- a/includes/Message.php
+++ b/includes/Message.php
@@ -1244,7 +1244,9 @@
                        $this->getLanguage()
                );
 
-               return $out instanceof ParserOutput ? $out->getText() : $out;
+               return $out instanceof ParserOutput
+                       ? $out->getText( [ 'enableSectionEditLinks' => false ] )
+                       : $out;
        }
 
        /**
diff --git a/includes/OutputPage.php b/includes/OutputPage.php
index a5f9c18..92963fd 100644
--- a/includes/OutputPage.php
+++ b/includes/OutputPage.php
@@ -1783,7 +1783,9 @@
 
                $popts->setTidy( $oldTidy );
 
-               $this->addParserOutput( $parserOutput );
+               $this->addParserOutput( $parserOutput, [
+                       'enableSectionEditLinks' => false,
+               ] );
        }
 
        /**
@@ -1868,9 +1870,10 @@
         *
         * @since 1.24
         * @param ParserOutput $parserOutput
+        * @param array $poOptions Options to ParserOutput::getText()
         */
-       public function addParserOutputContent( $parserOutput ) {
-               $this->addParserOutputText( $parserOutput );
+       public function addParserOutputContent( $parserOutput, $poOptions = [] 
) {
+               $this->addParserOutputText( $parserOutput, $poOptions );
 
                $this->addModules( $parserOutput->getModules() );
                $this->addModuleScripts( $parserOutput->getModuleScripts() );
@@ -1884,9 +1887,10 @@
         *
         * @since 1.24
         * @param ParserOutput $parserOutput
+        * @param array $poOptions Options to ParserOutput::getText()
         */
-       public function addParserOutputText( $parserOutput ) {
-               $text = $parserOutput->getText();
+       public function addParserOutputText( $parserOutput, $poOptions = [] ) {
+               $text = $parserOutput->getText( $poOptions );
                // Avoid PHP 7.1 warning of passing $this by reference
                $outputPage = $this;
                Hooks::runWithoutAbort( 'OutputPageBeforeHTML', [ &$outputPage, 
&$text ] );
@@ -1897,16 +1901,22 @@
         * Add everything from a ParserOutput object.
         *
         * @param ParserOutput $parserOutput
+        * @param array $poOptions Options to ParserOutput::getText()
         */
-       function addParserOutput( $parserOutput ) {
+       function addParserOutput( $parserOutput, $poOptions = [] ) {
                $this->addParserOutputMetadata( $parserOutput );
 
                // Touch section edit links only if not previously disabled
                if ( $parserOutput->getEditSectionTokens() ) {
                        $parserOutput->setEditSectionTokens( 
$this->mEnableSectionEditLinks );
                }
+               if ( !$this->mEnableSectionEditLinks
+                       && !array_key_exists( 'enableSectionEditLinks', 
$poOptions )
+               ) {
+                       $poOptions['enableSectionEditLinks'] = false;
+               }
 
-               $this->addParserOutputText( $parserOutput );
+               $this->addParserOutputText( $parserOutput, $poOptions );
        }
 
        /**
@@ -1957,7 +1967,9 @@
                        $popts->setTargetLanguage( $oldLang );
                }
 
-               return $parserOutput->getText();
+               return $parserOutput->getText( [
+                       'enableSectionEditLinks' => false,
+               ] );
        }
 
        /**
@@ -3957,6 +3969,7 @@
         * Enables/disables section edit links, doesn't override 
__NOEDITSECTION__
         * @param bool $flag
         * @since 1.23
+        * @deprecated since 1.31, use $poOptions to addParserOutput() instead.
         */
        public function enableSectionEditLinks( $flag = true ) {
                $this->mEnableSectionEditLinks = $flag;
@@ -3965,6 +3978,7 @@
        /**
         * @return bool
         * @since 1.23
+        * @deprecated since 1.31, use $poOptions to addParserOutput() instead.
         */
        public function sectionEditLinksEnabled() {
                return $this->mEnableSectionEditLinks;
diff --git a/includes/Status.php b/includes/Status.php
index a35af6e..f17f173 100644
--- a/includes/Status.php
+++ b/includes/Status.php
@@ -316,7 +316,9 @@
                $lang = $this->languageFromParam( $lang );
                $text = $this->getWikiText( $shortContext, $longContext, $lang 
);
                $out = MessageCache::singleton()->parse( $text, null, true, 
true, $lang );
-               return $out instanceof ParserOutput ? $out->getText() : $out;
+               return $out instanceof ParserOutput
+                       ? $out->getText( [ 'enableSectionEditLinks' => false ] )
+                       : $out;
        }
 
        /**
diff --git a/includes/api/ApiParse.php b/includes/api/ApiParse.php
index 15b94fb..ec015da 100644
--- a/includes/api/ApiParse.php
+++ b/includes/api/ApiParse.php
@@ -288,10 +288,6 @@
                        $result_array['textsuppressed'] = true;
                }
 
-               if ( $params['disabletoc'] ) {
-                       $p_result->setTOCEnabled( false );
-               }
-
                if ( isset( $params['useskin'] ) ) {
                        $factory = 
MediaWikiServices::getInstance()->getSkinFactory();
                        $skin = $factory->makeSkin( Skin::normalizeKey( 
$params['useskin'] ) );
@@ -347,7 +343,10 @@
                }
 
                if ( isset( $prop['text'] ) ) {
-                       $result_array['text'] = $p_result->getText();
+                       $result_array['text'] = $p_result->getText( [
+                               'allowTOC' => !$params['disabletoc'],
+                               'enableSectionEditLinks' => 
!$params['disableeditsection'],
+                       ] );
                        $result_array[ApiResult::META_BC_SUBELEMENTS][] = 
'text';
                }
 
diff --git a/includes/content/WikiTextStructure.php 
b/includes/content/WikiTextStructure.php
index aeb96b6..0eadc3c 100644
--- a/includes/content/WikiTextStructure.php
+++ b/includes/content/WikiTextStructure.php
@@ -146,9 +146,10 @@
                if ( !is_null( $this->allText ) ) {
                        return;
                }
-               $this->parserOutput->setEditSectionTokens( false );
-               $this->parserOutput->setTOCEnabled( false );
-               $text = $this->parserOutput->getText();
+               $text = $this->parserOutput->getText( [
+                       'enableSectionEditTokens' => false,
+                       'allowTOC' => false,
+               ] );
                if ( strlen( $text ) == 0 ) {
                        $this->allText = "";
                        // empty text - nothing to seek here
diff --git a/includes/diff/DifferenceEngine.php 
b/includes/diff/DifferenceEngine.php
index 51b9f15..7e05be6 100644
--- a/includes/diff/DifferenceEngine.php
+++ b/includes/diff/DifferenceEngine.php
@@ -634,7 +634,10 @@
                                        if ( Hooks::run( 
'DifferenceEngineRenderRevisionAddParserOutput',
                                                [ $this, $out, $parserOutput, 
$wikiPage ] )
                                        ) {
-                                               $out->addParserOutput( 
$parserOutput );
+                                               $out->addParserOutput( 
$parserOutput, [
+                                                       
'enableSectionEditLinks' => $this->mNewRev->isCurrent()
+                                                               && 
$this->mNewRev->getTitle()->quickUserCan( 'edit', $this->getUser() ),
+                                               ] );
                                        }
                                }
                        }
diff --git a/includes/installer/Installer.php b/includes/installer/Installer.php
index e99ea7c..e5d2310 100644
--- a/includes/installer/Installer.php
+++ b/includes/installer/Installer.php
@@ -688,7 +688,9 @@
 
                try {
                        $out = $wgParser->parse( $text, $this->parserTitle, 
$this->parserOptions, $lineStart );
-                       $html = $out->getText();
+                       $html = $out->getText( [
+                               'enableSectionEditLinks' => false,
+                       ] );
                } catch ( MediaWiki\Services\ServiceDisabledException $e ) {
                        $html = '<!--DB access attempted during parse-->  ' . 
htmlspecialchars( $text );
 
diff --git a/includes/page/Article.php b/includes/page/Article.php
index c9dc273..dadf311 100644
--- a/includes/page/Article.php
+++ b/includes/page/Article.php
@@ -76,6 +76,13 @@
        public $mParserOutput;
 
        /**
+        * @var bool Whether render() was called. With the way subclasses work
+        * here, there doesn't seem to be any other way to stop calling
+        * OutputPage::enableSectionEditLinks() and still have it work as it 
did before.
+        */
+       private $disableSectionEditForRender = false;
+
+       /**
         * Constructor and clear the article
         * @param Title $title Reference to a Title object.
         * @param int $oldId Revision ID, null to fetch from request, zero for 
current
@@ -469,12 +476,17 @@
                $parserCache = 
MediaWikiServices::getInstance()->getParserCache();
 
                $parserOptions = $this->getParserOptions();
+               $poOptions = [];
                # Render printable version, use printable version cache
                if ( $outputPage->isPrintable() ) {
                        $parserOptions->setIsPrintable( true );
                        $parserOptions->setEditSection( false );
-               } elseif ( !$this->isCurrent() || 
!$this->getTitle()->quickUserCan( 'edit', $user ) ) {
+                       $poOptions['enableSectionEditLinks'] = false;
+               } elseif ( $this->disableSectionEditForRender
+                       || !$this->isCurrent() || 
!$this->getTitle()->quickUserCan( 'edit', $user )
+               ) {
                        $parserOptions->setEditSection( false );
+                       $poOptions['enableSectionEditLinks'] = false;
                }
 
                # Try client and file cache
@@ -533,7 +545,7 @@
                                                        } else {
                                                                wfDebug( 
__METHOD__ . ": showing parser cache contents\n" );
                                                        }
-                                                       
$outputPage->addParserOutput( $this->mParserOutput );
+                                                       
$outputPage->addParserOutput( $this->mParserOutput, $poOptions );
                                                        # Ensure that UI 
elements requiring revision ID have
                                                        # the correct version 
information.
                                                        
$outputPage->setRevisionId( $this->mPage->getLatest() );
@@ -597,7 +609,7 @@
                                        }
 
                                        $this->mParserOutput = 
$poolArticleView->getParserOutput();
-                                       $outputPage->addParserOutput( 
$this->mParserOutput );
+                                       $outputPage->addParserOutput( 
$this->mParserOutput, $poOptions );
                                        if ( $content->getRedirectTarget() ) {
                                                $outputPage->addSubtitle( 
"<span id=\"redirectsub\">" .
                                                        
$this->getContext()->msg( 'redirectpagesub' )->parse() . "</span>" );
@@ -1515,6 +1527,7 @@
                $this->getContext()->getRequest()->response()->header( 
'X-Robots-Tag: noindex' );
                $this->getContext()->getOutput()->setArticleBodyOnly( true );
                $this->getContext()->getOutput()->enableSectionEditLinks( false 
);
+               $this->disableSectionEditForRender = true;
                $this->view();
        }
 
diff --git a/includes/parser/ParserOutput.php b/includes/parser/ParserOutput.php
index 7cbccf7..fb4a169 100644
--- a/includes/parser/ParserOutput.php
+++ b/includes/parser/ParserOutput.php
@@ -23,6 +23,12 @@
  */
 class ParserOutput extends CacheTime {
        /**
+        * Feature flag to indicate to extensions that MediaWiki core supports 
and
+        * uses getText() stateless transforms.
+        */
+       const SUPPORTS_STATELESS_TRANSFORMS = 1;
+
+       /**
         * @var string $mText The output text
         */
        public $mText;
@@ -147,7 +153,7 @@
         * @deprecated since 1.31 Use getText() options.
         * @var bool $mEditSectionTokens prefix/suffix markers if edit sections 
were output as tokens.
         */
-       public $mEditSectionTokens = false;
+       public $mEditSectionTokens = true;
 
        /**
         * @var array $mProperties Name/value pairs to be cached in the DB.
@@ -269,7 +275,7 @@
 
                // @todo Warn if !array_key_exists( 'enableSectionEditLinks', 
$options )
                //     && !$this->mEditSectionTokens
-               //  Note that while $this->mEditSectionTokens defaults to false,
+               //  Note that while $this->mEditSectionTokens formerly 
defaulted to false,
                //  ParserOptions->getEditSection() defaults to true and Parser 
copies
                //  that to us so true makes more sense as the stateless 
default.
 
diff --git a/includes/specials/SpecialRecentchanges.php 
b/includes/specials/SpecialRecentchanges.php
index dfa13b6..cfc7a85 100644
--- a/includes/specials/SpecialRecentchanges.php
+++ b/includes/specials/SpecialRecentchanges.php
@@ -608,7 +608,9 @@
                                /*interface*/false,
                                $wgContLang
                        );
-                       $content = $parserOutput->getText();
+                       $content = $parserOutput->getText( [
+                               'enableSectionEditLinks' => false,
+                       ] );
                        // Add only metadata here (including the language 
links), text is added below
                        $this->getOutput()->addParserOutputMetadata( 
$parserOutput );
 
diff --git a/includes/specials/SpecialUndelete.php 
b/includes/specials/SpecialUndelete.php
index 71dee3d..0c038c1 100644
--- a/includes/specials/SpecialUndelete.php
+++ b/includes/specials/SpecialUndelete.php
@@ -455,7 +455,9 @@
                        $popts->setEditSection( false );
 
                        $pout = $content->getParserOutput( $this->mTargetObj, 
$rev->getId(), $popts, true );
-                       $out->addParserOutput( $pout );
+                       $out->addParserOutput( $pout, [
+                               'enableSectionEditLinks' => false,
+                       ] );
                }
 
                if ( $isText ) {
diff --git a/tests/parser/ParserTestRunner.php 
b/tests/parser/ParserTestRunner.php
index 149ba80..44a00a8 100644
--- a/tests/parser/ParserTestRunner.php
+++ b/tests/parser/ParserTestRunner.php
@@ -853,8 +853,9 @@
                        $out = $parser->getPreloadText( $test['input'], $title, 
$options );
                } else {
                        $output = $parser->parse( $test['input'], $title, 
$options, true, true, 1337 );
-                       $output->setTOCEnabled( !isset( $opts['notoc'] ) );
-                       $out = $output->getText();
+                       $out = $output->getText( [
+                               'allowTOC' => !isset( $opts['notoc'] )
+                       ] );
                        if ( isset( $opts['tidy'] ) ) {
                                $out = preg_replace( '/\s+$/', '', $out );
                        }

-- 
To view, visit https://gerrit.wikimedia.org/r/393261
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I78b62ec33fcb8273acb9b3b4e9012215442be94c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Anomie <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to