MediaWiki  master
generatePhpCharToUpperMappings.php
Go to the documentation of this file.
1 <?php
2 
27 
28 require_once __DIR__ . '/../Maintenance.php';
29 
37 
38  public function __construct() {
39  parent::__construct();
40  $this->addDescription( 'Update list of upper case differences between JS and PHP.' );
41  }
42 
43  public function execute() {
44  global $IP;
45 
46  $data = [];
47 
48  $result = Shell::command(
49  [ 'node', $IP . '/maintenance/mediawiki.Title/generateJsToUpperCaseList.js' ]
50  )
51  // Node allocates lots of memory
52  ->limits( [ 'memory' => 1024 * 1024 ] )
53  ->execute();
54 
55  if ( $result->getExitCode() !== 0 ) {
56  $this->output( $result->getStderr() );
57  return;
58  }
59 
60  $jsUpperChars = json_decode( $result->getStdout() );
61  '@phan-var string[] $jsUpperChars';
62 
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 = MediaWikiServices::getInstance()->getContentLanguage()->ucfirst( $char );
70  $jsUpper = $jsUpperChars[$i];
71  if ( $jsUpper !== $phpUpper ) {
72  if ( $char === $phpUpper ) {
73  // Optimisation: Use the empty string to signal "leave character unchanged".
74  // Reduces the transfer size by ~50%. Reduces browser memory cost as well.
75  $data[$char] = '';
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;
98 require_once RUN_MAINTENANCE_IF_MAIN;
GeneratePhpCharToUpperMappings
Update list of upper case differences between JS and PHP.
Definition: generatePhpCharToUpperMappings.php:36
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
MediaWiki\Shell\Shell
Executes shell commands.
Definition: Shell.php:44
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:157
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:487
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
$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:55
GeneratePhpCharToUpperMappings\__construct
__construct()
Default constructor.
Definition: generatePhpCharToUpperMappings.php:38
$maintClass
$maintClass
Definition: generatePhpCharToUpperMappings.php:97
GeneratePhpCharToUpperMappings\execute
execute()
Do the actual work.
Definition: generatePhpCharToUpperMappings.php:43
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
$IP
$IP
Definition: WebStart.php:49