| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
use Wikimedia\StaticArrayWriter; |
| 36 |
|
| 37 |
|
| 38 |
require_once __DIR__ . '/../Maintenance.php'; |
| 39 |
|
| 40 |
|
| 41 |
class GenerateUcfirstOverrides extends Maintenance { |
| 42 |
|
| 43 |
public function __construct() { |
| 44 |
parent::__construct(); |
| 45 |
$this->addDescription( |
| 46 |
'Generates a php source file containing a definition for mb_strtoupper overrides' ); |
| 47 |
$this->addOption( 'outfile', 'Output file', true, true, 'o' ); |
| 48 |
$this->addOption( 'override', 'Char table we want to override', |
| 49 |
true, true, false, true ); |
| 50 |
$this->addOption( 'with', 'Char table we want to obtain', true, true ); |
| 51 |
} |
| 52 |
|
| 53 |
public function execute() { |
| 54 |
$outfile = $this->getOption( 'outfile' ); |
| 55 |
$fromTables = []; |
| 56 |
foreach ( $this->getOption( 'override' ) as $fileName ) { |
| 57 |
$fromTables[] = $this->loadJson( $fileName ); |
| 58 |
} |
| 59 |
$to = $this->loadJson( $this->getOption( 'with' ) ); |
| 60 |
$overrides = []; |
| 61 |
|
| 62 |
foreach ( $fromTables as $from ) { |
| 63 |
foreach ( $from as $lc => $uc ) { |
| 64 |
$ref = $to[$lc] ?? null; |
| 65 |
if ( $ref !== null && $ref !== $uc ) { |
| 66 |
$overrides[$lc] = $ref; |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
ksort( $overrides ); |
| 71 |
$writer = new StaticArrayWriter(); |
| 72 |
file_put_contents( |
| 73 |
$outfile, |
| 74 |
$writer->create( $overrides, 'File created by generateUcfirstOverrides.php' ) |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
private function loadJson( $filename ) { |
| 79 |
$data = file_get_contents( $filename ); |
| 80 |
if ( $data === false ) { |
| 81 |
$msg = sprintf( "Could not load data from file '%s'\n", $filename ); |
| 82 |
$this->fatalError( $msg ); |
| 83 |
} |
| 84 |
$json = json_decode( $data, true ); |
| 85 |
if ( $json === null ) { |
| 86 |
$msg = sprintf( "Invalid json in the data file %s\n", $filename ); |
| 87 |
$this->fatalError( $msg, 2 ); |
| 88 |
} |
| 89 |
return $json; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
$maintClass = GenerateUcfirstOverrides::class; |
| 95 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 96 |
|