Merge "Add passing ''italic'''s case to 'Unclosed and unmatched quotes' test"
[lhc/web/wiklou.git] / maintenance / checkSyntax.php
index 38b7f0a..3019b84 100644 (file)
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
  *
+ * @file
  * @ingroup Maintenance
  */
+
 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
 
+/**
+ * Maintenance script to check syntax of all PHP files in MediaWiki.
+ *
+ * @ingroup Maintenance
+ */
 class CheckSyntax extends Maintenance {
 
        // List of files we're going to check
@@ -33,13 +39,13 @@ class CheckSyntax extends Maintenance {
                $this->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)' );
+                       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 Git command-line client)' );
                $this->addOption( 'syntax-only', 'Check for syntax validity only, skip code style warnings' );
        }
 
-       protected function getDbType() {
+       public function getDbType() {
                return Maintenance::DB_NONE;
        }
 
@@ -49,16 +55,16 @@ class CheckSyntax extends Maintenance {
                // 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)' );
+               $str = 'Checking syntax (using ' . ( $useParseKit ?
+                       'parsekit' : ' php -l, this can take a long time' ) . ")\n";
                $this->output( $str );
-               foreach( $this->mFiles as $f ) {
-                       if( $useParseKit ) {
+               foreach ( $this->mFiles as $f ) {
+                       if ( $useParseKit ) {
                                $this->checkFileWithParsekit( $f );
                        } else {
                                $this->checkFileWithCli( $f );
                        }
-                       if( !$this->hasOption( 'syntax-only' ) ) {
+                       if ( !$this->hasOption( 'syntax-only' ) ) {
                                $this->checkForMistakes( $f );
                        }
                }
@@ -76,18 +82,17 @@ class CheckSyntax extends Maintenance {
                $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",
+                       "EmailPage/PHPMailer",
                        "FCKeditor/fckeditor/",
                        '\bphplot-',
                        "/svggraph/",
                        "\bjsmin.php$",
-                       "OggHandler/PEAR/",
+                       "PEAR/File_Ogg/",
                        "QPoll/Excel/",
                        "/geshi/",
                        "/smarty/",
@@ -101,28 +106,24 @@ class CheckSyntax extends Maintenance {
                        return; // process only this path
                } elseif ( $this->hasOption( 'list-file' ) ) {
                        $file = $this->getOption( 'list-file' );
-                       $f = @fopen( $file, 'r' );
+                       wfSuppressWarnings();
+                       $f = fopen( $file, 'r' );
+                       wfRestoreWarnings();
                        if ( !$f ) {
                                $this->error( "Can't open file $file\n", true );
                        }
-                       while( $path = trim( fgets( $f ) ) ) {
+                       $path = trim( fgets( $f ) );
+                       while ( $path ) {
                                $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->output( "Retrieving list from Git... " );
+                       $files = $this->getGitModifiedFiles( $IP );
+                       $this->output( "done\n" );
+                       foreach ( $files as $file ) {
+                               if ( $this->isSuitableFile( $file ) && !is_dir( $file ) ) {
                                        $this->mFiles[] = $file;
                                }
                        }
@@ -131,20 +132,20 @@ class CheckSyntax extends Maintenance {
 
                $this->output( 'Building file list...', 'listfiles' );
 
-               // Only check files in these directories. 
+               // Only check files in these directories.
                // Don't just put $IP, because the recursive dir thingie goes into all subdirs
-               $dirs = array( 
+               $dirs = array(
                        $IP . '/includes',
-                       $IP . '/config',
+                       $IP . '/mw-config',
                        $IP . '/languages',
                        $IP . '/maintenance',
                        $IP . '/skins',
                );
-               if( $this->hasOption( 'with-extensions' ) ) {
+               if ( $this->hasOption( 'with-extensions' ) ) {
                        $dirs[] = $IP . '/extensions';
                }
 
-               foreach( $dirs as $d ) {
+               foreach ( $dirs as $d ) {
                        $this->addDirectoryContent( $d );
                }
 
@@ -158,15 +159,71 @@ class CheckSyntax extends Maintenance {
 
                $this->output( 'done.', 'listfiles' );
        }
-       
+
+       /**
+        * Returns a list of tracked files in a Git work tree differing from the master branch.
+        * @param $path string: Path to the repository
+        * @return array: Resulting list of changed files
+        */
+       private function getGitModifiedFiles( $path ) {
+
+               global $wgMaxShellMemory;
+
+               if ( !is_dir( "$path/.git" ) ) {
+                       $this->error( "Error: Not a Git repository!\n", true );
+               }
+
+               // git diff eats memory.
+               $oldMaxShellMemory = $wgMaxShellMemory;
+               if ( $wgMaxShellMemory < 1024000 ) {
+                       $wgMaxShellMemory = 1024000;
+               }
+
+               $ePath = wfEscapeShellArg( $path );
+
+               // Find an ancestor in common with master (rather than just using its HEAD)
+               // to prevent files only modified there from showing up in the list.
+               $cmd = "cd $ePath && git merge-base master HEAD";
+               $retval = 0;
+               $output = wfShellExec( $cmd, $retval );
+               if ( $retval !== 0 ) {
+                       $this->error( "Error retrieving base SHA1 from Git!\n", true );
+               }
+
+               // Find files in the working tree that changed since then.
+               $eBase = wfEscapeShellArg( rtrim( $output, "\n" ) );
+               $cmd = "cd $ePath && git diff --name-only --diff-filter AM $eBase";
+               $retval = 0;
+               $output = wfShellExec( $cmd, $retval );
+               if ( $retval !== 0 ) {
+                       $this->error( "Error retrieving list from Git!\n", true );
+               }
+
+               $wgMaxShellMemory = $oldMaxShellMemory;
+
+               $arr = array();
+               $filename = strtok( $output, "\n" );
+               while ( $filename !== false ) {
+                       if ( $filename !== '' ) {
+                               $arr[] = "$path/$filename";
+                       }
+                       $filename = strtok( "\n" );
+               }
+
+               return $arr;
+       }
+
        /**
         * Returns true if $file is of a type we can check
+        * @param $file string
+        * @return bool
         */
        private function isSuitableFile( $file ) {
+               $file = str_replace( '\\', '/', $file );
                $ext = pathinfo( $file, PATHINFO_EXTENSION );
                if ( $ext != 'php' && $ext != 'inc' && $ext != 'php5' )
                        return false;
-               foreach( $this->mIgnorePaths as $regex ) {
+               foreach ( $this->mIgnorePaths as $regex ) {
                        $m = array();
                        if ( preg_match( "~{$regex}~", $file, $m ) )
                                return false;
@@ -176,6 +233,8 @@ class CheckSyntax extends Maintenance {
 
        /**
         * Add given path to file list, searching it in include path if needed
+        * @param $path string
+        * @return bool
         */
        private function addPath( $path ) {
                global $IP;
@@ -183,8 +242,10 @@ class CheckSyntax extends Maintenance {
        }
 
        /**
-       * Add given file to file list, or, if it's a directory, add its content
-       */
+        * Add given file to file list, or, if it's a directory, add its content
+        * @param $path string
+        * @return bool
+        */
        private function addFileOrDir( $path ) {
                if ( is_dir( $path ) ) {
                        $this->addDirectoryContent( $path );
@@ -203,7 +264,7 @@ class CheckSyntax extends Maintenance {
         */
        private function addDirectoryContent( $dir ) {
                $iterator = new RecursiveIteratorIterator(
-                       new RecursiveDirectoryIterator( $dir ), 
+                       new RecursiveDirectoryIterator( $dir ),
                        RecursiveIteratorIterator::SELF_FIRST
                );
                foreach ( $iterator as $file ) {
@@ -248,8 +309,8 @@ class CheckSyntax extends Maintenance {
         * @return boolean
         */
        private function checkFileWithCli( $file ) {
-               $res = exec( 'php -l ' . wfEscapeShellArg( $file ) ); 
-               if( strpos( $res, 'No syntax errors detected' ) === false ) {
+               $res = exec( 'php -l ' . wfEscapeShellArg( $file ) );
+               if ( strpos( $res, 'No syntax errors detected' ) === false ) {
                        $this->mFailures[$file] = $res;
                        $this->output( $res . "\n" );
                        return false;
@@ -265,14 +326,16 @@ class CheckSyntax extends Maintenance {
         * @return boolean
         */
        private function checkForMistakes( $file ) {
-               foreach( $this->mNoStyleCheckPaths as $regex ) {
+               foreach ( $this->mNoStyleCheckPaths as $regex ) {
                        $m = array();
                        if ( preg_match( "~{$regex}~", $file, $m ) )
                                return;
                }
 
                $text = file_get_contents( $file );
+               $tokens = token_get_all( $text );
 
+               $this->checkEvilToken( $file, $tokens, '@', 'Error supression operator (@)');
                $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' );
@@ -289,8 +352,20 @@ class CheckSyntax extends Maintenance {
                $this->mWarnings[$file][] = $desc;
                $this->output( "Warning in file $file: $desc found.\n" );
        }
+
+       private function checkEvilToken( $file, $tokens, $evilToken, $desc ) {
+               if ( !in_array( $evilToken, $tokens ) ) {
+                       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 );
+require_once( RUN_MAINTENANCE_IF_MAIN );