Merge "Add non DBMS depending SQL tests for DatabaseBase"
[lhc/web/wiklou.git] / maintenance / language / rebuildLanguage.php
index 6775f6d..ad83905 100644 (file)
@@ -1,63 +1,95 @@
 <?php
 /**
- * Rewrite the messages array in the files languages/messages/MessagesXX.php.
+ * Rewrite the messages array in the files languages/messages/MessagesXx.php.
  *
- * @addtogroup Maintenance
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup MaintenanceLanguage
+ * @defgroup MaintenanceLanguage MaintenanceLanguage
  */
 
-require_once( dirname(__FILE__).'/../commandLine.inc' );
+require_once( __DIR__ . '/../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?
+ * @param $languages
+ * @param $code string The language code.
+ * @param bool $write Write to the messages file?
+ * @param bool $listUnknown List the unknown messages?
+ * @param bool $removeUnknown Remove the unknown messages?
+ * @param bool $removeDupes Remove the duplicated messages?
+ * @param $dupeMsgSource string The source file intended to remove from the array.
+ * @param $messagesFolder String: path to a folder to store the MediaWiki messages.
  */
-function rebuildLanguage( $code, $write ) {
-       global $wgLanguages;
-
-       # Get messages
-       $messages = $wgLanguages->getMessages( $code );
+function rebuildLanguage( $languages, $code, $write, $listUnknown, $removeUnknown, $removeDupes, $dupeMsgSource, $messagesFolder ) {
+       $messages = $languages->getMessages( $code );
        $messages = $messages['all'];
+       if ( $removeDupes ) {
+               $messages = removeDupes( $messages, $dupeMsgSource );
+       }
+       MessageWriter::writeMessagesToFile( $messages, $code, $write, $listUnknown, $removeUnknown, $messagesFolder );
+}
 
-       # Rewrite messages array
-       $messagesText = writeMessagesArray( $messages, $code == 'en' );
-
-       # Write to the file
-       if ( $write ) {
-               $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 in language $code. Same to the current file.\n";
-                       } else {
-                               $new = $contents[0];
-                               $new .= $messagesText;
-                               $new .= "\n?>\n";
-                               file_put_contents( $filename, $new );
-                               echo "Generated and wrote messages in language $code.\n";
-                       }
+/**
+ * Remove duplicates from a message array.
+ *
+ * @param $oldMsgArray array The input message array.
+ * @param $dupeMsgSource string The source file path for duplicates.
+ * @return Array $newMsgArray The output message array, with duplicates removed.
+ */
+function removeDupes( $oldMsgArray, $dupeMsgSource ) {
+       if ( file_exists( $dupeMsgSource ) ) {
+               include( $dupeMsgSource );
+               if ( !isset( $dupeMessages ) ) {
+                       echo( "There are no duplicated messages in the source file provided." );
+                       exit( 1 );
                }
        } else {
-               echo "Generated messages in language $code.\n";
+               echo ( "The specified file $dupeMsgSource cannot be found." );
+               exit( 1 );
+       }
+       $newMsgArray = $oldMsgArray;
+       foreach ( $oldMsgArray as $key => $value ) {
+               if ( array_key_exists( $key, $dupeMessages ) ) {
+                       unset( $newMsgArray[$key] );
+               }
        }
+       return $newMsgArray;
 }
 
 # Show help
 if ( isset( $options['help'] ) ) {
-       echo <<<END
+       echo <<<TEXT
 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.
+       * dry-run: Do not write the array to the file.
+       * no-unknown: Do not list the unknown messages.
+       * remove-unknown: Remove unknown messages.
+       * remove-duplicates: Remove duplicated messages based on a PHP source file.
+       * messages-folder: An alternative folder with MediaWiki messages.
 
-END;
-       exit();
+TEXT;
+       exit( 1 );
 }
 
 # Get the language code
@@ -67,19 +99,28 @@ if ( isset( $options['lang'] ) ) {
        $wgCode = $wgContLang->getCode();
 }
 
-# Get the write options
+# Get the duplicate message source
+if ( isset( $options['remove-duplicates'] ) && ( strcmp( $options['remove-duplicates'], '' ) ) ) {
+       $wgDupeMessageSource = $options['remove-duplicates'];
+} else {
+       $wgDupeMessageSource = '';
+}
+
+# Get the options
 $wgWriteToFile = !isset( $options['dry-run'] );
+$wgListUnknownMessages = !isset( $options['no-unknown'] );
+$wgRemoveUnknownMessages = isset( $options['remove-unknown'] );
+$wgRemoveDuplicateMessages = isset( $options['remove-duplicates'] );
+$messagesFolder = isset( $options['messages-folder'] ) ? $options['messages-folder'] : false;
 
 # Get language objects
-$wgLanguages = new languages();
+$languages = new languages();
 
 # Write all the language
 if ( $wgCode == 'all' ) {
-       foreach ( $wgLanguages->getLanguages() as $language ) {
-               rebuildLanguage( $language, $wgWriteToFile );
+       foreach ( $languages->getLanguages() as $languageCode ) {
+               rebuildLanguage( $languages, $languageCode, $wgWriteToFile, $wgListUnknownMessages, $wgRemoveUnknownMessages, $wgRemoveDuplicateMessages, $wgDupeMessageSource, $messagesFolder );
        }
 } else {
-       rebuildLanguage( $wgCode, $wgWriteToFile );
+       rebuildLanguage( $languages, $wgCode, $wgWriteToFile, $wgListUnknownMessages, $wgRemoveUnknownMessages, $wgRemoveDuplicateMessages, $wgDupeMessageSource, $messagesFolder );
 }
-
-?>