Merge "Make generatePhpCharToUpperMappings.php a proper maintenance script"
[lhc/web/wiklou.git] / maintenance / mediawiki.Title / generatePhpCharToUpperMappings.php
1 <?php
2
3 /**
4 * Update list of upper case differences between JS and PHP
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup Maintenance
23 */
24
25 use MediaWiki\Shell\Shell;
26
27 require_once __DIR__ . '/../Maintenance.php';
28
29 /**
30 * Update list of upper case differences between JS and PHP
31 *
32 * @ingroup Maintenance
33 * @since 1.33
34 */
35 class GeneratePhpCharToUpperMappings extends Maintenance {
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;
44
45 $data = [];
46
47 $result = Shell::command( [ 'node', __DIR__ . '/generateJsToUpperCaseList.js' ] )
48 // Node allocates lots of memory
49 ->limits( [ 'memory' => 1024 * 1024 ] )
50 ->execute();
51
52 if ( $result->getExitcode() !== 0 ) {
53 $this->output( $result->getStderr() );
54 return;
55 }
56
57 $jsUpperChars = json_decode( $result->getStdout() );
58
59 for ( $i = 0; $i <= 0x10ffff; $i++ ) {
60 if ( $i >= 0xd800 && $i <= 0xdfff ) {
61 // Skip surrogate pairs
62 continue;
63 }
64 $char = \UtfNormal\Utils::codepointToUtf8( $i );
65 $phpUpper = $wgContLang->ucfirst( $char );
66 $jsUpper = $jsUpperChars[$i];
67 if ( $jsUpper !== $phpUpper ) {
68 $data[$char] = $phpUpper;
69 }
70 }
71
72 $this->output( str_replace( ' ', "\t",
73 json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE )
74 ) . "\n" );
75 }
76 }
77
78 $maintClass = GeneratePhpCharToUpperMappings::class;
79 require_once RUN_MAINTENANCE_IF_MAIN;