[ Index ] |
PHP Cross Reference of MediaWiki-1.24.0 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Check syntax of all PHP files in MediaWiki 4 * 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. 9 * 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. 14 * 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 19 * 20 * @file 21 * @ingroup Maintenance 22 */ 23 24 require_once __DIR__ . '/Maintenance.php'; 25 26 /** 27 * Maintenance script to check syntax of all PHP files in MediaWiki. 28 * 29 * @ingroup Maintenance 30 */ 31 class CheckSyntax extends Maintenance { 32 33 // List of files we're going to check 34 private $mFiles = array(), $mFailures = array(), $mWarnings = array(); 35 private $mIgnorePaths = array(), $mNoStyleCheckPaths = array(); 36 37 public function __construct() { 38 parent::__construct(); 39 $this->mDescription = "Check syntax for all PHP files in MediaWiki"; 40 $this->addOption( 'with-extensions', 'Also recurse the extensions folder' ); 41 $this->addOption( 42 'path', 43 'Specific path (file or directory) to check, either with absolute path or ' 44 . 'relative to the root of this MediaWiki installation', 45 false, 46 true 47 ); 48 $this->addOption( 49 'list-file', 50 'Text file containing list of files or directories to check', 51 false, 52 true 53 ); 54 $this->addOption( 55 'modified', 56 'Check only files that were modified (requires Git command-line client)' 57 ); 58 $this->addOption( 'syntax-only', 'Check for syntax validity only, skip code style warnings' ); 59 } 60 61 public function getDbType() { 62 return Maintenance::DB_NONE; 63 } 64 65 public function execute() { 66 $this->buildFileList(); 67 68 // ParseKit is broken on PHP 5.3+, disabled until this is fixed 69 $useParseKit = function_exists( 'parsekit_compile_file' ) 70 && version_compare( PHP_VERSION, '5.3', '<' ); 71 72 $str = 'Checking syntax (using ' . ( $useParseKit ? 73 'parsekit' : ' php -l, this can take a long time' ) . ")\n"; 74 $this->output( $str ); 75 foreach ( $this->mFiles as $f ) { 76 if ( $useParseKit ) { 77 $this->checkFileWithParsekit( $f ); 78 } else { 79 $this->checkFileWithCli( $f ); 80 } 81 if ( !$this->hasOption( 'syntax-only' ) ) { 82 $this->checkForMistakes( $f ); 83 } 84 } 85 $this->output( "\nDone! " . count( $this->mFiles ) . " files checked, " . 86 count( $this->mFailures ) . " failures and " . count( $this->mWarnings ) . 87 " warnings found\n" ); 88 } 89 90 /** 91 * Build the list of files we'll check for syntax errors 92 */ 93 private function buildFileList() { 94 global $IP; 95 96 $this->mIgnorePaths = array( 97 // Compat stuff, explodes on PHP 5.3 98 "includes/NamespaceCompat.php$", 99 ); 100 101 $this->mNoStyleCheckPaths = array( 102 // Third-party code we don't care about 103 "/activemq_stomp/", 104 "EmailPage/PHPMailer", 105 "FCKeditor/fckeditor/", 106 '\bphplot-', 107 "/svggraph/", 108 "\bjsmin.php$", 109 "PEAR/File_Ogg/", 110 "QPoll/Excel/", 111 "/geshi/", 112 "/smarty/", 113 ); 114 115 if ( $this->hasOption( 'path' ) ) { 116 $path = $this->getOption( 'path' ); 117 if ( !$this->addPath( $path ) ) { 118 $this->error( "Error: can't find file or directory $path\n", true ); 119 } 120 121 return; // process only this path 122 } elseif ( $this->hasOption( 'list-file' ) ) { 123 $file = $this->getOption( 'list-file' ); 124 wfSuppressWarnings(); 125 $f = fopen( $file, 'r' ); 126 wfRestoreWarnings(); 127 if ( !$f ) { 128 $this->error( "Can't open file $file\n", true ); 129 } 130 $path = trim( fgets( $f ) ); 131 while ( $path ) { 132 $this->addPath( $path ); 133 } 134 fclose( $f ); 135 136 return; 137 } elseif ( $this->hasOption( 'modified' ) ) { 138 $this->output( "Retrieving list from Git... " ); 139 $files = $this->getGitModifiedFiles( $IP ); 140 $this->output( "done\n" ); 141 foreach ( $files as $file ) { 142 if ( $this->isSuitableFile( $file ) && !is_dir( $file ) ) { 143 $this->mFiles[] = $file; 144 } 145 } 146 147 return; 148 } 149 150 $this->output( 'Building file list...', 'listfiles' ); 151 152 // Only check files in these directories. 153 // Don't just put $IP, because the recursive dir thingie goes into all subdirs 154 $dirs = array( 155 $IP . '/includes', 156 $IP . '/mw-config', 157 $IP . '/languages', 158 $IP . '/maintenance', 159 $IP . '/skins', 160 ); 161 if ( $this->hasOption( 'with-extensions' ) ) { 162 $dirs[] = $IP . '/extensions'; 163 } 164 165 foreach ( $dirs as $d ) { 166 $this->addDirectoryContent( $d ); 167 } 168 169 // Manually add two user-editable files that are usually sources of problems 170 if ( file_exists( "$IP/LocalSettings.php" ) ) { 171 $this->mFiles[] = "$IP/LocalSettings.php"; 172 } 173 174 $this->output( 'done.', 'listfiles' ); 175 } 176 177 /** 178 * Returns a list of tracked files in a Git work tree differing from the master branch. 179 * @param string $path Path to the repository 180 * @return array Resulting list of changed files 181 */ 182 private function getGitModifiedFiles( $path ) { 183 184 global $wgMaxShellMemory; 185 186 if ( !is_dir( "$path/.git" ) ) { 187 $this->error( "Error: Not a Git repository!\n", true ); 188 } 189 190 // git diff eats memory. 191 $oldMaxShellMemory = $wgMaxShellMemory; 192 if ( $wgMaxShellMemory < 1024000 ) { 193 $wgMaxShellMemory = 1024000; 194 } 195 196 $ePath = wfEscapeShellArg( $path ); 197 198 // Find an ancestor in common with master (rather than just using its HEAD) 199 // to prevent files only modified there from showing up in the list. 200 $cmd = "cd $ePath && git merge-base master HEAD"; 201 $retval = 0; 202 $output = wfShellExec( $cmd, $retval ); 203 if ( $retval !== 0 ) { 204 $this->error( "Error retrieving base SHA1 from Git!\n", true ); 205 } 206 207 // Find files in the working tree that changed since then. 208 $eBase = wfEscapeShellArg( rtrim( $output, "\n" ) ); 209 $cmd = "cd $ePath && git diff --name-only --diff-filter AM $eBase"; 210 $retval = 0; 211 $output = wfShellExec( $cmd, $retval ); 212 if ( $retval !== 0 ) { 213 $this->error( "Error retrieving list from Git!\n", true ); 214 } 215 216 $wgMaxShellMemory = $oldMaxShellMemory; 217 218 $arr = array(); 219 $filename = strtok( $output, "\n" ); 220 while ( $filename !== false ) { 221 if ( $filename !== '' ) { 222 $arr[] = "$path/$filename"; 223 } 224 $filename = strtok( "\n" ); 225 } 226 227 return $arr; 228 } 229 230 /** 231 * Returns true if $file is of a type we can check 232 * @param string $file 233 * @return bool 234 */ 235 private function isSuitableFile( $file ) { 236 $file = str_replace( '\\', '/', $file ); 237 $ext = pathinfo( $file, PATHINFO_EXTENSION ); 238 if ( $ext != 'php' && $ext != 'inc' && $ext != 'php5' ) { 239 return false; 240 } 241 foreach ( $this->mIgnorePaths as $regex ) { 242 $m = array(); 243 if ( preg_match( "~{$regex}~", $file, $m ) ) { 244 return false; 245 } 246 } 247 248 return true; 249 } 250 251 /** 252 * Add given path to file list, searching it in include path if needed 253 * @param string $path 254 * @return bool 255 */ 256 private function addPath( $path ) { 257 global $IP; 258 259 return $this->addFileOrDir( $path ) || $this->addFileOrDir( "$IP/$path" ); 260 } 261 262 /** 263 * Add given file to file list, or, if it's a directory, add its content 264 * @param string $path 265 * @return bool 266 */ 267 private function addFileOrDir( $path ) { 268 if ( is_dir( $path ) ) { 269 $this->addDirectoryContent( $path ); 270 } elseif ( file_exists( $path ) ) { 271 $this->mFiles[] = $path; 272 } else { 273 return false; 274 } 275 276 return true; 277 } 278 279 /** 280 * Add all suitable files in given directory or its subdirectories to the file list 281 * 282 * @param string $dir Directory to process 283 */ 284 private function addDirectoryContent( $dir ) { 285 $iterator = new RecursiveIteratorIterator( 286 new RecursiveDirectoryIterator( $dir ), 287 RecursiveIteratorIterator::SELF_FIRST 288 ); 289 foreach ( $iterator as $file ) { 290 if ( $this->isSuitableFile( $file->getRealPath() ) ) { 291 $this->mFiles[] = $file->getRealPath(); 292 } 293 } 294 } 295 296 /** 297 * Check a file for syntax errors using Parsekit. Shamelessly stolen 298 * from tools/lint.php by TimStarling 299 * @param string $file Path to a file to check for syntax errors 300 * @return bool 301 */ 302 private function checkFileWithParsekit( $file ) { 303 static $okErrors = array( 304 'Redefining already defined constructor', 305 'Assigning the return value of new by reference is deprecated', 306 ); 307 $errors = array(); 308 parsekit_compile_file( $file, $errors, PARSEKIT_SIMPLE ); 309 $ret = true; 310 if ( $errors ) { 311 foreach ( $errors as $error ) { 312 foreach ( $okErrors as $okError ) { 313 if ( substr( $error['errstr'], 0, strlen( $okError ) ) == $okError ) { 314 continue 2; 315 } 316 } 317 $ret = false; 318 $this->output( "Error in $file line {$error['lineno']}: {$error['errstr']}\n" ); 319 $this->mFailures[$file] = $errors; 320 } 321 } 322 323 return $ret; 324 } 325 326 /** 327 * Check a file for syntax errors using php -l 328 * @param string $file Path to a file to check for syntax errors 329 * @return bool 330 */ 331 private function checkFileWithCli( $file ) { 332 $res = exec( 'php -l ' . wfEscapeShellArg( $file ) ); 333 if ( strpos( $res, 'No syntax errors detected' ) === false ) { 334 $this->mFailures[$file] = $res; 335 $this->output( $res . "\n" ); 336 337 return false; 338 } 339 340 return true; 341 } 342 343 /** 344 * Check a file for non-fatal coding errors, such as byte-order marks in the beginning 345 * or pointless ?> closing tags at the end. 346 * 347 * @param string $file String Path to a file to check for errors 348 */ 349 private function checkForMistakes( $file ) { 350 foreach ( $this->mNoStyleCheckPaths as $regex ) { 351 $m = array(); 352 if ( preg_match( "~{$regex}~", $file, $m ) ) { 353 return; 354 } 355 } 356 357 $text = file_get_contents( $file ); 358 $tokens = token_get_all( $text ); 359 360 $this->checkEvilToken( $file, $tokens, '@', 'Error supression operator (@)' ); 361 $this->checkRegex( $file, $text, '/^[\s\r\n]+<\?/', 'leading whitespace' ); 362 $this->checkRegex( $file, $text, '/\?>[\s\r\n]*$/', 'trailing ?>' ); 363 $this->checkRegex( $file, $text, '/^[\xFF\xFE\xEF]/', 'byte-order mark' ); 364 } 365 366 private function checkRegex( $file, $text, $regex, $desc ) { 367 if ( !preg_match( $regex, $text ) ) { 368 return; 369 } 370 371 if ( !isset( $this->mWarnings[$file] ) ) { 372 $this->mWarnings[$file] = array(); 373 } 374 $this->mWarnings[$file][] = $desc; 375 $this->output( "Warning in file $file: $desc found.\n" ); 376 } 377 378 private function checkEvilToken( $file, $tokens, $evilToken, $desc ) { 379 if ( !in_array( $evilToken, $tokens ) ) { 380 return; 381 } 382 383 if ( !isset( $this->mWarnings[$file] ) ) { 384 $this->mWarnings[$file] = array(); 385 } 386 $this->mWarnings[$file][] = $desc; 387 $this->output( "Warning in file $file: $desc found.\n" ); 388 } 389 } 390 391 $maintClass = "CheckSyntax"; 392 require_once RUN_MAINTENANCE_IF_MAIN;
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 14:03:12 2014 | Cross-referenced by PHPXref 0.7.1 |