Adding a small test to verify that the regex worked. Apparently it's failing in IE8...
[mediawiki.git] / maintenance / language / rebuildLanguage.php
blob9b3a4b9d7fc4586396037d0f647059172089e721
1 <?php
2 /**
3 * Rewrite the messages array in the files languages/messages/MessagesXx.php.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup MaintenanceLanguage
22 * @defgroup MaintenanceLanguage MaintenanceLanguage
25 require_once( dirname( __FILE__ ) . '/../commandLine.inc' );
26 require_once( 'languages.inc' );
27 require_once( 'writeMessagesArray.inc' );
29 /**
30 * Rewrite a messages array.
32 * @param $languages
33 * @param $code The language code.
34 * @param bool $write Write to the messages file?
35 * @param bool $listUnknown List the unknown messages?
36 * @param bool $removeUnknown Remove the unknown messages?
37 * @param bool $removeDupes Remove the duplicated messages?
38 * @param $dupeMsgSource The source file intended to remove from the array.
40 function rebuildLanguage( $languages, $code, $write, $listUnknown, $removeUnknown, $removeDupes, $dupeMsgSource ) {
41 $messages = $languages->getMessages( $code );
42 $messages = $messages['all'];
43 if ( $removeDupes ) {
44 $messages = removeDupes( $messages, $dupeMsgSource );
46 MessageWriter::writeMessagesToFile( $messages, $code, $write, $listUnknown, $removeUnknown );
49 /**
50 * Remove duplicates from a message array.
52 * @param $oldMsgArray The input message array.
53 * @param $dupeMsgSource The source file path for duplicates.
54 * @return Array $newMsgArray The output message array, with duplicates removed.
56 function removeDupes( $oldMsgArray, $dupeMsgSource ) {
57 if ( file_exists( $dupeMsgSource ) ) {
58 include( $dupeMsgSource );
59 if ( !isset( $dupeMessages ) ) {
60 echo( "There are no duplicated messages in the source file provided." );
61 exit( 1 );
63 } else {
64 echo ( "The specified file $dupeMsgSource cannot be found." );
65 exit( 1 );
67 $newMsgArray = $oldMsgArray;
68 foreach ( $oldMsgArray as $key => $value ) {
69 if ( array_key_exists( $key, $dupeMessages ) ) {
70 unset( $newMsgArray[$key] );
73 return $newMsgArray;
76 # Show help
77 if ( isset( $options['help'] ) ) {
78 echo <<<TEXT
79 Run this script to rewrite the messages array in the files languages/messages/MessagesXX.php.
80 Parameters:
81 * lang: Language code (default: the installation default language). You can also specify "all" to check all the languages.
82 * help: Show this help.
83 Options:
84 * dry-run: Do not write the array to the file.
85 * no-unknown: Do not list the unknown messages.
86 * remove-unknown: Remove unknown messages.
87 * remove-duplicates: Remove duplicated messages based on a PHP source file.
89 TEXT;
90 exit( 1 );
93 # Get the language code
94 if ( isset( $options['lang'] ) ) {
95 $wgCode = $options['lang'];
96 } else {
97 $wgCode = $wgContLang->getCode();
100 # Get the duplicate message source
101 if ( isset( $options['remove-duplicates'] ) && ( strcmp( $options['remove-duplicates'], '' ) ) ) {
102 $wgDupeMessageSource = $options['remove-duplicates'];
103 } else {
104 $wgDupeMessageSource = '';
107 # Get the options
108 $wgWriteToFile = !isset( $options['dry-run'] );
109 $wgListUnknownMessages = !isset( $options['no-unknown'] );
110 $wgRemoveUnknownMessages = isset( $options['remove-unknown'] );
111 $wgRemoveDuplicateMessages = isset( $options['remove-duplicates'] );
113 # Get language objects
114 $languages = new languages();
116 # Write all the language
117 if ( $wgCode == 'all' ) {
118 foreach ( $languages->getLanguages() as $languageCode ) {
119 rebuildLanguage( $languages, $languageCode, $wgWriteToFile, $wgListUnknownMessages, $wgRemoveUnknownMessages, $wgRemoveDuplicateMessages, $wgDupeMessageSource );
121 } else {
122 rebuildLanguage( $languages, $wgCode, $wgWriteToFile, $wgListUnknownMessages, $wgRemoveUnknownMessages, $wgRemoveDuplicateMessages, $wgDupeMessageSource );