summaryrefslogtreecommitdiff
path: root/maintenance/language/checkExtensioni18n.php
blob: 7a131a08ee0765a1a92ef1ef103e4cfb7a5c18bb (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/**
 * Copyright (C) 2007 Ashar Voultoiz <hashar@altern.org>
 *
 * Based on dumpBackup:
 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
 *
 * http://www.mediawiki.org
 *
 * 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
 *
 * @addtogroup SpecialPage
 */

#
# Lacking documentation. Examples:
# php checkExtensioni18n.php /opt/mw/extensions/CentralAuth/CentralAuth.i18n.php wgCentralAuthMessages
# php checkExtensioni18n.php --extdir /opt/mw/extensions/
#
# BUGS: cant guess registered extensions :)
# TODO: let users set parameters to configure checklanguage.inc (it uses globals)

// Filename for the extension i18n files database:
define( 'EXT_I18N_DB', 'i18n.db' );

// Global parameters for checkLanguage.inc
$wgDisplayLevel = 2;
$wgChecks = array( 'untranslated', 'obsolete', 'variables', 'empty', 'whitespace', 'xhtml', 'chars' );

$optionsWithArgs = array( 'extdir', 'lang' );

require_once( dirname(__FILE__).'/../commandLine.inc' );
require_once( 'languages.inc' );
require_once( 'checkLanguage.inc' );


class extensionLanguages extends languages {
	private $mExt18nFilename, $mExtArrayName ;
	private $mExtArray;

	function __construct( $ext18nFilename, $extArrayName ) {
		$this->mExt18nFilename = $ext18nFilename;
		$this->mExtArrayName = $extArrayName;

		$this->mIgnoredMessages = array();
		$this->mOptionalMessages = array();

		if ( file_exists( $this->mExt18nFilename ) ) {
			require_once( $this->mExt18nFilename );

			$foundarray = false;
			if( isset( ${$this->mExtArrayName} ) )  {
				// File provided in the db file
				$foundarray = ${$this->mExtArrayName};
			} else {

				/* For extensions included elsewhere. For some reason other extensions
				 * break with the global statement, so recheck here.
				 */
				global ${$this->mExtArrayName};
				if( is_array( ${$this->mExtArrayName} ) )  {
					$foundarray = ${$this->mExtArrayName};
				}

				/* we might have been given a function name, test it too */
				if( function_exists( $this->mExtArrayName  ) ) {
					// Load data
					$funcName = $this->mExtArrayName ;
					$foundarray = $funcName();
				}

				if(!$foundarray) {
					// Provided array could not be found we try to guess it.

					# Using the extension path ($m[1]) and filename ($m[2]):
					$m = array();
					preg_match( '%.*/(.*)/(.*).i18n\.php%', $this->mExt18nFilename, $m);
					$arPathCandidate = 'wg' . $m[1].'Messages';
					$arFileCandidate = 'wg' . $m[2].'Messages';
					$funcCandidate = "ef{$m[2]}Messages";

					// Try them:
					if( isset($$arPathCandidate) && is_array( $$arPathCandidate ) ) {
						print "warning> messages from guessed path array \$$arPathCandidate.\n";
						$foundarray = $$arPathCandidate;
					} elseif( isset($$arFileCandidate) && is_array( $$arFileCandidate ) ) {
						print "warning> messages from guessed file array \$$arFileCandidate.\n";
						$foundarray = $$arFileCandidate;
					} elseif( function_exists( $funcCandidate ) ) {
						print "warning> messages build from guessed function {$funcCandidate}().\n";
						$foundarray = $funcCandidate();
					}
				}

				# We are unlucky, return empty stuff
				if(!$foundarray) {
					print "ERROR> failed to guess an array to use.\n";
					$this->mExtArray = null;
					$this->mLanguages = null;
					return;
				}
			}

			$this->mExtArray = $foundarray ;
			$this->mLanguages = array_keys( $this->mExtArray );
		} else {
			wfDie( "File $this->mExt18nFilename not found\n" );
		}
	}

	protected function loadRawMessages( $code ) {
		if ( isset( $this->mRawMessages[$code] ) ) {
			return;
		}
		if( isset( $this->mExtArray[$code] ) ) {
			$this->mRawMessages[$code] = $this->mExtArray[$code] ;
		} else {
			$this->mRawMessages[$code] = array();
		}
	}

	public function getLanguages() {
		return $this->mLanguages;
	}
}

/**
 * @param $filename Filename containing the extension i18n
 * @param $arrayname The name of the array in the filename
 * @param $filter Optional, restrict check to a given language code (default; null)
 */
function checkExtensionLanguage( $filename, $arrayname, $filter = null ) {
	global $wgGeneralMessages, $wgRequiredMessagesNumber;

	$extLanguages = new extensionLanguages($filename, $arrayname);

	// Stuff needed by the checkLanguage routine (globals)
	$wgGeneralMessages = $extLanguages->getGeneralMessages();
	$wgRequiredMessagesNumber = count( $wgGeneralMessages['required'] );

	$langs = $extLanguages->getLanguages();
	if( !$langs ) {
		print "ERROR> \$$arrayname array does not exist.\n";
		return false;
	}

	$nErrors = 0;
	if( $filter ) {
		$nErrors += checkLanguage( $extLanguages, $filter );
	} else {
		print "Will check ". count($langs) . " languages : " . implode(' ', $langs) .".\n";
		foreach( $langs as $lang ) {
			if( $lang == 'en' ) {
				#print "Skipped english language\n";
				continue;
			}

			$nErrors += checkLanguage( $extLanguages, $lang );
		}
	}

	return $nErrors;
}

/**
 * Read the db file, parse it, start the check.
 */
function checkExtensionRepository( $extdir, $db ) {
	$fh = fopen( $extdir. '/' . $db, 'r' );

	$line_number = 0;
	while( $line = fgets( $fh ) ) {
		$line_number++;

		// Ignore comments
		if( preg_match( '/^#/', $line ) ) {
			continue;
		}

		// Load data from i18n database
		$data = split( ' ', chop($line) );
		$i18n_file = @$data[0];
		$arrayname = @$data[1];

		print "------------------------------------------------------\n";
		print "Checking $i18n_file (\$$arrayname).\n";

		// Check data
		if( !file_exists( $extdir . '/' . $i18n_file ) ) {
			print "ERROR> $i18n_file not found ($db:$line_number).\n";
			continue;
		}
#		if( $arrayname == '' ) {
#			print "warning> no array name for $i18n_file ($db:$line_number).\n";
#		}

 		$i18n_file = $extdir . '/' . $i18n_file ;

		global $myLang;
		$nErrors = checkExtensionLanguage( $i18n_file, $arrayname, $myLang );
		if($nErrors == 1 ) {
			print "\nFound $nErrors error for this extension.\n";
		} elseif($nErrors) {
			print "\nFound $nErrors errors for this extension.\n";
		} else {
			print "Looks OK.\n";
		}

		print "\n";
	}
}


function usage() {
// Usage
print <<<END
Usage:
    php checkExtensioni18n.php <filename> <arrayname>
    php checkExtensioni18n.php --extdir <extension repository>

Common option:
    --lang <language code> : only check the given language.


END;
die;
}

// Play with options and arguments
$myLang = isset($options['lang']) ? $options['lang'] : null;

if( isset( $options['extdir'] ) ) {
	$extdb = $options['extdir'] . '/' . EXT_I18N_DB ;

	if( file_exists( $extdb ) ) {
		checkExtensionRepository( $options['extdir'], EXT_I18N_DB );
	} else {
		print "$extdb does not exist\n";
	}

} else {
	// Check arguments
	if ( isset( $argv[0] ) ) {

		if (file_exists( $argv[0] ) ) {
			$filename = $argv[0];
		} else {
			print "Unable to open file '{$argv[0]}'\n";
			usage();
		}

		if ( isset( $argv[1] ) ) {
			$arrayname = $argv[1];
		} else {
			print "You must give an array name to be checked\n";
			usage();
		}

		global $myLang;
		checkExtensionLanguage( $filename, $arrayname, $myLang );
	} else {
		usage();
	}
}

?>