Type hinting
[lhc/web/wiklou.git] / maintenance / preprocessorFuzzTest.php
index e3cd405..fbf3b93 100644 (file)
@@ -1,28 +1,50 @@
 <?php
-
-require_once( dirname( __FILE__ ). '/../maintenance/commandLine.inc' );
+/**
+ * Performs fuzz-style testing of MediaWiki's preprocessor.
+ *
+ * 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
+ *
+ * @file
+ * @ingroup Maintenance
+ */
+
+require_once( dirname( __FILE__ ) . '/commandLine.inc' );
 
 $wgHooks['BeforeParserFetchTemplateAndtitle'][] = 'PPFuzzTester::templateHook';
 
 class PPFuzzTester {
        var $hairs = array(
-               '[[', ']]', '{{', '}}', '{{{', '}}}', 
+               '[[', ']]', '{{', '{{', '}}', '}}', '{{{', '}}}',
                '<', '>', '<nowiki', '<gallery', '</nowiki>', '</gallery>', '<nOwIkI>', '</NoWiKi>',
                '<!--' , '-->',
                "\n==", "==\n",
                '|', '=', "\n", ' ', "\t", "\x7f",
                '~~', '~~~', '~~~~', 'subst:',
-               'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 
+               'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
 
                // extensions
-               //'<ref>', '</ref>', '<references/>',
+               // '<ref>', '</ref>', '<references/>',
        );
        var $minLength = 0;
        var $maxLength = 20;
        var $maxTemplates = 5;
-       //var $outputTypes = array( 'OT_HTML', 'OT_WIKI', 'OT_PREPROCESS' );
+       // var $outputTypes = array( 'OT_HTML', 'OT_WIKI', 'OT_PREPROCESS' );
        var $entryPoints = array( 'testSrvus', 'testPst', 'testPreprocess' );
+       var $verbose = false;
        static $currentTest = false;
 
        function execute() {
@@ -33,20 +55,46 @@ class PPFuzzTester {
                        echo "Unable to create 'results' directory\n";
                        exit( 1 );
                }
-               for ( $i = 0; true; $i++ ) {
+               $overallStart = microtime( true );
+               $reportInterval = 1000;
+               for ( $i = 1; true; $i++ ) {
+                       $t = -microtime( true );
                        try {
                                self::$currentTest = new PPFuzzTest( $this );
                                self::$currentTest->execute();
+                               $passed = 'passed';
                        } catch ( MWException $e ) {
                                $testReport = self::$currentTest->getReport();
                                $exceptionReport = $e->getText();
                                $hash = md5( $testReport );
                                file_put_contents( "results/ppft-$hash.in", serialize( self::$currentTest ) );
-                               file_put_contents( "results/ppft-$hash.fail", 
+                               file_put_contents( "results/ppft-$hash.fail",
                                        "Input:\n$testReport\n\nException report:\n$exceptionReport\n" );
                                print "Test $hash failed\n";
+                               $passed = 'failed';
+                       }
+                       $t += microtime( true );
+
+                       if ( $this->verbose ) {
+                               printf( "Test $passed in %.3f seconds\n", $t );
+                               print self::$currentTest->getReport();
+                       }
+
+                       $reportMetric = ( microtime( true ) - $overallStart ) / $i * $reportInterval;
+                       if ( $reportMetric > 25 ) {
+                               if ( substr( $reportInterval, 0, 1 ) === '1' ) {
+                                       $reportInterval /= 2;
+                               } else {
+                                       $reportInterval /= 5;
+                               }
+                       } elseif ( $reportMetric < 4 ) {
+                               if ( substr( $reportInterval, 0, 1 ) === '1' ) {
+                                       $reportInterval *= 5;
+                               } else {
+                                       $reportInterval *= 2;
+                               }
                        }
-                       if ( $i % 1000 == 0 ) {
+                       if ( $i % $reportInterval == 0 ) {
                                print "$i tests done\n";
                                /*
                                $testReport = self::$currentTest->getReport();
@@ -54,20 +102,25 @@ class PPFuzzTester {
                                file_put_contents( $filename, "Input:\n$testReport\n" );*/
                        }
                }
+               wfLogProfilingData();
        }
 
-       function makeInputText() {
-               $length = mt_rand( $this->minLength, $this->maxLength );
+       function makeInputText( $max = false ) {
+               if ( $max === false ) {
+                       $max = $this->maxLength;
+               }
+               $length = mt_rand( $this->minLength, $max );
                $s = '';
                for ( $i = 0; $i < $length; $i++ ) {
                        $hairIndex = mt_rand( 0, count( $this->hairs ) - 1 );
                        $s .= $this->hairs[$hairIndex];
                }
                // Send through the UTF-8 normaliser
-               // This resolves a few differences between the old preprocessor and the 
+               // This resolves a few differences between the old preprocessor and the
                // XML-based one, which doesn't like illegals and converts line endings.
                // It's done by the MW UI, so it's a reasonably legitimate thing to do.
-               $s = UtfNormal::cleanUp( $s );
+               global $wgContLang;
+               $s = $wgContLang->normalize( $s );
                return $s;
        }
 
@@ -88,22 +141,23 @@ class PPFuzzTester {
 }
 
 class PPFuzzTest {
-       var $templates, $mainText, $title, $entryPoint;
+       var $templates, $mainText, $title, $entryPoint, $output;
 
        function __construct( $tester ) {
+               global $wgMaxSigChars;
                $this->parent = $tester;
                $this->mainText = $tester->makeInputText();
                $this->title = $tester->makeTitle();
-               //$this->outputType = $tester->pickOutputType();
+               // $this->outputType = $tester->pickOutputType();
                $this->entryPoint = $tester->pickEntryPoint();
-               $this->nickname = $tester->makeInputText();
+               $this->nickname = $tester->makeInputText( $wgMaxSigChars + 10 );
                $this->fancySig = (bool)mt_rand( 0, 1 );
                $this->templates = array();
        }
 
        function templateHook( $title ) {
                $titleText = $title->getPrefixedDBkey();
-               
+
                if ( !isset( $this->templates[$titleText] ) ) {
                        $finalTitle = $title;
                        if ( count( $this->templates ) >= $this->parent->maxTemplates ) {
@@ -138,15 +192,16 @@ class PPFuzzTest {
 
                $options = new ParserOptions;
                $options->setTemplateCallback( array( $this, 'templateHook' ) );
-               //$wgParser->startExternalParse( $this->title, $options, constant( $this->outputType ) );
-               return call_user_func( array( $wgParser, $this->entryPoint ), $this->mainText, $this->title, $options );
+               $options->setTimestamp( wfTimestampNow() );
+               $this->output = call_user_func( array( $wgParser, $this->entryPoint ), $this->mainText, $this->title->getPrefixedText(), $options );
+               return $this->output;
        }
 
        function getReport() {
                $s = "Title: " . $this->title->getPrefixedDBkey() . "\n" .
-//                     "Output type: {$this->outputType}\n" . 
-                       "Entry point: {$this->entryPoint}\n" . 
-                       "User: " . ( $this->fancySig ? 'fancy' : 'no-fancy' ) . ' ' . var_export( $this->nickname, true ) . "\n" . 
+//                     "Output type: {$this->outputType}\n" .
+                       "Entry point: {$this->entryPoint}\n" .
+                       "User: " . ( $this->fancySig ? 'fancy' : 'no-fancy' ) . ' ' . var_export( $this->nickname, true ) . "\n" .
                        "Main text: " . var_export( $this->mainText, true ) . "\n";
                foreach ( $this->templates as $titleText => $template ) {
                        $finalTitle = $template['finalTitle'];
@@ -156,6 +211,7 @@ class PPFuzzTest {
                                $s .= "[[$titleText]]: " . var_export( $template['text'], true ) . "\n";
                        }
                }
+               $s .= "Output: " . var_export( $this->output, true ) . "\n";
                return $s;
        }
 }
@@ -163,6 +219,14 @@ class PPFuzzTest {
 class PPFuzzUser extends User {
        var $ppfz_test;
 
+       function load() {
+               if ( $this->mDataLoaded ) {
+                       return;
+               }
+               $this->mDataLoaded = true;
+               $this->loadDefaults( $this->mName );
+       }
+
        function getOption( $option, $defaultOverride = '' ) {
                if ( $option === 'fancysig' ) {
                        return $this->ppfz_test->fancySig;
@@ -182,10 +246,10 @@ if ( isset( $args[0] ) ) {
                exit( 1 );
        }
        $test = unserialize( $testText );
-       print $test->getReport();
        $result = $test->execute();
-       print "Test passed.\nResult: $result\n";
+       print "Test passed.\n";
 } else {
        $tester = new PPFuzzTester;
+       $tester->verbose = isset( $options['verbose'] );
        $tester->execute();
 }