3 * Merges in JavaScript with mwEmbed.i18n.php
9 # Abort if called from a web server
10 if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) {
11 print "This script must be run from the command line\n";
14 define( 'MEDIAWIKI', true );
15 // get the scriptLoader globals:
16 require_once( '../../jsScriptLoader.php' );
18 $mwSTART_MSG_KEY = '$messages[\'en\'] = array(';
21 $mwLangFilePath = '../languages/mwEmbed.i18n.php';
22 include_once( $mwLangFilePath );
24 function print_help(){
26 This script helps merge msgs between javascript
and php
28 -j merges javascript msgs into php
29 -p merges php msgs back into javascript
30 -q will disable screen output
and wait time
36 // get options (like override JS or override PHP)
37 if ( in_array($argv[1], array('--help', '-help', '-h', '-?')) ) {
40 $mergeToPhp = $mergeToJS = false;
42 foreach($argv as $inx => $arg){
47 }else if( $arg == '-p' ) {
49 }else if( $arg == '-q'){
59 print "Will merge *Javascript to PHP* in 3 seconds ";
62 print "Will merge *PHP to Javascript* in 3 seconds ";
64 for ( $i = 0; $i < 3; $i++
) {
71 // read in mwEmbed.i18n.php
72 $rawLangFile = file_get_contents( $mwLangFilePath );
74 $startInx = strpos( $rawLangFile, $mwSTART_MSG_KEY ) +
strlen( $mwSTART_MSG_KEY );
75 $endInx = strpos( $rawLangFile, $mwEND_MSG_KEY ) +
1;
76 if ( $startInx === false ||
$endInx === false ) {
78 print "Could not find $mwSTART_MSG_KEY or $mwEND_MSG_KEY in mwEmbed.i18n.php\n";
82 $preFile = substr( $rawLangFile, 0, $startInx );
83 $msgSet = substr( $rawLangFile, $startInx, $endInx - $startInx );
84 $postFile = substr( $rawLangFile, $endInx );
86 // build replacement from all javascript in mwEmbed
87 $path = realpath( '../../' );
90 // @@todo existing msgSet should be parsed (or we just "include" the file first)
93 $objects = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $path ), RecursiveIteratorIterator
::SELF_FIRST
);
94 foreach ( $objects as $fname => $object ) {
95 if ( substr( $fname, - 3 ) == '.js' ) {
96 $jsFileText = file_get_contents( $fname );
97 $mwPos = strpos( $fname, 'mwEmbed' ) +
7;
98 $curFileName = substr( $fname, $mwPos );
99 if ( preg_match( '/loadGM\s*\(\s*{(.*)}\s*\)\s*/siU', // @@todo fix: will break down if someone does }) in their msg text
102 $msgSet .= doJsonMerge( $matches[1] );
106 // rebuild and output to single php file if mergeToPHP is on
108 if ( file_put_contents( $mwLangFilePath, trim( $preFile ) . "\n\t" . trim( $msgSet ) . "\n" . ltrim( $postFile ) ) ) {
110 print "updated $mwLangFilePath file\n";
115 function doJsonMerge( $json_txt ) {
116 global $curFileName, $fname, $messages, $mergeToJS, $jsFileText, $showInfo;
118 $outPhp = "\n\t/*\n";
119 $outPhp .= "\t * js file: {$curFileName}\n";
120 $outPhp .= "\t */\n";
123 $doReplaceFlag = false;
125 $jmsg = json_decode( '{' . $json_txt . '}', true );
126 if ( count( $jmsg ) != 0 ) {
128 foreach ( $jmsg as $k => $v ) {
129 // check if the existing value is changed and merge and merge ->right
130 if ( isset( $messages['en'][$k] ) ) {
131 if ( $messages['en'][$k] != $v ) {
132 $doReplaceFlag = true;
134 print "'$k'does not match:\n" . $messages['en'][$k] . "\n!=\n" . $v . "\n";
136 // add the actual value: (replace new lines (not compatible json)
137 // $jsMsgAry[$k] = str_replace("\n", '\\n', $messages['en'][$k]);
138 $jsMsgAry[$k] = $messages['en'][$k];
139 $doReplaceFlag = true;
141 $outPhp .= "\t'{$k}' => '" . str_replace( '\'', '\\\'', $v ) . "',\n";
143 // merge the jsLanguage array back in and wrap the output
144 if ( $mergeToJS && $doReplaceFlag ) {
145 $json = json_encode( $jsMsgAry );
146 $json_txt = jsonReadable( $json );
147 // escape $1 for preg replace:
148 $json_txt = str_replace( '$', '\$', $json_txt );
149 // print "json:\n$json_txt \n";
150 $str = preg_replace ( '/loadGM\s*\(\s*{(.*)}\s*\)\s*/siU',
151 "loadGM(" . $json_txt . ")",
154 // print substr($str, 0, 600);
156 if ( file_put_contents( $fname, $str ) ) {
158 print "\nupdated $curFileName from php\n\n";
160 die( "Could not write to: " . $fname );
163 // return phpOut for building msgSet in outer function
168 print "Could not get any json vars from: $curFileName\n";
173 function jsonReadable( $json ) {
182 for ( $i = 0; $i < strlen( $json ); $i++
) {
192 $result .= $char . $newline . str_repeat( $tab, $tabcount );
196 $result = trim( $result ) . $newline . str_repeat( $tab, $tabcount ) . $char;
202 $result .= $char . $newline . str_repeat( $tab, $tabcount );
209 $result .= ' ' . $char . ' ';
213 $inquote = !$inquote;
217 if ( $inquote ) $ignorenext = true;