Merge "allow xml page content or metadata dumps to target specific namespaces"
[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, $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 $data[$char] = $phpUpper;
71 }
72 }
73
74 $mappingJson = str_replace( ' ', "\t",
75 json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE )
76 ) . "\n";
77 $outputPath = '/resources/src/mediawiki.Title/phpCharToUpper.json';
78 $file = fopen( $IP . $outputPath, 'w' );
79 fwrite( $file, $mappingJson );
80
81 $this->output( count( $data ) . " differences found.\n" );
82 $this->output( "Written to $outputPath\n" );
83 }
84 }
85
86 $maintClass = GeneratePhpCharToUpperMappings::class;
87 require_once RUN_MAINTENANCE_IF_MAIN;