MediaWiki master
generatePhpCharToUpperMappings.php
Go to the documentation of this file.
1<?php
2
13
14// @codeCoverageIgnoreStart
15require_once __DIR__ . '/../Maintenance.php';
16// @codeCoverageIgnoreEnd
17
25
26 public function __construct() {
27 parent::__construct();
28 $this->addDescription( 'Update list of upper case differences between JS and PHP.' );
29 }
30
31 public function execute() {
32 $result = Shell::command(
33 [ 'node', MW_INSTALL_PATH . '/maintenance/mediawiki.Title/generateJsToUpperCaseList.js' ]
34 )
35 // Node allocates lots of memory
36 ->limits( [ 'memory' => 1024 * 1024 ] )
37 ->execute();
38
39 if ( $result->getExitCode() !== 0 ) {
40 $this->output( $result->getStderr() );
41 return;
42 }
43
44 $jsUpperChars = json_decode( $result->getStdout() );
45 '@phan-var string[] $jsUpperChars';
46
47 $contentLanguage = $this->getServiceContainer()->getContentLanguage();
48
49 $data = [];
50 for ( $i = 0; $i <= 0x10ffff; $i++ ) {
51 if ( $i >= 0xd800 && $i <= 0xdfff ) {
52 // Skip surrogate pairs
53 continue;
54 }
55 $char = \UtfNormal\Utils::codepointToUtf8( $i );
56 $phpUpper = $contentLanguage->ucfirst( $char );
57 $jsUpper = $jsUpperChars[$i];
58 if ( $jsUpper !== $phpUpper ) {
59 if ( $char === $phpUpper ) {
60 // Optimisation: Use 0 to signal "leave character unchanged".
61 // Reduces the transfer size by ~50%. Reduces browser memory cost as well.
62 $data[$char] = 0;
63 } else {
64 $data[$char] = $phpUpper;
65 }
66 }
67 }
68
69 $mappingJson = str_replace( ' ', "\t",
70 json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE )
71 ) . "\n";
72 $outputPath = '/resources/src/mediawiki.Title/phpCharToUpper.json';
73 $file = fopen( MW_INSTALL_PATH . $outputPath, 'w' );
74 if ( !$file ) {
75 $this->fatalError( "Unable to write file \"$outputPath\"" );
76 }
77 fwrite( $file, $mappingJson );
78
79 $this->output( count( $data ) . " differences found.\n" );
80 $this->output( "Written to $outputPath\n" );
81 }
82}
83
84// @codeCoverageIgnoreStart
85$maintClass = GeneratePhpCharToUpperMappings::class;
86require_once RUN_MAINTENANCE_IF_MAIN;
87// @codeCoverageIgnoreEnd
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.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Executes shell commands.
Definition Shell.php:32