]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - maintenance/preprocessorFuzzTest.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / maintenance / preprocessorFuzzTest.php
index 31b372c2f9516cd8888ef053cd9613fee2a9abba..2503ed25e21cbf76a0c6d4a9a3fb7895092185fe 100644 (file)
@@ -1,18 +1,36 @@
 <?php
 /**
+ * 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' );
+$optionsWithoutArgs = [ 'verbose' ];
+require_once __DIR__ . '/commandLine.inc';
 
 $wgHooks['BeforeParserFetchTemplateAndtitle'][] = 'PPFuzzTester::templateHook';
 
 class PPFuzzTester {
-       var $hairs = array(
+       public $hairs = [
                '[[', ']]', '{{', '{{', '}}', '}}', '{{{', '}}}',
                '<', '>', '<nowiki', '<gallery', '</nowiki>', '</gallery>', '<nOwIkI>', '</NoWiKi>',
-               '<!--' , '-->',
+               '<!--', '-->',
                "\n==", "==\n",
                '|', '=', "\n", ' ', "\t", "\x7f",
                '~~', '~~~', '~~~~', 'subst:',
@@ -21,14 +39,18 @@ class PPFuzzTester {
 
                // extensions
                // '<ref>', '</ref>', '<references/>',
-       );
-       var $minLength = 0;
-       var $maxLength = 20;
-       var $maxTemplates = 5;
-       // var $outputTypes = array( 'OT_HTML', 'OT_WIKI', 'OT_PREPROCESS' );
-       var $entryPoints = array( 'testSrvus', 'testPst', 'testPreprocess' );
-       var $verbose = false;
-       static $currentTest = false;
+       ];
+       public $minLength = 0;
+       public $maxLength = 20;
+       public $maxTemplates = 5;
+       // public $outputTypes = [ 'OT_HTML', 'OT_WIKI', 'OT_PREPROCESS' ];
+       public $entryPoints = [ 'testSrvus', 'testPst', 'testPreprocess' ];
+       public $verbose = false;
+
+       /**
+        * @var bool|PPFuzzTest
+        */
+       private static $currentTest = false;
 
        function execute() {
                if ( !file_exists( 'results' ) ) {
@@ -46,7 +68,7 @@ class PPFuzzTester {
                                self::$currentTest = new PPFuzzTest( $this );
                                self::$currentTest->execute();
                                $passed = 'passed';
-                       } catch ( MWException $e ) {
+                       } catch ( Exception $e ) {
                                $testReport = self::$currentTest->getReport();
                                $exceptionReport = $e->getText();
                                $hash = md5( $testReport );
@@ -85,7 +107,6 @@ class PPFuzzTester {
                                file_put_contents( $filename, "Input:\n$testReport\n" );*/
                        }
                }
-               wfLogProfilingData();
        }
 
        function makeInputText( $max = false ) {
@@ -104,6 +125,7 @@ class PPFuzzTester {
                // It's done by the MW UI, so it's a reasonably legitimate thing to do.
                global $wgContLang;
                $s = $wgContLang->normalize( $s );
+
                return $s;
        }
 
@@ -119,12 +141,13 @@ class PPFuzzTester {
 
        function pickEntryPoint() {
                $count = count( $this->entryPoints );
-               return $this->entryPoints[ mt_rand( 0, $count - 1 ) ];
+
+               return $this->entryPoints[mt_rand( 0, $count - 1 )];
        }
 }
 
 class PPFuzzTest {
-       var $templates, $mainText, $title, $entryPoint, $output;
+       public $templates, $mainText, $title, $entryPoint, $output;
 
        function __construct( $tester ) {
                global $wgMaxSigChars;
@@ -135,9 +158,13 @@ class PPFuzzTest {
                $this->entryPoint = $tester->pickEntryPoint();
                $this->nickname = $tester->makeInputText( $wgMaxSigChars + 10 );
                $this->fancySig = (bool)mt_rand( 0, 1 );
-               $this->templates = array();
+               $this->templates = [];
        }
 
+       /**
+        * @param Title $title
+        * @return array
+        */
        function templateHook( $title ) {
                $titleText = $title->getPrefixedDBkey();
 
@@ -158,10 +185,11 @@ class PPFuzzTest {
                                        $text = $this->parent->makeInputText();
                                }
                        }
-                       $this->templates[$titleText] = array(
+                       $this->templates[$titleText] = [
                                'text' => $text,
-                               'finalTitle' => $finalTitle );
+                               'finalTitle' => $finalTitle ];
                }
+
                return $this->templates[$titleText];
        }
 
@@ -173,18 +201,25 @@ class PPFuzzTest {
                $wgUser->mFrom = 'name';
                $wgUser->ppfz_test = $this;
 
-               $options = new ParserOptions;
-               $options->setTemplateCallback( array( $this, 'templateHook' ) );
+               $options = ParserOptions::newFromUser( $wgUser );
+               $options->setTemplateCallback( [ $this, 'templateHook' ] );
                $options->setTimestamp( wfTimestampNow() );
-               $this->output = call_user_func( array( $wgParser, $this->entryPoint ), $this->mainText, $this->title->getPrefixedText(), $options );
+               $this->output = call_user_func(
+                       [ $wgParser, $this->entryPoint ],
+                       $this->mainText,
+                       $this->title,
+                       $options
+               );
+
                return $this->output;
        }
 
        function getReport() {
                $s = "Title: " . $this->title->getPrefixedDBkey() . "\n" .
-//                     "Output type: {$this->outputType}\n" .
+//                     "Output type: {$this->outputType}\n" .
                        "Entry point: {$this->entryPoint}\n" .
-                       "User: " . ( $this->fancySig ? 'fancy' : 'no-fancy' ) . ' ' . var_export( $this->nickname, true ) . "\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'];
@@ -195,12 +230,13 @@ class PPFuzzTest {
                        }
                }
                $s .= "Output: " . var_export( $this->output, true ) . "\n";
+
                return $s;
        }
 }
 
 class PPFuzzUser extends User {
-       var $ppfz_test;
+       public $ppfz_test, $mDataLoaded;
 
        function load() {
                if ( $this->mDataLoaded ) {
@@ -210,13 +246,13 @@ class PPFuzzUser extends User {
                $this->loadDefaults( $this->mName );
        }
 
-       function getOption( $option, $defaultOverride = '' ) {
-               if ( $option === 'fancysig' ) {
+       function getOption( $oname, $defaultOverride = null, $ignoreHidden = false ) {
+               if ( $oname === 'fancysig' ) {
                        return $this->ppfz_test->fancySig;
-               } elseif ( $option === 'nickname' ) {
+               } elseif ( $oname === 'nickname' ) {
                        return $this->ppfz_test->nickname;
                } else {
-                       return parent::getOption( $option, $defaultOverride );
+                       return parent::getOption( $oname, $defaultOverride, $ignoreHidden );
                }
        }
 }