jenkins-bot has submitted this change and it was merged.

Change subject: JSON i18n shim: Only register LocalisationCacheRecache handler 
once
......................................................................


JSON i18n shim: Only register LocalisationCacheRecache handler once

Because LocalisationCache::recache() includes PHP shims each time it is
called (e.g. for a different language), in such cases as automated testing,
the shims could end up registering handlers many times instead of only
once, leading to a rather significant, progressively worse slowdown.

Checking whether the shim has already been loaded, and if so, not
registering the handler again, avoids this performance problem.

I also made the shim compatible with PHP 5.2 (minimum PHP version for
MW 1.17 - 1.19) by avoiding closures and __DIR__, and I added a
--shim-only option to generateJsonI18n.php to allow regenerating
existing shims.

Bug: 63928
Change-Id: I3bb39e7d2bb094873061b3b2adf7066bf26c1b71
---
M maintenance/generateJsonI18n.php
1 file changed, 61 insertions(+), 15 deletions(-)

Approvals:
  Nikerabbit: Looks good to me, approved
  Siebrand: Looks good to me, but someone else must approve
  jenkins-bot: Verified



diff --git a/maintenance/generateJsonI18n.php b/maintenance/generateJsonI18n.php
index 8dc89ed..630c579 100644
--- a/maintenance/generateJsonI18n.php
+++ b/maintenance/generateJsonI18n.php
@@ -37,14 +37,26 @@
                parent::__construct();
                $this->mDescription = "Build JSON messages files from a PHP 
messages file";
                $this->addArg( 'phpfile', 'PHP file defining a $messages 
array', true );
-               $this->addArg( 'jsondir', 'Directory to write JSON files to', 
true );
+               $this->addArg( 'jsondir', 'Directory to write JSON files to. ' .
+                       'Required unless <phpfile> exists and --shim-only is 
specified', false );
                $this->addOption( 'langcode', 'Language code; only needed for 
converting core i18n files',
                        false, true );
+               $this->addOption( 'shim-only', 'Only create or update the 
backward-compatibility shim' );
        }
 
        public function execute() {
                $phpfile = $this->getArg( 0 );
                $jsondir = $this->getArg( 1 );
+
+               if ( $this->hasOption( 'shim-only' ) ) {
+                       $this->shimOnly( $phpfile, $jsondir );
+                       return;
+               }
+
+               if ( $jsondir === null ) {
+                       $this->error( 'Argument [jsondir] is required unless 
--shim-only is specified.' );
+                       $this->maybeHelp( true );
+               }
 
                if ( !is_readable( $phpfile ) ) {
                        $this->error( "Error reading $phpfile\n", 1 );
@@ -100,6 +112,35 @@
                $this->output( "Also add \$wgMessagesDirs['YourExtension'] = 
__DIR__ . '/i18n';\n" );
        }
 
+       protected function shimOnly( $phpfile, $jsondir ) {
+               if ( file_exists( $phpfile ) ) {
+                       if ( !is_readable( $phpfile ) ) {
+                               $this->error( "Error reading $phpfile\n", 1 );
+                       }
+
+                       $phpfileContents = file_get_contents( $phpfile );
+                       $m = array();
+                       if ( !preg_match( '!"/([^"$]+)/\$csCode.json";!', 
$phpfileContents, $m ) ) {
+                               $this->error( "Cannot recognize $phpfile as a 
shim.\n", 1 );
+                       }
+
+                       if ( $jsondir === null ) {
+                               $jsondir = $m[1];
+                       }
+
+                       $this->output( "Updating existing shim $phpfile\n" );
+               } elseif ( $jsondir === null ) {
+                       $this->error( "$phpfile does not exist.\n" .
+                               "Argument [jsondir] is required in order to 
create a new shim.\n", 1 );
+               } else {
+                       $this->output( "Creating new shim $phpfile\n" );
+               }
+
+               $shim = $this->doShim( $jsondir );
+               file_put_contents( $phpfile, $shim );
+               $this->output( "All done.\n" );
+       }
+
        protected function doShim( $jsondir ) {
                $shim = <<<'PHP'
 <?php
@@ -115,29 +156,34 @@
  * This shim maintains compatibility back to MediaWiki 1.17.
  */
 $messages = array();
-$GLOBALS['wgHooks']['LocalisationCacheRecache'][] = function ( $cache, $code, 
&$cachedData ) {
-       $codeSequence = array_merge( array( $code ), 
$cachedData['fallbackSequence'] );
-       foreach ( $codeSequence as $csCode ) {
-               $fileName = __DIR__ . "/{{OUT}}/$csCode.json";
-               if ( is_readable( $fileName ) ) {
-                       $data = FormatJson::decode( file_get_contents( 
$fileName ), true );
-                       foreach ( array_keys( $data ) as $key ) {
-                               if ( $key === '' || $key[0] === '@' ) {
-                                       unset( $data[$key] );
+if ( !function_exists( '{{FUNC}}' ) ) {
+       function {{FUNC}}( $cache, $code, &$cachedData ) {
+               $codeSequence = array_merge( array( $code ), 
$cachedData['fallbackSequence'] );
+               foreach ( $codeSequence as $csCode ) {
+                       $fileName = dirname( __FILE__ ) . 
"/{{OUT}}/$csCode.json";
+                       if ( is_readable( $fileName ) ) {
+                               $data = FormatJson::decode( file_get_contents( 
$fileName ), true );
+                               foreach ( array_keys( $data ) as $key ) {
+                                       if ( $key === '' || $key[0] === '@' ) {
+                                               unset( $data[$key] );
+                                       }
                                }
+                               $cachedData['messages'] = array_merge( $data, 
$cachedData['messages'] );
                        }
-                       $cachedData['messages'] = array_merge( $data, 
$cachedData['messages'] );
-               }
 
-               $cachedData['deps'][] = new FileDependency( $fileName );
+                       $cachedData['deps'][] = new FileDependency( $fileName );
+               }
+               return true;
        }
-       return true;
-};
+
+       $GLOBALS['wgHooks']['LocalisationCacheRecache'][] = '{{FUNC}}';
+}
 
 PHP;
 
                $jsondir = str_replace( '\\', '/', $jsondir );
                $shim = str_replace( '{{OUT}}', $jsondir, $shim );
+               $shim = str_replace( '{{FUNC}}', 'wfJsonI18nShim' . 
wfRandomString( 16 ), $shim );
                return $shim;
        }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/125706
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I3bb39e7d2bb094873061b3b2adf7066bf26c1b71
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: PleaseStand <[email protected]>
Gerrit-Reviewer: Adrian Lang <[email protected]>
Gerrit-Reviewer: Catrope <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: Nikerabbit <[email protected]>
Gerrit-Reviewer: Parent5446 <[email protected]>
Gerrit-Reviewer: PleaseStand <[email protected]>
Gerrit-Reviewer: Siebrand <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to