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