Introduce ParserOutput:setFromParserOptions() and use for preview flag

Bug: T341010
Co-Authored-by: cananian <cananian@wikimedia.org>
Co-Authored-by: ihurbain <ihurbainpalatin@wikimedia.org>
Change-Id: I03125fdaa7dd71ba57d593e85ecb98be6806f3f6
This commit is contained in:
C. Scott Ananian 2024-01-12 00:07:33 -05:00
parent 5c0e9216e4
commit 242c6d2cf9
7 changed files with 63 additions and 21 deletions

View file

@ -689,10 +689,10 @@ class Parser {
$this->mOutput->finalizeAdaptiveCacheExpiry();
# Warn if too many heavyweight parser functions were used
if ( $this->mExpensiveFunctionCount > $this->mOptions->getExpensiveParserFunctionLimit() ) {
if ( $this->mExpensiveFunctionCount > $options->getExpensiveParserFunctionLimit() ) {
$this->limitationWarn( 'expensive-parserfunction',
$this->mExpensiveFunctionCount,
$this->mOptions->getExpensiveParserFunctionLimit()
$options->getExpensiveParserFunctionLimit()
);
}
@ -701,12 +701,7 @@ class Parser {
$this->makeLimitReport();
}
# Wrap non-interface parser output in a <div> so it can be targeted
# with CSS (T37247)
$class = $this->mOptions->getWrapOutputClass();
if ( $class !== false && !$this->mOptions->getInterfaceMessage() ) {
$this->mOutput->addWrapperDivClass( $class );
}
$this->mOutput->setFromParserOptions( $options );
$this->mOutput->setText( $text );

View file

@ -34,6 +34,7 @@ use MediaWiki\Parser\Parsoid\PageBundleParserOutputConverter;
use MediaWiki\Title\Title;
use MediaWiki\Title\TitleValue;
use Parser;
use ParserOptions;
use UnexpectedValueException;
use Wikimedia\Bcp47Code\Bcp47Code;
use Wikimedia\Bcp47Code\Bcp47CodeValue;
@ -2071,6 +2072,29 @@ class ParserOutput extends CacheTime implements ContentMetadataCollector {
}
}
/**
* Transfer parser options which affect post-processing from ParserOptions
* to this ParserOutput.
* @param ParserOptions $parserOptions
*/
public function setFromParserOptions( ParserOptions $parserOptions ) {
// Copied from Parser.php::parse and should probably be abstracted
// into the parent base class (probably as part of T236809)
// Wrap non-interface parser output in a <div> so it can be targeted
// with CSS (T37247)
$class = $parserOptions->getWrapOutputClass();
if ( $class !== false && !$parserOptions->getInterfaceMessage() ) {
$this->addWrapperDivClass( $class );
}
// Record whether this is a preview parse in the output (T341010)
if ( $parserOptions->getIsPreview() ) {
$this->setOutputFlag( ParserOutputFlags::IS_PREVIEW, true );
// Ensure that previews aren't cacheable, just to be safe.
$this->updateCacheExpiry( 0 );
}
}
public function __sleep() {
return array_filter( array_keys( get_object_vars( $this ) ),
static function ( $field ) {

View file

@ -164,6 +164,15 @@ class ParserOutputFlags {
*/
public const USER_SIGNATURE = 'user-signature';
/**
* @var string Set when the parse is done in "preview mode", in which
* case various shortcuts are taken to work around the fact that the
* parsed text does not yet have an actual revision ID, revision time,
* etc.
* @see ParserOptions::getIsPreview()
*/
public const IS_PREVIEW = 'is-preview';
public static function cases(): array {
return [
self::NO_GALLERY,
@ -183,6 +192,7 @@ class ParserOutputFlags {
self::VARY_PAGE_ID,
self::VARY_USER,
self::USER_SIGNATURE,
self::IS_PREVIEW,
];
}
}

View file

@ -172,14 +172,7 @@ class ParsoidParser /* eventually this will extend \Parser */ {
$this->setParsoidRenderID( $pageConfig, $parserOutput );
}
// Copied from Parser.php::parse and should probably be abstracted
// into the parent base class (probably as part of T236809)
// Wrap non-interface parser output in a <div> so it can be targeted
// with CSS (T37247)
$class = $options->getWrapOutputClass();
if ( $class !== false && !$options->getInterfaceMessage() ) {
$parserOutput->addWrapperDivClass( $class );
}
$parserOutput->setFromParserOptions( $options );
$parserOutput->recordTimeProfile();
$this->makeLimitReport( $options, $parserOutput );

View file

@ -54,7 +54,7 @@ class WikitextContentHandlerIntegrationTest extends TextContentHandlerIntegratio
'UsedOptions' => [
'useParsoid', 'suppressTOC', 'maxIncludeSize', 'maxPPNodeCount',
'targetLanguage', 'interfaceMessage', 'maxPPExpandDepth', 'disableTitleConversion',
'disableContentConversion', 'expensiveParserFunctionLimit', 'wrapclass'
'disableContentConversion', 'expensiveParserFunctionLimit', 'isPreview', 'wrapclass'
],
],
];
@ -69,7 +69,8 @@ class WikitextContentHandlerIntegrationTest extends TextContentHandlerIntegratio
'Sections' => [
],
'UsedOptions' => [
'useParsoid', 'maxIncludeSize', 'interfaceMessage', 'disableContentConversion', 'wrapclass'
'useParsoid', 'maxIncludeSize', 'interfaceMessage', 'disableContentConversion', 'isPreview',
'wrapclass'
],
],
'options' => [ 'useParsoid' => true ]
@ -86,7 +87,8 @@ class WikitextContentHandlerIntegrationTest extends TextContentHandlerIntegratio
'Sections' => [
],
'UsedOptions' => [
'useParsoid', 'maxIncludeSize', 'interfaceMessage', 'disableContentConversion', 'wrapclass'
'useParsoid', 'maxIncludeSize', 'interfaceMessage', 'disableContentConversion', 'isPreview',
'wrapclass'
],
],
'options' => [ 'useParsoid' => true ]

View file

@ -2,6 +2,7 @@
use MediaWiki\MainConfigNames;
use MediaWiki\Parser\ParserOutput;
use MediaWiki\Parser\ParserOutputFlags;
use MediaWiki\Parser\ParserOutputStringSets;
use MediaWiki\Tests\Parser\ParserCacheSerializationTestCases;
use MediaWiki\Title\Title;
@ -1401,4 +1402,21 @@ EOF
$po->setExtensionData( 'parsoid-render-id', "1234/LegacyRenderId" );
$this->assertEquals( "LegacyRenderId", $po->getRenderId() );
}
public function testSetFromParserOptions() {
$pOptions = ParserOptions::newFromAnon();
$pOutput = new ParserOutput;
$pOutput->setFromParserOptions( $pOptions );
$this->assertSame( 'mw-parser-output', $pOutput->getWrapperDivClass() );
$this->assertFalse( $pOutput->getOutputFlag( ParserOutputFlags::IS_PREVIEW ) );
$this->assertTrue( $pOutput->isCacheable() );
$pOptions->setWrapOutputClass( 'test-wrapper' );
$pOptions->setIsPreview( true );
$pOutput = new ParserOutput;
$pOutput->setFromParserOptions( $pOptions );
$this->assertEquals( 'test-wrapper', $pOutput->getWrapperDivClass() );
$this->assertTrue( $pOutput->getOutputFlag( ParserOutputFlags::IS_PREVIEW ) );
$this->assertFalse( $pOutput->isCacheable() );
}
}

View file

@ -33,7 +33,7 @@ class ParsoidParserTest extends MediaWikiIntegrationTestCase {
$output->getExtensionData( ParsoidParser::PARSOID_TITLE_KEY )
);
$this->assertSame( [
'disableContentConversion', 'interfaceMessage', 'wrapclass', 'maxIncludeSize'
'disableContentConversion', 'interfaceMessage', 'wrapclass', 'isPreview', 'maxIncludeSize'
], $output->getUsedOptions() );
}
@ -70,7 +70,7 @@ class ParsoidParserTest extends MediaWikiIntegrationTestCase {
$output->getExtensionData( ParsoidParser::PARSOID_TITLE_KEY )
);
$this->assertSame( [
'disableContentConversion', 'interfaceMessage', 'wrapclass', 'maxIncludeSize'
'disableContentConversion', 'interfaceMessage', 'wrapclass', 'isPreview', 'maxIncludeSize'
], $output->getUsedOptions() );
}
}