Merge "BacklinkCache performance tweaks"
[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 __DIR__ . '/../commandLine.inc';
26 require_once 'languages.inc';
27 require_once 'writeMessagesArray.inc';
28
29 /**
30 * Rewrite a messages array.
31 *
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.
40 */
41 function rebuildLanguage( $languages, $code, $write, $listUnknown, $removeUnknown,
42 $removeDupes, $dupeMsgSource, $messagesFolder
43 ) {
44 $messages = $languages->getMessages( $code );
45 $messages = $messages['all'];
46 if ( $removeDupes ) {
47 $messages = removeDupes( $messages, $dupeMsgSource );
48 }
49 MessageWriter::writeMessagesToFile(
50 $messages,
51 $code,
52 $write,
53 $listUnknown,
54 $removeUnknown,
55 $messagesFolder
56 );
57 }
58
59 /**
60 * Remove duplicates from a message array.
61 *
62 * @param $oldMsgArray array The input message array.
63 * @param $dupeMsgSource string The source file path for duplicates.
64 * @return Array $newMsgArray The output message array, with duplicates removed.
65 */
66 function removeDupes( $oldMsgArray, $dupeMsgSource ) {
67 if ( file_exists( $dupeMsgSource ) ) {
68 include $dupeMsgSource;
69 if ( !isset( $dupeMessages ) ) {
70 echo "There are no duplicated messages in the source file provided.";
71 exit( 1 );
72 }
73 } else {
74 echo "The specified file $dupeMsgSource cannot be found.";
75 exit( 1 );
76 }
77 $newMsgArray = $oldMsgArray;
78 foreach ( $oldMsgArray as $key => $value ) {
79 if ( array_key_exists( $key, $dupeMessages ) ) {
80 unset( $newMsgArray[$key] );
81 }
82 }
83
84 return $newMsgArray;
85 }
86
87 # Show help
88 if ( isset( $options['help'] ) ) {
89 echo <<<TEXT
90 Run this script to rewrite the messages array in the files
91 languages/messages/MessagesXX.php.
92 Parameters:
93 * lang: Language code (default: the installation default language).
94 You can also specify "all" to check all the languages.
95 * help: Show this help.
96 Options:
97 * dry-run: Do not write the array to the file.
98 * no-unknown: Do not list the unknown messages.
99 * remove-unknown: Remove unknown messages.
100 * remove-duplicates: Remove duplicated messages based on a PHP source file.
101 * messages-folder: An alternative folder with MediaWiki messages.
102
103 TEXT;
104 exit( 1 );
105 }
106
107 # Get the language code
108 if ( isset( $options['lang'] ) ) {
109 $wgCode = $options['lang'];
110 } else {
111 $wgCode = $wgContLang->getCode();
112 }
113
114 # Get the duplicate message source
115 if ( isset( $options['remove-duplicates'] ) && ( strcmp( $options['remove-duplicates'], '' ) ) ) {
116 $wgDupeMessageSource = $options['remove-duplicates'];
117 } else {
118 $wgDupeMessageSource = '';
119 }
120
121 # Get the options
122 $wgWriteToFile = !isset( $options['dry-run'] );
123 $wgListUnknownMessages = !isset( $options['no-unknown'] );
124 $wgRemoveUnknownMessages = isset( $options['remove-unknown'] );
125 $wgRemoveDuplicateMessages = isset( $options['remove-duplicates'] );
126 $messagesFolder = isset( $options['messages-folder'] ) ? $options['messages-folder'] : false;
127
128 # Get language objects
129 $languages = new Languages();
130
131 # Write all the language
132 if ( $wgCode == 'all' ) {
133 foreach ( $languages->getLanguages() as $languageCode ) {
134 rebuildLanguage(
135 $languages,
136 $languageCode,
137 $wgWriteToFile,
138 $wgListUnknownMessages,
139 $wgRemoveUnknownMessages,
140 $wgRemoveDuplicateMessages,
141 $wgDupeMessageSource,
142 $messagesFolder
143 );
144 }
145 } else {
146 rebuildLanguage(
147 $languages,
148 $wgCode,
149 $wgWriteToFile,
150 $wgListUnknownMessages,
151 $wgRemoveUnknownMessages,
152 $wgRemoveDuplicateMessages,
153 $wgDupeMessageSource,
154 $messagesFolder
155 );
156 }