MediaWiki
REL1_22
|
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 } 00227 foreach ( $this->mIgnorePaths as $regex ) { 00228 $m = array(); 00229 if ( preg_match( "~{$regex}~", $file, $m ) ) { 00230 return false; 00231 } 00232 } 00233 return true; 00234 } 00235 00241 private function addPath( $path ) { 00242 global $IP; 00243 return $this->addFileOrDir( $path ) || $this->addFileOrDir( "$IP/$path" ); 00244 } 00245 00251 private function addFileOrDir( $path ) { 00252 if ( is_dir( $path ) ) { 00253 $this->addDirectoryContent( $path ); 00254 } elseif ( file_exists( $path ) ) { 00255 $this->mFiles[] = $path; 00256 } else { 00257 return false; 00258 } 00259 return true; 00260 } 00261 00267 private function addDirectoryContent( $dir ) { 00268 $iterator = new RecursiveIteratorIterator( 00269 new RecursiveDirectoryIterator( $dir ), 00270 RecursiveIteratorIterator::SELF_FIRST 00271 ); 00272 foreach ( $iterator as $file ) { 00273 if ( $this->isSuitableFile( $file->getRealPath() ) ) { 00274 $this->mFiles[] = $file->getRealPath(); 00275 } 00276 } 00277 } 00278 00285 private function checkFileWithParsekit( $file ) { 00286 static $okErrors = array( 00287 'Redefining already defined constructor', 00288 'Assigning the return value of new by reference is deprecated', 00289 ); 00290 $errors = array(); 00291 parsekit_compile_file( $file, $errors, PARSEKIT_SIMPLE ); 00292 $ret = true; 00293 if ( $errors ) { 00294 foreach ( $errors as $error ) { 00295 foreach ( $okErrors as $okError ) { 00296 if ( substr( $error['errstr'], 0, strlen( $okError ) ) == $okError ) { 00297 continue 2; 00298 } 00299 } 00300 $ret = false; 00301 $this->output( "Error in $file line {$error['lineno']}: {$error['errstr']}\n" ); 00302 $this->mFailures[$file] = $errors; 00303 } 00304 } 00305 return $ret; 00306 } 00307 00313 private function checkFileWithCli( $file ) { 00314 $res = exec( 'php -l ' . wfEscapeShellArg( $file ) ); 00315 if ( strpos( $res, 'No syntax errors detected' ) === false ) { 00316 $this->mFailures[$file] = $res; 00317 $this->output( $res . "\n" ); 00318 return false; 00319 } 00320 return true; 00321 } 00322 00330 private function checkForMistakes( $file ) { 00331 foreach ( $this->mNoStyleCheckPaths as $regex ) { 00332 $m = array(); 00333 if ( preg_match( "~{$regex}~", $file, $m ) ) { 00334 return; 00335 } 00336 } 00337 00338 $text = file_get_contents( $file ); 00339 $tokens = token_get_all( $text ); 00340 00341 $this->checkEvilToken( $file, $tokens, '@', 'Error supression operator (@)' ); 00342 $this->checkRegex( $file, $text, '/^[\s\r\n]+<\?/', 'leading whitespace' ); 00343 $this->checkRegex( $file, $text, '/\?>[\s\r\n]*$/', 'trailing ?>' ); 00344 $this->checkRegex( $file, $text, '/^[\xFF\xFE\xEF]/', 'byte-order mark' ); 00345 } 00346 00347 private function checkRegex( $file, $text, $regex, $desc ) { 00348 if ( !preg_match( $regex, $text ) ) { 00349 return; 00350 } 00351 00352 if ( !isset( $this->mWarnings[$file] ) ) { 00353 $this->mWarnings[$file] = array(); 00354 } 00355 $this->mWarnings[$file][] = $desc; 00356 $this->output( "Warning in file $file: $desc found.\n" ); 00357 } 00358 00359 private function checkEvilToken( $file, $tokens, $evilToken, $desc ) { 00360 if ( !in_array( $evilToken, $tokens ) ) { 00361 return; 00362 } 00363 00364 if ( !isset( $this->mWarnings[$file] ) ) { 00365 $this->mWarnings[$file] = array(); 00366 } 00367 $this->mWarnings[$file][] = $desc; 00368 $this->output( "Warning in file $file: $desc found.\n" ); 00369 } 00370 } 00371 00372 $maintClass = "CheckSyntax"; 00373 require_once RUN_MAINTENANCE_IF_MAIN;