r55290 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r55289‎ | r55290 | r55291 >
Date:23:49, 18 August 2009
Author:dale
Status:resolved (Comments)
Tags:
Comment:
* added support for merging php -> js (already supported js -> php)
Modified paths:
  • /trunk/phase3/js2/mwEmbed/php/maintenance/mergeJavascriptMsg.php (modified) (history)

Diff [purge]

Index: trunk/phase3/js2/mwEmbed/php/maintenance/mergeJavascriptMsg.php
@@ -1,10 +1,10 @@
22 <?php
33 /**
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+ */
99
1010 # Abort if called from a web server
1111 if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) {
@@ -13,69 +13,171 @@
1414 }
1515 define( 'MEDIAWIKI', true );
1616 // get the scriptLoader globals:
17 -require_once( '../../jsScriptLoader.php' );
 17+require_once('../../jsScriptLoader.php');
1818
1919 $mwSTART_MSG_KEY = '$messages[\'en\'] = array(';
2020 $mwEND_MSG_KEY = ',
2121 );';
2222 $mwLangFilePath = '../languages/mwEmbed.i18n.php';
 23+include_once( $mwLangFilePath );
2324 // get options (like override JS or override PHP)
2425
 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+
2544 // read in mwEmbed.i18n.php
2645 $rawLangFile = file_get_contents( $mwLangFilePath );
2746
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";
3251 exit();
3352 }
3453
3554 $preFile = substr( $rawLangFile, 0, $startInx );
36 -$msgSet = substr( $rawLangFile, $startInx, $endInx - $startInx );
 55+$msgSet = substr( $rawLangFile, $startInx, $endInx-$startInx );
3756 $postFile = substr( $rawLangFile, $endInx );
3857
3958 // build replacement from all javascript in mwEmbed
40 -$path = realpath( '../../' );
 59+$path = realpath('../../');
4160
4261 $curFileName = '';
4362 // @@todo existing msgSet should be parsed (or we just "include" the file first)
4463 $msgSet = "";
4564
4665 $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 ) ){
5574 $msgSet .= doJsonMerge( $matches[1] );
5675 }
5776 }
5877 }
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+ }
6484 }
6585
66 -function doJsonMerge( $json_txt ) {
67 - global $curFileName;
6886
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+
7297 $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";
76110 }
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+ }
82134 }
 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

Follow-up revisions

RevisionCommit summaryAuthorDate
r55527* for php2js, all colons in messages will be replaced by " : ". That's not re...siebrand12:53, 23 August 2009

Comments

#Comment by Siebrand (talk | contribs)   12:54, 23 August 2009

For php2js, all colons in messages will be replaced by " : ". Commented out in r55527, with a FIXME.

#Comment by Brion VIBBER (talk | contribs)   16:53, 27 August 2009

marking resolved, put a 'todo' on that other one.

Status & tagging log