From: Max Semenik Date: Wed, 24 Feb 2010 17:21:48 +0000 (+0000) Subject: Renamed syntaxChecker.php --> checkSyntax, our convention seems to prefer verbs.... X-Git-Tag: 1.31.0-rc.0~37616 X-Git-Url: http://git.cyclocoop.org/%28%28?a=commitdiff_plain;h=210d9a402d64e802b0c914f7ac445e4bd7679cbb;p=lhc%2Fweb%2Fwiklou.git Renamed syntaxChecker.php --> checkSyntax, our convention seems to prefer verbs. Also, fixed it to handle --modified with renamed files correctly :P --- diff --git a/maintenance/checkSyntax.php b/maintenance/checkSyntax.php new file mode 100644 index 0000000000..38b7f0ad2f --- /dev/null +++ b/maintenance/checkSyntax.php @@ -0,0 +1,296 @@ +mDescription = "Check syntax for all PHP files in MediaWiki"; + $this->addOption( 'with-extensions', 'Also recurse the extensions folder' ); + $this->addOption( 'path', 'Specific path (file or directory) to check, either with absolute path or relative to the root of this MediaWiki installation', + false, true); + $this->addOption( 'list-file', 'Text file containing list of files or directories to check', false, true); + $this->addOption( 'modified', 'Check only files that were modified (requires SVN command-line client)' ); + $this->addOption( 'syntax-only', 'Check for syntax validity only, skip code style warnings' ); + } + + protected function getDbType() { + return Maintenance::DB_NONE; + } + + public function execute() { + $this->buildFileList(); + + // ParseKit is broken on PHP 5.3+, disabled until this is fixed + $useParseKit = function_exists( 'parsekit_compile_file' ) && version_compare( PHP_VERSION, '5.3', '<' ); + + $str = 'Checking syntax (using ' . ( $useParseKit ? + 'parsekit)' : ' php -l, this can take a long time)' ); + $this->output( $str ); + foreach( $this->mFiles as $f ) { + if( $useParseKit ) { + $this->checkFileWithParsekit( $f ); + } else { + $this->checkFileWithCli( $f ); + } + if( !$this->hasOption( 'syntax-only' ) ) { + $this->checkForMistakes( $f ); + } + } + $this->output( "\nDone! " . count( $this->mFiles ) . " files checked, " . + count( $this->mFailures ) . " failures and " . count( $this->mWarnings ) . + " warnings found\n" ); + } + + /** + * Build the list of files we'll check for syntax errors + */ + private function buildFileList() { + global $IP; + + $this->mIgnorePaths = array( + // Compat stuff, explodes on PHP 5.3 + "includes/NamespaceCompat.php$", + "DiscussionThreading/REV", + ); + + $this->mNoStyleCheckPaths = array( + // Third-party code we don't care about + "/activemq_stomp/", + "EmailPage/phpMailer", + "FCKeditor/fckeditor/", + '\bphplot-', + "/svggraph/", + "\bjsmin.php$", + "OggHandler/PEAR/", + "QPoll/Excel/", + "/geshi/", + "/smarty/", + ); + + if ( $this->hasOption( 'path' ) ) { + $path = $this->getOption( 'path' ); + if ( !$this->addPath( $path ) ) { + $this->error( "Error: can't find file or directory $path\n", true ); + } + return; // process only this path + } elseif ( $this->hasOption( 'list-file' ) ) { + $file = $this->getOption( 'list-file' ); + $f = @fopen( $file, 'r' ); + if ( !$f ) { + $this->error( "Can't open file $file\n", true ); + } + while( $path = trim( fgets( $f ) ) ) { + $this->addPath( $path ); + } + fclose( $f ); + return; + } elseif ( $this->hasOption( 'modified' ) ) { + $this->output( "Retrieving list from Subversion... " ); + $parentDir = wfEscapeShellArg( dirname( __FILE__ ) . '/..' ); + $output = wfShellExec( "svn status --ignore-externals $parentDir", $retval ); + if ( $retval ) { + $this->error( "Error retrieving list from Subversion!\n", true ); + } else { + $this->output( "done\n" ); + } + + preg_match_all( '/^\s*[AM].{7}(.*?)\r?$/m', $output, $matches ); + foreach ( $matches[1] as $file ) { + if ( self::isSuitableFile( $file ) && !is_dir( $file ) ) { + $this->mFiles[] = $file; + } + } + return; + } + + $this->output( 'Building file list...', 'listfiles' ); + + // Only check files in these directories. + // Don't just put $IP, because the recursive dir thingie goes into all subdirs + $dirs = array( + $IP . '/includes', + $IP . '/config', + $IP . '/languages', + $IP . '/maintenance', + $IP . '/skins', + ); + if( $this->hasOption( 'with-extensions' ) ) { + $dirs[] = $IP . '/extensions'; + } + + foreach( $dirs as $d ) { + $this->addDirectoryContent( $d ); + } + + // Manually add two user-editable files that are usually sources of problems + if ( file_exists( "$IP/LocalSettings.php" ) ) { + $this->mFiles[] = "$IP/LocalSettings.php"; + } + if ( file_exists( "$IP/AdminSettings.php" ) ) { + $this->mFiles[] = "$IP/AdminSettings.php"; + } + + $this->output( 'done.', 'listfiles' ); + } + + /** + * Returns true if $file is of a type we can check + */ + private function isSuitableFile( $file ) { + $ext = pathinfo( $file, PATHINFO_EXTENSION ); + if ( $ext != 'php' && $ext != 'inc' && $ext != 'php5' ) + return false; + foreach( $this->mIgnorePaths as $regex ) { + $m = array(); + if ( preg_match( "~{$regex}~", $file, $m ) ) + return false; + } + return true; + } + + /** + * Add given path to file list, searching it in include path if needed + */ + private function addPath( $path ) { + global $IP; + return $this->addFileOrDir( $path ) || $this->addFileOrDir( "$IP/$path" ); + } + + /** + * Add given file to file list, or, if it's a directory, add its content + */ + private function addFileOrDir( $path ) { + if ( is_dir( $path ) ) { + $this->addDirectoryContent( $path ); + } elseif ( file_exists( $path ) ) { + $this->mFiles[] = $path; + } else { + return false; + } + return true; + } + + /** + * Add all suitable files in given directory or its subdirectories to the file list + * + * @param $dir String: directory to process + */ + private function addDirectoryContent( $dir ) { + $iterator = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator( $dir ), + RecursiveIteratorIterator::SELF_FIRST + ); + foreach ( $iterator as $file ) { + if ( $this->isSuitableFile( $file->getRealPath() ) ) { + $this->mFiles[] = $file->getRealPath(); + } + } + } + + /** + * Check a file for syntax errors using Parsekit. Shamelessly stolen + * from tools/lint.php by TimStarling + * @param $file String Path to a file to check for syntax errors + * @return boolean + */ + private function checkFileWithParsekit( $file ) { + static $okErrors = array( + 'Redefining already defined constructor', + 'Assigning the return value of new by reference is deprecated', + ); + $errors = array(); + parsekit_compile_file( $file, $errors, PARSEKIT_SIMPLE ); + $ret = true; + if ( $errors ) { + foreach ( $errors as $error ) { + foreach ( $okErrors as $okError ) { + if ( substr( $error['errstr'], 0, strlen( $okError ) ) == $okError ) { + continue 2; + } + } + $ret = false; + $this->output( "Error in $file line {$error['lineno']}: {$error['errstr']}\n" ); + $this->mFailures[$file] = $errors; + } + } + return $ret; + } + + /** + * Check a file for syntax errors using php -l + * @param $file String Path to a file to check for syntax errors + * @return boolean + */ + private function checkFileWithCli( $file ) { + $res = exec( 'php -l ' . wfEscapeShellArg( $file ) ); + if( strpos( $res, 'No syntax errors detected' ) === false ) { + $this->mFailures[$file] = $res; + $this->output( $res . "\n" ); + return false; + } + return true; + } + + /** + * Check a file for non-fatal coding errors, such as byte-order marks in the beginning + * or pointless ?> closing tags at the end. + * + * @param $file String String Path to a file to check for errors + * @return boolean + */ + private function checkForMistakes( $file ) { + foreach( $this->mNoStyleCheckPaths as $regex ) { + $m = array(); + if ( preg_match( "~{$regex}~", $file, $m ) ) + return; + } + + $text = file_get_contents( $file ); + + $this->checkRegex( $file, $text, '/^[\s\r\n]+<\?/', 'leading whitespace' ); + $this->checkRegex( $file, $text, '/\?>[\s\r\n]*$/', 'trailing ?>' ); + $this->checkRegex( $file, $text, '/^[\xFF\xFE\xEF]/', 'byte-order mark' ); + } + + private function checkRegex( $file, $text, $regex, $desc ) { + if ( !preg_match( $regex, $text ) ) { + return; + } + + if ( !isset( $this->mWarnings[$file] ) ) { + $this->mWarnings[$file] = array(); + } + $this->mWarnings[$file][] = $desc; + $this->output( "Warning in file $file: $desc found.\n" ); + } +} + +$maintClass = "CheckSyntax"; +require_once( DO_MAINTENANCE ); + diff --git a/maintenance/syntaxChecker.php b/maintenance/syntaxChecker.php deleted file mode 100644 index ffe402566e..0000000000 --- a/maintenance/syntaxChecker.php +++ /dev/null @@ -1,296 +0,0 @@ -mDescription = "Check syntax for all PHP files in MediaWiki"; - $this->addOption( 'with-extensions', 'Also recurse the extensions folder' ); - $this->addOption( 'path', 'Specific path (file or directory) to check, either with absolute path or relative to the root of this MediaWiki installation', - false, true); - $this->addOption( 'list-file', 'Text file containing list of files or directories to check', false, true); - $this->addOption( 'modified', 'Check only files that were modified (requires SVN command-line client)' ); - $this->addOption( 'syntax-only', 'Check for syntax validity only, skip code style warnings' ); - } - - protected function getDbType() { - return Maintenance::DB_NONE; - } - - public function execute() { - $this->buildFileList(); - - // ParseKit is broken on PHP 5.3+, disabled until this is fixed - $useParseKit = function_exists( 'parsekit_compile_file' ) && version_compare( PHP_VERSION, '5.3', '<' ); - - $str = 'Checking syntax (using ' . ( $useParseKit ? - 'parsekit)' : ' php -l, this can take a long time)' ); - $this->output( $str ); - foreach( $this->mFiles as $f ) { - if( $useParseKit ) { - $this->checkFileWithParsekit( $f ); - } else { - $this->checkFileWithCli( $f ); - } - if( !$this->hasOption( 'syntax-only' ) ) { - $this->checkForMistakes( $f ); - } - } - $this->output( "\nDone! " . count( $this->mFiles ) . " files checked, " . - count( $this->mFailures ) . " failures and " . count( $this->mWarnings ) . - " warnings found\n" ); - } - - /** - * Build the list of files we'll check for syntax errors - */ - private function buildFileList() { - global $IP; - - $this->mIgnorePaths = array( - // Compat stuff, explodes on PHP 5.3 - "includes/NamespaceCompat.php$", - "DiscussionThreading/REV", - ); - - $this->mNoStyleCheckPaths = array( - // Third-party code we don't care about - "/activemq_stomp/", - "EmailPage/phpMailer", - "FCKeditor/fckeditor/", - '\bphplot-', - "/svggraph/", - "\bjsmin.php$", - "OggHandler/PEAR/", - "QPoll/Excel/", - "/geshi/", - "/smarty/", - ); - - if ( $this->hasOption( 'path' ) ) { - $path = $this->getOption( 'path' ); - if ( !$this->addPath( $path ) ) { - $this->error( "Error: can't find file or directory $path\n", true ); - } - return; // process only this path - } elseif ( $this->hasOption( 'list-file' ) ) { - $file = $this->getOption( 'list-file' ); - $f = @fopen( $file, 'r' ); - if ( !$f ) { - $this->error( "Can't open file $file\n", true ); - } - while( $path = trim( fgets( $f ) ) ) { - $this->addPath( $path ); - } - fclose( $f ); - return; - } elseif ( $this->hasOption( 'modified' ) ) { - $this->output( "Retrieving list from Subversion... " ); - $parentDir = wfEscapeShellArg( dirname( __FILE__ ) . '/..' ); - $output = wfShellExec( "svn status --ignore-externals $parentDir", $retval ); - if ( $retval ) { - $this->error( "Error retrieving list from Subversion!\n", true ); - } else { - $this->output( "done\n" ); - } - - preg_match_all( '/^\s*[AM]\s+(.*?)\r?$/m', $output, $matches ); - foreach ( $matches[1] as $file ) { - if ( self::isSuitableFile( $file ) && !is_dir( $file ) ) { - $this->mFiles[] = $file; - } - } - return; - } - - $this->output( 'Building file list...', 'listfiles' ); - - // Only check files in these directories. - // Don't just put $IP, because the recursive dir thingie goes into all subdirs - $dirs = array( - $IP . '/includes', - $IP . '/config', - $IP . '/languages', - $IP . '/maintenance', - $IP . '/skins', - ); - if( $this->hasOption( 'with-extensions' ) ) { - $dirs[] = $IP . '/extensions'; - } - - foreach( $dirs as $d ) { - $this->addDirectoryContent( $d ); - } - - // Manually add two user-editable files that are usually sources of problems - if ( file_exists( "$IP/LocalSettings.php" ) ) { - $this->mFiles[] = "$IP/LocalSettings.php"; - } - if ( file_exists( "$IP/AdminSettings.php" ) ) { - $this->mFiles[] = "$IP/AdminSettings.php"; - } - - $this->output( 'done.', 'listfiles' ); - } - - /** - * Returns true if $file is of a type we can check - */ - private function isSuitableFile( $file ) { - $ext = pathinfo( $file, PATHINFO_EXTENSION ); - if ( $ext != 'php' && $ext != 'inc' && $ext != 'php5' ) - return false; - foreach( $this->mIgnorePaths as $regex ) { - $m = array(); - if ( preg_match( "~{$regex}~", $file, $m ) ) - return false; - } - return true; - } - - /** - * Add given path to file list, searching it in include path if needed - */ - private function addPath( $path ) { - global $IP; - return $this->addFileOrDir( $path ) || $this->addFileOrDir( "$IP/$path" ); - } - - /** - * Add given file to file list, or, if it's a directory, add its content - */ - private function addFileOrDir( $path ) { - if ( is_dir( $path ) ) { - $this->addDirectoryContent( $path ); - } elseif ( file_exists( $path ) ) { - $this->mFiles[] = $path; - } else { - return false; - } - return true; - } - - /** - * Add all suitable files in given directory or its subdirectories to the file list - * - * @param $dir String: directory to process - */ - private function addDirectoryContent( $dir ) { - $iterator = new RecursiveIteratorIterator( - new RecursiveDirectoryIterator( $dir ), - RecursiveIteratorIterator::SELF_FIRST - ); - foreach ( $iterator as $file ) { - if ( $this->isSuitableFile( $file->getRealPath() ) ) { - $this->mFiles[] = $file->getRealPath(); - } - } - } - - /** - * Check a file for syntax errors using Parsekit. Shamelessly stolen - * from tools/lint.php by TimStarling - * @param $file String Path to a file to check for syntax errors - * @return boolean - */ - private function checkFileWithParsekit( $file ) { - static $okErrors = array( - 'Redefining already defined constructor', - 'Assigning the return value of new by reference is deprecated', - ); - $errors = array(); - parsekit_compile_file( $file, $errors, PARSEKIT_SIMPLE ); - $ret = true; - if ( $errors ) { - foreach ( $errors as $error ) { - foreach ( $okErrors as $okError ) { - if ( substr( $error['errstr'], 0, strlen( $okError ) ) == $okError ) { - continue 2; - } - } - $ret = false; - $this->output( "Error in $file line {$error['lineno']}: {$error['errstr']}\n" ); - $this->mFailures[$file] = $errors; - } - } - return $ret; - } - - /** - * Check a file for syntax errors using php -l - * @param $file String Path to a file to check for syntax errors - * @return boolean - */ - private function checkFileWithCli( $file ) { - $res = exec( 'php -l ' . wfEscapeShellArg( $file ) ); - if( strpos( $res, 'No syntax errors detected' ) === false ) { - $this->mFailures[$file] = $res; - $this->output( $res . "\n" ); - return false; - } - return true; - } - - /** - * Check a file for non-fatal coding errors, such as byte-order marks in the beginning - * or pointless ?> closing tags at the end. - * - * @param $file String String Path to a file to check for errors - * @return boolean - */ - private function checkForMistakes( $file ) { - foreach( $this->mNoStyleCheckPaths as $regex ) { - $m = array(); - if ( preg_match( "~{$regex}~", $file, $m ) ) - return; - } - - $text = file_get_contents( $file ); - - $this->checkRegex( $file, $text, '/^[\s\r\n]+<\?/', 'leading whitespace' ); - $this->checkRegex( $file, $text, '/\?>[\s\r\n]*$/', 'trailing ?>' ); - $this->checkRegex( $file, $text, '/^[\xFF\xFE\xEF]/', 'byte-order mark' ); - } - - private function checkRegex( $file, $text, $regex, $desc ) { - if ( !preg_match( $regex, $text ) ) { - return; - } - - if ( !isset( $this->mWarnings[$file] ) ) { - $this->mWarnings[$file] = array(); - } - $this->mWarnings[$file][] = $desc; - $this->output( "Warning in file $file: $desc found.\n" ); - } -} - -$maintClass = "SyntaxChecker"; -require_once( DO_MAINTENANCE ); -