MediaWiki master
generateUcfirstOverrides.php
Go to the documentation of this file.
1<?php
36
37// @codeCoverageIgnoreStart
38require_once __DIR__ . '/../Maintenance.php';
39// @codeCoverageIgnoreEnd
40
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 avoid (e.g. future PHP)',
49 true, true, false, true );
50 $this->addOption( 'with', 'Char table we want to obtain or preserve (e.g. current PHP)', 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
79 private function loadJson( string $filename ) {
80 $data = file_get_contents( $filename );
81 if ( $data === false ) {
82 $msg = sprintf( "Could not load data from file '%s'\n", $filename );
83 $this->fatalError( $msg );
84 }
85 $json = json_decode( $data, true );
86 if ( $json === null ) {
87 $msg = sprintf( "Invalid json in the data file %s\n", $filename );
88 $this->fatalError( $msg, 2 );
89 }
90 return $json;
91 }
92}
93
94// @codeCoverageIgnoreStart
95$maintClass = GenerateUcfirstOverrides::class;
96require_once RUN_MAINTENANCE_IF_MAIN;
97// @codeCoverageIgnoreEnd
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
addDescription( $text)
Set the description text.
Format a static PHP array to be written to a file.