X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=maintenance%2Fmediawiki.Title%2FgeneratePhpCharToUpperMappings.php;h=9fc5513e3746f2307a0546ecaab84eab58097415;hp=a04958c6426267638deb71af12d271d413dd7340;hb=8c30c3704fa144f6d09cd27b127cef507d59d753;hpb=a6facc8a0a4f9b54e0cfb1e5ef6f3991de752342 diff --git a/maintenance/mediawiki.Title/generatePhpCharToUpperMappings.php b/maintenance/mediawiki.Title/generatePhpCharToUpperMappings.php index a04958c642..9fc5513e37 100755 --- a/maintenance/mediawiki.Title/generatePhpCharToUpperMappings.php +++ b/maintenance/mediawiki.Title/generatePhpCharToUpperMappings.php @@ -1,34 +1,96 @@ -#!/usr/bin/env php = 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, $IP; + + $data = []; + + $result = Shell::command( + [ 'node', $IP . '/maintenance/mediawiki.Title/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 ) { + if ( $char === $phpUpper ) { + // Optimisation: Use the empty string to signal "leave character unchanged". + // Reduces the transfer size by ~50%. Reduces browser memory cost as well. + $data[$char] = ''; + } else { + $data[$char] = $phpUpper; + } + } + } + + $mappingJson = str_replace( ' ', "\t", + json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE ) + ) . "\n"; + $outputPath = '/resources/src/mediawiki.Title/phpCharToUpper.json'; + $file = fopen( $IP . $outputPath, 'w' ); + if ( !$file ) { + $this->fatalError( "Unable to write file \"$IP$outputPath\"" ); + } + fwrite( $file, $mappingJson ); + + $this->output( count( $data ) . " differences found.\n" ); + $this->output( "Written to $outputPath\n" ); } } -echo str_replace( ' ', "\t", - json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE ) -) . "\n"; +$maintClass = GeneratePhpCharToUpperMappings::class; +require_once RUN_MAINTENANCE_IF_MAIN;