Add ability to override mb_strtoupper in Language::ucfirst
[lhc/web/wiklou.git] / maintenance / language / generateUcfirstOverrides.php
1 <?php
2 /**
3 * Generate a php file containg an array of
4 * utf8_lowercase => utf8_uppercase
5 * overrides. Takes as input two json files generated with generateUpperCharTable.php
6 * as input.
7 *
8 * Example run:
9 * # this will prepare a file to use to make hhvm's Language::ucfirst work like php7's
10 *
11 * $ php7.2 maintenance/language/generateUpperCharTable.php --outfile php7.2.json
12 * $ hhvm --php maintenance/language/generateUpperCharTable.php --outfile hhvm.json
13 * $ hhvm maintenance/language/generateUcfirstOverrides.php \
14 * --override hhvm.json --with php7.2.json --outfile test.php
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 * http://www.gnu.org/copyleft/gpl.html
30 *
31 * @file
32 * @ingroup MaintenanceLanguage
33 */
34
35 require_once __DIR__ . '/../Maintenance.php';
36
37 class GenerateUcfirstOverrides extends Maintenance {
38
39 public function __construct() {
40 parent::__construct();
41 $this->addDescription(
42 'Generates a php source file containing a definition for mb_strtoupper overrides' );
43 $this->addOption( 'outfile', 'Output file', true, true, 'o' );
44 $this->addOption( 'override', 'Char table we want to override', true, true );
45 $this->addOption( 'with', 'Char table we want to obtain', true, true );
46 }
47
48 public function execute() {
49 $outfile = $this->getOption( 'outfile' );
50 $from = $this->loadJson( $this->getOption( 'override' ) );
51 $to = $this->loadJson( $this->getOption( 'with' ) );
52 $overrides = [];
53
54 foreach ( $from as $lc => $uc ) {
55 $ref = $to[$lc] ?? null;
56 if ( $ref !== null && $ref !== $uc ) {
57 $overrides[$lc] = $uc;
58 }
59 }
60 $writer = new StaticArrayWriter();
61 file_put_contents(
62 $outfile,
63 $writer->create( $overrides, 'File created by generateUcfirstOverrides.php' )
64 );
65 }
66
67 private function loadJson( $filename ) {
68 $data = file_get_contents( $filename );
69 if ( $data === false ) {
70 $msg = sprintf( "Could not load data from file '%s'\n", $filename );
71 $this->fatalError( $msg );
72 }
73 $json = json_decode( $data );
74 if ( $result === null ) {
75 $msg = sprintf( "Invalid json in the data file %s\n", $filename );
76 $this->fatalError( $msg, 2 );
77 }
78 return $json;
79 }
80 }
81
82 $maintClass = GenerateUcfirstOverrides::class;
83 require_once RUN_MAINTENANCE_IF_MAIN;