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 );
120 return; // process only this path
121 } elseif ( $this->hasOption( 'list-file' ) ) {
122 $file = $this->getOption( 'list-file' );
123 wfSuppressWarnings();
124 $f = fopen( $file, 'r' );
127 $this->error( "Can't open file $file\n", true );
129 $path = trim( fgets( $f ) );
131 $this->addPath( $path );
135 } elseif ( $this->hasOption( 'modified' ) ) {
136 $this->output( "Retrieving list from Git... " );
137 $files = $this->getGitModifiedFiles( $IP );
138 $this->output( "done\n" );
139 foreach ( $files as $file ) {
140 if ( $this->isSuitableFile( $file ) && !is_dir( $file ) ) {
141 $this->mFiles
[] = $file;
147 $this->output( 'Building file list...', 'listfiles' );
149 // Only check files in these directories.
150 // Don't just put $IP, because the recursive dir thingie goes into all subdirs
155 $IP . '/maintenance',
158 if ( $this->hasOption( 'with-extensions' ) ) {
159 $dirs[] = $IP . '/extensions';
162 foreach ( $dirs as $d ) {
163 $this->addDirectoryContent( $d );
166 // Manually add two user-editable files that are usually sources of problems
167 if ( file_exists( "$IP/LocalSettings.php" ) ) {
168 $this->mFiles
[] = "$IP/LocalSettings.php";
171 $this->output( 'done.', 'listfiles' );
175 * Returns a list of tracked files in a Git work tree differing from the master branch.
176 * @param string $path Path to the repository
177 * @return array Resulting list of changed files
179 private function getGitModifiedFiles( $path ) {
181 global $wgMaxShellMemory;
183 if ( !is_dir( "$path/.git" ) ) {
184 $this->error( "Error: Not a Git repository!\n", true );
187 // git diff eats memory.
188 $oldMaxShellMemory = $wgMaxShellMemory;
189 if ( $wgMaxShellMemory < 1024000 ) {
190 $wgMaxShellMemory = 1024000;
193 $ePath = wfEscapeShellArg( $path );
195 // Find an ancestor in common with master (rather than just using its HEAD)
196 // to prevent files only modified there from showing up in the list.
197 $cmd = "cd $ePath && git merge-base master HEAD";
199 $output = wfShellExec( $cmd, $retval );
200 if ( $retval !== 0 ) {
201 $this->error( "Error retrieving base SHA1 from Git!\n", true );
204 // Find files in the working tree that changed since then.
205 $eBase = wfEscapeShellArg( rtrim( $output, "\n" ) );
206 $cmd = "cd $ePath && git diff --name-only --diff-filter AM $eBase";
208 $output = wfShellExec( $cmd, $retval );
209 if ( $retval !== 0 ) {
210 $this->error( "Error retrieving list from Git!\n", true );
213 $wgMaxShellMemory = $oldMaxShellMemory;
216 $filename = strtok( $output, "\n" );
217 while ( $filename !== false ) {
218 if ( $filename !== '' ) {
219 $arr[] = "$path/$filename";
221 $filename = strtok( "\n" );
228 * Returns true if $file is of a type we can check
229 * @param string $file
232 private function isSuitableFile( $file ) {
233 $file = str_replace( '\\', '/', $file );
234 $ext = pathinfo( $file, PATHINFO_EXTENSION
);
235 if ( $ext != 'php' && $ext != 'inc' && $ext != 'php5' ) {
238 foreach ( $this->mIgnorePaths
as $regex ) {
240 if ( preg_match( "~{$regex}~", $file, $m ) ) {
248 * Add given path to file list, searching it in include path if needed
249 * @param string $path
252 private function addPath( $path ) {
254 return $this->addFileOrDir( $path ) ||
$this->addFileOrDir( "$IP/$path" );
258 * Add given file to file list, or, if it's a directory, add its content
259 * @param string $path
262 private function addFileOrDir( $path ) {
263 if ( is_dir( $path ) ) {
264 $this->addDirectoryContent( $path );
265 } elseif ( file_exists( $path ) ) {
266 $this->mFiles
[] = $path;
274 * Add all suitable files in given directory or its subdirectories to the file list
276 * @param string $dir Directory to process
278 private function addDirectoryContent( $dir ) {
279 $iterator = new RecursiveIteratorIterator(
280 new RecursiveDirectoryIterator( $dir ),
281 RecursiveIteratorIterator
::SELF_FIRST
283 foreach ( $iterator as $file ) {
284 if ( $this->isSuitableFile( $file->getRealPath() ) ) {
285 $this->mFiles
[] = $file->getRealPath();
291 * Check a file for syntax errors using Parsekit. Shamelessly stolen
292 * from tools/lint.php by TimStarling
293 * @param string $file Path to a file to check for syntax errors
296 private function checkFileWithParsekit( $file ) {
297 static $okErrors = array(
298 'Redefining already defined constructor',
299 'Assigning the return value of new by reference is deprecated',
302 parsekit_compile_file( $file, $errors, PARSEKIT_SIMPLE
);
305 foreach ( $errors as $error ) {
306 foreach ( $okErrors as $okError ) {
307 if ( substr( $error['errstr'], 0, strlen( $okError ) ) == $okError ) {
312 $this->output( "Error in $file line {$error['lineno']}: {$error['errstr']}\n" );
313 $this->mFailures
[$file] = $errors;
320 * Check a file for syntax errors using php -l
321 * @param string $file Path to a file to check for syntax errors
324 private function checkFileWithCli( $file ) {
325 $res = exec( 'php -l ' . wfEscapeShellArg( $file ) );
326 if ( strpos( $res, 'No syntax errors detected' ) === false ) {
327 $this->mFailures
[$file] = $res;
328 $this->output( $res . "\n" );
335 * Check a file for non-fatal coding errors, such as byte-order marks in the beginning
336 * or pointless ?> closing tags at the end.
338 * @param string $file String Path to a file to check for errors
341 private function checkForMistakes( $file ) {
342 foreach ( $this->mNoStyleCheckPaths
as $regex ) {
344 if ( preg_match( "~{$regex}~", $file, $m ) ) {
349 $text = file_get_contents( $file );
350 $tokens = token_get_all( $text );
352 $this->checkEvilToken( $file, $tokens, '@', 'Error supression operator (@)' );
353 $this->checkRegex( $file, $text, '/^[\s\r\n]+<\?/', 'leading whitespace' );
354 $this->checkRegex( $file, $text, '/\?>[\s\r\n]*$/', 'trailing ?>' );
355 $this->checkRegex( $file, $text, '/^[\xFF\xFE\xEF]/', 'byte-order mark' );
358 private function checkRegex( $file, $text, $regex, $desc ) {
359 if ( !preg_match( $regex, $text ) ) {
363 if ( !isset( $this->mWarnings
[$file] ) ) {
364 $this->mWarnings
[$file] = array();
366 $this->mWarnings
[$file][] = $desc;
367 $this->output( "Warning in file $file: $desc found.\n" );
370 private function checkEvilToken( $file, $tokens, $evilToken, $desc ) {
371 if ( !in_array( $evilToken, $tokens ) ) {
375 if ( !isset( $this->mWarnings
[$file] ) ) {
376 $this->mWarnings
[$file] = array();
378 $this->mWarnings
[$file][] = $desc;
379 $this->output( "Warning in file $file: $desc found.\n" );
383 $maintClass = "CheckSyntax";
384 require_once RUN_MAINTENANCE_IF_MAIN
;