MediaWiki  master
generateUcfirstOverrides.php
Go to the documentation of this file.
1 <?php
36 
37 require_once __DIR__ . '/../Maintenance.php';
38 
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', true, true );
47  $this->addOption( 'with', 'Char table we want to obtain', true, true );
48  }
49 
50  public function execute() {
51  $outfile = $this->getOption( 'outfile' );
52  $from = $this->loadJson( $this->getOption( 'override' ) );
53  $to = $this->loadJson( $this->getOption( 'with' ) );
54  $overrides = [];
55 
56  foreach ( $from as $lc => $uc ) {
57  $ref = $to[$lc] ?? null;
58  if ( $ref !== null && $ref !== $uc ) {
59  $overrides[$lc] = $ref;
60  }
61  }
62  $writer = new StaticArrayWriter();
63  file_put_contents(
64  $outfile,
65  $writer->create( $overrides, 'File created by generateUcfirstOverrides.php' )
66  );
67  }
68 
69  private function loadJson( $filename ) {
70  $data = file_get_contents( $filename );
71  if ( $data === false ) {
72  $msg = sprintf( "Could not load data from file '%s'\n", $filename );
73  $this->fatalError( $msg );
74  }
75  $json = json_decode( $data, true );
76  if ( $json === null ) {
77  $msg = sprintf( "Invalid json in the data file %s\n", $filename );
78  $this->fatalError( $msg, 2 );
79  }
80  return $json;
81  }
82 }
83 
84 $maintClass = GenerateUcfirstOverrides::class;
85 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
GenerateUcfirstOverrides
Definition: generateUcfirstOverrides.php:39
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:480
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:323
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:53
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:241
GenerateUcfirstOverrides\execute
execute()
Do the actual work.
Definition: generateUcfirstOverrides.php:50
GenerateUcfirstOverrides\loadJson
loadJson( $filename)
Definition: generateUcfirstOverrides.php:69
Wikimedia\StaticArrayWriter
Format a static PHP array to be written to a file.
Definition: StaticArrayWriter.php:26
$maintClass
$maintClass
Definition: generateUcfirstOverrides.php:84
GenerateUcfirstOverrides\__construct
__construct()
Default constructor.
Definition: generateUcfirstOverrides.php:41
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:277