From f1016dcfc81cd70d2fc9c3502d6807604592adc0 Mon Sep 17 00:00:00 2001 From: Michael Dale Date: Tue, 18 Aug 2009 23:49:36 +0000 Subject: [PATCH] * added support for merging php -> js (already supported js -> php) --- .../php/maintenance/mergeJavascriptMsg.php | 178 ++++++++++++++---- 1 file changed, 140 insertions(+), 38 deletions(-) diff --git a/js2/mwEmbed/php/maintenance/mergeJavascriptMsg.php b/js2/mwEmbed/php/maintenance/mergeJavascriptMsg.php index ec97bf35f3..70f885548e 100644 --- a/js2/mwEmbed/php/maintenance/mergeJavascriptMsg.php +++ b/js2/mwEmbed/php/maintenance/mergeJavascriptMsg.php @@ -1,10 +1,10 @@ $object ) { - if ( substr( $fname, - 3 ) == '.js' ) { - $jsFileText = file_get_contents( $fname ); - $mwPos = strpos( $fname, 'mwEmbed' ) + 7; - $curFileName = substr( $fname, $mwPos ); - if ( preg_match( '/loadGM\s*\(\s*{(.*)}\s*\)\s*/siU', // @@todo fix: will break down if someone does }) in their msg text - $jsFileText, - $matches ) ) { +foreach( $objects as $fname => $object){ + if( substr( $fname, -3 ) == '.js' ){ + $jsFileText = file_get_contents( $fname ); + $mwPos = strpos( $fname, 'mwEmbed' ) + 7; + $curFileName = substr( $fname, $mwPos ); + if( preg_match( '/loadGM\s*\(\s*{(.*)}\s*\)\s*/siU', //@@todo fix: will break down if someone does }) in their msg text + $jsFileText, + $matches ) ){ $msgSet .= doJsonMerge( $matches[1] ); } } } - -// rebuild and output to file -if ( file_put_contents( $mwLangFilePath, trim( $preFile ) . "\n\t" . trim( $msgSet ) . "\n" . trim( $postFile ) ) ) { - print "Updated $mwLangFilePath file\n"; - exit(); +// rebuild and output to single php file if mergeToPHP is on +if($mergeToPhp){ + if( file_put_contents( $mwLangFilePath, trim( $preFile ) . "\n\t" . trim( $msgSet ) . "\n" . trim( $postFile ) ) ){ + print "updated $mwLangFilePath file\n"; + exit(); + } } -function doJsonMerge( $json_txt ) { - global $curFileName; - $out = "\n\t/* -\t * js file: {$curFileName} -\t */\n"; +function doJsonMerge( $json_txt ){ + global $curFileName,$fname, $messages, $mergeToJS, $jsFileText; + + $outPhp = "\n\t/* +\t* js file: {$curFileName} +\t*/\n"; + + $jsMsgAry = array(); + $doReplaceFlag = false; + $jmsg = json_decode( '{' . $json_txt . '}', true ); - if ( count( $jmsg ) != 0 ) { - foreach ( $jmsg as $k => $v ) { - $out .= "\t'{$k}' => '" . str_replace( '\'', '\\\'', $v ) . "',\n"; + if( count( $jmsg ) != 0 ){ + + foreach( $jmsg as $k => $v ){ + //check if the existing value is changed and merge and merge ->right + if(isset( $messages['en'][$k] )){ + if($messages['en'][$k] != $v ) + $doReplaceFlag=true; + //add the actual value: + $jsMsgAry[$k] = $messages['en'][$k]; + $doReplaceFlag=true; + }; + $outPhp.="\t'{$k}' => '" . str_replace( '\'', '\\\'', $v ) . "',\n"; } - return $out; - } else { - print "Could not get any json vars from $curFileName\n"; - return ''; - } + //merge the jsLanguage array back in and wrap the output + if($mergeToJS){ + $json = json_encode($jsMsgAry ); + $json_txt = jsonReadable($json); + //escape $1 for preg replace: + $json_txt = str_replace('$', '\$', $json_txt); + $str = preg_replace ('/loadGM\s*\(\s*{(.*)}\s*\)\s*/siU', + "loadGM(" . $json_txt . ")", + $jsFileText); + + if( file_put_contents($fname, $str) ){ + print "updated $curFileName from php\n"; + }else{ + die("could not write to: " . $fname); + } + } + //return phpOut for building msgSet in outer function + return $outPhp; + + } else { + print "could not get any json vars from:$curFileName \n"; + return ''; + } +} + +function jsonReadable($json) { + $tabcount = 0; + $result = ''; + $inquote = false; + $ignorenext = false; + + + $tab = "\t"; + $newline = "\n"; + + for($i = 0; $i < strlen($json); $i++) { + $char = $json[$i]; + + if ($ignorenext) { + $result .= $char; + $ignorenext = false; + } else { + switch($char) { + case '{': + $tabcount++; + $result .= $char . $newline . str_repeat($tab, $tabcount); + break; + case '}': + $tabcount--; + $result = trim($result) . $newline . str_repeat($tab, $tabcount) . $char; + break; + case ',': + $result .= $char . $newline . str_repeat($tab, $tabcount); + break; + case ':': + $result .= ' ' . $char . ' '; + break; + case '"': + $inquote = !$inquote; + $result .= $char; + break; + case '\\': + if ($inquote) $ignorenext = true; + $result .= $char; + break; + default: + $result .= $char; + } + } + } + + return $result; } +?> \ No newline at end of file -- 2.20.1