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 );
23 // get options (like override JS or override PHP)
29 if ( $mergeToPhp && $mergeToJS )
30 die( 'Please only set either $mergeToPhp or $mergeToJS' );
33 print "Will merge *Javascript to PHP* in 3 seconds ";
36 print "Will merge *PHP to Javascript* in 3 seconds ";
38 for ( $i = 0; $i < 3; $i++
) {
44 // read in mwEmbed.i18n.php
45 $rawLangFile = file_get_contents( $mwLangFilePath );
47 $startInx = strpos( $rawLangFile, $mwSTART_MSG_KEY ) +
strlen( $mwSTART_MSG_KEY );
48 $endInx = strpos( $rawLangFile, $mwEND_MSG_KEY ) +
1;
49 if ( $startInx === false ||
$endInx === false ) {
50 print "Could not find $mwSTART_MSG_KEY or $mwEND_MSG_KEY in mwEmbed.i18n.php\n";
54 $preFile = substr( $rawLangFile, 0, $startInx );
55 $msgSet = substr( $rawLangFile, $startInx, $endInx - $startInx );
56 $postFile = substr( $rawLangFile, $endInx );
58 // build replacement from all javascript in mwEmbed
59 $path = realpath( '../../' );
62 // @@todo existing msgSet should be parsed (or we just "include" the file first)
65 $objects = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $path ), RecursiveIteratorIterator
::SELF_FIRST
);
66 foreach ( $objects as $fname => $object ) {
67 if ( substr( $fname, -3 ) == '.js' ) {
68 $jsFileText = file_get_contents( $fname );
69 $mwPos = strpos( $fname, 'mwEmbed' ) +
7;
70 $curFileName = substr( $fname, $mwPos );
71 if ( preg_match( '/loadGM\s*\(\s*{(.*)}\s*\)\s*/siU', // @@todo fix: will break down if someone does }) in their msg text
74 $msgSet .= doJsonMerge( $matches[1] );
78 // rebuild and output to single php file if mergeToPHP is on
80 if ( file_put_contents( $mwLangFilePath, trim( $preFile ) . "\n\t" . trim( $msgSet ) . "\n" . ltrim( $postFile ) ) ) {
81 print "updated $mwLangFilePath file\n";
86 function doJsonMerge( $json_txt ) {
87 global $curFileName, $fname, $messages, $mergeToJS, $jsFileText;
90 $outPhp .= "\t * js file: {$curFileName}\n";
94 $doReplaceFlag = false;
96 $jmsg = json_decode( '{' . $json_txt . '}', true );
97 if ( count( $jmsg ) != 0 ) {
99 foreach ( $jmsg as $k => $v ) {
100 // check if the existing value is changed and merge and merge ->right
101 if ( isset( $messages['en'][$k] ) ) {
102 if ( $messages['en'][$k] != $v ) {
103 $doReplaceFlag = true;
104 print "'$k'does not match:\n" . $messages['en'][$k] . "\n!=\n" . $v . "\n";
106 // add the actual value: (replace new lines (not compatible json)
107 // $jsMsgAry[$k] = str_replace("\n", '\\n', $messages['en'][$k]);
108 $jsMsgAry[$k] = $messages['en'][$k];
109 $doReplaceFlag = true;
111 $outPhp .= "\t'{$k}' => '" . str_replace( '\'', '\\\'', $v ) . "',\n";
113 // merge the jsLanguage array back in and wrap the output
114 if ( $mergeToJS && $doReplaceFlag ) {
115 $json = json_encode( $jsMsgAry );
116 $json_txt = jsonReadable( $json );
117 // escape $1 for preg replace:
118 $json_txt = str_replace( '$', '\$', $json_txt );
119 // print "json:\n$json_txt \n";
120 $str = preg_replace ( '/loadGM\s*\(\s*{(.*)}\s*\)\s*/siU',
121 "loadGM(" . $json_txt . ")",
124 // print substr($str, 0, 600);
126 if ( file_put_contents( $fname, $str ) ) {
127 print "\nupdated $curFileName from php\n\n";
129 die( "Could not write to: " . $fname );
132 // return phpOut for building msgSet in outer function
136 print "Could not get any json vars from: $curFileName\n";
141 function jsonReadable( $json ) {
150 for ( $i = 0; $i < strlen( $json ); $i++
) {
160 $result .= $char . $newline . str_repeat( $tab, $tabcount );
164 $result = trim( $result ) . $newline . str_repeat( $tab, $tabcount ) . $char;
170 $result .= $char . $newline . str_repeat( $tab, $tabcount );
177 $result .= ' ' . $char . ' ';
181 $inquote = !$inquote;
185 if ( $inquote ) $ignorenext = true;