From 72ab013be01a98123e69d785b899e1b09ac87f8e Mon Sep 17 00:00:00 2001 From: Aryeh Gregor Date: Wed, 25 Jul 2018 15:14:13 +0300 Subject: [PATCH] Update CoreParserFunctions to use MagicWordFactory Bug: T200247 Change-Id: I122d8acf601581b18756a5b8d65e50953b28c21d --- includes/parser/CoreParserFunctions.php | 28 ++++++++++++++++++------- includes/parser/Parser.php | 10 +++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/includes/parser/CoreParserFunctions.php b/includes/parser/CoreParserFunctions.php index 10da0b064f..c0747f2118 100644 --- a/includes/parser/CoreParserFunctions.php +++ b/includes/parser/CoreParserFunctions.php @@ -296,9 +296,11 @@ class CoreParserFunctions { * @return string */ public static function formatnum( $parser, $num = '', $arg = null ) { - if ( self::matchAgainstMagicword( 'rawsuffix', $arg ) ) { + if ( self::matchAgainstMagicword( $parser->getMagicWordFactory(), 'rawsuffix', $arg ) ) { $func = [ $parser->getFunctionLang(), 'parseFormattedNumber' ]; - } elseif ( self::matchAgainstMagicword( 'nocommafysuffix', $arg ) ) { + } elseif ( + self::matchAgainstMagicword( $parser->getMagicWordFactory(), 'nocommafysuffix', $arg ) + ) { $func = [ $parser->getFunctionLang(), 'formatNumNoSeparators' ]; } else { $func = [ $parser->getFunctionLang(), 'formatNum' ]; @@ -475,16 +477,20 @@ class CoreParserFunctions { /** * Matches the given value against the value of given magic word * + * @param MagicWordFactory $magicWordFactory A factory to get the word from, e.g., from + * $parser->getMagicWordFactory() * @param string $magicword Magic word key * @param string $value Value to match * @return bool True on successful match */ - private static function matchAgainstMagicword( $magicword, $value ) { + private static function matchAgainstMagicword( + MagicWordFactory $magicWordFactory, $magicword, $value + ) { $value = trim( strval( $value ) ); if ( $value === '' ) { return false; } - $mwObject = MagicWord::get( $magicword ); + $mwObject = $magicWordFactory->get( $magicword ); return $mwObject->matchStartToEnd( $value ); } @@ -494,10 +500,18 @@ class CoreParserFunctions { * @param int|float $num * @param string $raw * @param Language|StubUserLang $language + * @param MagicWordFactory|null $magicWordFactory To evaluate $raw * @return string */ - public static function formatRaw( $num, $raw, $language ) { - if ( self::matchAgainstMagicword( 'rawsuffix', $raw ) ) { + public static function formatRaw( + $num, $raw, $language, MagicWordFactory $magicWordFactory = null + ) { + if ( $raw !== null && !$magicWordFactory ) { + $magicWordFactory = MediaWikiServices::getInstance()->getMagicWordFactory(); + } + if ( + $raw !== null && self::matchAgainstMagicword( $magicWordFactory, 'rawsuffix', $raw ) + ) { return $num; } else { return $language->formatNum( $num ); @@ -739,7 +753,7 @@ class CoreParserFunctions { static $cache = []; // split the given option to its variable - if ( self::matchAgainstMagicword( 'rawsuffix', $arg1 ) ) { + if ( self::matchAgainstMagicword( $parser->getMagicWordFactory(), 'rawsuffix', $arg1 ) ) { // {{pagesincategory:|raw[|type]}} $raw = $arg1; $type = $magicWords->matchStartToEnd( $arg2 ); diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index a0da0ae471..92cbcbcb89 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -957,6 +957,16 @@ class Parser { return $this->mLinkRenderer; } + /** + * Get the MagicWordFactory that this Parser is using + * + * @since 1.32 + * @return MagicWordFactory + */ + public function getMagicWordFactory() { + return $this->magicWordFactory; + } + /** * Replaces all occurrences of HTML-style comments and the given tags * in the text with a random marker and returns the next text. The output -- 2.20.1