summaryrefslogtreecommitdiff
path: root/maintenance/language/rebuildLanguage.php
blob: 9b3a4b9d7fc4586396037d0f647059172089e721 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
 * Rewrite the messages array in the files languages/messages/MessagesXx.php.
 *
 * 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( 'languages.inc' );
require_once( 'writeMessagesArray.inc' );

/**
 * Rewrite a messages array.
 *
 * @param $languages
 * @param $code 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 The source file intended to remove from the array.
 */
function rebuildLanguage( $languages, $code, $write, $listUnknown, $removeUnknown, $removeDupes, $dupeMsgSource ) {
	$messages = $languages->getMessages( $code );
	$messages = $messages['all'];
	if ( $removeDupes ) {
		$messages = removeDupes( $messages, $dupeMsgSource );
	}
	MessageWriter::writeMessagesToFile( $messages, $code, $write, $listUnknown, $removeUnknown );
}

/**
 * Remove duplicates from a message array.
 *
 * @param $oldMsgArray The input message array.
 * @param $dupeMsgSource 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 ( "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 <<<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: 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.

TEXT;
	exit( 1 );
}

# Get the language code
if ( isset( $options['lang'] ) ) {
	$wgCode = $options['lang'];
} else {
	$wgCode = $wgContLang->getCode();
}

# 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'] );

# Get language objects
$languages = new languages();

# Write all the language
if ( $wgCode == 'all' ) {
	foreach ( $languages->getLanguages() as $languageCode ) {
		rebuildLanguage( $languages, $languageCode, $wgWriteToFile, $wgListUnknownMessages, $wgRemoveUnknownMessages, $wgRemoveDuplicateMessages, $wgDupeMessageSource );
	}
} else {
	rebuildLanguage( $languages, $wgCode, $wgWriteToFile, $wgListUnknownMessages, $wgRemoveUnknownMessages, $wgRemoveDuplicateMessages, $wgDupeMessageSource );
}