Library to write an organised messages array, and script which uses it to rewrite...
authorRotem Liss <rotem@users.mediawiki.org>
Sun, 5 Nov 2006 19:43:18 +0000 (19:43 +0000)
committerRotem Liss <rotem@users.mediawiki.org>
Sun, 5 Nov 2006 19:43:18 +0000 (19:43 +0000)
maintenance/language/rebuildLanguage.php [new file with mode: 0644]
maintenance/language/writeMessagesArray.inc [new file with mode: 0644]

diff --git a/maintenance/language/rebuildLanguage.php b/maintenance/language/rebuildLanguage.php
new file mode 100644 (file)
index 0000000..4817daa
--- /dev/null
@@ -0,0 +1,82 @@
+<?php
+/**
+ * Rewrite the messages array in the files languages/messages/MessagesXX.php.
+ *
+ * @package MediaWiki
+ * @subpackage Maintenance
+ */
+
+require_once( dirname(__FILE__).'/../commandLine.inc' );
+require_once( 'languages.inc' );
+require_once( 'writeMessagesArray.inc' );
+
+/**
+ * Rewrite a messages array.
+ *
+ * @param $code The language code.
+ * @param $write Write to the messages file?
+ */
+function rebuildLanguage( $code, $write ) {
+       global $wgLanguages, $wg;
+
+       # Get messages
+       $messages = $wgLanguages->getMessages( $code );
+       $messages = $messages['all'];
+
+       # Rewrite messages array
+       $messagesText = writeMessagesArray( $messages );
+
+       # Write to the file
+       if ( $write ) {
+               $filename = Language::getMessagesFileName( $code );
+               $contents = file_get_contents( $filename );
+               if ( strpos( $contents, '$messages' ) !== false ) {
+                       $new = explode( '$messages', $contents );
+                       $new = $new[0];
+                       $new .= $messagesText;
+                       $new .= "\n?>\n";
+                       file_put_contents( $filename, $new );
+                       echo "Generated and wrote messages in language $code.\n";
+               }
+       } else {
+               echo "Generated messages in language $code.\n";
+       }
+}
+
+# Show help
+if ( isset( $options['help'] ) ) {
+       echo <<<END
+Run this script to rewrite the messages array in the files languages/messages/MessagesXX.php.
+Parameters:
+       * lang: Language code (default: the installation default language). You can also specify "all" to check all the languages.
+       * help: Show this help.
+Options:
+       * dry-run: Don't write the array to the file.
+
+END;
+       exit();
+}
+
+# Get the language code
+if ( isset( $options['lang'] ) ) {
+       $wgCode = $options['lang'];
+} else {
+       $wgCode = $wgContLang->getCode();
+}
+
+# Get the write options
+$wgWriteToFile = !isset( $options['dry-run'] );
+
+# Get language objects
+$wgLanguages = new languages();
+
+# Write all the language
+if ( $wgCode == 'all' ) {
+       foreach ( $wgLanguages->getLanguages() as $language ) {
+               rebuildLanguage( $language, $wgWriteToFile );
+       }
+} else {
+       rebuildLanguage( $wgCode, $wgWriteToFile );
+}
+
+?>
diff --git a/maintenance/language/writeMessagesArray.inc b/maintenance/language/writeMessagesArray.inc
new file mode 100644 (file)
index 0000000..8fc6b38
--- /dev/null
@@ -0,0 +1,98 @@
+<?php
+/**
+ * Write a messages array as a PHP text.
+ *
+ * @package MediaWiki
+ * @subpackage Maintenance
+ */
+
+require_once( 'messages.inc' );
+
+/**
+ * Write a messages array as a PHP text.
+ *
+ * @param $messages The messages array.
+ */
+function writeMessagesArray( $messages ) {
+       global $wgMessageStrucutre, $wgBlockComments, $wgMessageComments;
+
+       # Sort messages to blocks
+       $sortedMessages['unknown'] = $messages;
+       foreach ( $messages as $key => $value ) {
+               foreach ( $wgMessageStrucutre as $blockName => $block ) {
+                       if ( in_array( $key, $block ) ) {
+                               $sortedMessages[$blockName][$key] = $value;
+                               unset( $sortedMessages['unknown'][$key] );
+                               break;
+                       }
+               }
+       }
+
+       # 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 ) ) {
+                       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";
+                       }
+               }
+
+               # Get max key length
+               $maxKeyLength = 0;
+               foreach( array_keys( $block ) as $key ) {
+                       if ( strlen( $key ) > $maxKeyLength ) {
+                               $maxKeyLength = strlen( $key );
+                       }
+               }
+
+               # Format the messages
+               foreach( $block as $key => $value ) {
+                       # Add the key name
+                       $messagesText .= "'$key'";
+
+                       # Add the appropriate block whitespace
+                       for ( $i = 1; $i <= ( $maxKeyLength - strlen( $key ) ); $i++ ) {
+                               $messagesText .= ' ';
+                       }
+
+                       # Refer to the value
+                       $messagesText .= ' => ';
+
+                       # 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 ) . "'";
+                       }
+
+                       # Comma
+                       $messagesText .= ',';
+
+                       if ( array_key_exists( $key, $wgMessageComments ) ) {
+                               $messagesText .= ' # ' . $wgMessageComments[$key];
+                       }
+
+                       # Newline
+                       $messagesText .= "\n";
+               }
+
+               # Newline to end the block
+               $messagesText .= "\n";
+       }
+       $messagesText .= ");\n";
+
+       return $messagesText;
+}
+
+?>