From: Kunal Mehta Date: Wed, 3 Dec 2014 22:12:52 +0000 (-0800) Subject: Move core message dirs from $wgMessagesDirs to LocalisationCache::getMessagesDirs() X-Git-Tag: 1.31.0-rc.0~13098^2 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/exercices/journal.php?a=commitdiff_plain;h=27d21e31177d4b98947d98976118491818e17383;p=lhc%2Fweb%2Fwiklou.git Move core message dirs from $wgMessagesDirs to LocalisationCache::getMessagesDirs() 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 --- diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index ca410884a1..be79e0bad7 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -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 diff --git a/includes/cache/LocalisationCache.php b/includes/cache/LocalisationCache.php index 2a3cd38fd7..2c908aff26 100644 --- a/includes/cache/LocalisationCache.php +++ b/includes/cache/LocalisationCache.php @@ -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' ); diff --git a/tests/phpunit/includes/cache/LocalisationCacheTest.php b/tests/phpunit/includes/cache/LocalisationCacheTest.php index 35ff919924..24b518601d 100644 --- a/tests/phpunit/includes/cache/LocalisationCacheTest.php +++ b/tests/phpunit/includes/cache/LocalisationCacheTest.php @@ -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(