r55484 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r55483‎ | r55484 | r55485 >
Date:18:31, 22 August 2009
Author:siebrand
Status:deferred
Tags:
Comment:
* update comment writing (add space to second and third line)
* stylize.php and indentation updated
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,7 +13,7 @@
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 = ',
@@ -22,173 +22,170 @@
2323 include_once( $mwLangFilePath );
2424 // get options (like override JS or override PHP)
2525
26 -//merge left
 26+// merge left
2727 $mergeToPhp = false;
2828 $mergeToJS = true;
2929
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' );
3232
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 ";
3535
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 ";
3838
39 -for($i=0;$i<3;$i++){
 39+for ( $i = 0; $i < 3; $i++ ) {
4040 print '.';
41 - sleep(1);
 41+ sleep( 1 );
4242 }
4343 print "\n";
4444
4545 // read in mwEmbed.i18n.php
4646 $rawLangFile = file_get_contents( $mwLangFilePath );
4747
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";
5252 exit();
5353 }
5454
5555 $preFile = substr( $rawLangFile, 0, $startInx );
56 -$msgSet = substr( $rawLangFile, $startInx, $endInx-$startInx );
 56+$msgSet = substr( $rawLangFile, $startInx, $endInx - $startInx );
5757 $postFile = substr( $rawLangFile, $endInx );
5858
5959 // build replacement from all javascript in mwEmbed
60 -$path = realpath('../../');
 60+$path = realpath( '../../' );
6161
6262 $curFileName = '';
6363 // @@todo existing msgSet should be parsed (or we just "include" the file first)
6464 $msgSet = "";
6565
6666 $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 ) ) {
7575 $msgSet .= doJsonMerge( $matches[1] );
7676 }
7777 }
7878 }
7979 // 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();
8484 }
8585 }
8686
 87+function doJsonMerge( $json_txt ) {
 88+ global $curFileName, $fname, $messages, $mergeToJS, $jsFileText;
8789
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";
9093
91 - $outPhp = "\n\t/*
92 -\t* js file: {$curFileName}
93 -\t*/\n";
94 -
9594 $jsMsgAry = array();
9695 $doReplaceFlag = false;
9796
9897 $jmsg = json_decode( '{' . $json_txt . '}', true );
99 - if( count( $jmsg ) != 0 ){
 98+ if ( count( $jmsg ) != 0 ) {
10099
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;
106105 print "'$k'does not match:\n" . $messages['en'][$k] . "\n!=\n" . $v . "\n";
107106 }
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]);
110109 $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";
114113 }
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 );
125124
126 - //print substr($str, 0, 600);
 125+ // print substr($str, 0, 600);
127126
128 - if( file_put_contents($fname, $str) ){
 127+ if ( file_put_contents( $fname, $str ) ) {
129128 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 );
132131 }
133132 }
134 - //return phpOut for building msgSet in outer function
 133+ // return phpOut for building msgSet in outer function
135134 return $outPhp;
136135
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+ }
141140 }
142141
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;
148147
149 -
150148 $tab = "\t";
151149 $newline = "\n";
152150
153 - for($i = 0; $i < strlen($json); $i++) {
154 - $char = $json[$i];
 151+ for ( $i = 0; $i < strlen( $json ); $i++ ) {
 152+ $char = $json[$i];
155153
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+ }
192190
193 - return $result;
 191+ return $result;
194192 }
195 -?>
\ No newline at end of file

Status & tagging log