MediaWiki master
generateUcfirstOverrides.php
Go to the documentation of this file.
1<?php
22
23// @codeCoverageIgnoreStart
24require_once __DIR__ . '/../Maintenance.php';
25// @codeCoverageIgnoreEnd
26
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
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;
82require_once RUN_MAINTENANCE_IF_MAIN;
83// @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.