]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/language/checkExtensions.php
MediaWiki 1.11.0
[autoinstalls/mediawiki.git] / maintenance / language / checkExtensions.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 Maintenance
26  */
27
28 #
29 # Lacking documentation. Examples:
30 # php checkExtensions.php /opt/mw/extensions/CentralAuth/CentralAuth.i18n.php wgCentralAuthMessages
31 # php checkExtensions.php --extdir /opt/mw/extensions/
32 #
33 # BUGS: cant guess registered extensions :)
34 # TODO: let users set parameters to configure checklanguage.inc
35
36 // Filename for the extension i18n files database:
37 define( 'EXT_I18N_DB', 'i18n.db' );
38
39 $optionsWithArgs = array( 'extdir', 'lang' );
40
41 require_once( dirname(__FILE__).'/../commandLine.inc' );
42 require_once( 'languages.inc' );
43 require_once( 'checkLanguage.inc' );
44
45
46 class extensionLanguages extends languages {
47         private $mExt18nFilename, $mExtArrayName ;
48         private $mExtArray;
49
50         function __construct( $ext18nFilename, $extArrayName ) {
51                 $this->mExt18nFilename = $ext18nFilename;
52                 $this->mExtArrayName = $extArrayName;
53
54                 $this->mIgnoredMessages = array();
55                 $this->mOptionalMessages = array();
56
57                 if ( file_exists( $this->mExt18nFilename ) ) {
58                         require_once( $this->mExt18nFilename );
59
60                         $foundarray = false;
61                         if( isset( ${$this->mExtArrayName} ) )  {
62                                 // File provided in the db file
63                                 $foundarray = ${$this->mExtArrayName};
64                         } else {
65
66                                 /* For extensions included elsewhere. For some reason other extensions
67                                  * break with the global statement, so recheck here.
68                                  */
69                                 global ${$this->mExtArrayName};
70                                 if( is_array( ${$this->mExtArrayName} ) )  {
71                                         $foundarray = ${$this->mExtArrayName};
72                                 }
73
74                                 /* we might have been given a function name, test it too */
75                                 if( function_exists( $this->mExtArrayName  ) ) {
76                                         // Load data
77                                         $funcName = $this->mExtArrayName ;
78                                         $foundarray = $funcName();
79                                 }
80
81                                 if(!$foundarray) {
82                                         // Provided array could not be found we try to guess it.
83
84                                         # Using the extension path ($m[1]) and filename ($m[2]):
85                                         $m = array();
86                                         preg_match( '%.*/(.*)/(.*).i18n\.php%', $this->mExt18nFilename, $m);
87                                         $arPathCandidate = 'wg' . $m[1].'Messages';
88                                         $arFileCandidate = 'wg' . $m[2].'Messages';
89                                         $funcCandidate = "ef{$m[2]}Messages";
90
91                                         // Try them:
92                                         if( isset($$arPathCandidate) && is_array( $$arPathCandidate ) ) {
93                                                 print "warning> messages from guessed path array \$$arPathCandidate.\n";
94                                                 $foundarray = $$arPathCandidate;
95                                         } elseif( isset($$arFileCandidate) && is_array( $$arFileCandidate ) ) {
96                                                 print "warning> messages from guessed file array \$$arFileCandidate.\n";
97                                                 $foundarray = $$arFileCandidate;
98                                         } elseif( function_exists( $funcCandidate ) ) {
99                                                 print "warning> messages build from guessed function {$funcCandidate}().\n";
100                                                 $foundarray = $funcCandidate();
101                                         }
102                                 }
103
104                                 # We are unlucky, return empty stuff
105                                 if(!$foundarray) {
106                                         print "ERROR> failed to guess an array to use.\n";
107                                         $this->mExtArray = null;
108                                         $this->mLanguages = null;
109                                         return;
110                                 }
111                         }
112
113                         $this->mExtArray = $foundarray ;
114                         $this->mLanguages = array_keys( $this->mExtArray );
115                 } else {
116                         wfDie( "File $this->mExt18nFilename not found\n" );
117                 }
118         }
119
120         protected function loadRawMessages( $code ) {
121                 if ( isset( $this->mRawMessages[$code] ) ) {
122                         return;
123                 }
124                 if( isset( $this->mExtArray[$code] ) ) {
125                         $this->mRawMessages[$code] = $this->mExtArray[$code] ;
126                 } else {
127                         $this->mRawMessages[$code] = array();
128                 }
129         }
130
131         public function getLanguages() {
132                 return $this->mLanguages;
133         }
134 }
135
136 /**
137  * @param $filename Filename containing the extension i18n
138  * @param $arrayname The name of the array in the filename
139  * @param $filter Optional, restrict check to a given language code (default; null)
140  */
141 function checkExtensionLanguage( $filename, $arrayname, $filter = null ) {
142         $extLanguages = new extensionLanguages($filename, $arrayname);
143
144         $langs = $extLanguages->getLanguages();
145         if( !$langs ) {
146                 print "ERROR> \$$arrayname array does not exist.\n";
147                 return false;
148         }
149
150         $nErrors = 0;
151         if( $filter ) {
152                 $nErrors += checkLanguage( $extLanguages, $filter );
153         } else {
154                 print "Will check ". count($langs) . " languages : " . implode(' ', $langs) .".\n";
155                 foreach( $langs as $lang ) {
156                         if( $lang == 'en' ) {
157                                 #print "Skipped english language\n";
158                                 continue;
159                         }
160
161                         $nErrors += checkLanguage( $extLanguages, $lang );
162                 }
163         }
164
165         return $nErrors;
166 }
167
168 /**
169  * Read the db file, parse it, start the check.
170  */
171 function checkExtensionRepository( $extdir, $db ) {
172         $fh = fopen( $extdir. '/' . $db, 'r' );
173
174         $line_number = 0;
175         while( $line = fgets( $fh ) ) {
176                 $line_number++;
177
178                 // Ignore comments
179                 if( preg_match( '/^#/', $line ) ) {
180                         continue;
181                 }
182
183                 // Load data from i18n database
184                 $data = split( ' ', chop($line) );
185                 $i18n_file = @$data[0];
186                 $arrayname = @$data[1];
187
188                 print "------------------------------------------------------\n";
189                 print "Checking $i18n_file (\$$arrayname).\n";
190
191                 // Check data
192                 if( !file_exists( $extdir . '/' . $i18n_file ) ) {
193                         print "ERROR> $i18n_file not found ($db:$line_number).\n";
194                         continue;
195                 }
196 #               if( $arrayname == '' ) {
197 #                       print "warning> no array name for $i18n_file ($db:$line_number).\n";
198 #               }
199
200                 $i18n_file = $extdir . '/' . $i18n_file ;
201
202                 global $myLang;
203                 $nErrors = checkExtensionLanguage( $i18n_file, $arrayname, $myLang );
204                 if($nErrors == 1 ) {
205                         print "\nFound $nErrors error for this extension.\n";
206                 } elseif($nErrors) {
207                         print "\nFound $nErrors errors for this extension.\n";
208                 } else {
209                         print "Looks OK.\n";
210                 }
211
212                 print "\n";
213         }
214 }
215
216
217 function usage() {
218 // Usage
219 print <<<END
220 Usage:
221     php checkExtensions.php <filename> <arrayname>
222     php checkExtensions.php --extdir <extension repository>
223
224 Common option:
225     --lang <language code> : only check the given language.
226
227
228 END;
229 die;
230 }
231
232 // Play with options and arguments
233 $myLang = isset($options['lang']) ? $options['lang'] : null;
234
235 if( isset( $options['extdir'] ) ) {
236         $extdb = $options['extdir'] . '/' . EXT_I18N_DB ;
237
238         if( file_exists( $extdb ) ) {
239                 checkExtensionRepository( $options['extdir'], EXT_I18N_DB );
240         } else {
241                 print "$extdb does not exist\n";
242         }
243
244 } else {
245         // Check arguments
246         if ( isset( $argv[0] ) ) {
247
248                 if (file_exists( $argv[0] ) ) {
249                         $filename = $argv[0];
250                 } else {
251                         print "Unable to open file '{$argv[0]}'\n";
252                         usage();
253                 }
254
255                 if ( isset( $argv[1] ) ) {
256                         $arrayname = $argv[1];
257                 } else {
258                         print "You must give an array name to be checked\n";
259                         usage();
260                 }
261
262                 global $myLang;
263                 checkExtensionLanguage( $filename, $arrayname, $myLang );
264         } else {
265                 usage();
266         }
267 }
268
269