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, $mFailures = array(), $mWarnings = array();
30 public function __construct() {
31 parent
::__construct();
32 $this->mDescription
= "Check syntax for all PHP files in MediaWiki";
33 $this->addOption( 'with-extensions', 'Also recurse the extensions folder' );
34 $this->addOption( 'file', 'Specific file to check, either with absolute path or relative to the root of this MediaWiki installation',
38 protected function getDbType() {
39 return Maintenance
::DB_NONE
;
42 public function execute() {
43 $this->buildFileList();
45 // ParseKit is broken on PHP 5.3+, disabled until this is fixed
46 $useParseKit = function_exists( 'parsekit_compile_file' ) && version_compare( PHP_VERSION
, '5.3', '<' );
48 $this->output( "Checking syntax (this can take a really long time)...\n\n" );
49 foreach( $this->mFiles
as $f ) {
51 $this->checkFileWithParsekit( $f );
53 $this->checkFileWithCli( $f );
55 $this->checkForMistakes( $f );
57 $this->output( "\nDone! " . count( $this->mFiles
) . " files checked, " .
58 count( $this->mFailures
) . " failures and " . count( $this->mWarnings
) .
59 " warnings found\n" );
63 * Build the list of files we'll check for syntax errors
65 private function buildFileList() {
68 if ( $this->hasOption( 'file' ) ) {
69 $file = $this->getOption( 'file' );
70 if ( !file_exists( $file ) ) {
71 if ( file_exists( "$IP/$file" ) ) {
74 $this->error( "Error: couldn't open file $file.\n", true );
77 $this->mFiles
[] = $file;
78 $this->output( "Checking file $file.\n" );
79 return; // process only this file
82 $this->output( "Building file list..." );
84 // Only check files in these directories.
85 // Don't just put $IP, because the recursive dir thingie goes into all subdirs
93 if( $this->hasOption( 'with-extensions' ) ) {
94 $dirs[] = $IP . '/extensions';
97 foreach( $dirs as $d ) {
98 $iterator = new RecursiveIteratorIterator(
99 new RecursiveDirectoryIterator( $d ),
100 RecursiveIteratorIterator
::SELF_FIRST
102 foreach ( $iterator as $file ) {
103 $ext = pathinfo( $file->getFilename(), PATHINFO_EXTENSION
);
104 if ( $ext == 'php' ||
$ext == 'inc' ||
$ext == 'php5' ) {
105 $this->mFiles
[] = $file->getRealPath();
110 // Manually add two user-editable files that are usually sources of problems
111 if ( file_exists( "$IP/LocalSettings.php" ) ) {
112 $this->mFiles
[] = "$IP/LocalSettings.php";
114 if ( file_exists( "$IP/AdminSettings.php" ) ) {
115 $this->mFiles
[] = "$IP/AdminSettings.php";
118 $this->output( "done.\n" );
122 * Check a file for syntax errors using Parsekit. Shamelessly stolen
123 * from tools/lint.php by TimStarling
124 * @param $file String Path to a file to check for syntax errors
127 private function checkFileWithParsekit( $file ) {
128 static $okErrors = array(
129 'Redefining already defined constructor',
130 'Assigning the return value of new by reference is deprecated',
133 parsekit_compile_file( $file, $errors, PARSEKIT_SIMPLE
);
136 foreach ( $errors as $error ) {
137 foreach ( $okErrors as $okError ) {
138 if ( substr( $error['errstr'], 0, strlen( $okError ) ) == $okError ) {
143 $this->output( "Error in $file line {$error['lineno']}: {$error['errstr']}\n" );
144 $this->mFailures
[$file] = $errors;
151 * Check a file for syntax errors using php -l
152 * @param $file String Path to a file to check for syntax errors
155 private function checkFileWithCli( $file ) {
156 $res = exec( 'php -l ' . wfEscapeShellArg( $file ) );
157 if( strpos( $res, 'No syntax errors detected' ) === false ) {
158 $this->mFailures
[$file] = $res;
159 $this->output( $res . "\n" );
166 * Check a file for non-fatal coding errors, such as byte-order marks in the beginning
167 * or pointless ?> closing tags at the end.
169 * @param $file String String Path to a file to check for errors
172 private function checkForMistakes( $file ) {
173 $text = file_get_contents( $file );
175 $this->checkRegex( $file, $text, '/^[\s\r\n]+<\?/', 'leading whitespace' );
176 $this->checkRegex( $file, $text, '/\?>[\s\r\n]*$/', 'trailing ?>' );
177 $this->checkRegex( $file, $text, '/^[\xFF\xFE\xEF]/', 'byte-order mark' );
180 private function checkRegex( $file, $text, $regex, $desc ) {
181 if ( !preg_match( $regex, $text ) ) {
185 if ( !isset( $this->mWarnings
[$file] ) ) {
186 $this->mWarnings
[$file] = array();
188 $this->mWarnings
[$file][] = $desc;
189 $this->output( "Warning in file $file: $desc found.\n" );
193 $maintClass = "SyntaxChecker";
194 require_once( DO_MAINTENANCE
);