MediaWiki  REL1_20
checkSyntax.php
Go to the documentation of this file.
00001 <?php
00024 require_once( __DIR__ . '/Maintenance.php' );
00025 
00031 class CheckSyntax extends Maintenance {
00032 
00033         // List of files we're going to check
00034         private $mFiles = array(), $mFailures = array(), $mWarnings = array();
00035         private $mIgnorePaths = array(), $mNoStyleCheckPaths = array();
00036 
00037         public function __construct() {
00038                 parent::__construct();
00039                 $this->mDescription = "Check syntax for all PHP files in MediaWiki";
00040                 $this->addOption( 'with-extensions', 'Also recurse the extensions folder' );
00041                 $this->addOption( 'path', 'Specific path (file or directory) to check, either with absolute path or relative to the root of this MediaWiki installation',
00042                         false, true );
00043                 $this->addOption( 'list-file', 'Text file containing list of files or directories to check', false, true );
00044                 $this->addOption( 'modified', 'Check only files that were modified (requires Git command-line client)' );
00045                 $this->addOption( 'syntax-only', 'Check for syntax validity only, skip code style warnings' );
00046         }
00047 
00048         public function getDbType() {
00049                 return Maintenance::DB_NONE;
00050         }
00051 
00052         public function execute() {
00053                 $this->buildFileList();
00054 
00055                 // ParseKit is broken on PHP 5.3+, disabled until this is fixed
00056                 $useParseKit = function_exists( 'parsekit_compile_file' ) && version_compare( PHP_VERSION, '5.3', '<' );
00057 
00058                 $str = 'Checking syntax (using ' . ( $useParseKit ?
00059                         'parsekit' : ' php -l, this can take a long time' ) . ")\n";
00060                 $this->output( $str );
00061                 foreach ( $this->mFiles as $f ) {
00062                         if ( $useParseKit ) {
00063                                 $this->checkFileWithParsekit( $f );
00064                         } else {
00065                                 $this->checkFileWithCli( $f );
00066                         }
00067                         if ( !$this->hasOption( 'syntax-only' ) ) {
00068                                 $this->checkForMistakes( $f );
00069                         }
00070                 }
00071                 $this->output( "\nDone! " . count( $this->mFiles ) . " files checked, " .
00072                         count( $this->mFailures ) . " failures and " . count( $this->mWarnings ) .
00073                         " warnings found\n" );
00074         }
00075 
00079         private function buildFileList() {
00080                 global $IP;
00081 
00082                 $this->mIgnorePaths = array(
00083                         // Compat stuff, explodes on PHP 5.3
00084                         "includes/NamespaceCompat.php$",
00085                         );
00086 
00087                 $this->mNoStyleCheckPaths = array(
00088                         // Third-party code we don't care about
00089                         "/activemq_stomp/",
00090                         "EmailPage/PHPMailer",
00091                         "FCKeditor/fckeditor/",
00092                         '\bphplot-',
00093                         "/svggraph/",
00094                         "\bjsmin.php$",
00095                         "PEAR/File_Ogg/",
00096                         "QPoll/Excel/",
00097                         "/geshi/",
00098                         "/smarty/",
00099                         );
00100 
00101                 if ( $this->hasOption( 'path' ) ) {
00102                         $path = $this->getOption( 'path' );
00103                         if ( !$this->addPath( $path ) ) {
00104                                 $this->error( "Error: can't find file or directory $path\n", true );
00105                         }
00106                         return; // process only this path
00107                 } elseif ( $this->hasOption( 'list-file' ) ) {
00108                         $file = $this->getOption( 'list-file' );
00109                         wfSuppressWarnings();
00110                         $f = fopen( $file, 'r' );
00111                         wfRestoreWarnings();
00112                         if ( !$f ) {
00113                                 $this->error( "Can't open file $file\n", true );
00114                         }
00115                         $path = trim( fgets( $f ) );
00116                         while ( $path ) {
00117                                 $this->addPath( $path );
00118                         }
00119                         fclose( $f );
00120                         return;
00121                 } elseif ( $this->hasOption( 'modified' ) ) {
00122                         $this->output( "Retrieving list from Git... " );
00123                         $files = $this->getGitModifiedFiles( $IP );
00124                         $this->output( "done\n" );
00125                         foreach ( $files as $file ) {
00126                                 if ( $this->isSuitableFile( $file ) && !is_dir( $file ) ) {
00127                                         $this->mFiles[] = $file;
00128                                 }
00129                         }
00130                         return;
00131                 }
00132 
00133                 $this->output( 'Building file list...', 'listfiles' );
00134 
00135                 // Only check files in these directories.
00136                 // Don't just put $IP, because the recursive dir thingie goes into all subdirs
00137                 $dirs = array(
00138                         $IP . '/includes',
00139                         $IP . '/mw-config',
00140                         $IP . '/languages',
00141                         $IP . '/maintenance',
00142                         $IP . '/skins',
00143                 );
00144                 if ( $this->hasOption( 'with-extensions' ) ) {
00145                         $dirs[] = $IP . '/extensions';
00146                 }
00147 
00148                 foreach ( $dirs as $d ) {
00149                         $this->addDirectoryContent( $d );
00150                 }
00151 
00152                 // Manually add two user-editable files that are usually sources of problems
00153                 if ( file_exists( "$IP/LocalSettings.php" ) ) {
00154                         $this->mFiles[] = "$IP/LocalSettings.php";
00155                 }
00156                 if ( file_exists( "$IP/AdminSettings.php" ) ) {
00157                         $this->mFiles[] = "$IP/AdminSettings.php";
00158                 }
00159 
00160                 $this->output( 'done.', 'listfiles' );
00161         }
00162 
00168         private function getGitModifiedFiles( $path ) {
00169 
00170                 global $wgMaxShellMemory;
00171 
00172                 if ( !is_dir( "$path/.git" ) ) {
00173                         $this->error( "Error: Not a Git repository!\n", true );
00174                 }
00175 
00176                 // git diff eats memory.
00177                 $oldMaxShellMemory = $wgMaxShellMemory;
00178                 if ( $wgMaxShellMemory < 1024000 ) {
00179                         $wgMaxShellMemory = 1024000;
00180                 }
00181 
00182                 $ePath = wfEscapeShellArg( $path );
00183 
00184                 // Find an ancestor in common with master (rather than just using its HEAD)
00185                 // to prevent files only modified there from showing up in the list.
00186                 $cmd = "cd $ePath && git merge-base master HEAD";
00187                 $retval = 0;
00188                 $output = wfShellExec( $cmd, $retval );
00189                 if ( $retval !== 0 ) {
00190                         $this->error( "Error retrieving base SHA1 from Git!\n", true );
00191                 }
00192 
00193                 // Find files in the working tree that changed since then.
00194                 $eBase = wfEscapeShellArg( rtrim( $output, "\n" ) );
00195                 $cmd = "cd $ePath && git diff --name-only --diff-filter AM $eBase";
00196                 $retval = 0;
00197                 $output = wfShellExec( $cmd, $retval );
00198                 if ( $retval !== 0 ) {
00199                         $this->error( "Error retrieving list from Git!\n", true );
00200                 }
00201 
00202                 $wgMaxShellMemory = $oldMaxShellMemory;
00203 
00204                 $arr = array();
00205                 $filename = strtok( $output, "\n" );
00206                 while ( $filename !== false ) {
00207                         if ( $filename !== '' ) {
00208                                 $arr[] = "$path/$filename";
00209                         }
00210                         $filename = strtok( "\n" );
00211                 }
00212 
00213                 return $arr;
00214         }
00215 
00221         private function isSuitableFile( $file ) {
00222                 $file = str_replace( '\\', '/', $file );
00223                 $ext = pathinfo( $file, PATHINFO_EXTENSION );
00224                 if ( $ext != 'php' && $ext != 'inc' && $ext != 'php5' )
00225                         return false;
00226                 foreach ( $this->mIgnorePaths as $regex ) {
00227                         $m = array();
00228                         if ( preg_match( "~{$regex}~", $file, $m ) )
00229                                 return false;
00230                 }
00231                 return true;
00232         }
00233 
00239         private function addPath( $path ) {
00240                 global $IP;
00241                 return $this->addFileOrDir( $path ) || $this->addFileOrDir( "$IP/$path" );
00242         }
00243 
00249         private function addFileOrDir( $path ) {
00250                 if ( is_dir( $path ) ) {
00251                         $this->addDirectoryContent( $path );
00252                 } elseif ( file_exists( $path ) ) {
00253                         $this->mFiles[] = $path;
00254                 } else {
00255                         return false;
00256                 }
00257                 return true;
00258         }
00259 
00265         private function addDirectoryContent( $dir ) {
00266                 $iterator = new RecursiveIteratorIterator(
00267                         new RecursiveDirectoryIterator( $dir ),
00268                         RecursiveIteratorIterator::SELF_FIRST
00269                 );
00270                 foreach ( $iterator as $file ) {
00271                         if ( $this->isSuitableFile( $file->getRealPath() ) ) {
00272                                 $this->mFiles[] = $file->getRealPath();
00273                         }
00274                 }
00275         }
00276 
00283         private function checkFileWithParsekit( $file ) {
00284                 static $okErrors = array(
00285                         'Redefining already defined constructor',
00286                         'Assigning the return value of new by reference is deprecated',
00287                 );
00288                 $errors = array();
00289                 parsekit_compile_file( $file, $errors, PARSEKIT_SIMPLE );
00290                 $ret = true;
00291                 if ( $errors ) {
00292                         foreach ( $errors as $error ) {
00293                                 foreach ( $okErrors as $okError ) {
00294                                         if ( substr( $error['errstr'], 0, strlen( $okError ) ) == $okError ) {
00295                                                 continue 2;
00296                                         }
00297                                 }
00298                                 $ret = false;
00299                                 $this->output( "Error in $file line {$error['lineno']}: {$error['errstr']}\n" );
00300                                 $this->mFailures[$file] = $errors;
00301                         }
00302                 }
00303                 return $ret;
00304         }
00305 
00311         private function checkFileWithCli( $file ) {
00312                 $res = exec( 'php -l ' . wfEscapeShellArg( $file ) );
00313                 if ( strpos( $res, 'No syntax errors detected' ) === false ) {
00314                         $this->mFailures[$file] = $res;
00315                         $this->output( $res . "\n" );
00316                         return false;
00317                 }
00318                 return true;
00319         }
00320 
00328         private function checkForMistakes( $file ) {
00329                 foreach ( $this->mNoStyleCheckPaths as $regex ) {
00330                         $m = array();
00331                         if ( preg_match( "~{$regex}~", $file, $m ) )
00332                                 return;
00333                 }
00334 
00335                 $text = file_get_contents( $file );
00336                 $tokens = token_get_all( $text );
00337 
00338                 $this->checkEvilToken( $file, $tokens, '@', 'Error supression operator (@)');
00339                 $this->checkRegex( $file, $text, '/^[\s\r\n]+<\?/', 'leading whitespace' );
00340                 $this->checkRegex( $file, $text, '/\?>[\s\r\n]*$/', 'trailing ?>' );
00341                 $this->checkRegex( $file, $text, '/^[\xFF\xFE\xEF]/', 'byte-order mark' );
00342         }
00343 
00344         private function checkRegex( $file, $text, $regex, $desc ) {
00345                 if ( !preg_match( $regex, $text ) ) {
00346                         return;
00347                 }
00348 
00349                 if ( !isset( $this->mWarnings[$file] ) ) {
00350                         $this->mWarnings[$file] = array();
00351                 }
00352                 $this->mWarnings[$file][] = $desc;
00353                 $this->output( "Warning in file $file: $desc found.\n" );
00354         }
00355 
00356         private function checkEvilToken( $file, $tokens, $evilToken, $desc ) {
00357                 if ( !in_array( $evilToken, $tokens ) ) {
00358                         return;
00359                 }
00360 
00361                 if ( !isset( $this->mWarnings[$file] ) ) {
00362                         $this->mWarnings[$file] = array();
00363                 }
00364                 $this->mWarnings[$file][] = $desc;
00365                 $this->output( "Warning in file $file: $desc found.\n" );
00366         }
00367 }
00368 
00369 $maintClass = "CheckSyntax";
00370 require_once( RUN_MAINTENANCE_IF_MAIN );
00371