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