Commit RELEASE-NOTES line for the wgCategories js variable I added some time ago.
[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
24 function print_help(){
25 ?>
26 This script helps merge msgs between javascript and php
27 Usage:
28 -j merges javascript msgs into php
29 -p merges php msgs back into javascript
30 -q will disable screen output and wait time
31
32 <?php
33 die();
34 }
35
36 // get options (like override JS or override PHP)
37 if ( in_array($argv[1], array('--help', '-help', '-h', '-?')) ) {
38 print_help();
39 }
40 $mergeToPhp = $mergeToJS = false;
41 $showInfo = true;
42 foreach($argv as $inx => $arg){
43 if($inx == 0)
44 continue;
45 if( $arg == '-j' ){
46 $mergeToPhp = true;
47 }else if( $arg == '-p' ) {
48 $mergeToJS = true;
49 }else if( $arg == '-q'){
50 $showInfo = false;
51 }else{
52 print_help();
53 }
54 }
55
56
57 if($showInfo){
58 if ( $mergeToPhp )
59 print "Will merge *Javascript to PHP* in 3 seconds ";
60
61 if ( $mergeToJS )
62 print "Will merge *PHP to Javascript* in 3 seconds ";
63
64 for ( $i = 0; $i < 3; $i++ ) {
65 print '.';
66 sleep( 1 );
67 }
68 print "\n";
69 }
70
71 // read in mwEmbed.i18n.php
72 $rawLangFile = file_get_contents( $mwLangFilePath );
73
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 ) {
77 if($showInfo)
78 print "Could not find $mwSTART_MSG_KEY or $mwEND_MSG_KEY in mwEmbed.i18n.php\n";
79 exit();
80 }
81
82 $preFile = substr( $rawLangFile, 0, $startInx );
83 $msgSet = substr( $rawLangFile, $startInx, $endInx - $startInx );
84 $postFile = substr( $rawLangFile, $endInx );
85
86 // build replacement from all javascript in mwEmbed
87 $path = realpath( '../../' );
88
89 $curFileName = '';
90 // @@todo existing msgSet should be parsed (or we just "include" the file first)
91 $msgSet = "";
92
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
100 $jsFileText,
101 $matches ) ) {
102 $msgSet .= doJsonMerge( $matches[1] );
103 }
104 }
105 }
106 // rebuild and output to single php file if mergeToPHP is on
107 if ( $mergeToPhp ) {
108 if ( file_put_contents( $mwLangFilePath, trim( $preFile ) . "\n\t" . trim( $msgSet ) . "\n" . ltrim( $postFile ) ) ) {
109 if( $showInfo )
110 print "updated $mwLangFilePath file\n";
111 exit();
112 }
113 }
114
115 function doJsonMerge( $json_txt ) {
116 global $curFileName, $fname, $messages, $mergeToJS, $jsFileText, $showInfo;
117
118 $outPhp = "\n\t/*\n";
119 $outPhp .= "\t * js file: {$curFileName}\n";
120 $outPhp .= "\t */\n";
121
122 $jsMsgAry = array();
123 $doReplaceFlag = false;
124
125 $jmsg = json_decode( '{' . $json_txt . '}', true );
126 if ( count( $jmsg ) != 0 ) {
127
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;
133 if( $showInfo )
134 print "'$k'does not match:\n" . $messages['en'][$k] . "\n!=\n" . $v . "\n";
135 }
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;
140 } ;
141 $outPhp .= "\t'{$k}' => '" . str_replace( '\'', '\\\'', $v ) . "',\n";
142 }
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 . ")",
152 $jsFileText );
153
154 // print substr($str, 0, 600);
155
156 if ( file_put_contents( $fname, $str ) ) {
157 if( $showInfo )
158 print "\nupdated $curFileName from php\n\n";
159 } else {
160 die( "Could not write to: " . $fname );
161 }
162 }
163 // return phpOut for building msgSet in outer function
164 return $outPhp;
165
166 } else {
167 if($showInfo)
168 print "Could not get any json vars from: $curFileName\n";
169 return '';
170 }
171 }
172
173 function jsonReadable( $json ) {
174 $tabcount = 0;
175 $result = '';
176 $inquote = false;
177 $ignorenext = false;
178
179 $tab = "\t";
180 $newline = "\n";
181
182 for ( $i = 0; $i < strlen( $json ); $i++ ) {
183 $char = $json[$i];
184
185 if ( $ignorenext ) {
186 $result .= $char;
187 $ignorenext = false;
188 } else {
189 switch( $char ) {
190 case '{':
191 $tabcount++;
192 $result .= $char . $newline . str_repeat( $tab, $tabcount );
193 break;
194 case '}':
195 $tabcount--;
196 $result = trim( $result ) . $newline . str_repeat( $tab, $tabcount ) . $char;
197 break;
198 case ',':
199 if ( $inquote ) {
200 $result .= $char;
201 } else {
202 $result .= $char . $newline . str_repeat( $tab, $tabcount );
203 }
204 break;
205 case ':':
206 if ( $inquote ) {
207 $result .= $char;
208 } else {
209 $result .= ' ' . $char . ' ';
210 }
211 break;
212 case '"':
213 $inquote = !$inquote;
214 $result .= $char;
215 break;
216 case '\\':
217 if ( $inquote ) $ignorenext = true;
218 $result .= $char;
219 break;
220 default:
221 $result .= $char;
222 }
223 }
224 }
225
226 return $result;
227 }