Update MagicWord to use ContentLanguage
authorAryeh Gregor <ayg@aryeh.name>
Thu, 26 Jul 2018 12:21:31 +0000 (15:21 +0300)
committerLegoktm <legoktm@member.fsf.org>
Wed, 1 Aug 2018 10:39:33 +0000 (10:39 +0000)
Bug: T200246
Change-Id: I9f17142d25bc851c973bebe27e6cbc5be8b70729

includes/MagicWord.php
includes/MagicWordArray.php
includes/MagicWordFactory.php

index a193c9f..4420d1d 100644 (file)
@@ -90,6 +90,9 @@ class MagicWord {
        /** @var bool */
        private $mFound = false;
 
+       /** @var Language */
+       private $contLang;
+
        /**#@-*/
 
        /**
@@ -100,11 +103,17 @@ class MagicWord {
         * @param string|null $id The internal name of the magic word
         * @param string[]|string $syn synonyms for the magic word
         * @param bool $cs If magic word is case sensitive
+        * @param Language|null $contLang Content language
         */
-       public function __construct( $id = null, $syn = [], $cs = false ) {
+       public function __construct( $id = null, $syn = [], $cs = false, Language $contLang = null ) {
                $this->mId = $id;
                $this->mSynonyms = (array)$syn;
                $this->mCaseSensitive = $cs;
+               $this->contLang = $contLang;
+
+               if ( !$contLang ) {
+                       $this->contLang = MediaWikiServices::getInstance()->getContentLanguage();
+               }
        }
 
        /**
@@ -166,9 +175,8 @@ class MagicWord {
         * @throws MWException
         */
        public function load( $id ) {
-               global $wgContLang;
                $this->mId = $id;
-               $wgContLang->getMagic( $this );
+               $this->contLang->getMagic( $this );
                if ( !$this->mSynonyms ) {
                        $this->mSynonyms = [ 'brionmademeputthishere' ];
                        throw new MWException( "Error: invalid magic word '$id'" );
@@ -486,9 +494,8 @@ class MagicWord {
         * @param string $value
         */
        public function addToArray( &$array, $value ) {
-               global $wgContLang;
                foreach ( $this->mSynonyms as $syn ) {
-                       $array[$wgContLang->lc( $syn )] = $value;
+                       $array[$this->contLang->lc( $syn )] = $value;
                }
        }
 
index 20ac680..fde32ce 100644 (file)
@@ -81,14 +81,13 @@ class MagicWordArray {
         */
        public function getHash() {
                if ( is_null( $this->hash ) ) {
-                       global $wgContLang;
                        $this->hash = [ 0 => [], 1 => [] ];
                        foreach ( $this->names as $name ) {
                                $magic = $this->factory->get( $name );
                                $case = intval( $magic->isCaseSensitive() );
                                foreach ( $magic->getSynonyms() as $syn ) {
                                        if ( !$case ) {
-                                               $syn = $wgContLang->lc( $syn );
+                                               $syn = $this->factory->getContentLanguage()->lc( $syn );
                                        }
                                        $this->hash[$case][$syn] = $name;
                                }
@@ -268,8 +267,7 @@ class MagicWordArray {
                if ( isset( $hash[1][$text] ) ) {
                        return $hash[1][$text];
                }
-               global $wgContLang;
-               $lc = $wgContLang->lc( $text );
+               $lc = $this->factory->getContentLanguage()->lc( $text );
                if ( isset( $hash[0][$lc] ) ) {
                        return $hash[0][$lc];
                }
index 15a7678..e62716d 100644 (file)
@@ -192,8 +192,22 @@ class MagicWordFactory {
        /** @var MagicWordArray */
        private $mDoubleUnderscoreArray = null;
 
+       /** @var Language */
+       private $contLang;
+
        /**#@-*/
 
+       /**
+        * @param Language $contLang Content language
+        */
+       public function __construct( Language $contLang ) {
+               $this->contLang = $contLang;
+       }
+
+       public function getContentLanguage() {
+               return $this->contLang;
+       }
+
        /**
         * Factory: creates an object representing an ID
         *
@@ -203,7 +217,7 @@ class MagicWordFactory {
         */
        public function get( $id ) {
                if ( !isset( $this->mObjects[$id] ) ) {
-                       $mw = new MagicWord();
+                       $mw = new MagicWord( null, [], false, $this->contLang );
                        $mw->load( $id );
                        $this->mObjects[$id] = $mw;
                }