MediaWiki master
generatePhpCharToUpperMappings.php
Go to the documentation of this file.
1<?php
2
26
27require_once __DIR__ . '/../Maintenance.php';
28
36
37 public function __construct() {
38 parent::__construct();
39 $this->addDescription( 'Update list of upper case differences between JS and PHP.' );
40 }
41
42 public function execute() {
43 global $IP;
44
45 $data = [];
46
47 $result = Shell::command(
48 [ 'node', $IP . '/maintenance/mediawiki.Title/generateJsToUpperCaseList.js' ]
49 )
50 // Node allocates lots of memory
51 ->limits( [ 'memory' => 1024 * 1024 ] )
52 ->execute();
53
54 if ( $result->getExitCode() !== 0 ) {
55 $this->output( $result->getStderr() );
56 return;
57 }
58
59 $jsUpperChars = json_decode( $result->getStdout() );
60 '@phan-var string[] $jsUpperChars';
61
62 $contentLanguage = $this->getServiceContainer()->getContentLanguage();
63 for ( $i = 0; $i <= 0x10ffff; $i++ ) {
64 if ( $i >= 0xd800 && $i <= 0xdfff ) {
65 // Skip surrogate pairs
66 continue;
67 }
68 $char = \UtfNormal\Utils::codepointToUtf8( $i );
69 $phpUpper = $contentLanguage->ucfirst( $char );
70 $jsUpper = $jsUpperChars[$i];
71 if ( $jsUpper !== $phpUpper ) {
72 if ( $char === $phpUpper ) {
73 // Optimisation: Use 0 to signal "leave character unchanged".
74 // Reduces the transfer size by ~50%. Reduces browser memory cost as well.
75 $data[$char] = 0;
76 } else {
77 $data[$char] = $phpUpper;
78 }
79 }
80 }
81
82 $mappingJson = str_replace( ' ', "\t",
83 json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE )
84 ) . "\n";
85 $outputPath = '/resources/src/mediawiki.Title/phpCharToUpper.json';
86 $file = fopen( $IP . $outputPath, 'w' );
87 if ( !$file ) {
88 $this->fatalError( "Unable to write file \"$IP$outputPath\"" );
89 }
90 fwrite( $file, $mappingJson );
91
92 $this->output( count( $data ) . " differences found.\n" );
93 $this->output( "Written to $outputPath\n" );
94 }
95}
96
97$maintClass = GeneratePhpCharToUpperMappings::class;
98require_once RUN_MAINTENANCE_IF_MAIN;
if(!defined( 'MEDIAWIKI')) if(ini_get('mbstring.func_overload')) if(!defined( 'MW_ENTRY_POINT')) global $IP
Environment checks.
Definition Setup.php:98
Update list of upper case differences between JS and PHP.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Executes shell commands.
Definition Shell.php:46