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 global $IP;
33
34 $data = [];
35
36 $result = Shell::command(
37 [ 'node', $IP . '/maintenance/mediawiki.Title/generateJsToUpperCaseList.js' ]
38 )
39 // Node allocates lots of memory
40 ->limits( [ 'memory' => 1024 * 1024 ] )
41 ->execute();
42
43 if ( $result->getExitCode() !== 0 ) {
44 $this->output( $result->getStderr() );
45 return;
46 }
47
48 $jsUpperChars = json_decode( $result->getStdout() );
49 '@phan-var string[] $jsUpperChars';
50
51 $contentLanguage = $this->getServiceContainer()->getContentLanguage();
52 for ( $i = 0; $i <= 0x10ffff; $i++ ) {
53 if ( $i >= 0xd800 && $i <= 0xdfff ) {
54 // Skip surrogate pairs
55 continue;
56 }
57 $char = \UtfNormal\Utils::codepointToUtf8( $i );
58 $phpUpper = $contentLanguage->ucfirst( $char );
59 $jsUpper = $jsUpperChars[$i];
60 if ( $jsUpper !== $phpUpper ) {
61 if ( $char === $phpUpper ) {
62 // Optimisation: Use 0 to signal "leave character unchanged".
63 // Reduces the transfer size by ~50%. Reduces browser memory cost as well.
64 $data[$char] = 0;
65 } else {
66 $data[$char] = $phpUpper;
67 }
68 }
69 }
70
71 $mappingJson = str_replace( ' ', "\t",
72 json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE )
73 ) . "\n";
74 $outputPath = '/resources/src/mediawiki.Title/phpCharToUpper.json';
75 $file = fopen( $IP . $outputPath, 'w' );
76 if ( !$file ) {
77 $this->fatalError( "Unable to write file \"$IP$outputPath\"" );
78 }
79 fwrite( $file, $mappingJson );
80
81 $this->output( count( $data ) . " differences found.\n" );
82 $this->output( "Written to $outputPath\n" );
83 }
84}
85
86// @codeCoverageIgnoreStart
87$maintClass = GeneratePhpCharToUpperMappings::class;
88require_once RUN_MAINTENANCE_IF_MAIN;
89// @codeCoverageIgnoreEnd
if(!defined('MEDIAWIKI')) if(!defined( 'MW_ENTRY_POINT')) global $IP
Environment checks.
Definition Setup.php:90
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