9a14542731b354acd5193e71ee8f9ac2ee2a1567
[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 * @package MediaWiki
26 * @subpackage SpecialPage
27 */
28
29 #
30 # Lacking documentation. Examples:
31 # php checkExtensioni18n.php /opt/mw/extensions/CentralAuth/CentralAuth.i18n.php wgCentralAuthMessages
32 # php checkExtensioni18n.php --extdir /opt/mw/extensions/
33 #
34 # BUGS: cant guess registered extensions :)
35 # TODO: let users set parameters to configure checklanguage.inc (it uses globals)
36
37 // Filename for the extension i18n files database:
38 define( 'EXT_I18N_DB', 'i18n.db' );
39
40 // Global parameters for checkLanguage.inc
41 $wgDisplayLevel = 2;
42 $wgChecks = array( 'untranslated', 'obsolete', 'variables', 'empty', 'whitespace', 'xhtml', 'chars' );
43
44
45 $optionsWithArgs = array( 'extdir' );
46
47
48 require_once( dirname(__FILE__).'/../commandLine.inc' );
49 require_once( 'languages.inc' );
50 require_once( 'checkLanguage.inc' );
51
52
53 class extensionLanguages extends languages {
54 private $mExt18nFilename, $mExtArrayName ;
55 private $mExtArray;
56
57 function __construct( $ext18nFilename, $extArrayName ) {
58 $this->mExt18nFilename = $ext18nFilename;
59 $this->mExtArrayName = $extArrayName;
60
61 $this->mIgnoredMessages = array() ;
62 $this->mOptionalMessages = array() ;
63
64 if ( file_exists( $this->mExt18nFilename ) ) {
65 require_once( $this->mExt18nFilename );
66
67 $foundarray = false ;
68
69 if( isset( ${$this->mExtArrayName} ) ) {
70 // File provided in the db file
71 $foundarray = ${$this->mExtArrayName} ;
72 } else {
73 // Provided array could not be found we try to guess it.
74
75 # Using the extension path ($m[1]) and filename ($m[2]):
76 $m = array();
77 preg_match( '%.*/(.*)/(.*).i18n\.php%', $this->mExt18nFilename, $m);
78 $arPathCandidate = 'wg' . $m[1].'Messages';
79 $arFileCandidate = 'wg' . $m[2].'Messages';
80 $funcCandidate = "ef{$m[2]}Messages";
81
82 // Try them:
83 if( isset($$arPathCandidate) && is_array( $$arPathCandidate ) ) {
84 print "warning> messages from guessed path array \$$arPathCandidate.\n";
85 $foundarray = $$arPathCandidate;
86 } elseif( isset($$arFileCandidate) && is_array( $$arFileCandidate ) ) {
87 print "warning> messages from guessed file array \$$arFileCandidate.\n";
88 $foundarray = $$arFileCandidate;
89 } elseif( function_exists( $funcCandidate ) ) {
90 print "warning> messages build from guessed function {$funcCandidate}().\n";
91 $foundarray = $funcCandidate();
92 }
93
94 # We are unlucky, return empty stuff
95 if(!$foundarray) {
96 print "ERROR> failed to guess an array to use.\n";
97 $this->mExtArray = null;
98 $this->mLanguages = null;
99 return;
100 }
101 }
102
103 $this->mExtArray = $foundarray ;
104 $this->mLanguages = array_keys( $this->mExtArray );
105 } else {
106 wfDie( "File $this->mExt18nFilename not found\n" );
107 }
108 }
109
110 protected function loadRawMessages( $code ) {
111 if ( isset( $this->mRawMessages[$code] ) ) {
112 return;
113 }
114 if( isset( $this->mExtArray[$code] ) ) {
115 $this->mRawMessages[$code] = $this->mExtArray[$code] ;
116 } else {
117 $this->mRawMessages[$code] = array();
118 }
119 }
120
121 public function getLanguages() {
122 return $this->mLanguages;
123 }
124 }
125
126
127 function checkExtensionLanguage( $filename, $arrayname ) {
128 global $wgGeneralMessages, $wgRequiredMessagesNumber;
129
130 $extLanguages = new extensionLanguages($filename, $arrayname);
131
132 // Stuff needed by the checkLanguage routine (globals)
133 $wgGeneralMessages = $extLanguages->getGeneralMessages();
134 $wgRequiredMessagesNumber = count( $wgGeneralMessages['required'] );
135
136 $langs = $extLanguages->getLanguages();
137 if( !$langs ) {
138 print "ERROR> \$$arrayname array does not exist.\n";
139 return false;
140 }
141
142 print "Found ". count($langs) . " languages : " . implode(' ', $langs) ."\n";
143 foreach( $langs as $lang ) {
144 if( $lang == 'en' ) {
145 #print "Skipped english language\n";
146 continue;
147 }
148
149 checkLanguage( $extLanguages, $lang );
150 }
151 }
152
153 function checkExtensionRepository( $extdir, $db ) {
154 $fh = fopen( $extdir. '/' . $db, 'r' );
155
156 $line_number = 0;
157 while( $line = fgets( $fh ) ) {
158 $line_number++;
159
160 // Ignore comments
161 if( preg_match( '/^#/', $line ) ) {
162 continue;
163 }
164
165 // Load data from i18n database
166 $data = split( ' ', chop($line) );
167 $i18n_file = @$data[0];
168 $arrayname = @$data[1];
169
170 print "------------------------------------------------------\n";
171 print "Checking $i18n_file (\$$arrayname).\n";
172
173 // Check data
174 if( !file_exists( $extdir . '/' . $i18n_file ) ) {
175 print "ERROR> $i18n_file not found ($db:$line_number).\n";
176 continue;
177 }
178 # if( $arrayname == '' ) {
179 # print "warning> no array name for $i18n_file ($db:$line_number).\n";
180 # }
181
182 $i18n_file = $extdir . '/' . $i18n_file ;
183
184 checkExtensionLanguage( $i18n_file, $arrayname );
185
186 print "\n";
187 }
188 }
189
190
191 function usage() {
192 // Usage
193 print <<<END
194 Usage:
195 php checkExtensioni18n.php <filename> <arrayname>
196 php checkExtensioni18n.php --extdir <exstention repository>
197
198
199 END;
200 die;
201 }
202
203 // Play with options and arguments
204 if( isset( $options['extdir'] ) ) {
205 $extdb = $options['extdir'] . '/' . EXT_I18N_DB ;
206
207 if( file_exists( $extdb ) ) {
208 checkExtensionRepository( $options['extdir'], EXT_I18N_DB );
209 } else {
210 print "$extdb does not exist\n";
211 }
212
213 } else {
214 // Check arguments
215 if ( isset( $argv[0] ) ) {
216
217 if (file_exists( $argv[0] ) ) {
218 $filename = $argv[0];
219 } else {
220 print "Unable to open file '{$argv[0]}'\n";
221 usage();
222 }
223
224 if ( isset( $argv[1] ) ) {
225 $arrayname = $argv[1];
226 } else {
227 print "You must give an array name to be checked\n";
228 usage();
229 }
230
231 checkExtensionLanguage( $filename, $arrayname );
232 } else {
233 usage();
234 }
235 }
236
237 ?>