MediaWiki REL1_28
generateJsonI18n.php
Go to the documentation of this file.
1<?php
2
28require_once __DIR__ . '/Maintenance.php';
29
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription( 'Build JSON messages files from a PHP messages file' );
39
40 $this->addArg( 'phpfile', 'PHP file defining a $messages array', false );
41 $this->addArg( 'jsondir', 'Directory to write JSON files to', false );
42 $this->addOption( 'extension', 'Perform default conversion on an extension',
43 false, true );
44 $this->addOption( 'supplementary', 'Find supplementary i18n files in subdirs and convert those',
45 false, false );
46 }
47
48 public function execute() {
49 global $IP;
50
51 $phpfile = $this->getArg( 0 );
52 $jsondir = $this->getArg( 1 );
53 $extension = $this->getOption( 'extension' );
54 $convertSupplementaryI18nFiles = $this->hasOption( 'supplementary' );
55
56 if ( $extension ) {
57 if ( $phpfile ) {
58 $this->error( "The phpfile is already specified, conflicts with --extension.", 1 );
59 }
60 $phpfile = "$IP/extensions/$extension/$extension.i18n.php";
61 }
62
63 if ( !$phpfile ) {
64 $this->error( "I'm here for an argument!" );
65 $this->maybeHelp( true );
66 // dies.
67 }
68
69 if ( $convertSupplementaryI18nFiles ) {
70 if ( is_readable( $phpfile ) ) {
71 $this->transformI18nFile( $phpfile, $jsondir );
72 } else {
73 // This is non-fatal because we might want to continue searching for
74 // i18n files in subdirs even if the extension does not include a
75 // primary i18n.php.
76 $this->error( "Warning: no primary i18n file was found." );
77 }
78 $this->output( "Searching for supplementary i18n files...\n" );
79 $dir_iterator = new RecursiveDirectoryIterator( dirname( $phpfile ) );
80 $iterator = new RecursiveIteratorIterator(
81 $dir_iterator, RecursiveIteratorIterator::LEAVES_ONLY );
82 foreach ( $iterator as $path => $fileObject ) {
83 if ( fnmatch( "*.i18n.php", $fileObject->getFilename() ) ) {
84 $this->output( "Converting $path.\n" );
85 $this->transformI18nFile( $path );
86 }
87 }
88 } else {
89 // Just convert the primary i18n file.
90 $this->transformI18nFile( $phpfile, $jsondir );
91 }
92 }
93
94 public function transformI18nFile( $phpfile, $jsondir = null ) {
95 if ( !$jsondir ) {
96 // Assume the json directory should be in the same directory as the
97 // .i18n.php file.
98 $jsondir = dirname( $phpfile ) . "/i18n";
99 }
100 if ( !is_dir( $jsondir ) ) {
101 $this->output( "Creating directory $jsondir.\n" );
102 $success = mkdir( $jsondir );
103 if ( !$success ) {
104 $this->error( "Could not create directory $jsondir", 1 );
105 }
106 }
107
108 if ( !is_readable( $phpfile ) ) {
109 $this->error( "Error reading $phpfile", 1 );
110 }
111 include $phpfile;
112 $phpfileContents = file_get_contents( $phpfile );
113
114 if ( !isset( $messages ) ) {
115 $this->error( "PHP file $phpfile does not define \$messages array", 1 );
116 }
117
118 if ( !$messages ) {
119 $this->error( "PHP file $phpfile contains an empty \$messages array. " .
120 "Maybe it was already converted?", 1 );
121 }
122
123 if ( !isset( $messages['en'] ) || !is_array( $messages['en'] ) ) {
124 $this->error( "PHP file $phpfile does not set language codes", 1 );
125 }
126
127 foreach ( $messages as $langcode => $langmsgs ) {
128 $authors = $this->getAuthorsFromComment( $this->findCommentBefore(
129 "\$messages['$langcode'] =",
130 $phpfileContents
131 ) );
132 // Make sure the @metadata key is the first key in the output
133 $langmsgs = array_merge(
134 [ '@metadata' => [ 'authors' => $authors ] ],
135 $langmsgs
136 );
137
138 $jsonfile = "$jsondir/$langcode.json";
139 $success = file_put_contents(
140 $jsonfile,
141 FormatJson::encode( $langmsgs, "\t", FormatJson::ALL_OK ) . "\n"
142 );
143 if ( $success === false ) {
144 $this->error( "FAILED to write $jsonfile", 1 );
145 }
146 $this->output( "$jsonfile\n" );
147 }
148
149 $this->output(
150 "All done. To complete the conversion, please do the following:\n" .
151 "* Add \$wgMessagesDirs['YourExtension'] = __DIR__ . '/i18n';\n" .
152 "* Remove \$wgExtensionMessagesFiles['YourExtension']\n" .
153 "* Delete the old PHP message file\n" .
154 "This script no longer generates backward compatibility shims! If you need\n" .
155 "compatibility with MediaWiki 1.22 and older, use the MediaWiki 1.23 version\n" .
156 "of this script instead, or create a shim manually.\n"
157 );
158 }
159
166 protected function findCommentBefore( $needle, $haystack ) {
167 $needlePos = strpos( $haystack, $needle );
168 if ( $needlePos === false ) {
169 return '';
170 }
171 // Need to pass a negative offset to strrpos() so it'll search backwards from the
172 // offset
173 $startPos = strrpos( $haystack, '
186 protected function getAuthorsFromComment( $comment ) {
187 $matches = null;
188 preg_match_all( '/@author (.*?)$/m', $comment, $matches );
189
190 return $matches && $matches[1] ? $matches[1] : [];
191 }
192}
193
194$maintClass = "GenerateJsonI18n";
195require_once RUN_MAINTENANCE_IF_MAIN;
$messages
$IP
Definition WebStart.php:58
Maintenance script to generate JSON i18n files from a PHP i18n file.
__construct()
Default constructor.
getAuthorsFromComment( $comment)
Get an array of author names from a documentation comment containing.
transformI18nFile( $phpfile, $jsondir=null)
findCommentBefore( $needle, $haystack)
Find the documentation comment immediately before a given search string.
execute()
Do the actual work.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
addArg( $arg, $description, $required=true)
Add some args that are needed.
hasOption( $name)
Checks to see if a particular param exists.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
maybeHelp( $force=false)
Maybe show the help.
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.
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling output() to send it all. It could be easily changed to send incrementally if that becomes useful
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object to manipulate or replace but no entry for that model exists in $wgContentHandlers if desired whether it is OK to use $contentModel on $title Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok inclusive false for true for descending in case the handler function wants to provide a converted Content object Note that $result getContentModel() must return $toModel. 'CustomEditor' $rcid is used in generating this variable which contains information about the new such as the revision s author
Definition hooks.txt:1206
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37