Basic syntax checker. Needs to not be slow and support uploading to CodeReview.
authorChad Horohoe <demon@users.mediawiki.org>
Fri, 7 Aug 2009 04:19:09 +0000 (04:19 +0000)
committerChad Horohoe <demon@users.mediawiki.org>
Fri, 7 Aug 2009 04:19:09 +0000 (04:19 +0000)
maintenance/syntaxChecker.php [new file with mode: 0644]

diff --git a/maintenance/syntaxChecker.php b/maintenance/syntaxChecker.php
new file mode 100644 (file)
index 0000000..6a49395
--- /dev/null
@@ -0,0 +1,96 @@
+<?php
+/**
+ * Check syntax of all PHP files in MediaWiki
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @ingroup Maintenance
+ */
+require_once( dirname( __FILE__ ) . '/Maintenance.php' );
+
+class SyntaxChecker extends Maintenance {
+
+       // List of files we're going to check
+       private $mFiles = array();
+
+       public function __construct() {
+               parent::__construct();
+               $this->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 );