Let's use the variables introduced in r59758 :)
[lhc/web/wiklou.git] / maintenance / syntaxChecker.php
1 <?php
2 /**
3 * Check syntax of all PHP files in MediaWiki
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @ingroup Maintenance
21 */
22
23 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
24
25 class SyntaxChecker extends Maintenance {
26
27 // List of files we're going to check
28 private $mFiles = array(), $mFailures = array(), $mWarnings = array();
29 private $mIgnorePaths = array(), $mNoStyleCheckPaths = array();
30
31 public function __construct() {
32 parent::__construct();
33 $this->mDescription = "Check syntax for all PHP files in MediaWiki";
34 $this->addOption( 'with-extensions', 'Also recurse the extensions folder' );
35 $this->addOption( 'path', 'Specific path (file or directory) to check, either with absolute path or relative to the root of this MediaWiki installation',
36 false, true);
37 $this->addOption( 'list-file', 'Text file containing list of files or directories to check', false, true);
38 $this->addOption( 'modified', 'Check only files that were modified (requires SVN command-line client)' );
39 $this->addOption( 'syntax-only', 'Check for syntax validity only, skip code style warnings' );
40 }
41
42 protected function getDbType() {
43 return Maintenance::DB_NONE;
44 }
45
46 public function execute() {
47 $this->buildFileList();
48
49 // ParseKit is broken on PHP 5.3+, disabled until this is fixed
50 $useParseKit = function_exists( 'parsekit_compile_file' ) && version_compare( PHP_VERSION, '5.3', '<' );
51
52 $this->output( "Checking syntax (this can take a really long time)...\n\n" );
53 foreach( $this->mFiles as $f ) {
54 if( $useParseKit ) {
55 $this->checkFileWithParsekit( $f );
56 } else {
57 $this->checkFileWithCli( $f );
58 }
59 if( !$this->hasOption( 'syntax-only' ) ) {
60 $this->checkForMistakes( $f );
61 }
62 }
63 $this->output( "\nDone! " . count( $this->mFiles ) . " files checked, " .
64 count( $this->mFailures ) . " failures and " . count( $this->mWarnings ) .
65 " warnings found\n" );
66 }
67
68 /**
69 * Build the list of files we'll check for syntax errors
70 */
71 private function buildFileList() {
72 global $IP;
73
74 $this->mIgnorePaths = array(
75 // Compat stuff, explodes on PHP 5.3
76 "includes/NamespaceCompat.php$",
77 "DiscussionThreading/REV",
78 );
79
80 $this->mNoStyleCheckPaths = array(
81 // Third-party code we don't care about
82 "/activemq_stomp/",
83 "EmailPage/phpMailer",
84 "FCKeditor/fckeditor/",
85 '\bphplot-',
86 "/svggraph/",
87 "\bjsmin.php$",
88 "OggHandler/PEAR/",
89 "QPoll/Excel/",
90 "/smarty/",
91 );
92
93 if ( $this->hasOption( 'path' ) ) {
94 $path = $this->getOption( 'path' );
95 if ( !$this->addPath( $path ) ) {
96 $this->error( "Error: can't find file or directory $path\n", true );
97 }
98 return; // process only this path
99 } elseif ( $this->hasOption( 'list-file' ) ) {
100 $file = $this->getOption( 'list-file' );
101 $f = @fopen( $file, 'r' );
102 if ( !$f ) {
103 $this->error( "Can't open file $file\n", true );
104 }
105 while( $path = trim( fgets( $f ) ) ) {
106 $this->addPath( $path );
107 }
108 fclose( $f );
109 return;
110 } elseif ( $this->hasOption( 'modified' ) ) {
111 $this->output( "Retrieving list from Subversion... " );
112 $parentDir = wfEscapeShellArg( dirname( __FILE__ ) . '/..' );
113 $output = wfShellExec( "svn status --ignore-externals $parentDir", $retval );
114 if ( $retval ) {
115 $this->error( "Error retrieving list from Subversion!\n", true );
116 } else {
117 $this->output( "done\n" );
118 }
119
120 preg_match_all( '/^\s*[AM]\s+(.*?)\r?$/m', $output, $matches );
121 foreach ( $matches[1] as $file ) {
122 if ( self::isSuitableFile( $file ) && !is_dir( $file ) ) {
123 $this->mFiles[] = $file;
124 }
125 }
126 return;
127 }
128
129 $this->output( "Building file list..." );
130
131 // Only check files in these directories.
132 // Don't just put $IP, because the recursive dir thingie goes into all subdirs
133 $dirs = array(
134 $IP . '/includes',
135 $IP . '/config',
136 $IP . '/languages',
137 $IP . '/maintenance',
138 $IP . '/skins',
139 );
140 if( $this->hasOption( 'with-extensions' ) ) {
141 $dirs[] = $IP . '/extensions';
142 }
143
144 foreach( $dirs as $d ) {
145 $this->addDirectoryContent( $d );
146 }
147
148 // Manually add two user-editable files that are usually sources of problems
149 if ( file_exists( "$IP/LocalSettings.php" ) ) {
150 $this->mFiles[] = "$IP/LocalSettings.php";
151 }
152 if ( file_exists( "$IP/AdminSettings.php" ) ) {
153 $this->mFiles[] = "$IP/AdminSettings.php";
154 }
155
156 $this->output( "done.\n" );
157 }
158
159 /**
160 * Returns true if $file is of a type we can check
161 */
162 private function isSuitableFile( $file ) {
163 $ext = pathinfo( $file, PATHINFO_EXTENSION );
164 if ( $ext != 'php' && $ext != 'inc' && $ext != 'php5' )
165 return false;
166 foreach( $this->mIgnorePaths as $regex ) {
167 $m = array();
168 if ( preg_match( "~{$regex}~", $file, $m ) )
169 return false;
170 }
171 return true;
172 }
173
174 /**
175 * Add given path to file list, searching it in include path if needed
176 */
177 private function addPath( $path ) {
178 global $IP;
179 return $this->addFileOrDir( $path ) || $this->addFileOrDir( "$IP/$path" );
180 }
181
182 /**
183 * Add given file to file list, or, if it's a directory, add its content
184 */
185 private function addFileOrDir( $path ) {
186 if ( is_dir( $path ) ) {
187 $this->addDirectoryContent( $path );
188 } elseif ( file_exists( $path ) ) {
189 $this->mFiles[] = $path;
190 } else {
191 return false;
192 }
193 return true;
194 }
195
196 /**
197 * Add all suitable files in given directory or its subdirectories to the file list
198 *
199 * @param $dir String: directory to process
200 */
201 private function addDirectoryContent( $dir ) {
202 $iterator = new RecursiveIteratorIterator(
203 new RecursiveDirectoryIterator( $dir ),
204 RecursiveIteratorIterator::SELF_FIRST
205 );
206 foreach ( $iterator as $file ) {
207 if ( $this->isSuitableFile( $file->getRealPath() ) ) {
208 $this->mFiles[] = $file->getRealPath();
209 }
210 }
211 }
212
213 /**
214 * Check a file for syntax errors using Parsekit. Shamelessly stolen
215 * from tools/lint.php by TimStarling
216 * @param $file String Path to a file to check for syntax errors
217 * @return boolean
218 */
219 private function checkFileWithParsekit( $file ) {
220 static $okErrors = array(
221 'Redefining already defined constructor',
222 'Assigning the return value of new by reference is deprecated',
223 );
224 $errors = array();
225 parsekit_compile_file( $file, $errors, PARSEKIT_SIMPLE );
226 $ret = true;
227 if ( $errors ) {
228 foreach ( $errors as $error ) {
229 foreach ( $okErrors as $okError ) {
230 if ( substr( $error['errstr'], 0, strlen( $okError ) ) == $okError ) {
231 continue 2;
232 }
233 }
234 $ret = false;
235 $this->output( "Error in $file line {$error['lineno']}: {$error['errstr']}\n" );
236 $this->mFailures[$file] = $errors;
237 }
238 }
239 return $ret;
240 }
241
242 /**
243 * Check a file for syntax errors using php -l
244 * @param $file String Path to a file to check for syntax errors
245 * @return boolean
246 */
247 private function checkFileWithCli( $file ) {
248 $res = exec( 'php -l ' . wfEscapeShellArg( $file ) );
249 if( strpos( $res, 'No syntax errors detected' ) === false ) {
250 $this->mFailures[$file] = $res;
251 $this->output( $res . "\n" );
252 return false;
253 }
254 return true;
255 }
256
257 /**
258 * Check a file for non-fatal coding errors, such as byte-order marks in the beginning
259 * or pointless ?> closing tags at the end.
260 *
261 * @param $file String String Path to a file to check for errors
262 * @return boolean
263 */
264 private function checkForMistakes( $file ) {
265 foreach( $this->mNoStyleCheckPaths as $regex ) {
266 $m = array();
267 if ( preg_match( "~{$regex}~", $file, $m ) )
268 return;
269 }
270
271 $text = file_get_contents( $file );
272
273 $this->checkRegex( $file, $text, '/^[\s\r\n]+<\?/', 'leading whitespace' );
274 $this->checkRegex( $file, $text, '/\?>[\s\r\n]*$/', 'trailing ?>' );
275 $this->checkRegex( $file, $text, '/^[\xFF\xFE\xEF]/', 'byte-order mark' );
276 }
277
278 private function checkRegex( $file, $text, $regex, $desc ) {
279 if ( !preg_match( $regex, $text ) ) {
280 return;
281 }
282
283 if ( !isset( $this->mWarnings[$file] ) ) {
284 $this->mWarnings[$file] = array();
285 }
286 $this->mWarnings[$file][] = $desc;
287 $this->output( "Warning in file $file: $desc found.\n" );
288 }
289 }
290
291 $maintClass = "SyntaxChecker";
292 require_once( DO_MAINTENANCE );
293