58b8d7184ea89dbda9da985629d1bbe33cdc7e3e
[lhc/web/wiklou.git] / js2 / mwEmbed / php / maintenance / mergeJavascriptMsg.php
1 <?php
2 /**
3 * Merges in JavaScript with mwEmbed.i18n.php
4 *
5 * @file
6 * @ingroup Maintenance
7 */
8
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";
12 exit();
13 }
14 define( 'MEDIAWIKI', true );
15 // get the scriptLoader globals:
16 require_once( '../../jsScriptLoader.php' );
17
18 $mwSTART_MSG_KEY = '$messages[\'en\'] = array(';
19 $mwEND_MSG_KEY = ',
20 );';
21 $mwLangFilePath = '../languages/mwEmbed.i18n.php';
22 include_once( $mwLangFilePath );
23 // get options (like override JS or override PHP)
24
25 // merge left
26 $mergeToPhp = false;
27 $mergeToJS = true;
28
29 if ( $mergeToPhp && $mergeToJS )
30 die( 'Please only set either $mergeToPhp or $mergeToJS' );
31
32 if ( $mergeToPhp )
33 print "Will merge *Javascript to PHP* in 3 seconds ";
34
35 if ( $mergeToJS )
36 print "Will merge *PHP to Javascript* in 3 seconds ";
37
38 for ( $i = 0; $i < 3; $i++ ) {
39 print '.';
40 sleep( 1 );
41 }
42 print "\n";
43
44 // read in mwEmbed.i18n.php
45 $rawLangFile = file_get_contents( $mwLangFilePath );
46
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";
51 exit();
52 }
53
54 $preFile = substr( $rawLangFile, 0, $startInx );
55 $msgSet = substr( $rawLangFile, $startInx, $endInx - $startInx );
56 $postFile = substr( $rawLangFile, $endInx );
57
58 // build replacement from all javascript in mwEmbed
59 $path = realpath( '../../' );
60
61 $curFileName = '';
62 // @@todo existing msgSet should be parsed (or we just "include" the file first)
63 $msgSet = "";
64
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
72 $jsFileText,
73 $matches ) ) {
74 $msgSet .= doJsonMerge( $matches[1] );
75 }
76 }
77 }
78 // rebuild and output to single php file if mergeToPHP is on
79 if ( $mergeToPhp ) {
80 if ( file_put_contents( $mwLangFilePath, trim( $preFile ) . "\n\t" . trim( $msgSet ) . "\n" . ltrim( $postFile ) ) ) {
81 print "updated $mwLangFilePath file\n";
82 exit();
83 }
84 }
85
86 function doJsonMerge( $json_txt ) {
87 global $curFileName, $fname, $messages, $mergeToJS, $jsFileText;
88
89 $outPhp = "\n\t/*\n";
90 $outPhp .= "\t * js file: {$curFileName}\n";
91 $outPhp .= "\t */\n";
92
93 $jsMsgAry = array();
94 $doReplaceFlag = false;
95
96 $jmsg = json_decode( '{' . $json_txt . '}', true );
97 if ( count( $jmsg ) != 0 ) {
98
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";
105 }
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;
110 } ;
111 $outPhp .= "\t'{$k}' => '" . str_replace( '\'', '\\\'', $v ) . "',\n";
112 }
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 . ")",
122 $jsFileText );
123
124 // print substr($str, 0, 600);
125
126 if ( file_put_contents( $fname, $str ) ) {
127 print "\nupdated $curFileName from php\n\n";
128 } else {
129 die( "Could not write to: " . $fname );
130 }
131 }
132 // return phpOut for building msgSet in outer function
133 return $outPhp;
134
135 } else {
136 print "Could not get any json vars from: $curFileName\n";
137 return '';
138 }
139 }
140
141 function jsonReadable( $json ) {
142 $tabcount = 0;
143 $result = '';
144 $inquote = false;
145 $ignorenext = false;
146
147 $tab = "\t";
148 $newline = "\n";
149
150 for ( $i = 0; $i < strlen( $json ); $i++ ) {
151 $char = $json[$i];
152
153 if ( $ignorenext ) {
154 $result .= $char;
155 $ignorenext = false;
156 } else {
157 switch( $char ) {
158 case '{':
159 $tabcount++;
160 $result .= $char . $newline . str_repeat( $tab, $tabcount );
161 break;
162 case '}':
163 $tabcount--;
164 $result = trim( $result ) . $newline . str_repeat( $tab, $tabcount ) . $char;
165 break;
166 case ',':
167 if ( $inquote ) {
168 $result .= $char;
169 } else {
170 $result .= $char . $newline . str_repeat( $tab, $tabcount );
171 }
172 break;
173 case ':':
174 if ( $inquote ) {
175 $result .= $char;
176 } else {
177 $result .= ' ' . $char . ' ';
178 }
179 break;
180 case '"':
181 $inquote = !$inquote;
182 $result .= $char;
183 break;
184 case '\\':
185 if ( $inquote ) $ignorenext = true;
186 $result .= $char;
187 break;
188 default:
189 $result .= $char;
190 }
191 }
192 }
193
194 return $result;
195 }