Merge "RevisionStoreDbTestBase, remove redundant needsDB override"
[lhc/web/wiklou.git] / includes / parser / CoreParserFunctions.php
index 42d5db7..d44ac8c 100644 (file)
@@ -98,11 +98,6 @@ class CoreParserFunctions {
                        $args = array_slice( func_get_args(), 2 );
                        $message = wfMessage( $part1, $args )
                                ->inLanguage( $parser->getOptions()->getUserLangObj() );
-                       if ( !$message->exists() ) {
-                               // When message does not exists, the message name is surrounded by angle
-                               // and can result in a tag, therefore escape the angles
-                               return $message->escaped();
-                       }
                        return [ $message->plain(), 'noparse' => false ];
                } else {
                        return [ 'found' => false ];
@@ -112,7 +107,7 @@ class CoreParserFunctions {
        /**
         * @param Parser $parser
         * @param string $date
-        * @param string $defaultPref
+        * @param string|null $defaultPref
         *
         * @return string
         */
@@ -135,14 +130,13 @@ class CoreParserFunctions {
        }
 
        public static function ns( $parser, $part1 = '' ) {
-               global $wgContLang;
                if ( intval( $part1 ) || $part1 == "0" ) {
                        $index = intval( $part1 );
                } else {
-                       $index = $wgContLang->getNsIndex( str_replace( ' ', '_', $part1 ) );
+                       $index = $parser->getContentLanguage()->getNsIndex( str_replace( ' ', '_', $part1 ) );
                }
                if ( $index !== false ) {
-                       return $wgContLang->getFormattedNsText( $index );
+                       return $parser->getContentLanguage()->getFormattedNsText( $index );
                } else {
                        return [ 'found' => false ];
                }
@@ -165,13 +159,14 @@ class CoreParserFunctions {
         *
         * @param Parser $parser
         * @param string $s The text to encode.
-        * @param string $arg (optional): The type of encoding.
+        * @param string|null $arg (optional): The type of encoding.
         * @return string
         */
        public static function urlencode( $parser, $s = '', $arg = null ) {
                static $magicWords = null;
                if ( is_null( $magicWords ) ) {
-                       $magicWords = new MagicWordArray( [ 'url_path', 'url_query', 'url_wiki' ] );
+                       $magicWords =
+                               $parser->getMagicWordFactory()->newArray( [ 'url_path', 'url_query', 'url_wiki' ] );
                }
                switch ( $magicWords->matchStartToEnd( $arg ) ) {
                        // Encode as though it's a wiki page, '_' for ' '.
@@ -196,13 +191,11 @@ class CoreParserFunctions {
        }
 
        public static function lcfirst( $parser, $s = '' ) {
-               global $wgContLang;
-               return $wgContLang->lcfirst( $s );
+               return $parser->getContentLanguage()->lcfirst( $s );
        }
 
        public static function ucfirst( $parser, $s = '' ) {
-               global $wgContLang;
-               return $wgContLang->ucfirst( $s );
+               return $parser->getContentLanguage()->ucfirst( $s );
        }
 
        /**
@@ -211,8 +204,7 @@ class CoreParserFunctions {
         * @return string
         */
        public static function lc( $parser, $s = '' ) {
-               global $wgContLang;
-               return $parser->markerSkipCallback( $s, [ $wgContLang, 'lc' ] );
+               return $parser->markerSkipCallback( $s, [ $parser->getContentLanguage(), 'lc' ] );
        }
 
        /**
@@ -221,8 +213,7 @@ class CoreParserFunctions {
         * @return string
         */
        public static function uc( $parser, $s = '' ) {
-               global $wgContLang;
-               return $parser->markerSkipCallback( $s, [ $wgContLang, 'uc' ] );
+               return $parser->markerSkipCallback( $s, [ $parser->getContentLanguage(), 'uc' ] );
        }
 
        public static function localurl( $parser, $s = '', $arg = null ) {
@@ -292,13 +283,15 @@ class CoreParserFunctions {
        /**
         * @param Parser $parser
         * @param string $num
-        * @param string $arg
+        * @param string|null $arg
         * @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' ];
@@ -391,7 +384,8 @@ class CoreParserFunctions {
 
                static $magicWords = null;
                if ( is_null( $magicWords ) ) {
-                       $magicWords = new MagicWordArray( [ 'displaytitle_noerror', 'displaytitle_noreplace' ] );
+                       $magicWords = $parser->getMagicWordFactory()->newArray(
+                               [ 'displaytitle_noerror', 'displaytitle_noreplace' ] );
                }
                $arg = $magicWords->matchStartToEnd( $uarg );
 
@@ -475,16 +469,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 +492,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 );
@@ -556,7 +562,7 @@ class CoreParserFunctions {
         * Note: function name changed to "mwnamespace" rather than "namespace"
         * to not break PHP 5.3
         * @param Parser $parser
-        * @param string $title
+        * @param string|null $title
         * @return mixed|string
         */
        public static function mwnamespace( $parser, $title = null ) {
@@ -613,7 +619,7 @@ class CoreParserFunctions {
         * Functions to get and normalize pagenames, corresponding to the magic words
         * of the same names
         * @param Parser $parser
-        * @param string $title
+        * @param string|null $title
         * @return string
         */
        public static function pagename( $parser, $title = null ) {
@@ -721,15 +727,14 @@ class CoreParserFunctions {
         * can't be called too many times per page.
         * @param Parser $parser
         * @param string $name
-        * @param string $arg1
-        * @param string $arg2
+        * @param string|null $arg1
+        * @param string|null $arg2
         * @return string
         */
        public static function pagesincategory( $parser, $name = '', $arg1 = null, $arg2 = null ) {
-               global $wgContLang;
                static $magicWords = null;
                if ( is_null( $magicWords ) ) {
-                       $magicWords = new MagicWordArray( [
+                       $magicWords = $parser->getMagicWordFactory()->newArray( [
                                'pagesincategory_all',
                                'pagesincategory_pages',
                                'pagesincategory_subcats',
@@ -739,7 +744,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 );
@@ -756,7 +761,7 @@ class CoreParserFunctions {
                if ( !$title ) { # invalid title
                        return self::formatRaw( 0, $raw, $parser->getFunctionLang() );
                }
-               $wgContLang->findVariantLink( $name, $title, true );
+               $parser->getContentLanguage()->findVariantLink( $name, $title, true );
 
                // Normalize name for cache
                $name = $title->getDBkey();
@@ -789,7 +794,7 @@ class CoreParserFunctions {
         *
         * @param Parser $parser
         * @param string $page Name of page to check (Default: empty string)
-        * @param string $raw Should number be human readable with commas or just number
+        * @param string|null $raw Should number be human readable with commas or just number
         * @return string
         */
        public static function pagesize( $parser, $page = '', $raw = null ) {
@@ -940,7 +945,8 @@ class CoreParserFunctions {
        }
 
        public static function special( $parser, $text ) {
-               list( $page, $subpage ) = SpecialPageFactory::resolveAlias( $text );
+               list( $page, $subpage ) = MediaWikiServices::getInstance()->getSpecialPageFactory()->
+                       resolveAlias( $text );
                if ( $page ) {
                        $title = SpecialPage::getTitleFor( $page, $subpage );
                        return $title->getPrefixedText();
@@ -966,7 +972,8 @@ class CoreParserFunctions {
        public static function defaultsort( $parser, $text, $uarg = '' ) {
                static $magicWords = null;
                if ( is_null( $magicWords ) ) {
-                       $magicWords = new MagicWordArray( [ 'defaultsort_noerror', 'defaultsort_noreplace' ] );
+                       $magicWords = $parser->getMagicWordFactory()->newArray(
+                               [ 'defaultsort_noerror', 'defaultsort_noreplace' ] );
                }
                $arg = $magicWords->matchStartToEnd( $uarg );
 
@@ -1141,7 +1148,7 @@ class CoreParserFunctions {
        /**
         * Get the pageid of a specified page
         * @param Parser $parser
-        * @param string $title Title to get the pageid from
+        * @param string|null $title Title to get the pageid from
         * @return int|null|string
         * @since 1.23
         */
@@ -1162,7 +1169,7 @@ class CoreParserFunctions {
                }
 
                // Check the link cache, maybe something already looked it up.
-               $linkCache = LinkCache::singleton();
+               $linkCache = MediaWikiServices::getInstance()->getLinkCache();
                $pdbk = $t->getPrefixedDBkey();
                $id = $linkCache->getGoodLinkID( $pdbk );
                if ( $id != 0 ) {
@@ -1186,7 +1193,7 @@ class CoreParserFunctions {
        /**
         * Get the id from the last revision of a specified page.
         * @param Parser $parser
-        * @param string $title Title to get the id from
+        * @param string|null $title Title to get the id from
         * @return int|null|string
         * @since 1.23
         */
@@ -1203,7 +1210,7 @@ class CoreParserFunctions {
        /**
         * Get the day from the last revision of a specified page.
         * @param Parser $parser
-        * @param string $title Title to get the day from
+        * @param string|null $title Title to get the day from
         * @return string
         * @since 1.23
         */
@@ -1220,7 +1227,7 @@ class CoreParserFunctions {
        /**
         * Get the day with leading zeros from the last revision of a specified page.
         * @param Parser $parser
-        * @param string $title Title to get the day from
+        * @param string|null $title Title to get the day from
         * @return string
         * @since 1.23
         */
@@ -1237,7 +1244,7 @@ class CoreParserFunctions {
        /**
         * Get the month with leading zeros from the last revision of a specified page.
         * @param Parser $parser
-        * @param string $title Title to get the month from
+        * @param string|null $title Title to get the month from
         * @return string
         * @since 1.23
         */
@@ -1254,7 +1261,7 @@ class CoreParserFunctions {
        /**
         * Get the month from the last revision of a specified page.
         * @param Parser $parser
-        * @param string $title Title to get the month from
+        * @param string|null $title Title to get the month from
         * @return string
         * @since 1.23
         */
@@ -1271,7 +1278,7 @@ class CoreParserFunctions {
        /**
         * Get the year from the last revision of a specified page.
         * @param Parser $parser
-        * @param string $title Title to get the year from
+        * @param string|null $title Title to get the year from
         * @return string
         * @since 1.23
         */
@@ -1288,7 +1295,7 @@ class CoreParserFunctions {
        /**
         * Get the timestamp from the last revision of a specified page.
         * @param Parser $parser
-        * @param string $title Title to get the timestamp from
+        * @param string|null $title Title to get the timestamp from
         * @return string
         * @since 1.23
         */
@@ -1305,7 +1312,7 @@ class CoreParserFunctions {
        /**
         * Get the user from the last revision of a specified page.
         * @param Parser $parser
-        * @param string $title Title to get the user from
+        * @param string|null $title Title to get the user from
         * @return string
         * @since 1.23
         */