Index: trunk/phase3/js2/mwEmbed/php/maintenance/mergeJavascriptMsg.php |
— | — | @@ -1,10 +1,10 @@ |
2 | 2 | <?php |
3 | 3 | /** |
4 | | - * Merges in JavaScript with mwEmbed.i18n.php |
5 | | - * |
6 | | - * @file |
7 | | - * @ingroup Maintenance |
8 | | - */ |
| 4 | +* Merges in JavaScript with mwEmbed.i18n.php |
| 5 | +* |
| 6 | +* @file |
| 7 | +* @ingroup Maintenance |
| 8 | +*/ |
9 | 9 | |
10 | 10 | # Abort if called from a web server |
11 | 11 | if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) { |
— | — | @@ -13,7 +13,7 @@ |
14 | 14 | } |
15 | 15 | define( 'MEDIAWIKI', true ); |
16 | 16 | // get the scriptLoader globals: |
17 | | -require_once('../../jsScriptLoader.php'); |
| 17 | +require_once( '../../jsScriptLoader.php' ); |
18 | 18 | |
19 | 19 | $mwSTART_MSG_KEY = '$messages[\'en\'] = array('; |
20 | 20 | $mwEND_MSG_KEY = ', |
— | — | @@ -22,173 +22,170 @@ |
23 | 23 | include_once( $mwLangFilePath ); |
24 | 24 | // get options (like override JS or override PHP) |
25 | 25 | |
26 | | -//merge left |
| 26 | +// merge left |
27 | 27 | $mergeToPhp = false; |
28 | 28 | $mergeToJS = true; |
29 | 29 | |
30 | | -if( $mergeToPhp && $mergeToJS) |
31 | | - die('please only set either $mergeToPhp or $mergeToJS'); |
| 30 | +if ( $mergeToPhp && $mergeToJS ) |
| 31 | +die( 'please only set either $mergeToPhp or $mergeToJS' ); |
32 | 32 | |
33 | | -if( $mergeToPhp ) |
34 | | - print "Will merge *Javascript to PHP* in 3 seconds "; |
| 33 | +if ( $mergeToPhp ) |
| 34 | +print "Will merge *Javascript to PHP* in 3 seconds "; |
35 | 35 | |
36 | | -if( $mergeToJS ) |
37 | | - print "Will merge *PHP to Javascript* in 3 seconds "; |
| 36 | +if ( $mergeToJS ) |
| 37 | +print "Will merge *PHP to Javascript* in 3 seconds "; |
38 | 38 | |
39 | | -for($i=0;$i<3;$i++){ |
| 39 | +for ( $i = 0; $i < 3; $i++ ) { |
40 | 40 | print '.'; |
41 | | - sleep(1); |
| 41 | + sleep( 1 ); |
42 | 42 | } |
43 | 43 | print "\n"; |
44 | 44 | |
45 | 45 | // read in mwEmbed.i18n.php |
46 | 46 | $rawLangFile = file_get_contents( $mwLangFilePath ); |
47 | 47 | |
48 | | -$startInx = strpos( $rawLangFile, $mwSTART_MSG_KEY) + strlen( $mwSTART_MSG_KEY ); |
49 | | -$endInx = strpos( $rawLangFile, $mwEND_MSG_KEY ) +1; |
50 | | -if( $startInx === false || $endInx === false ){ |
51 | | - print "Could not find $mwSTART_MSG_KEY or $mwEND_MSG_KEY in mwEmbed.i18n.php \n"; |
| 48 | +$startInx = strpos( $rawLangFile, $mwSTART_MSG_KEY ) + strlen( $mwSTART_MSG_KEY ); |
| 49 | +$endInx = strpos( $rawLangFile, $mwEND_MSG_KEY ) + 1; |
| 50 | +if ( $startInx === false || $endInx === false ) { |
| 51 | + print "Could not find $mwSTART_MSG_KEY or $mwEND_MSG_KEY in mwEmbed.i18n.php \n"; |
52 | 52 | exit(); |
53 | 53 | } |
54 | 54 | |
55 | 55 | $preFile = substr( $rawLangFile, 0, $startInx ); |
56 | | -$msgSet = substr( $rawLangFile, $startInx, $endInx-$startInx ); |
| 56 | +$msgSet = substr( $rawLangFile, $startInx, $endInx - $startInx ); |
57 | 57 | $postFile = substr( $rawLangFile, $endInx ); |
58 | 58 | |
59 | 59 | // build replacement from all javascript in mwEmbed |
60 | | -$path = realpath('../../'); |
| 60 | +$path = realpath( '../../' ); |
61 | 61 | |
62 | 62 | $curFileName = ''; |
63 | 63 | // @@todo existing msgSet should be parsed (or we just "include" the file first) |
64 | 64 | $msgSet = ""; |
65 | 65 | |
66 | 66 | $objects = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $path ), RecursiveIteratorIterator::SELF_FIRST ); |
67 | | -foreach( $objects as $fname => $object){ |
68 | | - if( substr( $fname, -3 ) == '.js' ){ |
69 | | - $jsFileText = file_get_contents( $fname ); |
70 | | - $mwPos = strpos( $fname, 'mwEmbed' ) + 7; |
71 | | - $curFileName = substr( $fname, $mwPos ); |
72 | | - if( preg_match( '/loadGM\s*\(\s*{(.*)}\s*\)\s*/siU', //@@todo fix: will break down if someone does }) in their msg text |
73 | | - $jsFileText, |
74 | | - $matches ) ){ |
| 67 | +foreach ( $objects as $fname => $object ) { |
| 68 | + if ( substr( $fname, -3 ) == '.js' ) { |
| 69 | + $jsFileText = file_get_contents( $fname ); |
| 70 | + $mwPos = strpos( $fname, 'mwEmbed' ) + 7; |
| 71 | + $curFileName = substr( $fname, $mwPos ); |
| 72 | + if ( preg_match( '/loadGM\s*\(\s*{(.*)}\s*\)\s*/siU', // @@todo fix: will break down if someone does }) in their msg text |
| 73 | + $jsFileText, |
| 74 | + $matches ) ) { |
75 | 75 | $msgSet .= doJsonMerge( $matches[1] ); |
76 | 76 | } |
77 | 77 | } |
78 | 78 | } |
79 | 79 | // rebuild and output to single php file if mergeToPHP is on |
80 | | -if($mergeToPhp){ |
81 | | - if( file_put_contents( $mwLangFilePath, trim( $preFile ) . "\n\t" . trim( $msgSet ) . "\n" . trim( $postFile ) ) ){ |
82 | | - print "updated $mwLangFilePath file\n"; |
83 | | - exit(); |
| 80 | +if ( $mergeToPhp ) { |
| 81 | + if ( file_put_contents( $mwLangFilePath, trim( $preFile ) . "\n\t" . trim( $msgSet ) . "\n" . trim( $postFile ) ) ) { |
| 82 | + print "updated $mwLangFilePath file\n"; |
| 83 | + exit(); |
84 | 84 | } |
85 | 85 | } |
86 | 86 | |
| 87 | +function doJsonMerge( $json_txt ) { |
| 88 | + global $curFileName, $fname, $messages, $mergeToJS, $jsFileText; |
87 | 89 | |
88 | | -function doJsonMerge( $json_txt ){ |
89 | | - global $curFileName,$fname, $messages, $mergeToJS, $jsFileText; |
| 90 | + $outPhp = "\n\t/*\n"; |
| 91 | + $outPhp .= "\t * js file: {$curFileName}\n"; |
| 92 | + $outPhp .= "\t */\n"; |
90 | 93 | |
91 | | - $outPhp = "\n\t/* |
92 | | -\t* js file: {$curFileName} |
93 | | -\t*/\n"; |
94 | | - |
95 | 94 | $jsMsgAry = array(); |
96 | 95 | $doReplaceFlag = false; |
97 | 96 | |
98 | 97 | $jmsg = json_decode( '{' . $json_txt . '}', true ); |
99 | | - if( count( $jmsg ) != 0 ){ |
| 98 | + if ( count( $jmsg ) != 0 ) { |
100 | 99 | |
101 | | - foreach( $jmsg as $k => $v ){ |
102 | | - //check if the existing value is changed and merge and merge ->right |
103 | | - if(isset( $messages['en'][$k] )){ |
104 | | - if($messages['en'][$k] != $v ){ |
105 | | - $doReplaceFlag=true; |
| 100 | + foreach ( $jmsg as $k => $v ) { |
| 101 | + // check if the existing value is changed and merge and merge ->right |
| 102 | + if ( isset( $messages['en'][$k] ) ) { |
| 103 | + if ( $messages['en'][$k] != $v ) { |
| 104 | + $doReplaceFlag = true; |
106 | 105 | print "'$k'does not match:\n" . $messages['en'][$k] . "\n!=\n" . $v . "\n"; |
107 | 106 | } |
108 | | - //add the actual value: (replace new lines (not compatible json) |
109 | | - //$jsMsgAry[$k] = str_replace("\n", '\\n', $messages['en'][$k]); |
| 107 | + // add the actual value: (replace new lines (not compatible json) |
| 108 | + // $jsMsgAry[$k] = str_replace("\n", '\\n', $messages['en'][$k]); |
110 | 109 | $jsMsgAry[$k] = $messages['en'][$k]; |
111 | | - $doReplaceFlag=true; |
112 | | - }; |
113 | | - $outPhp.="\t'{$k}' => '" . str_replace( '\'', '\\\'', $v ) . "',\n"; |
| 110 | + $doReplaceFlag = true; |
| 111 | + } ; |
| 112 | + $outPhp .= "\t'{$k}' => '" . str_replace( '\'', '\\\'', $v ) . "',\n"; |
114 | 113 | } |
115 | | - //merge the jsLanguage array back in and wrap the output |
116 | | - if($mergeToJS && $doReplaceFlag){ |
117 | | - $json = json_encode($jsMsgAry ); |
118 | | - $json_txt = jsonReadable($json); |
119 | | - //escape $1 for preg replace: |
120 | | - $json_txt = str_replace('$', '\$', $json_txt); |
121 | | - //print "json:\n$json_txt \n"; |
122 | | - $str = preg_replace ('/loadGM\s*\(\s*{(.*)}\s*\)\s*/siU', |
123 | | - "loadGM(" . $json_txt . ")", |
124 | | - $jsFileText); |
| 114 | + // merge the jsLanguage array back in and wrap the output |
| 115 | + if ( $mergeToJS && $doReplaceFlag ) { |
| 116 | + $json = json_encode( $jsMsgAry ); |
| 117 | + $json_txt = jsonReadable( $json ); |
| 118 | + // escape $1 for preg replace: |
| 119 | + $json_txt = str_replace( '$', '\$', $json_txt ); |
| 120 | + // print "json:\n$json_txt \n"; |
| 121 | + $str = preg_replace ( '/loadGM\s*\(\s*{(.*)}\s*\)\s*/siU', |
| 122 | + "loadGM(" . $json_txt . ")", |
| 123 | + $jsFileText ); |
125 | 124 | |
126 | | - //print substr($str, 0, 600); |
| 125 | + // print substr($str, 0, 600); |
127 | 126 | |
128 | | - if( file_put_contents($fname, $str) ){ |
| 127 | + if ( file_put_contents( $fname, $str ) ) { |
129 | 128 | print "\nupdated $curFileName from php\n\n"; |
130 | | - }else{ |
131 | | - die("could not write to: " . $fname); |
| 129 | + } else { |
| 130 | + die( "could not write to: " . $fname ); |
132 | 131 | } |
133 | 132 | } |
134 | | - //return phpOut for building msgSet in outer function |
| 133 | + // return phpOut for building msgSet in outer function |
135 | 134 | return $outPhp; |
136 | 135 | |
137 | | - } else { |
138 | | - print "could not get any json vars from:$curFileName \n"; |
139 | | - return ''; |
140 | | - } |
| 136 | + } else { |
| 137 | + print "could not get any json vars from:$curFileName \n"; |
| 138 | + return ''; |
| 139 | + } |
141 | 140 | } |
142 | 141 | |
143 | | -function jsonReadable($json) { |
144 | | - $tabcount = 0; |
145 | | - $result = ''; |
146 | | - $inquote = false; |
147 | | - $ignorenext = false; |
| 142 | +function jsonReadable( $json ) { |
| 143 | + $tabcount = 0; |
| 144 | + $result = ''; |
| 145 | + $inquote = false; |
| 146 | + $ignorenext = false; |
148 | 147 | |
149 | | - |
150 | 148 | $tab = "\t"; |
151 | 149 | $newline = "\n"; |
152 | 150 | |
153 | | - for($i = 0; $i < strlen($json); $i++) { |
154 | | - $char = $json[$i]; |
| 151 | + for ( $i = 0; $i < strlen( $json ); $i++ ) { |
| 152 | + $char = $json[$i]; |
155 | 153 | |
156 | | - if ($ignorenext) { |
157 | | - $result .= $char; |
158 | | - $ignorenext = false; |
159 | | - } else { |
160 | | - switch($char) { |
161 | | - case '{': |
162 | | - $tabcount++; |
163 | | - $result .= $char . $newline . str_repeat($tab, $tabcount); |
164 | | - break; |
165 | | - case '}': |
166 | | - $tabcount--; |
167 | | - $result = trim($result) . $newline . str_repeat($tab, $tabcount) . $char; |
168 | | - break; |
169 | | - case ',': |
170 | | - if($inquote){ |
171 | | - $result .= $char; |
172 | | - }else{ |
173 | | - $result .= $char . $newline . str_repeat($tab, $tabcount); |
174 | | - } |
175 | | - break; |
176 | | - case ':': |
177 | | - $result .= ' ' . $char . ' '; |
178 | | - break; |
179 | | - case '"': |
180 | | - $inquote = !$inquote; |
181 | | - $result .= $char; |
182 | | - break; |
183 | | - case '\\': |
184 | | - if ($inquote) $ignorenext = true; |
185 | | - $result .= $char; |
186 | | - break; |
187 | | - default: |
188 | | - $result .= $char; |
189 | | - } |
190 | | - } |
191 | | - } |
| 154 | + if ( $ignorenext ) { |
| 155 | + $result .= $char; |
| 156 | + $ignorenext = false; |
| 157 | + } else { |
| 158 | + switch( $char ) { |
| 159 | + case '{': |
| 160 | + $tabcount++; |
| 161 | + $result .= $char . $newline . str_repeat( $tab, $tabcount ); |
| 162 | + break; |
| 163 | + case '}': |
| 164 | + $tabcount--; |
| 165 | + $result = trim( $result ) . $newline . str_repeat( $tab, $tabcount ) . $char; |
| 166 | + break; |
| 167 | + case ',': |
| 168 | + if ( $inquote ) { |
| 169 | + $result .= $char; |
| 170 | + } else { |
| 171 | + $result .= $char . $newline . str_repeat( $tab, $tabcount ); |
| 172 | + } |
| 173 | + break; |
| 174 | + case ':': |
| 175 | + $result .= ' ' . $char . ' '; |
| 176 | + break; |
| 177 | + case '"': |
| 178 | + $inquote = !$inquote; |
| 179 | + $result .= $char; |
| 180 | + break; |
| 181 | + case '\\': |
| 182 | + if ( $inquote ) $ignorenext = true; |
| 183 | + $result .= $char; |
| 184 | + break; |
| 185 | + default: |
| 186 | + $result .= $char; |
| 187 | + } |
| 188 | + } |
| 189 | + } |
192 | 190 | |
193 | | - return $result; |
| 191 | + return $result; |
194 | 192 | } |
195 | | -?> |
\ No newline at end of file |