MediaWiki  REL1_19
writeMessagesArray.inc
Go to the documentation of this file.
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 
00045         public static function writeMessagesToFile( $messages, $code, $write, $listUnknown, $removeUnknown ) {
00046                 # Rewrite the messages array
00047                 $messages = self::writeMessagesArray( $messages, $code == 'en', false, $removeUnknown );
00048                 $messagesText = $messages[0];
00049                 $sortedMessages = $messages[1];
00050 
00051                 # Write to the file
00052                 $filename = Language::getMessagesFileName( $code );
00053                 $contents = file_get_contents( $filename );
00054                 if( strpos( $contents, '$messages' ) !== false ) {
00055                         $contents = explode( '$messages', $contents );
00056                         if( $messagesText == '$messages' . $contents[1] ) {
00057                                 echo "Generated messages for language $code. Same as the current file.\n";
00058                         } else {
00059                                 if( $write ) {
00060                                         $new = $contents[0];
00061                                         $new .= $messagesText;
00062                                         file_put_contents( $filename, $new );
00063                                         echo "Generated and wrote messages for language $code.\n";
00064                                 } else {
00065                                         echo "Generated messages for language $code. Please run the script again (without the parameter \"dry-run\") to write the array to the file.\n";
00066                                 }
00067                         }
00068                         if( $listUnknown && isset( $sortedMessages['unknown'] ) && !empty( $sortedMessages['unknown'] ) ) {
00069                                 if ( $removeUnknown )
00070                                         echo "\nThe following " . count( $sortedMessages['unknown'] ) . " unknown messages have been removed:\n";
00071                                 else
00072                                         echo "\nThere are " . count( $sortedMessages['unknown'] ) . " unknown messages, please check them:\n";
00073                                 foreach( $sortedMessages['unknown'] as $key => $value ) {
00074                                         echo "* " . $key . "\n";
00075                                 }
00076                         }
00077                 } else {
00078                         echo "Generated messages for language $code. There seem to be no messages array in the file.\n";
00079                 }
00080         }
00081 
00094         public static function writeMessagesArray( $messages, $ignoredComments = false, $prefix = false, $removeUnknown = false ) {
00095                 # Load messages
00096                 $dir = $prefix ? $prefix : dirname( __FILE__ );
00097 
00098                 require( $dir . '/messages.inc' );
00099                 self::$messageStructure = $wgMessageStructure;
00100                 self::$blockComments = $wgBlockComments;
00101 
00102                 require( $dir . '/messageTypes.inc' );
00103                 self::$ignoredMessages = $wgIgnoredMessages;
00104                 self::$optionalMessages = $wgOptionalMessages;
00105 
00106                 # Sort messages to blocks
00107                 $sortedMessages['unknown'] = $messages;
00108                 foreach( self::$messageStructure as $blockName => $block ) {
00112                         foreach( $block as $key ) {
00113                                 if( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
00114                                         $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
00115                                         unset( $sortedMessages['unknown'][$key] );
00116                                 }
00117                         }
00118                 }
00119 
00120                 # Write all the messages
00121                 $messagesText = "\$messages = array(
00122 ";
00123                 foreach( $sortedMessages as $block => $messages ) {
00124                         # Skip if it's the block of unknown messages - handle that in the end of file
00125                         if( $block == 'unknown' ) {
00126                                 continue;
00127                         }
00128 
00129                         if( $ignoredComments ) {
00130                                 $ignored = self::$ignoredMessages;
00131                                 $optional = self::$optionalMessages;
00132                         } else {
00133                                 $ignored = array();
00134                                 $optional = array();
00135                         }
00136                         $comments = self::makeComments( array_keys( $messages ), $ignored, $optional );
00137 
00138                         # Write the block
00139                         $messagesText .= self::writeMessagesBlock( self::$blockComments[$block], $messages, $comments );
00140                 }
00141 
00142                 # Write the unknown messages, alphabetically sorted.
00143                 # Of course, we don't have any comments for them, because they are unknown.
00144                 if ( !$removeUnknown ) {
00145                         ksort( $sortedMessages['unknown'] );
00146                         $messagesText .= self::writeMessagesBlock( 'Unknown messages', $sortedMessages['unknown'] );
00147                 }
00148                 $messagesText .= ");
00149 ";
00150                 return array( $messagesText, $sortedMessages );
00151         }
00152 
00160         public static function makeComments( $messages, $ignored, $optional ) {
00161                 # Comment collector
00162                 $commentArray = array();
00163 
00164                 # List of keys only
00165                 foreach( $messages as $key ) {
00166                         if( in_array( $key, $ignored ) ) {
00167                                 $commentArray[$key] = ' # ' . self::$ignoredComment;
00168                         } elseif( in_array( $key, $optional ) ) {
00169                                 $commentArray[$key] = ' # ' . self::$optionalComment;
00170                         }
00171                 }
00172 
00173                 return $commentArray;
00174         }
00175 
00186         public static function writeMessagesBlock( $blockComment, $messages,
00187                 $messageComments = array(), $prefix = '' ) {
00188 
00189                 $blockText = '';
00190 
00191                 # Skip the block if it includes no messages
00192                 if( empty( $messages ) ) {
00193                         return '';
00194                 }
00195 
00196                 # Format the block comment (if exists); check for multiple lines comments
00197                 if( !empty( $blockComment ) ) {
00198                         if( strpos( $blockComment, "\n" ) === false ) {
00199                                 $blockText .= "$prefix# $blockComment
00200 ";
00201                         } else {
00202                                 $blockText .= "$prefix/*
00203 $blockComment
00204 */
00205 ";
00206                         }
00207                 }
00208 
00209                 # Get max key length
00210                 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
00211 
00212                 # Format the messages
00213                 foreach( $messages as $key => $value ) {
00214                         # Add the key name
00215                         $blockText .= "$prefix'$key'";
00216 
00217                         # Add the appropriate block whitespace
00218                         $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
00219 
00220                         # Refer to the value
00221                         $blockText .= ' => ';
00222 
00223                         # Check for the appropriate apostrophe and add the value
00224                         # Quote \ here, because it needs always escaping
00225                         $value = addcslashes( $value, '\\' );
00226 
00227                         # For readability
00228                         $single = "'";
00229                         $double = '"';
00230 
00231                         if( strpos( $value, $single ) === false ) {
00232                                 # Nothing ugly, just use '
00233                                 $blockText .= $single.$value.$single;
00234                         } elseif( strpos( $value, $double ) === false && !preg_match('/\$[a-zA-Z_\x7f-\xff]/', $value) ) {
00235                                 # No "-quotes, no variables that need quoting, use "
00236                                 $blockText .= $double.$value.$double;
00237                         } else {
00238                                 # Something needs quoting, pick the quote which causes less quoting
00239                                 $quote = substr_count( $value, $double ) + substr_count( $value, '$' ) >= substr_count( $value, $single ) ? $single : $double;
00240                                 if( $quote === $double ) {
00241                                         $extra = '$';
00242                                 } else {
00243                                         $extra = '';
00244                                 }
00245                                 $blockText .= $quote . addcslashes( $value, $quote . $extra ) . $quote;
00246                         }
00247 
00248                         # Comma
00249                         $blockText .= ',';
00250 
00251                         # Add comments, if there is any
00252                         if( array_key_exists( $key, $messageComments ) ) {
00253                                 $blockText .= $messageComments[$key];
00254                         }
00255 
00256                         # Newline
00257                         $blockText .= "
00258 ";
00259                 }
00260 
00261                 # Newline to end the block
00262                 $blockText .= "
00263 ";
00264 
00265                 return $blockText;
00266         }
00267 }