Merge "Make generatePhpCharToUpperMappings.php a proper maintenance script"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 2 May 2019 19:34:21 +0000 (19:34 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 2 May 2019 19:34:21 +0000 (19:34 +0000)
autoload.php
maintenance/mediawiki.Title/generateJsToUpperCaseList.js
maintenance/mediawiki.Title/generatePhpCharToUpperMappings.php

index be07cea..f8e90b7 100644 (file)
@@ -563,6 +563,7 @@ $wgAutoloadLocalClasses = [
        'GenerateJsonI18n' => __DIR__ . '/maintenance/generateJsonI18n.php',
        'GenerateNormalizerDataAr' => __DIR__ . '/maintenance/language/generateNormalizerDataAr.php',
        'GenerateNormalizerDataMl' => __DIR__ . '/maintenance/language/generateNormalizerDataMl.php',
+       'GeneratePhpCharToUpperMappings' => __DIR__ . '/maintenance/mediawiki.Title/generatePhpCharToUpperMappings.php',
        'GenerateSitemap' => __DIR__ . '/maintenance/generateSitemap.php',
        'GenerateUcfirstOverrides' => __DIR__ . '/maintenance/language/generateUcfirstOverrides.php',
        'GenerateUpperCharTable' => __DIR__ . '/maintenance/language/generateUpperCharTable.php',
index fd742f6..0104ec2 100644 (file)
@@ -1,8 +1,9 @@
 /* eslint-env node, es6 */
 var i, chars = [];
 
-for ( i = 0; i < 65536; i++ ) {
-       chars.push( String.fromCharCode( i ).toUpperCase() );
+for ( i = 0; i <= 0x10ffff; i++ ) {
+       // eslint-disable-next-line no-restricted-properties
+       chars.push( String.fromCodePoint( i ).toUpperCase() );
 }
 // eslint-disable-next-line no-console
 console.log( JSON.stringify( chars ) );
index a04958c..8073c7c 100755 (executable)
@@ -1,34 +1,79 @@
-#!/usr/bin/env php
 <?php
+
 /**
- * Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
+ * Update list of upper case differences between JS and PHP
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
  *
- * Compares output of String.toUpperCase in JavaScript with
- * mb_strtoupper in PHP, and outputs a list of lower:upper
- * mappings where they differ. This is then used by Title.js
- * to provide the same normalization in the client as on
- * the server.
+ * @file
+ * @ingroup Maintenance
  */
 
-$data = [];
+use MediaWiki\Shell\Shell;
 
-// phpcs:disable MediaWiki.Usage.ForbiddenFunctions.exec
-$jsUpperChars = json_decode( exec( 'node generateJsToUpperCaseList.js' ) );
-// phpcs:enable MediaWiki.Usage.ForbiddenFunctions.exec
+require_once __DIR__ . '/../Maintenance.php';
+
+/**
+ * Update list of upper case differences between JS and PHP
+ *
+ * @ingroup Maintenance
+ * @since 1.33
+ */
+class GeneratePhpCharToUpperMappings extends Maintenance {
 
-for ( $i = 0; $i < 65536; $i++ ) {
-       if ( $i >= 0xd800 && $i <= 0xdfff ) {
-               // Skip surrogate pairs
-               continue;
+       public function __construct() {
+               parent::__construct();
+               $this->addDescription( 'Update list of upper case differences between JS and PHP.' );
        }
-       $char = mb_convert_encoding( '&#' . $i . ';', 'UTF-8', 'HTML-ENTITIES' );
-       $phpUpper = mb_strtoupper( $char );
-       $jsUpper = $jsUpperChars[$i];
-       if ( $jsUpper !== $phpUpper ) {
-               $data[$char] = $phpUpper;
+
+       public function execute() {
+               global $wgContLang;
+
+               $data = [];
+
+               $result = Shell::command( [ 'node', __DIR__ . '/generateJsToUpperCaseList.js' ] )
+                       // Node allocates lots of memory
+                       ->limits( [ 'memory' => 1024 * 1024 ] )
+                       ->execute();
+
+               if ( $result->getExitcode() !== 0 ) {
+                       $this->output( $result->getStderr() );
+                       return;
+               }
+
+               $jsUpperChars = json_decode( $result->getStdout() );
+
+               for ( $i = 0; $i <= 0x10ffff; $i++ ) {
+                       if ( $i >= 0xd800 && $i <= 0xdfff ) {
+                               // Skip surrogate pairs
+                               continue;
+                       }
+                       $char = \UtfNormal\Utils::codepointToUtf8( $i );
+                       $phpUpper = $wgContLang->ucfirst( $char );
+                       $jsUpper = $jsUpperChars[$i];
+                       if ( $jsUpper !== $phpUpper ) {
+                               $data[$char] = $phpUpper;
+                       }
+               }
+
+               $this->output( str_replace( '    ', "\t",
+                       json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE )
+               ) . "\n" );
        }
 }
 
-echo str_replace( '    ', "\t",
-       json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE )
-) . "\n";
+$maintClass = GeneratePhpCharToUpperMappings::class;
+require_once RUN_MAINTENANCE_IF_MAIN;