X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=includes%2Fparser%2FParserOutput.php;h=e2efaff40f706a2b5d92f3f746d1a4368af75a69;hp=3480a51f3c3ad7624dee2fe5b087aeaabb554293;hb=689c847a32e7fe8a0b3a559a88a627a252c5018e;hpb=911de590d5517d08ee00c33c980d35b9c6f501c0 diff --git a/includes/parser/ParserOutput.php b/includes/parser/ParserOutput.php index 3480a51f3c..e2efaff40f 100644 --- a/includes/parser/ParserOutput.php +++ b/includes/parser/ParserOutput.php @@ -22,6 +22,13 @@ * @ingroup Parser */ class ParserOutput extends CacheTime { + /** + * Feature flags to indicate to extensions that MediaWiki core supports and + * uses getText() stateless transforms. + */ + const SUPPORTS_STATELESS_TRANSFORMS = 1; + const SUPPORTS_UNWRAP_TRANSFORM = 1; + /** * @var string $mText The output text */ @@ -144,9 +151,10 @@ class ParserOutput extends CacheTime { public $mSections = []; /** + * @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. @@ -164,6 +172,7 @@ class ParserOutput extends CacheTime { public $mTimestamp; /** + * @deprecated since 1.31 Use getText() options. * @var bool $mTOCEnabled Whether TOC should be shown, can't override __NOTOC__. */ public $mTOCEnabled = true; @@ -250,9 +259,56 @@ class ParserOutput extends CacheTime { return $this->mText; } - public function getText() { + /** + * Get the output HTML + * + * @param array $options (since 1.31) Transformations to apply to the HTML + * - allowTOC: (bool) Show the TOC, assuming there were enough headings + * to generate one and `__NOTOC__` wasn't used. Default is true, + * but might be statefully overridden. + * - enableSectionEditLinks: (bool) Include section edit links, assuming + * section edit link tokens are present in the HTML. Default is true, + * but might be statefully overridden. + * - unwrap: (bool) Remove a wrapping mw-parser-output div. Default is false. + * @return string HTML + */ + public function getText( $options = [] ) { + if ( !array_key_exists( 'allowTOC', $options ) && empty( $this->mTOCEnabled ) ) { + wfDeprecated( 'ParserOutput stateful allowTOC', '1.31' ); + } + + // 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. + if ( !array_key_exists( 'enableSectionEditLinks', $options ) && !$this->mEditSectionTokens ) { + wfDeprecated( 'ParserOutput stateful enableSectionEditLinks', '1.31' ); + } + + $options += [ + // empty() here because old cached versions might lack the field somehow. + // In that situation, the historical behavior (possibly buggy) is to remove the TOC. + 'allowTOC' => !empty( $this->mTOCEnabled ), + 'enableSectionEditLinks' => $this->mEditSectionTokens, + 'unwrap' => false, + ]; $text = $this->mText; - if ( $this->mEditSectionTokens ) { + + Hooks::runWithoutAbort( 'ParserOutputPostCacheTransform', [ $this, &$text, &$options ] ); + + if ( $options['unwrap'] !== false ) { + $start = Html::openElement( 'div', [ + 'class' => 'mw-parser-output' + ] ); + $startLen = strlen( $start ); + $end = Html::closeElement( 'div' ); + $endLen = strlen( $end ); + + if ( substr( $text, 0, $startLen ) === $start && substr( $text, -$endLen ) === $end ) { + $text = substr( $text, $startLen, -$endLen ); + } + } + + if ( $options['enableSectionEditLinks'] ) { $text = preg_replace_callback( self::EDITSECTION_REGEX, function ( $m ) { @@ -278,8 +334,7 @@ class ParserOutput extends CacheTime { $text = preg_replace( self::EDITSECTION_REGEX, '', $text ); } - // If you have an old cached version of this class - sorry, you can't disable the TOC - if ( isset( $this->mTOCEnabled ) && $this->mTOCEnabled ) { + if ( $options['allowTOC'] ) { $text = str_replace( [ Parser::TOC_START, Parser::TOC_END ], '', $text ); } else { $text = preg_replace( @@ -288,6 +343,7 @@ class ParserOutput extends CacheTime { $text ); } + return $text; } @@ -339,6 +395,9 @@ class ParserOutput extends CacheTime { return $this->mSections; } + /** + * @deprecated since 1.31 Use getText() options. + */ public function getEditSectionTokens() { return $this->mEditSectionTokens; } @@ -426,6 +485,9 @@ class ParserOutput extends CacheTime { return $this->mLimitReportJSData; } + /** + * @deprecated since 1.31 Use getText() options. + */ public function getTOCEnabled() { return $this->mTOCEnabled; } @@ -454,6 +516,9 @@ class ParserOutput extends CacheTime { return wfSetVar( $this->mSections, $toc ); } + /** + * @deprecated since 1.31 Use getText() options. + */ public function setEditSectionTokens( $t ) { return wfSetVar( $this->mEditSectionTokens, $t ); } @@ -470,6 +535,9 @@ class ParserOutput extends CacheTime { return wfSetVar( $this->mTimestamp, $timestamp ); } + /** + * @deprecated since 1.31 Use getText() options. + */ public function setTOCEnabled( $flag ) { return wfSetVar( $this->mTOCEnabled, $flag ); } @@ -547,7 +615,7 @@ class ParserOutput extends CacheTime { # Replace unnecessary URL escape codes with the referenced character # This prevents spammers from hiding links from the filters - $url = parser::normalizeLinkUrl( $url ); + $url = Parser::normalizeLinkUrl( $url ); $registerExternalLink = true; if ( !$wgRegisterInternalExternals ) {