7a131a08ee0765a1a92ef1ef103e4cfb7a5c18bb
[lhc/web/wiklou.git] / maintenance / language / checkExtensioni18n.php
1 <?php
2 /**
3 * Copyright (C) 2007 Ashar Voultoiz <hashar@altern.org>
4 *
5 * Based on dumpBackup:
6 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
7 *
8 * http://www.mediawiki.org
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @addtogroup SpecialPage
26 */
27
28 #
29 # Lacking documentation. Examples:
30 # php checkExtensioni18n.php /opt/mw/extensions/CentralAuth/CentralAuth.i18n.php wgCentralAuthMessages
31 # php checkExtensioni18n.php --extdir /opt/mw/extensions/
32 #
33 # BUGS: cant guess registered extensions :)
34 # TODO: let users set parameters to configure checklanguage.inc (it uses globals)
35
36 // Filename for the extension i18n files database:
37 define( 'EXT_I18N_DB', 'i18n.db' );
38
39 // Global parameters for checkLanguage.inc
40 $wgDisplayLevel = 2;
41 $wgChecks = array( 'untranslated', 'obsolete', 'variables', 'empty', 'whitespace', 'xhtml', 'chars' );
42
43 $optionsWithArgs = array( 'extdir', 'lang' );
44
45 require_once( dirname(__FILE__).'/../commandLine.inc' );
46 require_once( 'languages.inc' );
47 require_once( 'checkLanguage.inc' );
48
49
50 class extensionLanguages extends languages {
51 private $mExt18nFilename, $mExtArrayName ;
52 private $mExtArray;
53
54 function __construct( $ext18nFilename, $extArrayName ) {
55 $this->mExt18nFilename = $ext18nFilename;
56 $this->mExtArrayName = $extArrayName;
57
58 $this->mIgnoredMessages = array();
59 $this->mOptionalMessages = array();
60
61 if ( file_exists( $this->mExt18nFilename ) ) {
62 require_once( $this->mExt18nFilename );
63
64 $foundarray = false;
65 if( isset( ${$this->mExtArrayName} ) ) {
66 // File provided in the db file
67 $foundarray = ${$this->mExtArrayName};
68 } else {
69
70 /* For extensions included elsewhere. For some reason other extensions
71 * break with the global statement, so recheck here.
72 */
73 global ${$this->mExtArrayName};
74 if( is_array( ${$this->mExtArrayName} ) ) {
75 $foundarray = ${$this->mExtArrayName};
76 }
77
78 /* we might have been given a function name, test it too */
79 if( function_exists( $this->mExtArrayName ) ) {
80 // Load data
81 $funcName = $this->mExtArrayName ;
82 $foundarray = $funcName();
83 }
84
85 if(!$foundarray) {
86 // Provided array could not be found we try to guess it.
87
88 # Using the extension path ($m[1]) and filename ($m[2]):
89 $m = array();
90 preg_match( '%.*/(.*)/(.*).i18n\.php%', $this->mExt18nFilename, $m);
91 $arPathCandidate = 'wg' . $m[1].'Messages';
92 $arFileCandidate = 'wg' . $m[2].'Messages';
93 $funcCandidate = "ef{$m[2]}Messages";
94
95 // Try them:
96 if( isset($$arPathCandidate) && is_array( $$arPathCandidate ) ) {
97 print "warning> messages from guessed path array \$$arPathCandidate.\n";
98 $foundarray = $$arPathCandidate;
99 } elseif( isset($$arFileCandidate) && is_array( $$arFileCandidate ) ) {
100 print "warning> messages from guessed file array \$$arFileCandidate.\n";
101 $foundarray = $$arFileCandidate;
102 } elseif( function_exists( $funcCandidate ) ) {
103 print "warning> messages build from guessed function {$funcCandidate}().\n";
104 $foundarray = $funcCandidate();
105 }
106 }
107
108 # We are unlucky, return empty stuff
109 if(!$foundarray) {
110 print "ERROR> failed to guess an array to use.\n";
111 $this->mExtArray = null;
112 $this->mLanguages = null;
113 return;
114 }
115 }
116
117 $this->mExtArray = $foundarray ;
118 $this->mLanguages = array_keys( $this->mExtArray );
119 } else {
120 wfDie( "File $this->mExt18nFilename not found\n" );
121 }
122 }
123
124 protected function loadRawMessages( $code ) {
125 if ( isset( $this->mRawMessages[$code] ) ) {
126 return;
127 }
128 if( isset( $this->mExtArray[$code] ) ) {
129 $this->mRawMessages[$code] = $this->mExtArray[$code] ;
130 } else {
131 $this->mRawMessages[$code] = array();
132 }
133 }
134
135 public function getLanguages() {
136 return $this->mLanguages;
137 }
138 }
139
140 /**
141 * @param $filename Filename containing the extension i18n
142 * @param $arrayname The name of the array in the filename
143 * @param $filter Optional, restrict check to a given language code (default; null)
144 */
145 function checkExtensionLanguage( $filename, $arrayname, $filter = null ) {
146 global $wgGeneralMessages, $wgRequiredMessagesNumber;
147
148 $extLanguages = new extensionLanguages($filename, $arrayname);
149
150 // Stuff needed by the checkLanguage routine (globals)
151 $wgGeneralMessages = $extLanguages->getGeneralMessages();
152 $wgRequiredMessagesNumber = count( $wgGeneralMessages['required'] );
153
154 $langs = $extLanguages->getLanguages();
155 if( !$langs ) {
156 print "ERROR> \$$arrayname array does not exist.\n";
157 return false;
158 }
159
160 $nErrors = 0;
161 if( $filter ) {
162 $nErrors += checkLanguage( $extLanguages, $filter );
163 } else {
164 print "Will check ". count($langs) . " languages : " . implode(' ', $langs) .".\n";
165 foreach( $langs as $lang ) {
166 if( $lang == 'en' ) {
167 #print "Skipped english language\n";
168 continue;
169 }
170
171 $nErrors += checkLanguage( $extLanguages, $lang );
172 }
173 }
174
175 return $nErrors;
176 }
177
178 /**
179 * Read the db file, parse it, start the check.
180 */
181 function checkExtensionRepository( $extdir, $db ) {
182 $fh = fopen( $extdir. '/' . $db, 'r' );
183
184 $line_number = 0;
185 while( $line = fgets( $fh ) ) {
186 $line_number++;
187
188 // Ignore comments
189 if( preg_match( '/^#/', $line ) ) {
190 continue;
191 }
192
193 // Load data from i18n database
194 $data = split( ' ', chop($line) );
195 $i18n_file = @$data[0];
196 $arrayname = @$data[1];
197
198 print "------------------------------------------------------\n";
199 print "Checking $i18n_file (\$$arrayname).\n";
200
201 // Check data
202 if( !file_exists( $extdir . '/' . $i18n_file ) ) {
203 print "ERROR> $i18n_file not found ($db:$line_number).\n";
204 continue;
205 }
206 # if( $arrayname == '' ) {
207 # print "warning> no array name for $i18n_file ($db:$line_number).\n";
208 # }
209
210 $i18n_file = $extdir . '/' . $i18n_file ;
211
212 global $myLang;
213 $nErrors = checkExtensionLanguage( $i18n_file, $arrayname, $myLang );
214 if($nErrors == 1 ) {
215 print "\nFound $nErrors error for this extension.\n";
216 } elseif($nErrors) {
217 print "\nFound $nErrors errors for this extension.\n";
218 } else {
219 print "Looks OK.\n";
220 }
221
222 print "\n";
223 }
224 }
225
226
227 function usage() {
228 // Usage
229 print <<<END
230 Usage:
231 php checkExtensioni18n.php <filename> <arrayname>
232 php checkExtensioni18n.php --extdir <extension repository>
233
234 Common option:
235 --lang <language code> : only check the given language.
236
237
238 END;
239 die;
240 }
241
242 // Play with options and arguments
243 $myLang = isset($options['lang']) ? $options['lang'] : null;
244
245 if( isset( $options['extdir'] ) ) {
246 $extdb = $options['extdir'] . '/' . EXT_I18N_DB ;
247
248 if( file_exists( $extdb ) ) {
249 checkExtensionRepository( $options['extdir'], EXT_I18N_DB );
250 } else {
251 print "$extdb does not exist\n";
252 }
253
254 } else {
255 // Check arguments
256 if ( isset( $argv[0] ) ) {
257
258 if (file_exists( $argv[0] ) ) {
259 $filename = $argv[0];
260 } else {
261 print "Unable to open file '{$argv[0]}'\n";
262 usage();
263 }
264
265 if ( isset( $argv[1] ) ) {
266 $arrayname = $argv[1];
267 } else {
268 print "You must give an array name to be checked\n";
269 usage();
270 }
271
272 global $myLang;
273 checkExtensionLanguage( $filename, $arrayname, $myLang );
274 } else {
275 usage();
276 }
277 }
278
279 ?>