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