Reduce code duplication in MessageCache
authorAdrian Heine <adrian.heine@wikimedia.de>
Thu, 7 Apr 2016 09:56:06 +0000 (11:56 +0200)
committerAdrian Heine <adrian.heine@wikimedia.de>
Tue, 12 Apr 2016 08:26:08 +0000 (10:26 +0200)
Change-Id: I3432958c8f81b7a33079b7933e85b87ced7363fa

includes/cache/MessageCache.php

index b26dc8d..0e566ea 100644 (file)
@@ -818,84 +818,96 @@ class MessageCache {
         * @return string|bool The message, or false if not found
         */
        protected function getMessageFromFallbackChain( $lang, $lckey, $useDB ) {
-               global $wgLanguageCode, $wgContLang;
-
-               $uckey = $wgContLang->ucfirst( $lckey );
-               $langcode = $lang->getCode();
-               $message = false;
+               global $wgContLang;
 
-               // First try the requested language.
-               if ( $useDB ) {
-                       if ( $langcode === $wgLanguageCode ) {
-                               // Messages created in the content language will not have the /lang extension
-                               $message = $this->getMsgFromNamespace( $uckey, $langcode );
-                       } else {
-                               $message = $this->getMsgFromNamespace( "$uckey/$langcode", $langcode );
-                       }
-               }
+               $alreadyTried = [];
 
+                // First try the requested language.
+               $message = $this->getMessageForLang( $lang, $lckey, $useDB, $alreadyTried );
                if ( $message !== false ) {
                        return $message;
                }
 
-               // Check the CDB cache
-               $message = $lang->getMessage( $lckey );
-               if ( $message !== null ) {
-                       return $message;
-               }
+               // Now try checking the site language.
+               $message = $this->getMessageForLang( $wgContLang, $lckey, $useDB, $alreadyTried );
+               return $message;
+       }
 
-               list( $fallbackChain, $siteFallbackChain ) =
-                       Language::getFallbacksIncludingSiteLanguage( $langcode );
+       /**
+        * Given a language, try and fetch messages from that language and its fallbacks.
+        *
+        * @see MessageCache::get
+        * @param Language|StubObject $lang Preferred language
+        * @param string $lckey Lowercase key for the message (as for localisation cache)
+        * @param bool $useDB Whether to include messages from the wiki database
+        * @param bool[] $alreadyTried Contains true for each language that has been tried already
+        * @return string|bool The message, or false if not found
+        */
+       private function getMessageForLang( $lang, $lckey, $useDB, &$alreadyTried ) {
+               global $wgContLang;
+               $langcode = $lang->getCode();
 
-               // Next try checking the database for all of the fallback languages of the requested language.
+               // Try checking the database for the requested language
                if ( $useDB ) {
-                       foreach ( $fallbackChain as $code ) {
-                               if ( $code === $wgLanguageCode ) {
-                                       // Messages created in the content language will not have the /lang extension
-                                       $message = $this->getMsgFromNamespace( $uckey, $code );
-                               } else {
-                                       $message = $this->getMsgFromNamespace( "$uckey/$code", $code );
-                               }
+                       $uckey = $wgContLang->ucfirst( $lckey );
+
+                       if ( !isset( $alreadyTried[ $langcode ] ) ) {
+                               $message = $this->getMsgFromNamespace(
+                                       $this->getMessagePageName( $langcode, $uckey ),
+                                       $langcode
+                               );
 
                                if ( $message !== false ) {
-                                       // Found the message.
                                        return $message;
                                }
+                               $alreadyTried[ $langcode ] = true;
                        }
                }
 
-               // Now try checking the site language.
-               if ( $useDB ) {
-                       $message = $this->getMsgFromNamespace( $uckey, $wgLanguageCode );
-                       if ( $message !== false ) {
-                               return $message;
-                       }
-               }
-
-               $message = $wgContLang->getMessage( $lckey );
+               // Check the CDB cache
+               $message = $lang->getMessage( $lckey );
                if ( $message !== null ) {
                        return $message;
                }
 
-               // Finally try the DB for the site language's fallbacks.
+               // Try checking the database for all of the fallback languages
                if ( $useDB ) {
-                       foreach ( $siteFallbackChain as $code ) {
-                               $message = $this->getMsgFromNamespace( "$uckey/$code", $code );
-                               if ( $message === false && $code === $wgLanguageCode ) {
-                                       // Messages created in the content language will not have the /lang extension
-                                       $message = $this->getMsgFromNamespace( $uckey, $code );
+                       $fallbackChain = Language::getFallbacksFor( $langcode );
+
+                       foreach ( $fallbackChain as $code ) {
+                               if ( isset( $alreadyTried[ $code ] ) ) {
+                                       continue;
                                }
 
+                               $message = $this->getMsgFromNamespace( $this->getMessagePageName( $code, $uckey ), $code );
+
                                if ( $message !== false ) {
-                                       // Found the message.
                                        return $message;
                                }
+                               $alreadyTried[ $code ] = true;
                        }
                }
 
                return false;
        }
 
+       /**
+        * Get the message page name for a given language
+        *
+        * @param string $langcode
+        * @param string $uckey Uppercase key for the message
+        * @return string The page name
+        */
+       private function getMessagePageName( $langcode, $uckey ) {
+               global $wgLanguageCode;
+               if ( $langcode === $wgLanguageCode ) {
+                       // Messages created in the content language will not have the /lang extension
+                       return $uckey;
+               } else {
+                       return "$uckey/$langcode";
+               }
+       }
+
        /**
         * Get a message from the MediaWiki namespace, with caching. The key must
         * first be converted to two-part lang/msg form if necessary.