Merge "rest: Return a 400 for invalid render IDs"
[mediawiki.git] / maintenance / language / generateUcfirstOverrides.php
blobd6698eec9a2719fc5f87c2ea0691eb95df399a9d
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.
8 * Example: Prepare Language::ucfirst on newer PHP 7.4 to work like the current PHP 7.2
10 * $ php7.2 maintenance/language/generateUpperCharTable.php --outfile php72.json
11 * $ php7.4 maintenance/language/generateUpperCharTable.php --outfile php74.json
12 * $ php7.2 maintenance/language/generateUcfirstOverrides.php \
13 * --override php74.json --with php72.json --outfile test.php
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 * http://www.gnu.org/copyleft/gpl.html
30 * @file
31 * @ingroup MaintenanceLanguage
34 use Wikimedia\StaticArrayWriter;
36 // @codeCoverageIgnoreStart
37 require_once __DIR__ . '/../Maintenance.php';
38 // @codeCoverageIgnoreEnd
40 class GenerateUcfirstOverrides extends Maintenance {
42 public function __construct() {
43 parent::__construct();
44 $this->addDescription(
45 'Generates a php source file containing a definition for mb_strtoupper overrides' );
46 $this->addOption( 'outfile', 'Output file', true, true, 'o' );
47 $this->addOption( 'override', 'Char table we want to avoid (e.g. future PHP)',
48 true, true, false, true );
49 $this->addOption( 'with', 'Char table we want to obtain or preserve (e.g. current PHP)', true, true );
52 public function execute() {
53 $outfile = $this->getOption( 'outfile' );
54 $fromTables = [];
55 foreach ( $this->getOption( 'override' ) as $fileName ) {
56 $fromTables[] = $this->loadJson( $fileName );
58 $to = $this->loadJson( $this->getOption( 'with' ) );
59 $overrides = [];
61 foreach ( $fromTables as $from ) {
62 foreach ( $from as $lc => $uc ) {
63 $ref = $to[$lc] ?? null;
64 if ( $ref !== null && $ref !== $uc ) {
65 $overrides[$lc] = $ref;
69 ksort( $overrides );
70 $writer = new StaticArrayWriter();
71 file_put_contents(
72 $outfile,
73 $writer->create( $overrides, 'File created by generateUcfirstOverrides.php' )
77 private function loadJson( $filename ) {
78 $data = file_get_contents( $filename );
79 if ( $data === false ) {
80 $msg = sprintf( "Could not load data from file '%s'\n", $filename );
81 $this->fatalError( $msg );
83 $json = json_decode( $data, true );
84 if ( $json === null ) {
85 $msg = sprintf( "Invalid json in the data file %s\n", $filename );
86 $this->fatalError( $msg, 2 );
88 return $json;
92 // @codeCoverageIgnoreStart
93 $maintClass = GenerateUcfirstOverrides::class;
94 require_once RUN_MAINTENANCE_IF_MAIN;
95 // @codeCoverageIgnoreEnd