MediaWiki  REL1_23
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 
00157         $this->output( 'done.', 'listfiles' );
00158     }
00159 
00165     private function getGitModifiedFiles( $path ) {
00166 
00167         global $wgMaxShellMemory;
00168 
00169         if ( !is_dir( "$path/.git" ) ) {
00170             $this->error( "Error: Not a Git repository!\n", true );
00171         }
00172 
00173         // git diff eats memory.
00174         $oldMaxShellMemory = $wgMaxShellMemory;
00175         if ( $wgMaxShellMemory < 1024000 ) {
00176             $wgMaxShellMemory = 1024000;
00177         }
00178 
00179         $ePath = wfEscapeShellArg( $path );
00180 
00181         // Find an ancestor in common with master (rather than just using its HEAD)
00182         // to prevent files only modified there from showing up in the list.
00183         $cmd = "cd $ePath && git merge-base master HEAD";
00184         $retval = 0;
00185         $output = wfShellExec( $cmd, $retval );
00186         if ( $retval !== 0 ) {
00187             $this->error( "Error retrieving base SHA1 from Git!\n", true );
00188         }
00189 
00190         // Find files in the working tree that changed since then.
00191         $eBase = wfEscapeShellArg( rtrim( $output, "\n" ) );
00192         $cmd = "cd $ePath && git diff --name-only --diff-filter AM $eBase";
00193         $retval = 0;
00194         $output = wfShellExec( $cmd, $retval );
00195         if ( $retval !== 0 ) {
00196             $this->error( "Error retrieving list from Git!\n", true );
00197         }
00198 
00199         $wgMaxShellMemory = $oldMaxShellMemory;
00200 
00201         $arr = array();
00202         $filename = strtok( $output, "\n" );
00203         while ( $filename !== false ) {
00204             if ( $filename !== '' ) {
00205                 $arr[] = "$path/$filename";
00206             }
00207             $filename = strtok( "\n" );
00208         }
00209 
00210         return $arr;
00211     }
00212 
00218     private function isSuitableFile( $file ) {
00219         $file = str_replace( '\\', '/', $file );
00220         $ext = pathinfo( $file, PATHINFO_EXTENSION );
00221         if ( $ext != 'php' && $ext != 'inc' && $ext != 'php5' ) {
00222             return false;
00223         }
00224         foreach ( $this->mIgnorePaths as $regex ) {
00225             $m = array();
00226             if ( preg_match( "~{$regex}~", $file, $m ) ) {
00227                 return false;
00228             }
00229         }
00230         return true;
00231     }
00232 
00238     private function addPath( $path ) {
00239         global $IP;
00240         return $this->addFileOrDir( $path ) || $this->addFileOrDir( "$IP/$path" );
00241     }
00242 
00248     private function addFileOrDir( $path ) {
00249         if ( is_dir( $path ) ) {
00250             $this->addDirectoryContent( $path );
00251         } elseif ( file_exists( $path ) ) {
00252             $this->mFiles[] = $path;
00253         } else {
00254             return false;
00255         }
00256         return true;
00257     }
00258 
00264     private function addDirectoryContent( $dir ) {
00265         $iterator = new RecursiveIteratorIterator(
00266             new RecursiveDirectoryIterator( $dir ),
00267             RecursiveIteratorIterator::SELF_FIRST
00268         );
00269         foreach ( $iterator as $file ) {
00270             if ( $this->isSuitableFile( $file->getRealPath() ) ) {
00271                 $this->mFiles[] = $file->getRealPath();
00272             }
00273         }
00274     }
00275 
00282     private function checkFileWithParsekit( $file ) {
00283         static $okErrors = array(
00284             'Redefining already defined constructor',
00285             'Assigning the return value of new by reference is deprecated',
00286         );
00287         $errors = array();
00288         parsekit_compile_file( $file, $errors, PARSEKIT_SIMPLE );
00289         $ret = true;
00290         if ( $errors ) {
00291             foreach ( $errors as $error ) {
00292                 foreach ( $okErrors as $okError ) {
00293                     if ( substr( $error['errstr'], 0, strlen( $okError ) ) == $okError ) {
00294                         continue 2;
00295                     }
00296                 }
00297                 $ret = false;
00298                 $this->output( "Error in $file line {$error['lineno']}: {$error['errstr']}\n" );
00299                 $this->mFailures[$file] = $errors;
00300             }
00301         }
00302         return $ret;
00303     }
00304 
00310     private function checkFileWithCli( $file ) {
00311         $res = exec( 'php -l ' . wfEscapeShellArg( $file ) );
00312         if ( strpos( $res, 'No syntax errors detected' ) === false ) {
00313             $this->mFailures[$file] = $res;
00314             $this->output( $res . "\n" );
00315             return false;
00316         }
00317         return true;
00318     }
00319 
00327     private function checkForMistakes( $file ) {
00328         foreach ( $this->mNoStyleCheckPaths as $regex ) {
00329             $m = array();
00330             if ( preg_match( "~{$regex}~", $file, $m ) ) {
00331                 return;
00332             }
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;