MediaWiki  1.34.0
generatePhpCharToUpperMappings.php
Go to the documentation of this file.
1 <?php
2 
26 
27 require_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 $wgContLang, $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 
61  for ( $i = 0; $i <= 0x10ffff; $i++ ) {
62  if ( $i >= 0xd800 && $i <= 0xdfff ) {
63  // Skip surrogate pairs
64  continue;
65  }
66  $char = \UtfNormal\Utils::codepointToUtf8( $i );
67  $phpUpper = $wgContLang->ucfirst( $char );
68  $jsUpper = $jsUpperChars[$i];
69  if ( $jsUpper !== $phpUpper ) {
70  if ( $char === $phpUpper ) {
71  // Optimisation: Use the empty string to signal "leave character unchanged".
72  // Reduces the transfer size by ~50%. Reduces browser memory cost as well.
73  $data[$char] = '';
74  } else {
75  $data[$char] = $phpUpper;
76  }
77  }
78  }
79 
80  $mappingJson = str_replace( ' ', "\t",
81  json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE )
82  ) . "\n";
83  $outputPath = '/resources/src/mediawiki.Title/phpCharToUpper.json';
84  $file = fopen( $IP . $outputPath, 'w' );
85  if ( !$file ) {
86  $this->fatalError( "Unable to write file \"$IP$outputPath\"" );
87  }
88  fwrite( $file, $mappingJson );
89 
90  $this->output( count( $data ) . " differences found.\n" );
91  $this->output( "Written to $outputPath\n" );
92  }
93 }
94 
95 $maintClass = GeneratePhpCharToUpperMappings::class;
96 require_once RUN_MAINTENANCE_IF_MAIN;
GeneratePhpCharToUpperMappings
Update list of upper case differences between JS and PHP.
Definition: generatePhpCharToUpperMappings.php:35
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
MediaWiki\Shell\Shell
Executes shell commands.
Definition: Shell.php:44
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:504
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
GeneratePhpCharToUpperMappings\__construct
__construct()
Default constructor.
Definition: generatePhpCharToUpperMappings.php:37
$IP
$IP
Definition: update.php:3
$maintClass
$maintClass
Definition: generatePhpCharToUpperMappings.php:95
GeneratePhpCharToUpperMappings\execute
execute()
Do the actual work.
Definition: generatePhpCharToUpperMappings.php:42
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
$wgContLang
$wgContLang
Definition: Setup.php:801