| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
use MediaWiki\Json\FormatJson; |
| 29 |
|
| 30 |
|
| 31 |
require_once __DIR__ . '/Maintenance.php'; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class GenerateJsonI18n extends Maintenance { |
| 40 |
public function __construct() { |
| 41 |
parent::__construct(); |
| 42 |
$this->addDescription( 'Build JSON messages files from a PHP messages file' ); |
| 43 |
|
| 44 |
$this->addArg( 'phpfile', 'PHP file defining a $messages array', false ); |
| 45 |
$this->addArg( 'jsondir', 'Directory to write JSON files to', false ); |
| 46 |
$this->addOption( 'extension', 'Perform default conversion on an extension', |
| 47 |
false, true ); |
| 48 |
$this->addOption( 'supplementary', 'Find supplementary i18n files in subdirs and convert those', |
| 49 |
false, false ); |
| 50 |
} |
| 51 |
|
| 52 |
public function execute() { |
| 53 |
global $IP; |
| 54 |
|
| 55 |
$phpfile = $this->getArg( 0 ); |
| 56 |
$jsondir = $this->getArg( 1 ); |
| 57 |
$extension = $this->getOption( 'extension' ); |
| 58 |
$convertSupplementaryI18nFiles = $this->hasOption( 'supplementary' ); |
| 59 |
|
| 60 |
if ( $extension ) { |
| 61 |
if ( $phpfile ) { |
| 62 |
$this->fatalError( "The phpfile is already specified, conflicts with --extension." ); |
| 63 |
} |
| 64 |
$phpfile = "$IP/extensions/$extension/$extension.i18n.php"; |
| 65 |
} |
| 66 |
|
| 67 |
if ( !$phpfile ) { |
| 68 |
$this->error( "I'm here for an argument!" ); |
| 69 |
$this->maybeHelp( true ); |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
if ( $convertSupplementaryI18nFiles ) { |
| 74 |
if ( is_readable( $phpfile ) ) { |
| 75 |
$this->transformI18nFile( $phpfile, $jsondir ); |
| 76 |
} else { |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
$this->error( "Warning: no primary i18n file was found." ); |
| 81 |
} |
| 82 |
$this->output( "Searching for supplementary i18n files...\n" ); |
| 83 |
$dir_iterator = new RecursiveDirectoryIterator( dirname( $phpfile ) ); |
| 84 |
$iterator = new RecursiveIteratorIterator( |
| 85 |
$dir_iterator, RecursiveIteratorIterator::LEAVES_ONLY ); |
| 86 |
|
| 87 |
foreach ( $iterator as $path => $fileObject ) { |
| 88 |
if ( fnmatch( "*.i18n.php", $fileObject->getFilename() ) ) { |
| 89 |
$this->output( "Converting $path.\n" ); |
| 90 |
$this->transformI18nFile( $path ); |
| 91 |
} |
| 92 |
} |
| 93 |
} else { |
| 94 |
|
| 95 |
$this->transformI18nFile( $phpfile, $jsondir ); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
public function transformI18nFile( $phpfile, $jsondir = null ) { |
| 100 |
if ( !$jsondir ) { |
| 101 |
|
| 102 |
|
| 103 |
$jsondir = dirname( $phpfile ) . "/i18n"; |
| 104 |
} |
| 105 |
if ( !is_dir( $jsondir ) ) { |
| 106 |
$this->output( "Creating directory $jsondir.\n" ); |
| 107 |
$success = mkdir( $jsondir ); |
| 108 |
if ( !$success ) { |
| 109 |
$this->fatalError( "Could not create directory $jsondir" ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
if ( !is_readable( $phpfile ) ) { |
| 114 |
$this->fatalError( "Error reading $phpfile" ); |
| 115 |
} |
| 116 |
$messages = null; |
| 117 |
include $phpfile; |
| 118 |
$phpfileContents = file_get_contents( $phpfile ); |
| 119 |
|
| 120 |
|
| 121 |
if ( !isset( $messages ) ) { |
| 122 |
$this->fatalError( "PHP file $phpfile does not define \$messages array" ); |
| 123 |
} |
| 124 |
|
| 125 |
if ( !$messages ) { |
| 126 |
$this->fatalError( "PHP file $phpfile contains an empty \$messages array. " . |
| 127 |
"Maybe it was already converted?" ); |
| 128 |
} |
| 129 |
|
| 130 |
if ( !isset( $messages['en'] ) || !is_array( $messages['en'] ) ) { |
| 131 |
$this->fatalError( "PHP file $phpfile does not set language codes" ); |
| 132 |
} |
| 133 |
|
| 134 |
foreach ( $messages as $langcode => $langmsgs ) { |
| 135 |
$authors = $this->getAuthorsFromComment( $this->findCommentBefore( |
| 136 |
"\$messages['$langcode'] =", |
| 137 |
$phpfileContents |
| 138 |
) ); |
| 139 |
|
| 140 |
$langmsgs = array_merge( |
| 141 |
[ '@metadata' => [ 'authors' => $authors ] ], |
| 142 |
$langmsgs |
| 143 |
); |
| 144 |
|
| 145 |
$jsonfile = "$jsondir/$langcode.json"; |
| 146 |
$success = file_put_contents( |
| 147 |
$jsonfile, |
| 148 |
FormatJson::encode( $langmsgs, "\t", FormatJson::ALL_OK ) . "\n" |
| 149 |
); |
| 150 |
if ( $success === false ) { |
| 151 |
$this->fatalError( "FAILED to write $jsonfile" ); |
| 152 |
} |
| 153 |
$this->output( "$jsonfile\n" ); |
| 154 |
} |
| 155 |
|
| 156 |
$this->output( |
| 157 |
"All done. To complete the conversion, please do the following:\n" . |
| 158 |
"* Add \$wgMessagesDirs['YourExtension'] = __DIR__ . '/i18n';\n" . |
| 159 |
"* Remove \$wgExtensionMessagesFiles['YourExtension']\n" . |
| 160 |
"* Delete the old PHP message file\n" . |
| 161 |
"This script no longer generates backward compatibility shims! If you need\n" . |
| 162 |
"compatibility with MediaWiki 1.22 and older, use the MediaWiki 1.23 version\n" . |
| 163 |
"of this script instead, or create a shim manually.\n" |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
protected function findCommentBefore( $needle, $haystack ) { |
| 174 |
$needlePos = strpos( $haystack, $needle ); |
| 175 |
if ( $needlePos === false ) { |
| 176 |
return ''; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
$startPos = strrpos( $haystack, '/**', $needlePos - strlen( $haystack ) ); |
| 181 |
if ( $startPos === false ) { |
| 182 |
return ''; |
| 183 |
} |
| 184 |
|
| 185 |
return substr( $haystack, $startPos, $needlePos - $startPos ); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
protected function getAuthorsFromComment( $comment ) { |
| 194 |
return preg_match_all( '/@author (.*?)$/m', $comment, $matches ) ? $matches[1] : []; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
$maintClass = GenerateJsonI18n::class; |
| 200 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 201 |
|