2019-03-26 13:42:29 +00:00
|
|
|
<?php
|
2019-04-10 13:46:32 +00:00
|
|
|
|
2019-03-26 13:42:29 +00:00
|
|
|
/**
|
2019-04-10 13:46:32 +00:00
|
|
|
* Update list of upper case differences between JS and PHP
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2019-03-26 13:42:29 +00:00
|
|
|
*
|
2019-04-10 13:46:32 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup Maintenance
|
2019-03-26 13:42:29 +00:00
|
|
|
*/
|
|
|
|
|
|
2020-02-23 13:25:56 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2019-04-10 13:46:32 +00:00
|
|
|
use MediaWiki\Shell\Shell;
|
2019-03-26 13:42:29 +00:00
|
|
|
|
2019-04-10 13:46:32 +00:00
|
|
|
require_once __DIR__ . '/../Maintenance.php';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update list of upper case differences between JS and PHP
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
* @since 1.33
|
|
|
|
|
*/
|
|
|
|
|
class GeneratePhpCharToUpperMappings extends Maintenance {
|
2019-03-26 13:42:29 +00:00
|
|
|
|
2019-04-10 13:46:32 +00:00
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
|
|
|
|
$this->addDescription( 'Update list of upper case differences between JS and PHP.' );
|
2019-03-26 13:42:29 +00:00
|
|
|
}
|
2019-04-10 13:46:32 +00:00
|
|
|
|
|
|
|
|
public function execute() {
|
2020-02-23 13:25:56 +00:00
|
|
|
global $IP;
|
2019-04-10 13:46:32 +00:00
|
|
|
|
|
|
|
|
$data = [];
|
|
|
|
|
|
2019-04-10 14:35:18 +00:00
|
|
|
$result = Shell::command(
|
|
|
|
|
[ 'node', $IP . '/maintenance/mediawiki.Title/generateJsToUpperCaseList.js' ]
|
|
|
|
|
)
|
2019-04-10 13:46:32 +00:00
|
|
|
// Node allocates lots of memory
|
|
|
|
|
->limits( [ 'memory' => 1024 * 1024 ] )
|
|
|
|
|
->execute();
|
|
|
|
|
|
2020-05-26 13:14:46 +00:00
|
|
|
if ( $result->getExitCode() !== 0 ) {
|
2019-04-10 13:46:32 +00:00
|
|
|
$this->output( $result->getStderr() );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$jsUpperChars = json_decode( $result->getStdout() );
|
2019-10-11 14:45:54 +00:00
|
|
|
'@phan-var string[] $jsUpperChars';
|
2019-04-10 13:46:32 +00:00
|
|
|
|
|
|
|
|
for ( $i = 0; $i <= 0x10ffff; $i++ ) {
|
|
|
|
|
if ( $i >= 0xd800 && $i <= 0xdfff ) {
|
|
|
|
|
// Skip surrogate pairs
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$char = \UtfNormal\Utils::codepointToUtf8( $i );
|
2020-02-23 13:25:56 +00:00
|
|
|
$phpUpper = MediaWikiServices::getInstance()->getContentLanguage()->ucfirst( $char );
|
2019-04-10 13:46:32 +00:00
|
|
|
$jsUpper = $jsUpperChars[$i];
|
|
|
|
|
if ( $jsUpper !== $phpUpper ) {
|
2019-09-17 18:40:03 +00:00
|
|
|
if ( $char === $phpUpper ) {
|
2021-09-21 07:43:28 +00:00
|
|
|
// Optimisation: Use 0 to signal "leave character unchanged".
|
2019-09-17 18:40:03 +00:00
|
|
|
// Reduces the transfer size by ~50%. Reduces browser memory cost as well.
|
2021-09-21 07:43:28 +00:00
|
|
|
$data[$char] = 0;
|
2019-09-17 18:40:03 +00:00
|
|
|
} else {
|
|
|
|
|
$data[$char] = $phpUpper;
|
|
|
|
|
}
|
2019-04-10 13:46:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-10 14:35:18 +00:00
|
|
|
$mappingJson = str_replace( ' ', "\t",
|
2019-04-10 13:46:32 +00:00
|
|
|
json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE )
|
2019-04-10 14:35:18 +00:00
|
|
|
) . "\n";
|
|
|
|
|
$outputPath = '/resources/src/mediawiki.Title/phpCharToUpper.json';
|
|
|
|
|
$file = fopen( $IP . $outputPath, 'w' );
|
2019-09-18 19:46:42 +00:00
|
|
|
if ( !$file ) {
|
|
|
|
|
$this->fatalError( "Unable to write file \"$IP$outputPath\"" );
|
|
|
|
|
}
|
2019-04-10 14:35:18 +00:00
|
|
|
fwrite( $file, $mappingJson );
|
|
|
|
|
|
|
|
|
|
$this->output( count( $data ) . " differences found.\n" );
|
|
|
|
|
$this->output( "Written to $outputPath\n" );
|
2019-03-26 13:42:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-10 13:46:32 +00:00
|
|
|
$maintClass = GeneratePhpCharToUpperMappings::class;
|
|
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|