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
20 * @ingroup Maintenance
23 require_once( dirname( __FILE__
) . '/Maintenance.php' );
25 class SyntaxChecker
extends Maintenance
{
27 // List of files we're going to check
28 private $mFiles = array(), $mFailures = array(), $mWarnings = array();
29 private $mIgnorePaths = array(), $mNoStyleCheckPaths = array();
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',
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' );
42 protected function getDbType() {
43 return Maintenance
::DB_NONE
;
46 public function execute() {
47 $this->buildFileList();
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', '<' );
52 $this->output( "Checking syntax (this can take a really long time)...\n\n" );
53 foreach( $this->mFiles
as $f ) {
55 $this->checkFileWithParsekit( $f );
57 $this->checkFileWithCli( $f );
59 if( !$this->hasOption( 'syntax-only' ) ) {
60 $this->checkForMistakes( $f );
63 $this->output( "\nDone! " . count( $this->mFiles
) . " files checked, " .
64 count( $this->mFailures
) . " failures and " . count( $this->mWarnings
) .
65 " warnings found\n" );
69 * Build the list of files we'll check for syntax errors
71 private function buildFileList() {
74 $this->mIgnorePaths
= array(
75 // Compat stuff, explodes on PHP 5.3
76 "includes/NamespaceCompat.php$",
77 "DiscussionThreading/REV",
80 $this->mNoStyleCheckPaths
= array(
81 // Third-party code we don't care about
83 "EmailPage/phpMailer",
84 "FCKeditor/fckeditor/",
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 );
98 return; // process only this path
99 } elseif ( $this->hasOption( 'list-file' ) ) {
100 $file = $this->getOption( 'list-file' );
101 $f = @fopen
( $file, 'r' );
103 $this->error( "Can't open file $file\n", true );
105 while( $path = trim( fgets( $f ) ) ) {
106 $this->addPath( $path );
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 );
115 $this->error( "Error retrieving list from Subversion!\n", true );
117 $this->output( "done\n" );
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;
129 $this->output( "Building file list..." );
131 // Only check files in these directories.
132 // Don't just put $IP, because the recursive dir thingie goes into all subdirs
137 $IP . '/maintenance',
140 if( $this->hasOption( 'with-extensions' ) ) {
141 $dirs[] = $IP . '/extensions';
144 foreach( $dirs as $d ) {
145 $this->addDirectoryContent( $d );
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";
152 if ( file_exists( "$IP/AdminSettings.php" ) ) {
153 $this->mFiles
[] = "$IP/AdminSettings.php";
156 $this->output( "done.\n" );
160 * Returns true if $file is of a type we can check
162 private static function isSuitableFile( $file ) {
163 $ext = pathinfo( $file, PATHINFO_EXTENSION
);
164 return $ext == 'php' ||
$ext == 'inc' ||
$ext == 'php5';
168 * Add given path to file list, searching it in include path if needed
170 private function addPath( $path ) {
172 return $this->addFileOrDir( $path ) ||
$this->addFileOrDir( "$IP/$path" );
176 * Add given file to file list, or, if it's a directory, add its content
178 private function addFileOrDir( $path ) {
179 if ( is_dir( $path ) ) {
180 $this->addDirectoryContent( $path );
181 } elseif ( file_exists( $path ) ) {
182 $this->mFiles
[] = $path;
190 * Add all suitable files in given directory or its subdirectories to the file list
192 * @param $dir String: directory to process
194 private function addDirectoryContent( $dir ) {
195 $iterator = new RecursiveIteratorIterator(
196 new RecursiveDirectoryIterator( $dir ),
197 RecursiveIteratorIterator
::SELF_FIRST
199 foreach ( $iterator as $file ) {
200 if ( self
::isSuitableFile( $file->getRealPath() ) ) {
201 $this->mFiles
[] = $file->getRealPath();
207 * Check a file for syntax errors using Parsekit. Shamelessly stolen
208 * from tools/lint.php by TimStarling
209 * @param $file String Path to a file to check for syntax errors
212 private function checkFileWithParsekit( $file ) {
213 static $okErrors = array(
214 'Redefining already defined constructor',
215 'Assigning the return value of new by reference is deprecated',
218 parsekit_compile_file( $file, $errors, PARSEKIT_SIMPLE
);
221 foreach ( $errors as $error ) {
222 foreach ( $okErrors as $okError ) {
223 if ( substr( $error['errstr'], 0, strlen( $okError ) ) == $okError ) {
228 $this->output( "Error in $file line {$error['lineno']}: {$error['errstr']}\n" );
229 $this->mFailures
[$file] = $errors;
236 * Check a file for syntax errors using php -l
237 * @param $file String Path to a file to check for syntax errors
240 private function checkFileWithCli( $file ) {
241 $res = exec( 'php -l ' . wfEscapeShellArg( $file ) );
242 if( strpos( $res, 'No syntax errors detected' ) === false ) {
243 $this->mFailures
[$file] = $res;
244 $this->output( $res . "\n" );
251 * Check a file for non-fatal coding errors, such as byte-order marks in the beginning
252 * or pointless ?> closing tags at the end.
254 * @param $file String String Path to a file to check for errors
257 private function checkForMistakes( $file ) {
258 $text = file_get_contents( $file );
260 $this->checkRegex( $file, $text, '/^[\s\r\n]+<\?/', 'leading whitespace' );
261 $this->checkRegex( $file, $text, '/\?>[\s\r\n]*$/', 'trailing ?>' );
262 $this->checkRegex( $file, $text, '/^[\xFF\xFE\xEF]/', 'byte-order mark' );
265 private function checkRegex( $file, $text, $regex, $desc ) {
266 if ( !preg_match( $regex, $text ) ) {
270 if ( !isset( $this->mWarnings
[$file] ) ) {
271 $this->mWarnings
[$file] = array();
273 $this->mWarnings
[$file][] = $desc;
274 $this->output( "Warning in file $file: $desc found.\n" );
278 $maintClass = "SyntaxChecker";
279 require_once( DO_MAINTENANCE
);