From ab475b2b82149b22227f228d3c14c30f8f3fdb26 Mon Sep 17 00:00:00 2001 From: Chad Horohoe Date: Fri, 7 Aug 2009 04:19:09 +0000 Subject: [PATCH] Basic syntax checker. Needs to not be slow and support uploading to CodeReview. --- maintenance/syntaxChecker.php | 96 +++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 maintenance/syntaxChecker.php diff --git a/maintenance/syntaxChecker.php b/maintenance/syntaxChecker.php new file mode 100644 index 0000000000..6a49395625 --- /dev/null +++ b/maintenance/syntaxChecker.php @@ -0,0 +1,96 @@ +mDescription = "Check syntax for all PHP files in MediaWiki"; + $this->addOption( 'with-extensions', 'Also recurse the extensions folder' ); + } + + public function execute() { + $this->output( "Building file list..." ); + $this->buildFileList(); + $this->output( "done.\n" ); + + $this->output( "Checking syntax (this can take a really long time)...\n\n" ); + $res = $this->checkSyntax(); + } + + /** + * Build the list of files we'll check for syntax errors + */ + private function buildFileList() { + global $IP; + + // 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 ) { + $iterator = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator( $d ), + RecursiveIteratorIterator::SELF_FIRST + ); + foreach ( $iterator as $file ) { + $ext = pathinfo( $file->getFilename(), PATHINFO_EXTENSION ); + if ( $ext == 'php' || $ext == 'inc' || $ext == 'php5' ) { + $this->mFiles[] = $file->getRealPath(); + } + } + } + } + + /** + * Check the files for syntax errors + */ + private function checkSyntax() { + $count = $bad = 0; + foreach( $this->mFiles as $f ) { + $count++; + $res = exec( 'php -l ' . $f ); + if( strpos( $res, 'No syntax errors detected' ) === false ) { + $bad++; + $this->error( $res . "\n" ); + } + } + $this->output( "$count files checked, $bad failures\n" ); + } +} + +$maintClass = "SyntaxChecker"; +require_once( DO_MAINTENANCE ); -- 2.20.1