Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
GenerateUcfirstOverrides
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 3
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
42
 loadJson
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Generate a php file containing 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
35use Wikimedia\StaticArrayWriter;
36
37require_once __DIR__ . '/../Maintenance.php';
38
39class GenerateUcfirstOverrides extends Maintenance {
40
41    public function __construct() {
42        parent::__construct();
43        $this->addDescription(
44            'Generates a php source file containing a definition for mb_strtoupper overrides' );
45        $this->addOption( 'outfile', 'Output file', true, true, 'o' );
46        $this->addOption( 'override', 'Char table we want to override',
47            true, true, false, true );
48        $this->addOption( 'with', 'Char table we want to obtain', true, true );
49    }
50
51    public function execute() {
52        $outfile = $this->getOption( 'outfile' );
53        $fromTables = [];
54        foreach ( $this->getOption( 'override' ) as $fileName ) {
55            $fromTables[] = $this->loadJson( $fileName );
56        }
57        $to = $this->loadJson( $this->getOption( 'with' ) );
58        $overrides = [];
59
60        foreach ( $fromTables as $from ) {
61            foreach ( $from as $lc => $uc ) {
62                $ref = $to[$lc] ?? null;
63                if ( $ref !== null && $ref !== $uc ) {
64                    $overrides[$lc] = $ref;
65                }
66            }
67        }
68        ksort( $overrides );
69        $writer = new StaticArrayWriter();
70        file_put_contents(
71            $outfile,
72            $writer->create( $overrides, 'File created by generateUcfirstOverrides.php' )
73        );
74    }
75
76    private function loadJson( $filename ) {
77        $data = file_get_contents( $filename );
78        if ( $data === false ) {
79            $msg = sprintf( "Could not load data from file '%s'\n", $filename );
80            $this->fatalError( $msg );
81        }
82        $json = json_decode( $data, true );
83        if ( $json === null ) {
84            $msg = sprintf( "Invalid json in the data file %s\n", $filename );
85            $this->fatalError( $msg, 2 );
86        }
87        return $json;
88    }
89}
90
91$maintClass = GenerateUcfirstOverrides::class;
92require_once RUN_MAINTENANCE_IF_MAIN;