Part of bug 26280: added license headers to PHP files in maintenance
[lhc/web/wiklou.git] / maintenance / language / rebuildLanguage.php
1 <?php
2 /**
3 * Rewrite the messages array in the files languages/messages/MessagesXx.php.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup MaintenanceLanguage
22 * @defgroup MaintenanceLanguage MaintenanceLanguage
23 */
24
25 require_once( dirname( __FILE__ ) . '/../commandLine.inc' );
26 require_once( 'languages.inc' );
27 require_once( 'writeMessagesArray.inc' );
28
29 /**
30 * Rewrite a messages array.
31 *
32 * @param $code The language code.
33 * @param $write Write to the messages file?
34 * @param $listUnknown List the unknown messages?
35 * @param $removeUnknown Remove the unknown messages?
36 * @param $removeDupes Remove the duplicated messages?
37 * @param $dupeMsgSource The source file intended to remove from the array.
38 */
39 function rebuildLanguage( $languages, $code, $write, $listUnknown, $removeUnknown, $removeDupes, $dupeMsgSource ) {
40 $messages = $languages->getMessages( $code );
41 $messages = $messages['all'];
42 if ( $removeDupes ) {
43 $messages = removeDupes( $messages, $dupeMsgSource );
44 }
45 MessageWriter::writeMessagesToFile( $messages, $code, $write, $listUnknown, $removeUnknown );
46 }
47
48 /**
49 * Remove duplicates from a message array.
50 *
51 * @param $oldMsgArray The input message array.
52 * @param $dupeMsgSource The source file path for duplicates.
53 * @return $newMsgArray The output message array, with duplicates removed.
54 */
55 function removeDupes( $oldMsgArray, $dupeMsgSource ) {
56 if ( file_exists( $dupeMsgSource ) ) {
57 include( $dupeMsgSource );
58 if ( !isset( $dupeMessages ) ) {
59 echo( "There are no duplicated messages in the source file provided." );
60 exit( 1 );
61 }
62 } else {
63 echo ( "The specified file $dupeMsgSource cannot be found." );
64 exit( 1 );
65 }
66 $newMsgArray = $oldMsgArray;
67 foreach ( $oldMsgArray as $key => $value ) {
68 if ( array_key_exists( $key, $dupeMessages ) ) {
69 unset( $newMsgArray[$key] );
70 }
71 }
72 return $newMsgArray;
73 }
74
75 # Show help
76 if ( isset( $options['help'] ) ) {
77 echo <<<TEXT
78 Run this script to rewrite the messages array in the files languages/messages/MessagesXX.php.
79 Parameters:
80 * lang: Language code (default: the installation default language). You can also specify "all" to check all the languages.
81 * help: Show this help.
82 Options:
83 * dry-run: Do not write the array to the file.
84 * no-unknown: Do not list the unknown messages.
85 * remove-unknown: Remove unknown messages.
86 * remove-duplicates: Remove duplicated messages based on a PHP source file.
87
88 TEXT;
89 exit( 1 );
90 }
91
92 # Get the language code
93 if ( isset( $options['lang'] ) ) {
94 $wgCode = $options['lang'];
95 } else {
96 $wgCode = $wgContLang->getCode();
97 }
98
99 # Get the duplicate message source
100 if ( isset( $options['remove-duplicates'] ) && ( strcmp( $options['remove-duplicates'], '' ) ) ) {
101 $wgDupeMessageSource = $options['remove-duplicates'];
102 } else {
103 $wgDupeMessageSource = '';
104 }
105
106 # Get the options
107 $wgWriteToFile = !isset( $options['dry-run'] );
108 $wgListUnknownMessages = !isset( $options['no-unknown'] );
109 $wgRemoveUnknownMessages = isset( $options['remove-unknown'] );
110 $wgRemoveDuplicateMessages = isset( $options['remove-duplicates'] );
111
112 # Get language objects
113 $languages = new languages();
114
115 # Write all the language
116 if ( $wgCode == 'all' ) {
117 foreach ( $languages->getLanguages() as $languageCode ) {
118 rebuildLanguage( $languages, $languageCode, $wgWriteToFile, $wgListUnknownMessages, $wgRemoveUnknownMessages, $wgRemoveDuplicateMessages, $wgDupeMessageSource );
119 }
120 } else {
121 rebuildLanguage( $languages, $wgCode, $wgWriteToFile, $wgListUnknownMessages, $wgRemoveUnknownMessages, $wgRemoveDuplicateMessages, $wgDupeMessageSource );
122 }