From 3c2ac4e92f16a771293e7219f99e407b36e1ec94 Mon Sep 17 00:00:00 2001 From: Siebrand Mazeland Date: Sat, 22 Aug 2009 18:31:43 +0000 Subject: [PATCH] * update comment writing (add space to second and third line) * stylize.php and indentation updated --- .../php/maintenance/mergeJavascriptMsg.php | 235 +++++++++--------- 1 file changed, 116 insertions(+), 119 deletions(-) diff --git a/js2/mwEmbed/php/maintenance/mergeJavascriptMsg.php b/js2/mwEmbed/php/maintenance/mergeJavascriptMsg.php index c8b10e7684..2e19b813fa 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 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(); +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, $fname, $messages, $mergeToJS, $jsFileText; -function doJsonMerge( $json_txt ){ - global $curFileName,$fname, $messages, $mergeToJS, $jsFileText; - - $outPhp = "\n\t/* -\t* js file: {$curFileName} -\t*/\n"; + $outPhp = "\n\t/*\n"; + $outPhp .= "\t * js file: {$curFileName}\n"; + $outPhp .= "\t */\n"; $jsMsgAry = array(); $doReplaceFlag = false; $jmsg = json_decode( '{' . $json_txt . '}', true ); - if( count( $jmsg ) != 0 ){ + 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; + 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; print "'$k'does not match:\n" . $messages['en'][$k] . "\n!=\n" . $v . "\n"; } - //add the actual value: (replace new lines (not compatible json) - //$jsMsgAry[$k] = str_replace("\n", '\\n', $messages['en'][$k]); + // add the actual value: (replace new lines (not compatible json) + // $jsMsgAry[$k] = str_replace("\n", '\\n', $messages['en'][$k]); $jsMsgAry[$k] = $messages['en'][$k]; - $doReplaceFlag=true; - }; - $outPhp.="\t'{$k}' => '" . str_replace( '\'', '\\\'', $v ) . "',\n"; + $doReplaceFlag = true; + } ; + $outPhp .= "\t'{$k}' => '" . str_replace( '\'', '\\\'', $v ) . "',\n"; } - //merge the jsLanguage array back in and wrap the output - if($mergeToJS && $doReplaceFlag){ - $json = json_encode($jsMsgAry ); - $json_txt = jsonReadable($json); - //escape $1 for preg replace: - $json_txt = str_replace('$', '\$', $json_txt); - //print "json:\n$json_txt \n"; - $str = preg_replace ('/loadGM\s*\(\s*{(.*)}\s*\)\s*/siU', - "loadGM(" . $json_txt . ")", - $jsFileText); - - //print substr($str, 0, 600); - - if( file_put_contents($fname, $str) ){ + // merge the jsLanguage array back in and wrap the output + if ( $mergeToJS && $doReplaceFlag ) { + $json = json_encode( $jsMsgAry ); + $json_txt = jsonReadable( $json ); + // escape $1 for preg replace: + $json_txt = str_replace( '$', '\$', $json_txt ); + // print "json:\n$json_txt \n"; + $str = preg_replace ( '/loadGM\s*\(\s*{(.*)}\s*\)\s*/siU', + "loadGM(" . $json_txt . ")", + $jsFileText ); + + // print substr($str, 0, 600); + + if ( file_put_contents( $fname, $str ) ) { print "\nupdated $curFileName from php\n\n"; - }else{ - die("could not write to: " . $fname); + } else { + die( "could not write to: " . $fname ); } } - //return phpOut for building msgSet in outer function + // return phpOut for building msgSet in outer function return $outPhp; - } else { - print "could not get any json vars from:$curFileName \n"; - return ''; - } + } else { + print "could not get any json vars from:$curFileName \n"; + return ''; + } } -function jsonReadable($json) { - $tabcount = 0; - $result = ''; - $inquote = false; - $ignorenext = false; - +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 ',': - if($inquote){ - $result .= $char; - }else{ - $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; + 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 ',': + if ( $inquote ) { + $result .= $char; + } else { + $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