MediaWiki
REL1_22
|
00001 <?php 00027 class MessageWriter { 00028 static $optionalComment = 'only translate this message to other languages if you have to change it'; 00029 static $ignoredComment = "do not translate or duplicate this message to other languages"; 00030 00031 static $messageStructure; 00032 static $blockComments; 00033 static $ignoredMessages; 00034 static $optionalMessages; 00035 00046 public static function writeMessagesToFile( $messages, $code, $write, $listUnknown, $removeUnknown, $messagesFolder = false ) { 00047 # Rewrite the messages array 00048 $messages = self::writeMessagesArray( $messages, $code == 'en', false, $removeUnknown ); 00049 $messagesText = $messages[0]; 00050 $sortedMessages = $messages[1]; 00051 00052 # Write to the file 00053 if ( $messagesFolder ) { 00054 $filename = Language::getFileName( "$messagesFolder/Messages", $code ); 00055 } else { 00056 $filename = Language::getMessagesFileName( $code ); 00057 } 00058 00059 if ( file_exists( $filename ) ) { 00060 $contents = file_get_contents( $filename ); 00061 } else { 00062 $contents = '<?php 00063 $messages = array( 00064 ); 00065 '; 00066 } 00067 00068 if ( strpos( $contents, '$messages' ) !== false ) { 00069 $contents = explode( '$messages', $contents ); 00070 if ( $messagesText == '$messages' . $contents[1] ) { 00071 echo "Generated messages for language $code. Same as the current file.\n"; 00072 } else { 00073 if ( $write ) { 00074 $new = $contents[0]; 00075 $new .= $messagesText; 00076 file_put_contents( $filename, $new ); 00077 echo "Generated and wrote messages for language $code.\n"; 00078 } else { 00079 echo "Generated messages for language $code. Please run the script again (without the parameter \"dry-run\") to write the array to the file.\n"; 00080 } 00081 } 00082 if ( $listUnknown && isset( $sortedMessages['unknown'] ) && !empty( $sortedMessages['unknown'] ) ) { 00083 if ( $removeUnknown ) { 00084 echo "\nThe following " . count( $sortedMessages['unknown'] ) . " unknown messages have been removed:\n"; 00085 } else { 00086 echo "\nThere are " . count( $sortedMessages['unknown'] ) . " unknown messages, please check them:\n"; 00087 } 00088 foreach ( $sortedMessages['unknown'] as $key => $value ) { 00089 echo "* " . $key . "\n"; 00090 } 00091 } 00092 } else { 00093 echo "Generated messages for language $code. There seem to be no messages array in the file.\n"; 00094 } 00095 } 00096 00109 public static function writeMessagesArray( $messages, $ignoredComments = false, $prefix = false, $removeUnknown = false ) { 00110 # Load messages 00111 $dir = $prefix ? $prefix : __DIR__; 00112 00113 require $dir . '/messages.inc'; 00114 self::$messageStructure = $wgMessageStructure; 00115 self::$blockComments = $wgBlockComments; 00116 00117 require $dir . '/messageTypes.inc'; 00118 self::$ignoredMessages = $wgIgnoredMessages; 00119 self::$optionalMessages = $wgOptionalMessages; 00120 00121 # Sort messages to blocks 00122 $sortedMessages['unknown'] = $messages; 00123 foreach ( self::$messageStructure as $blockName => $block ) { 00127 foreach ( $block as $key ) { 00128 if ( array_key_exists( $key, $sortedMessages['unknown'] ) ) { 00129 $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key]; 00130 unset( $sortedMessages['unknown'][$key] ); 00131 } 00132 } 00133 } 00134 00135 # Write all the messages 00136 $messagesText = "\$messages = array( 00137 "; 00138 foreach ( $sortedMessages as $block => $messages ) { 00139 # Skip if it's the block of unknown messages - handle that in the end of file 00140 if ( $block == 'unknown' ) { 00141 continue; 00142 } 00143 00144 if ( $ignoredComments ) { 00145 $ignored = self::$ignoredMessages; 00146 $optional = self::$optionalMessages; 00147 } else { 00148 $ignored = array(); 00149 $optional = array(); 00150 } 00151 $comments = self::makeComments( array_keys( $messages ), $ignored, $optional ); 00152 00153 # Write the block 00154 $messagesText .= self::writeMessagesBlock( self::$blockComments[$block], $messages, $comments ); 00155 } 00156 00157 # Write the unknown messages, alphabetically sorted. 00158 # Of course, we don't have any comments for them, because they are unknown. 00159 if ( !$removeUnknown ) { 00160 ksort( $sortedMessages['unknown'] ); 00161 $messagesText .= self::writeMessagesBlock( 'Unknown messages', $sortedMessages['unknown'] ); 00162 } 00163 $messagesText .= "); 00164 "; 00165 return array( $messagesText, $sortedMessages ); 00166 } 00167 00176 public static function makeComments( $messages, $ignored, $optional ) { 00177 # Comment collector 00178 $commentArray = array(); 00179 00180 # List of keys only 00181 foreach ( $messages as $key ) { 00182 if ( in_array( $key, $ignored ) ) { 00183 $commentArray[$key] = ' # ' . self::$ignoredComment; 00184 } elseif ( in_array( $key, $optional ) ) { 00185 $commentArray[$key] = ' # ' . self::$optionalComment; 00186 } 00187 } 00188 00189 return $commentArray; 00190 } 00191 00202 public static function writeMessagesBlock( $blockComment, $messages, 00203 $messageComments = array(), $prefix = '' ) { 00204 00205 $blockText = ''; 00206 00207 # Skip the block if it includes no messages 00208 if ( empty( $messages ) ) { 00209 return ''; 00210 } 00211 00212 # Format the block comment (if exists); check for multiple lines comments 00213 if ( !empty( $blockComment ) ) { 00214 if ( strpos( $blockComment, "\n" ) === false ) { 00215 $blockText .= "$prefix# $blockComment 00216 "; 00217 } else { 00218 $blockText .= "$prefix/* 00219 $blockComment 00220 */ 00221 "; 00222 } 00223 } 00224 00225 # Get max key length 00226 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) ); 00227 00228 # Format the messages 00229 foreach ( $messages as $key => $value ) { 00230 # Add the key name 00231 $blockText .= "$prefix'$key'"; 00232 00233 # Add the appropriate block whitespace 00234 $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) ); 00235 00236 # Refer to the value 00237 $blockText .= ' => '; 00238 00239 # Check for the appropriate apostrophe and add the value 00240 # Quote \ here, because it needs always escaping 00241 $value = addcslashes( $value, '\\' ); 00242 00243 # For readability 00244 $single = "'"; 00245 $double = '"'; 00246 00247 if ( strpos( $value, $single ) === false ) { 00248 # Nothing ugly, just use ' 00249 $blockText .= $single . $value . $single; 00250 } elseif ( strpos( $value, $double ) === false && !preg_match( '/\$[a-zA-Z_\x7f-\xff]/', $value ) ) { 00251 # No "-quotes, no variables that need quoting, use " 00252 $blockText .= $double . $value . $double; 00253 } else { 00254 # Something needs quoting, pick the quote which causes less quoting 00255 $quote = substr_count( $value, $double ) + substr_count( $value, '$' ) >= substr_count( $value, $single ) ? $single : $double; 00256 if ( $quote === $double ) { 00257 $extra = '$'; 00258 } else { 00259 $extra = ''; 00260 } 00261 $blockText .= $quote . addcslashes( $value, $quote . $extra ) . $quote; 00262 } 00263 00264 # Comma 00265 $blockText .= ','; 00266 00267 # Add comments, if there is any 00268 if ( array_key_exists( $key, $messageComments ) ) { 00269 $blockText .= $messageComments[$key]; 00270 } 00271 00272 # Newline 00273 $blockText .= " 00274 "; 00275 } 00276 00277 # Newline to end the block 00278 $blockText .= " 00279 "; 00280 00281 return $blockText; 00282 } 00283 }