From: Rotem Liss Date: Sat, 16 Feb 2008 15:32:47 +0000 (+0000) Subject: Indentation, and including files only once to avoid memory problems. If it breaks... X-Git-Tag: 1.31.0-rc.0~49470 X-Git-Url: https://git.cyclocoop.org/%27.%24link.%27?a=commitdiff_plain;h=c9f32232861635dfc7226679de696e43f10f8467;p=lhc%2Fweb%2Fwiklou.git Indentation, and including files only once to avoid memory problems. If it breaks Translate extension, I think it is possible to change require_once => require. --- diff --git a/maintenance/language/writeMessagesArray.inc b/maintenance/language/writeMessagesArray.inc index f96448988b..8bb190a5cc 100644 --- a/maintenance/language/writeMessagesArray.inc +++ b/maintenance/language/writeMessagesArray.inc @@ -5,237 +5,236 @@ * @addtogroup Maintenance */ -class MessageWriter { +require_once( dirname( __FILE__ ) . '/messages.inc' ); +require_once( dirname( __FILE__ ) . '/messageTypes.inc' ); -/** - * Write a messages array as a PHP text and write it to the messages file. - * - * @param $messages The messages array. - * @param $code The language code. - * @param $write Write to the messages file? - * @param $listUnknown List the unknown messages? - */ -public static function writeMessagesToFile( $messages, $code, $write, $listUnknown ) { - # Rewrite the messages array - $messages = self::writeMessagesArray( $messages, $code == 'en' ); - $messagesText = $messages[0]; - $sortedMessages = $messages[1]; - - # Write to the file - $filename = Language::getMessagesFileName( $code ); - $contents = file_get_contents( $filename ); - if ( strpos( $contents, '$messages' ) !== false ) { - $contents = explode( '$messages', $contents ); - if ( $messagesText == '$messages' . $contents[1] ) { - echo "Generated messages for language $code. Same as the current file.\n"; - } else { - if ( $write ) { - $new = $contents[0]; - $new .= $messagesText; - file_put_contents( $filename, $new ); - echo "Generated and wrote messages for language $code.\n"; +class MessageWriter { + static $optionalComment = 'only translate this message to other languages if you have to change it'; + static $ignoredComment = "don't translate or duplicate this message to other languages"; + + /** + * Write a messages array as a PHP text and write it to the messages file. + * + * @param $messages The messages array. + * @param $code The language code. + * @param $write Write to the messages file? + * @param $listUnknown List the unknown messages? + */ + public static function writeMessagesToFile( $messages, $code, $write, $listUnknown ) { + # Rewrite the messages array + $messages = self::writeMessagesArray( $messages, $code == 'en' ); + $messagesText = $messages[0]; + $sortedMessages = $messages[1]; + + # Write to the file + $filename = Language::getMessagesFileName( $code ); + $contents = file_get_contents( $filename ); + if( strpos( $contents, '$messages' ) !== false ) { + $contents = explode( '$messages', $contents ); + if( $messagesText == '$messages' . $contents[1] ) { + echo "Generated messages for language $code. Same as the current file.\n"; } else { - echo "Generated messages for language $code. Please run the script again (without the parameter \"dry-run\") to write the array to the file.\n"; + if( $write ) { + $new = $contents[0]; + $new .= $messagesText; + file_put_contents( $filename, $new ); + echo "Generated and wrote messages for language $code.\n"; + } else { + echo "Generated messages for language $code. Please run the script again (without the parameter \"dry-run\") to write the array to the file.\n"; + } } - } - if ( $listUnknown && isset( $sortedMessages['unknown'] ) && !empty( $sortedMessages['unknown'] ) ) { - echo "\nThere are " . count( $sortedMessages['unknown'] ) . " unknown messages, please check them:\n"; - foreach ( $sortedMessages['unknown'] as $key => $value ) { - echo "* " . $key . "\n"; + if( $listUnknown && isset( $sortedMessages['unknown'] ) && !empty( $sortedMessages['unknown'] ) ) { + echo "\nThere are " . count( $sortedMessages['unknown'] ) . " unknown messages, please check them:\n"; + foreach ( $sortedMessages['unknown'] as $key => $value ) { + echo "* " . $key . "\n"; + } } + } else { + echo "Generated messages for language $code. There seems to be no messages array in the file.\n"; } - } else { - echo "Generated messages for language $code. There seems to be no messages array in the file.\n"; - } -} - -/** - * Write a messages array as a PHP text. - * - * @param $messages The messages array. - * @param $ignoredComments Show comments about ignored and optional messages? (For English.) - * - * @return Array of the PHP text and the sorted messages array. - */ -public static function writeMessagesArray( $messages, $ignoredComments = false ) { - # $wgMessageStructure, $wgBlockComments, $wgMessageComments; - require( dirname( __FILE__ ) . '/messages.inc' ); - # $wgIgnoredMessages, $wgOptionalMessages; - require( dirname( __FILE__ ) . '/messageTypes.inc' ); - - if (!$ignoredComments) { - $wgOptionalMessages = $wgIgnoredMessages = array(); } - # Sort messages to blocks - $sortedMessages['unknown'] = $messages; - foreach ( $wgMessageStructure as $blockName => $block ) { - foreach ( $block as $key ) { - if ( array_key_exists( $key, $sortedMessages['unknown'] ) ) { - $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key]; - unset( $sortedMessages['unknown'][$key] ); + /** + * Write a messages array as a PHP text. + * + * @param $messages The messages array. + * @param $ignoredComments Show comments about ignored and optional messages? (For English.) + * + * @return Array of the PHP text and the sorted messages array. + */ + public static function writeMessagesArray( $messages, $ignoredComments = false ) { + global $wgMessageStructure, $wgBlockComments, $wgMessageComments; + global $wgIgnoredMessages, $wgOptionalMessages; + + # Sort messages to blocks + $sortedMessages['unknown'] = $messages; + foreach ( $wgMessageStructure as $blockName => $block ) { + foreach ( $block as $key ) { + if( array_key_exists( $key, $sortedMessages['unknown'] ) ) { + $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key]; + unset( $sortedMessages['unknown'][$key] ); + } } } - } - # Write all the messages - $messagesText = "\$messages = array( + # Write all the messages + $messagesText = "\$messages = array( "; - foreach( $sortedMessages as $block => $messages ) { - # Skip if it's the block of unknown messages - handle that in the end of file - if ( $block == 'unknown' ) { - continue; - } + foreach( $sortedMessages as $block => $messages ) { + # Skip if it's the block of unknown messages - handle that in the end of file + if( $block == 'unknown' ) { + continue; + } - $comments = self::makeComments( array_keys($messages), $wgMessageComments, - $wgIgnoredMessages, $wgOptionalMessages - ); + if( $ignoredComments ) { + $ignored = $wgIgnoredMessages; + $optional = $wgOptionalMessages; + } else { + $ignored = array(); + $optional = array(); + } + $comments = self::makeComments( array_keys($messages), $wgMessageComments, $ignored, $optional ); - # Write the block - $messagesText .= self::writeMessagesBlock( $wgBlockComments[$block], $messages, $comments ); - } + # Write the block + $messagesText .= self::writeMessagesBlock( $wgBlockComments[$block], $messages, $comments ); + } - // Write the unknown messages, alphabetically sorted. - // Of course, we don't have any comments for them, because the are unknown. - ksort( $sortedMessages['unknown'] ); - $messagesText .= self::writeMessagesBlock( 'Unknown messages', $sortedMessages['unknown'] ); - $messagesText .= "); + # Write the unknown messages, alphabetically sorted. + # Of course, we don't have any comments for them, because the are unknown. + ksort( $sortedMessages['unknown'] ); + $messagesText .= self::writeMessagesBlock( 'Unknown messages', $sortedMessages['unknown'] ); + $messagesText .= "); "; - return array( $messagesText, $sortedMessages ); -} - -static $optionalComment = 'only translate this message to other languages if you have to change it'; -static $ignoredComment = 'don\'t translate or duplicate this message to other languages'; - -/** - * Generates an array of comments for messages. - * - * @param $messages Key of messages. - * @param $comments Comments for messages, indexed by key. - * @param $ignored List of ingored message keys. - * @param $optional List of optional message keys. - */ -public static function makeComments( $messages, $comments, $ignored, $optional ) { - // Comment collector - $commentArray = array(); + return array( $messagesText, $sortedMessages ); + } - // List of keys only - foreach ( $messages as $key ) { - $commentsForKey = array(); + /** + * Generates an array of comments for messages. + * + * @param $messages Key of messages. + * @param $comments Comments for messages, indexed by key. + * @param $ignored List of ingored message keys. + * @param $optional List of optional message keys. + */ + public static function makeComments( $messages, $comments, $ignored, $optional ) { + # Comment collector + $commentArray = array(); + + # List of keys only + foreach( $messages as $key ) { + $commentsForKey = array(); + + # Add descriptive comment for this message if there is one + if( array_key_exists( $key, $comments ) ) { + $commentsForKey[] = $comments[$key]; + } - // Add descriptive comment for this message if there is one - if ( array_key_exists( $key, $comments ) ) { - $commentsForKey[] = $comments[$key]; - } + # For translator information only + if( in_array( $key, $ignored ) ) { + $commentsForKey[] = self::$ignoredComment; + } elseif( in_array( $key, $optional ) ) { + $commentsForKey[] = self::$optionalComment; + } - // For translator information only - if ( in_array( $key, $ignored ) ) { - $commentsForKey[] = self::$ignoredComment; - } elseif ( in_array( $key, $optional ) ) { - $commentsForKey[] = self::$optionalComment; + # Format one or more comments nicely and store in array + if( count( $commentsForKey ) ) { + $commentArray[$key] = ' # ' . implode( '; ', $commentsForKey ); + } } - // Format one or more comments nicely and store in array - if ( count($commentsForKey) ) { - $commentArray[$key] = ' # ' . implode( '; ', $commentsForKey ); - } + return $commentArray; } - return $commentArray; -} - -/** - * Write a block of messages to PHP. - * - * @param $blockComment The comment of whole block. - * @param $messages The block messages. - * @param $messageComments Optional comments for messages in this block. - * @param $prefix Prefix for every line, for indenting purposes. - * - * @return The block, formatted in PHP. - */ -public static function writeMessagesBlock( $blockComment, $messages, - $messageComments = array(), $prefix = '' ) { - - $blockText = ''; - - # Skip the block if it includes no messages - if ( empty( $messages ) ) { - return ''; - } + /** + * Write a block of messages to PHP. + * + * @param $blockComment The comment of whole block. + * @param $messages The block messages. + * @param $messageComments Optional comments for messages in this block. + * @param $prefix Prefix for every line, for indenting purposes. + * + * @return The block, formatted in PHP. + */ + public static function writeMessagesBlock( $blockComment, $messages, + $messageComments = array(), $prefix = '' ) { + + $blockText = ''; + + # Skip the block if it includes no messages + if( empty( $messages ) ) { + return ''; + } - # Format the block comment (if exists); check for multiple lines comments - if ( !empty( $blockComment ) ) { - if ( strpos( $blockComment, "\n" ) === false ) { - $blockText .= "$prefix# $blockComment + # Format the block comment (if exists); check for multiple lines comments + if( !empty( $blockComment ) ) { + if( strpos( $blockComment, "\n" ) === false ) { + $blockText .= "$prefix# $blockComment "; - } else { - $blockText .= "$prefix/* + } else { + $blockText .= "$prefix/* $blockComment */ "; + } } - } - # Get max key length - $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) ); + # Get max key length + $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) ); - # Format the messages - foreach( $messages as $key => $value ) { - # Add the key name - $blockText .= "$prefix'$key'"; + # Format the messages + foreach( $messages as $key => $value ) { + # Add the key name + $blockText .= "$prefix'$key'"; - # Add the appropriate block whitespace - $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) ); + # Add the appropriate block whitespace + $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) ); - # Refer to the value - $blockText .= ' => '; + # Refer to the value + $blockText .= ' => '; - # Check for the appropriate apostrophe and add the value - # Quote \ here, because it needs always escaping - $value = addcslashes( $value, '\\' ); + # Check for the appropriate apostrophe and add the value + # Quote \ here, because it needs always escaping + $value = addcslashes( $value, '\\' ); - # For readability - $single = "'"; - $double = '"'; + # For readability + $single = "'"; + $double = '"'; - # Nothing ugly, just use ' - if ( strpos( $value, $single ) === false ) { - $blockText .= $single.$value.$single; - - # No "-quotes, no variables that need quoting, use " - } elseif ( strpos( $value, $double ) === false && !preg_match('/\$[a-zA-Z_\x7f-\xff]/', $value) ) { - $blockText .= $double.$value.$double; + if( strpos( $value, $single ) === false ) { + # Nothing ugly, just use ' + $blockText .= $single.$value.$single; + } elseif( strpos( $value, $double ) === false && !preg_match('/\$[a-zA-Z_\x7f-\xff]/', $value) ) { + # No "-quotes, no variables that need quoting, use " + $blockText .= $double.$value.$double; + } else { + # Something needs quoting, pick the quote which causes less quoting + $quote = substr_count( $value, $double ) + substr_count( $value, '$' ) >= substr_count( $value, $single ) ? $single : $double; + if( $quote === $double ) { + $extra = '$'; + } else { + $extra = ''; + } + $blockText .= $quote . addcslashes( $value, $quote . $extra ) . $quote; + } - # Something needs quoting, pick the quote which causes less quoting - } else { - $quote = substr_count( $value, $double ) + substr_count( $value, '$' ) >= - substr_count( $value, $single ) ? $single : $double; - if ( $quote === $double ) { $extra = '$'; } else { $extra = ''; } - $blockText .= $quote . addcslashes( $value, $quote.$extra ) . $quote; - } + # Comma + $blockText .= ','; - # Comma - $blockText .= ','; + # Add comments, if there is any + if( array_key_exists( $key, $messageComments ) ) { + $blockText .= $messageComments[$key]; + } - # Add comments, if there is any - if ( array_key_exists( $key, $messageComments ) ) { - $blockText .= $messageComments[$key]; + # Newline + $blockText .= " +"; } - # Newline + # Newline to end the block $blockText .= " "; - } - - # Newline to end the block - $blockText .= " -"; - return $blockText; + return $blockText; + } } - -} // CLASS MessageWriter - -?>