Move core message dirs from $wgMessagesDirs to LocalisationCache::getMessagesDirs()
authorKunal Mehta <legoktm@gmail.com>
Wed, 3 Dec 2014 22:12:52 +0000 (14:12 -0800)
committerKunal Mehta <legoktm@gmail.com>
Wed, 3 Dec 2014 22:32:18 +0000 (14:32 -0800)
If $wgMessagesDirs is initially empty, we can optimize when batch loading
extensions:

    if ( !$wgMessagesDirs ) {
        $wgMessagesDirs = $cache['MessagesDirs'];
    }

With APC, this should be O(1) CPU time.

This was suggested by Tim in the code review of I7074b65d07c5.

Change-Id: I66fa907cdaafe18b74b5b9afaa8b6b1db069bea3

includes/DefaultSettings.php
includes/cache/LocalisationCache.php
tests/phpunit/includes/cache/LocalisationCacheTest.php

index ca41088..be79e0b 100644 (file)
@@ -6229,6 +6229,8 @@ $wgExtensionMessagesFiles = array();
  * en.json, de.json, etc. Extensions with messages in multiple places may specify an array of
  * message directories.
  *
+ * Message directories in core should be added to LocalisationCache::getMessagesDirs()
+ *
  * @par Simple example:
  * @code
  *    $wgMessagesDirs['Example'] = __DIR__ . '/i18n';
@@ -6244,11 +6246,7 @@ $wgExtensionMessagesFiles = array();
  * @endcode
  * @since 1.23
  */
-$wgMessagesDirs = array(
-       'core' => "$IP/languages/i18n",
-       'api' => "$IP/includes/api/i18n",
-       'oojs-ui' => "$IP/resources/lib/oojs-ui/i18n",
-);
+$wgMessagesDirs = array();
 
 /**
  * Array of files with list(s) of extension entry points to be used in
index 2a3cd38..2c908af 100644 (file)
@@ -36,7 +36,7 @@ use Cdb\Writer as CdbWriter;
  * as grammatical transformation, is done by the caller.
  */
 class LocalisationCache {
-       const VERSION = 2;
+       const VERSION = 3;
 
        /** Configuration associative array */
        private $conf;
@@ -792,6 +792,22 @@ class LocalisationCache {
                return $used;
        }
 
+       /**
+        * Gets the combined list of messages dirs from
+        * core and extensions
+        *
+        * @since 1.25
+        * @return array
+        */
+       protected function getMessagesDirs() {
+               global $wgMessagesDirs, $IP;
+               return array(
+                       'core' => "$IP/languages/i18n",
+                       'api' => "$IP/includes/api/i18n",
+                       'oojs-ui' => "$IP/resources/lib/oojs-ui/i18n",
+               ) + $wgMessagesDirs;
+       }
+
        /**
         * Load localisation data for a given language for both core and extensions
         * and save it to the persistent cache store and the process cache
@@ -799,7 +815,7 @@ class LocalisationCache {
         * @throws MWException
         */
        public function recache( $code ) {
-               global $wgExtensionMessagesFiles, $wgMessagesDirs;
+               global $wgExtensionMessagesFiles;
                wfProfileIn( __METHOD__ );
 
                if ( !$code ) {
@@ -846,6 +862,7 @@ class LocalisationCache {
                }
 
                $codeSequence = array_merge( array( $code ), $coreData['fallbackSequence'] );
+               $messageDirs = $this->getMessagesDirs();
 
                wfProfileIn( __METHOD__ . '-fallbacks' );
 
@@ -854,7 +871,7 @@ class LocalisationCache {
                        $codeSequence,
                        array_fill( 0, count( $codeSequence ), $initialData ) );
                foreach ( $wgExtensionMessagesFiles as $extension => $fileName ) {
-                       if ( isset( $wgMessagesDirs[$extension] ) ) {
+                       if ( isset( $messageDirs[$extension] ) ) {
                                # This extension has JSON message data; skip the PHP shim
                                continue;
                        }
@@ -882,7 +899,7 @@ class LocalisationCache {
                        $csData = $initialData;
 
                        # Load core messages and the extension localisations.
-                       foreach ( $wgMessagesDirs as $dirs ) {
+                       foreach ( $messageDirs as $dirs ) {
                                foreach ( (array)$dirs as $dir ) {
                                        $fileName = "$dir/$csCode.json";
                                        $data = $this->readJSONFile( $fileName );
@@ -949,6 +966,7 @@ class LocalisationCache {
 
                # Add cache dependencies for any referenced globals
                $deps['wgExtensionMessagesFiles'] = new GlobalDependency( 'wgExtensionMessagesFiles' );
+               // $wgMessagesDirs is used in LocalisationCache::getMessagesDirs()
                $deps['wgMessagesDirs'] = new GlobalDependency( 'wgMessagesDirs' );
                $deps['version'] = new ConstantDependency( 'LocalisationCache::VERSION' );
 
index 35ff919..24b5186 100644 (file)
@@ -11,14 +11,30 @@ class LocalisationCacheTest extends MediaWikiTestCase {
 
                parent::setUp();
                $this->setMwGlobals( array(
-                       'wgMessagesDirs' => array( "$IP/tests/phpunit/data/localisationcache" ),
                        'wgExtensionMessagesFiles' => array(),
                        'wgHooks' => array(),
                ) );
        }
 
+       /**
+        * @return PHPUnit_Framework_MockObject_MockObject|LocalisationCache
+        */
+       protected function getMockLocalisationCache() {
+               global $IP;
+               $lc = $this->getMockBuilder( 'LocalisationCache' )
+                       ->setConstructorArgs( array( array( 'store' => 'detect' ) ) )
+                       ->setMethods( array( 'getMessagesDirs' ) )
+                       ->getMock();
+               $lc->expects( $this->any() )->method( 'getMessagesDirs' )
+                       ->will( $this->returnValue(
+                               array( "$IP/tests/phpunit/data/localisationcache" )
+                       ) );
+
+               return $lc;
+       }
+
        public function testPuralRulesFallback() {
-               $cache = new LocalisationCache( array( 'store' => 'detect' ) );
+               $cache = $this->getMockLocalisationCache();
 
                $this->assertEquals(
                        $cache->getItem( 'ar', 'pluralRules' ),
@@ -46,7 +62,7 @@ class LocalisationCacheTest extends MediaWikiTestCase {
        }
 
        public function testRecacheFallbacks() {
-               $lc = new LocalisationCache( array( 'store' => 'detect' ) );
+               $lc = $this->getMockLocalisationCache();
                $lc->recache( 'uk' );
                $this->assertEquals(
                        array(
@@ -78,7 +94,7 @@ class LocalisationCacheTest extends MediaWikiTestCase {
                        )
                ) );
 
-               $lc = new LocalisationCache( array( 'store' => 'detect' ) );
+               $lc = $this->getMockLocalisationCache();
                $lc->recache( 'uk' );
                $this->assertEquals(
                        array(