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 function isSuitableFile( $file ) {
163 $ext = pathinfo( $file, PATHINFO_EXTENSION
);
164 if ( $ext != 'php' && $ext != 'inc' && $ext != 'php5' )
166 foreach( $this->mIgnorePaths
as $regex ) {
168 if ( preg_match( "~{$regex}~", $file, $m ) )
175 * Add given path to file list, searching it in include path if needed
177 private function addPath( $path ) {
179 return $this->addFileOrDir( $path ) ||
$this->addFileOrDir( "$IP/$path" );
183 * Add given file to file list, or, if it's a directory, add its content
185 private function addFileOrDir( $path ) {
186 if ( is_dir( $path ) ) {
187 $this->addDirectoryContent( $path );
188 } elseif ( file_exists( $path ) ) {
189 $this->mFiles
[] = $path;
197 * Add all suitable files in given directory or its subdirectories to the file list
199 * @param $dir String: directory to process
201 private function addDirectoryContent( $dir ) {
202 $iterator = new RecursiveIteratorIterator(
203 new RecursiveDirectoryIterator( $dir ),
204 RecursiveIteratorIterator
::SELF_FIRST
206 foreach ( $iterator as $file ) {
207 if ( $this->isSuitableFile( $file->getRealPath() ) ) {
208 $this->mFiles
[] = $file->getRealPath();
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
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',
225 parsekit_compile_file( $file, $errors, PARSEKIT_SIMPLE
);
228 foreach ( $errors as $error ) {
229 foreach ( $okErrors as $okError ) {
230 if ( substr( $error['errstr'], 0, strlen( $okError ) ) == $okError ) {
235 $this->output( "Error in $file line {$error['lineno']}: {$error['errstr']}\n" );
236 $this->mFailures
[$file] = $errors;
243 * Check a file for syntax errors using php -l
244 * @param $file String Path to a file to check for syntax errors
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" );
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.
261 * @param $file String String Path to a file to check for errors
264 private function checkForMistakes( $file ) {
265 foreach( $this->mNoStyleCheckPaths
as $regex ) {
267 if ( preg_match( "~{$regex}~", $file, $m ) )
271 $text = file_get_contents( $file );
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' );
278 private function checkRegex( $file, $text, $regex, $desc ) {
279 if ( !preg_match( $regex, $text ) ) {
283 if ( !isset( $this->mWarnings
[$file] ) ) {
284 $this->mWarnings
[$file] = array();
286 $this->mWarnings
[$file][] = $desc;
287 $this->output( "Warning in file $file: $desc found.\n" );
291 $maintClass = "SyntaxChecker";
292 require_once( DO_MAINTENANCE
);