MediaWiki master
generateUcfirstOverrides.php
Go to the documentation of this file.
1<?php
36
37require_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',
47 true, true, false, true );
48 $this->addOption( 'with', 'Char table we want to obtain', true, true );
49 }
50
51 public function execute() {
52 $outfile = $this->getOption( 'outfile' );
53 $fromTables = [];
54 foreach ( $this->getOption( 'override' ) as $fileName ) {
55 $fromTables[] = $this->loadJson( $fileName );
56 }
57 $to = $this->loadJson( $this->getOption( 'with' ) );
58 $overrides = [];
59
60 foreach ( $fromTables as $from ) {
61 foreach ( $from as $lc => $uc ) {
62 $ref = $to[$lc] ?? null;
63 if ( $ref !== null && $ref !== $uc ) {
64 $overrides[$lc] = $ref;
65 }
66 }
67 }
68 ksort( $overrides );
69 $writer = new StaticArrayWriter();
70 file_put_contents(
71 $outfile,
72 $writer->create( $overrides, 'File created by generateUcfirstOverrides.php' )
73 );
74 }
75
76 private function loadJson( $filename ) {
77 $data = file_get_contents( $filename );
78 if ( $data === false ) {
79 $msg = sprintf( "Could not load data from file '%s'\n", $filename );
80 $this->fatalError( $msg );
81 }
82 $json = json_decode( $data, true );
83 if ( $json === null ) {
84 $msg = sprintf( "Invalid json in the data file %s\n", $filename );
85 $this->fatalError( $msg, 2 );
86 }
87 return $json;
88 }
89}
90
91$maintClass = GenerateUcfirstOverrides::class;
92require_once RUN_MAINTENANCE_IF_MAIN;
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addDescription( $text)
Set the description text.
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.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Format a static PHP array to be written to a file.