Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / maintenance / language / rebuildLanguage.php
blob66948aebc5d7a260531d44ea620e83785b740147
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 __DIR__ . '/../commandLine.inc';
26 require_once 'languages.inc';
27 require_once 'writeMessagesArray.inc';
29 /**
30 * Rewrite a messages array.
32 * @param $languages
33 * @param $code string 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 string The source file intended to remove from the array.
39 * @param $messagesFolder String: path to a folder to store the MediaWiki messages.
41 function rebuildLanguage( $languages, $code, $write, $listUnknown, $removeUnknown, $removeDupes, $dupeMsgSource, $messagesFolder ) {
42 $messages = $languages->getMessages( $code );
43 $messages = $messages['all'];
44 if ( $removeDupes ) {
45 $messages = removeDupes( $messages, $dupeMsgSource );
47 MessageWriter::writeMessagesToFile( $messages, $code, $write, $listUnknown, $removeUnknown, $messagesFolder );
50 /**
51 * Remove duplicates from a message array.
53 * @param $oldMsgArray array The input message array.
54 * @param $dupeMsgSource string The source file path for duplicates.
55 * @return Array $newMsgArray The output message array, with duplicates removed.
57 function removeDupes( $oldMsgArray, $dupeMsgSource ) {
58 if ( file_exists( $dupeMsgSource ) ) {
59 include $dupeMsgSource;
60 if ( !isset( $dupeMessages ) ) {
61 echo "There are no duplicated messages in the source file provided.";
62 exit( 1 );
64 } else {
65 echo "The specified file $dupeMsgSource cannot be found.";
66 exit( 1 );
68 $newMsgArray = $oldMsgArray;
69 foreach ( $oldMsgArray as $key => $value ) {
70 if ( array_key_exists( $key, $dupeMessages ) ) {
71 unset( $newMsgArray[$key] );
74 return $newMsgArray;
77 # Show help
78 if ( isset( $options['help'] ) ) {
79 echo <<<TEXT
80 Run this script to rewrite the messages array in the files languages/messages/MessagesXX.php.
81 Parameters:
82 * lang: Language code (default: the installation default language). You can also specify "all" to check all the languages.
83 * help: Show this help.
84 Options:
85 * dry-run: Do not write the array to the file.
86 * no-unknown: Do not list the unknown messages.
87 * remove-unknown: Remove unknown messages.
88 * remove-duplicates: Remove duplicated messages based on a PHP source file.
89 * messages-folder: An alternative folder with MediaWiki messages.
91 TEXT;
92 exit( 1 );
95 # Get the language code
96 if ( isset( $options['lang'] ) ) {
97 $wgCode = $options['lang'];
98 } else {
99 $wgCode = $wgContLang->getCode();
102 # Get the duplicate message source
103 if ( isset( $options['remove-duplicates'] ) && ( strcmp( $options['remove-duplicates'], '' ) ) ) {
104 $wgDupeMessageSource = $options['remove-duplicates'];
105 } else {
106 $wgDupeMessageSource = '';
109 # Get the options
110 $wgWriteToFile = !isset( $options['dry-run'] );
111 $wgListUnknownMessages = !isset( $options['no-unknown'] );
112 $wgRemoveUnknownMessages = isset( $options['remove-unknown'] );
113 $wgRemoveDuplicateMessages = isset( $options['remove-duplicates'] );
114 $messagesFolder = isset( $options['messages-folder'] ) ? $options['messages-folder'] : false;
116 # Get language objects
117 $languages = new languages();
119 # Write all the language
120 if ( $wgCode == 'all' ) {
121 foreach ( $languages->getLanguages() as $languageCode ) {
122 rebuildLanguage( $languages, $languageCode, $wgWriteToFile, $wgListUnknownMessages, $wgRemoveUnknownMessages, $wgRemoveDuplicateMessages, $wgDupeMessageSource, $messagesFolder );
124 } else {
125 rebuildLanguage( $languages, $wgCode, $wgWriteToFile, $wgListUnknownMessages, $wgRemoveUnknownMessages, $wgRemoveDuplicateMessages, $wgDupeMessageSource, $messagesFolder );