3 * Check syntax of all PHP files in MediaWiki
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.
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.
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
21 * @ingroup Maintenance
24 require_once __DIR__
. '/Maintenance.php';
27 * Maintenance script to check syntax of all PHP files in MediaWiki.
29 * @ingroup Maintenance
31 class CheckSyntax
extends Maintenance
{
33 // List of files we're going to check
34 private $mFiles = array(), $mFailures = array(), $mWarnings = array();
35 private $mIgnorePaths = array(), $mNoStyleCheckPaths = array();
37 public function __construct() {
38 parent
::__construct();
39 $this->mDescription
= "Check syntax for all PHP files in MediaWiki";
40 $this->addOption( 'with-extensions', 'Also recurse the extensions folder' );
43 'Specific path (file or directory) to check, either with absolute path or '
44 . 'relative to the root of this MediaWiki installation',
50 'Text file containing list of files or directories to check',
56 'Check only files that were modified (requires Git command-line client)'
58 $this->addOption( 'syntax-only', 'Check for syntax validity only, skip code style warnings' );
61 public function getDbType() {
62 return Maintenance
::DB_NONE
;
65 public function execute() {
66 $this->buildFileList();
68 // ParseKit is broken on PHP 5.3+, disabled until this is fixed
69 $useParseKit = function_exists( 'parsekit_compile_file' )
70 && version_compare( PHP_VERSION
, '5.3', '<' );
72 $str = 'Checking syntax (using ' . ( $useParseKit ?
73 'parsekit' : ' php -l, this can take a long time' ) . ")\n";
74 $this->output( $str );
75 foreach ( $this->mFiles
as $f ) {
77 $this->checkFileWithParsekit( $f );
79 $this->checkFileWithCli( $f );
81 if ( !$this->hasOption( 'syntax-only' ) ) {
82 $this->checkForMistakes( $f );
85 $this->output( "\nDone! " . count( $this->mFiles
) . " files checked, " .
86 count( $this->mFailures
) . " failures and " . count( $this->mWarnings
) .
87 " warnings found\n" );
91 * Build the list of files we'll check for syntax errors
93 private function buildFileList() {
96 $this->mIgnorePaths
= array(
97 // Compat stuff, explodes on PHP 5.3
98 "includes/NamespaceCompat.php$",
101 $this->mNoStyleCheckPaths
= array(
102 // Third-party code we don't care about
104 "EmailPage/PHPMailer",
105 "FCKeditor/fckeditor/",
115 if ( $this->hasOption( 'path' ) ) {
116 $path = $this->getOption( 'path' );
117 if ( !$this->addPath( $path ) ) {
118 $this->error( "Error: can't find file or directory $path\n", true );
121 return; // process only this path
122 } elseif ( $this->hasOption( 'list-file' ) ) {
123 $file = $this->getOption( 'list-file' );
124 wfSuppressWarnings();
125 $f = fopen( $file, 'r' );
128 $this->error( "Can't open file $file\n", true );
130 $path = trim( fgets( $f ) );
132 $this->addPath( $path );
137 } elseif ( $this->hasOption( 'modified' ) ) {
138 $this->output( "Retrieving list from Git... " );
139 $files = $this->getGitModifiedFiles( $IP );
140 $this->output( "done\n" );
141 foreach ( $files as $file ) {
142 if ( $this->isSuitableFile( $file ) && !is_dir( $file ) ) {
143 $this->mFiles
[] = $file;
150 $this->output( 'Building file list...', 'listfiles' );
152 // Only check files in these directories.
153 // Don't just put $IP, because the recursive dir thingie goes into all subdirs
158 $IP . '/maintenance',
161 if ( $this->hasOption( 'with-extensions' ) ) {
162 $dirs[] = $IP . '/extensions';
165 foreach ( $dirs as $d ) {
166 $this->addDirectoryContent( $d );
169 // Manually add two user-editable files that are usually sources of problems
170 if ( file_exists( "$IP/LocalSettings.php" ) ) {
171 $this->mFiles
[] = "$IP/LocalSettings.php";
174 $this->output( 'done.', 'listfiles' );
178 * Returns a list of tracked files in a Git work tree differing from the master branch.
179 * @param string $path Path to the repository
180 * @return array Resulting list of changed files
182 private function getGitModifiedFiles( $path ) {
184 global $wgMaxShellMemory;
186 if ( !is_dir( "$path/.git" ) ) {
187 $this->error( "Error: Not a Git repository!\n", true );
190 // git diff eats memory.
191 $oldMaxShellMemory = $wgMaxShellMemory;
192 if ( $wgMaxShellMemory < 1024000 ) {
193 $wgMaxShellMemory = 1024000;
196 $ePath = wfEscapeShellArg( $path );
198 // Find an ancestor in common with master (rather than just using its HEAD)
199 // to prevent files only modified there from showing up in the list.
200 $cmd = "cd $ePath && git merge-base master HEAD";
202 $output = wfShellExec( $cmd, $retval );
203 if ( $retval !== 0 ) {
204 $this->error( "Error retrieving base SHA1 from Git!\n", true );
207 // Find files in the working tree that changed since then.
208 $eBase = wfEscapeShellArg( rtrim( $output, "\n" ) );
209 $cmd = "cd $ePath && git diff --name-only --diff-filter AM $eBase";
211 $output = wfShellExec( $cmd, $retval );
212 if ( $retval !== 0 ) {
213 $this->error( "Error retrieving list from Git!\n", true );
216 $wgMaxShellMemory = $oldMaxShellMemory;
219 $filename = strtok( $output, "\n" );
220 while ( $filename !== false ) {
221 if ( $filename !== '' ) {
222 $arr[] = "$path/$filename";
224 $filename = strtok( "\n" );
231 * Returns true if $file is of a type we can check
232 * @param string $file
235 private function isSuitableFile( $file ) {
236 $file = str_replace( '\\', '/', $file );
237 $ext = pathinfo( $file, PATHINFO_EXTENSION
);
238 if ( $ext != 'php' && $ext != 'inc' && $ext != 'php5' ) {
241 foreach ( $this->mIgnorePaths
as $regex ) {
243 if ( preg_match( "~{$regex}~", $file, $m ) ) {
252 * Add given path to file list, searching it in include path if needed
253 * @param string $path
256 private function addPath( $path ) {
259 return $this->addFileOrDir( $path ) ||
$this->addFileOrDir( "$IP/$path" );
263 * Add given file to file list, or, if it's a directory, add its content
264 * @param string $path
267 private function addFileOrDir( $path ) {
268 if ( is_dir( $path ) ) {
269 $this->addDirectoryContent( $path );
270 } elseif ( file_exists( $path ) ) {
271 $this->mFiles
[] = $path;
280 * Add all suitable files in given directory or its subdirectories to the file list
282 * @param string $dir Directory to process
284 private function addDirectoryContent( $dir ) {
285 $iterator = new RecursiveIteratorIterator(
286 new RecursiveDirectoryIterator( $dir ),
287 RecursiveIteratorIterator
::SELF_FIRST
289 foreach ( $iterator as $file ) {
290 if ( $this->isSuitableFile( $file->getRealPath() ) ) {
291 $this->mFiles
[] = $file->getRealPath();
297 * Check a file for syntax errors using Parsekit. Shamelessly stolen
298 * from tools/lint.php by TimStarling
299 * @param string $file Path to a file to check for syntax errors
302 private function checkFileWithParsekit( $file ) {
303 static $okErrors = array(
304 'Redefining already defined constructor',
305 'Assigning the return value of new by reference is deprecated',
308 parsekit_compile_file( $file, $errors, PARSEKIT_SIMPLE
);
311 foreach ( $errors as $error ) {
312 foreach ( $okErrors as $okError ) {
313 if ( substr( $error['errstr'], 0, strlen( $okError ) ) == $okError ) {
318 $this->output( "Error in $file line {$error['lineno']}: {$error['errstr']}\n" );
319 $this->mFailures
[$file] = $errors;
327 * Check a file for syntax errors using php -l
328 * @param string $file Path to a file to check for syntax errors
331 private function checkFileWithCli( $file ) {
332 $res = exec( 'php -l ' . wfEscapeShellArg( $file ) );
333 if ( strpos( $res, 'No syntax errors detected' ) === false ) {
334 $this->mFailures
[$file] = $res;
335 $this->output( $res . "\n" );
344 * Check a file for non-fatal coding errors, such as byte-order marks in the beginning
345 * or pointless ?> closing tags at the end.
347 * @param string $file String Path to a file to check for errors
349 private function checkForMistakes( $file ) {
350 foreach ( $this->mNoStyleCheckPaths
as $regex ) {
352 if ( preg_match( "~{$regex}~", $file, $m ) ) {
357 $text = file_get_contents( $file );
358 $tokens = token_get_all( $text );
360 $this->checkEvilToken( $file, $tokens, '@', 'Error supression operator (@)' );
361 $this->checkRegex( $file, $text, '/^[\s\r\n]+<\?/', 'leading whitespace' );
362 $this->checkRegex( $file, $text, '/\?>[\s\r\n]*$/', 'trailing ?>' );
363 $this->checkRegex( $file, $text, '/^[\xFF\xFE\xEF]/', 'byte-order mark' );
366 private function checkRegex( $file, $text, $regex, $desc ) {
367 if ( !preg_match( $regex, $text ) ) {
371 if ( !isset( $this->mWarnings
[$file] ) ) {
372 $this->mWarnings
[$file] = array();
374 $this->mWarnings
[$file][] = $desc;
375 $this->output( "Warning in file $file: $desc found.\n" );
378 private function checkEvilToken( $file, $tokens, $evilToken, $desc ) {
379 if ( !in_array( $evilToken, $tokens ) ) {
383 if ( !isset( $this->mWarnings
[$file] ) ) {
384 $this->mWarnings
[$file] = array();
386 $this->mWarnings
[$file][] = $desc;
387 $this->output( "Warning in file $file: $desc found.\n" );
391 $maintClass = "CheckSyntax";
392 require_once RUN_MAINTENANCE_IF_MAIN
;