Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 33 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| GenerateUcfirstOverrides | |
0.00% |
0 / 33 |
|
0.00% |
0 / 3 |
110 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
42 | |||
| loadJson | |
0.00% |
0 / 9 |
|
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: Prepare Language::ucfirst on newer PHP 7.4 to work like the current PHP 7.2 |
| 9 | * |
| 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 |
| 14 | * |
| 15 | * @license GPL-2.0-or-later |
| 16 | * @file |
| 17 | * @ingroup MaintenanceLanguage |
| 18 | */ |
| 19 | |
| 20 | use MediaWiki\Maintenance\Maintenance; |
| 21 | use Wikimedia\StaticArrayWriter; |
| 22 | |
| 23 | // @codeCoverageIgnoreStart |
| 24 | require_once __DIR__ . '/../Maintenance.php'; |
| 25 | // @codeCoverageIgnoreEnd |
| 26 | |
| 27 | class GenerateUcfirstOverrides extends Maintenance { |
| 28 | |
| 29 | public function __construct() { |
| 30 | parent::__construct(); |
| 31 | $this->addDescription( |
| 32 | 'Generates a php source file containing a definition for mb_strtoupper overrides' ); |
| 33 | $this->addOption( 'outfile', 'Output file', true, true, 'o' ); |
| 34 | $this->addOption( 'override', 'Char table we want to avoid (e.g. future PHP)', |
| 35 | true, true, false, true ); |
| 36 | $this->addOption( 'with', 'Char table we want to obtain or preserve (e.g. current PHP)', true, true ); |
| 37 | } |
| 38 | |
| 39 | public function execute() { |
| 40 | $outfile = $this->getOption( 'outfile' ); |
| 41 | $fromTables = []; |
| 42 | foreach ( $this->getOption( 'override' ) as $fileName ) { |
| 43 | $fromTables[] = $this->loadJson( $fileName ); |
| 44 | } |
| 45 | $to = $this->loadJson( $this->getOption( 'with' ) ); |
| 46 | $overrides = []; |
| 47 | |
| 48 | foreach ( $fromTables as $from ) { |
| 49 | foreach ( $from as $lc => $uc ) { |
| 50 | $ref = $to[$lc] ?? null; |
| 51 | if ( $ref !== null && $ref !== $uc ) { |
| 52 | $overrides[$lc] = $ref; |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | ksort( $overrides ); |
| 57 | $writer = new StaticArrayWriter(); |
| 58 | file_put_contents( |
| 59 | $outfile, |
| 60 | $writer->create( $overrides, 'File created by generateUcfirstOverrides.php' ) |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | /** @return mixed */ |
| 65 | private function loadJson( string $filename ) { |
| 66 | $data = file_get_contents( $filename ); |
| 67 | if ( $data === false ) { |
| 68 | $msg = sprintf( "Could not load data from file '%s'\n", $filename ); |
| 69 | $this->fatalError( $msg ); |
| 70 | } |
| 71 | $json = json_decode( $data, true ); |
| 72 | if ( $json === null ) { |
| 73 | $msg = sprintf( "Invalid json in the data file %s\n", $filename ); |
| 74 | $this->fatalError( $msg, 2 ); |
| 75 | } |
| 76 | return $json; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // @codeCoverageIgnoreStart |
| 81 | $maintClass = GenerateUcfirstOverrides::class; |
| 82 | require_once RUN_MAINTENANCE_IF_MAIN; |
| 83 | // @codeCoverageIgnoreEnd |