Debian Codegraph @

maintenance/mediawiki.Title/generatePhpCharToUpperMappings.php

raw symbols SCIP
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\Maintenance\Maintenance;
26 use MediaWiki\Shell\Shell;
27
28 // @codeCoverageIgnoreStart
29 require_once __DIR__ . '/../Maintenance.php';
30 // @codeCoverageIgnoreEnd
31
32 /**
33 * Update list of upper case differences between JS and PHP
34 *
35 * @ingroup Maintenance
36 * @since 1.33
37 */
38 class GeneratePhpCharToUpperMappings extends Maintenance {
39
40 public function __construct() {
41 parent::__construct();
42 $this->addDescription( 'Update list of upper case differences between JS and PHP.' );
43 }
44
45 public function execute() {
46 global $IP;
47
48 $data = [];
49
50 $result = Shell::command(
51 [ 'node', $IP . '/maintenance/mediawiki.Title/generateJsToUpperCaseList.js' ]
52 )
53 // Node allocates lots of memory
54 ->limits( [ 'memory' => 1024 * 1024 ] )
55 ->execute();
56
57 if ( $result->getExitCode() !== 0 ) {
58 $this->output( $result->getStderr() );
59 return;
60 }
61
62 $jsUpperChars = json_decode( $result->getStdout() );
63 '@phan-var string[] $jsUpperChars';
64
65 $contentLanguage = $this->getServiceContainer()->getContentLanguage();
66 for ( $i = 0; $i <= 0x10ffff; $i++ ) {
67 if ( $i >= 0xd800 && $i <= 0xdfff ) {
68 // Skip surrogate pairs
69 continue;
70 }
71 $char = \UtfNormal\Utils::codepointToUtf8( $i );
72 $phpUpper = $contentLanguage->ucfirst( $char );
73 $jsUpper = $jsUpperChars[$i];
74 if ( $jsUpper !== $phpUpper ) {
75 if ( $char === $phpUpper ) {
76 // Optimisation: Use 0 to signal "leave character unchanged".
77 // Reduces the transfer size by ~50%. Reduces browser memory cost as well.
78 $data[$char] = 0;
79 } else {
80 $data[$char] = $phpUpper;
81 }
82 }
83 }
84
85 $mappingJson = str_replace( ' ', "\t",
86 json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE )
87 ) . "\n";
88 $outputPath = '/resources/src/mediawiki.Title/phpCharToUpper.json';
89 $file = fopen( $IP . $outputPath, 'w' );
90 if ( !$file ) {
91 $this->fatalError( "Unable to write file \"$IP$outputPath\"" );
92 }
93 fwrite( $file, $mappingJson );
94
95 $this->output( count( $data ) . " differences found.\n" );
96 $this->output( "Written to $outputPath\n" );
97 }
98 }
99
100 // @codeCoverageIgnoreStart
101 $maintClass = GeneratePhpCharToUpperMappings::class;
102 require_once RUN_MAINTENANCE_IF_MAIN;
103 // @codeCoverageIgnoreEnd