* Write a messages array as a PHP text.
*
* @param $messages The messages array.
+ *
+ * @return The PHP text.
*/
function writeMessagesArray( $messages ) {
global $wgMessageStrucutre, $wgBlockComments, $wgMessageComments;
# Write all the messages
$messagesText = "\$messages = array(\n";
- foreach( $sortedMessages as $blockName => $block ) {
- # Continue if it's the block of unknown messages, or the block is empty
- if ( $blockName == 'unknown' || empty( $block ) ) {
+ 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;
}
- # Get the block comment and format it (if exist); check for multiple lines comments
- $comment = $wgBlockComments[$blockName];
- if ( !empty( $comment ) ) {
- if ( strpos( $comment, "\n" ) === false ) {
- $messagesText .= "# $comment\n";
- } else {
- $messagesText .= "/*\n$comment\n*/\n";
- }
- }
+ # Write the block
+ $messagesText .= writeMessagesBlock( $block, $wgBlockComments[$block], $messages );
+ }
+ $messagesText .= writeMessagesBlock( 'unknown', 'Unknown messages', $sortedMessages['unknown'] );
+ $messagesText .= ");\n";
- # Get max key length
- $maxKeyLength = 0;
- foreach( array_keys( $block ) as $key ) {
- if ( strlen( $key ) > $maxKeyLength ) {
- $maxKeyLength = strlen( $key );
- }
+ return $messagesText;
+}
+
+/**
+ * Write a block of messages to PHP.
+ *
+ * @param $name The block name.
+ * @param $comment The block comment.
+ * @param $messages The block messages.
+ *
+ * @return The block, formatted in PHP.
+ */
+function writeMessagesBlock( $name, $comment, $messages ) {
+ global $wgMessageComments;
+ $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( $comment ) ) {
+ if ( strpos( $comment, "\n" ) === false ) {
+ $blockText .= "# $comment\n";
+ } else {
+ $blockText .= "/*\n$comment\n*/\n";
}
+ }
- # Format the messages
- foreach( $block as $key => $value ) {
- # Add the key name
- $messagesText .= "'$key'";
+ # Get max key length
+ $maxKeyLength = 0;
+ foreach( array_keys( $messages ) as $key ) {
+ if ( strlen( $key ) > $maxKeyLength ) {
+ $maxKeyLength = strlen( $key );
+ }
+ }
- # Add the appropriate block whitespace
- for ( $i = 1; $i <= ( $maxKeyLength - strlen( $key ) ); $i++ ) {
- $messagesText .= ' ';
- }
+ # Format the messages
+ foreach( $messages as $key => $value ) {
+ # Add the key name
+ $blockText .= "'$key'";
- # Refer to the value
- $messagesText .= ' => ';
+ # Add the appropriate block whitespace
+ for ( $i = 1; $i <= ( $maxKeyLength - strlen( $key ) ); $i++ ) {
+ $blockText .= ' ';
+ }
- # Check for the appropriate apostrophe and add the value
- if ( strpos( $value, "'" ) === false ) {
- $messagesText .= "'$value'";
- } elseif ( strpos( $value, '"' ) === false ) {
- $messagesText .= "\"$value\"";
- } else {
- $messagesText .= "'" . str_replace( "'", "\'", $value ) . "'";
- }
+ # Refer to the value
+ $blockText .= ' => ';
- # Comma
- $messagesText .= ',';
+ # Check for the appropriate apostrophe and add the value
+ if ( strpos( $value, "'" ) === false ) {
+ $blockText .= "'$value'";
+ } elseif ( strpos( $value, '"' ) === false ) {
+ $blockText .= "\"$value\"";
+ } else {
+ $blockText .= "'" . str_replace( "'", "\'", $value ) . "'";
+ }
- if ( array_key_exists( $key, $wgMessageComments ) ) {
- $messagesText .= ' # ' . $wgMessageComments[$key];
- }
+ # Comma
+ $blockText .= ',';
- # Newline
- $messagesText .= "\n";
+ if ( array_key_exists( $key, $wgMessageComments ) ) {
+ $blockText .= ' # ' . $wgMessageComments[$key];
}
- # Newline to end the block
- $messagesText .= "\n";
+ # Newline
+ $blockText .= "\n";
}
- $messagesText .= ");\n";
- return $messagesText;
+ # Newline to end the block
+ $blockText .= "\n";
+
+ return $blockText;
}
?>