3 require_once( dirname( __FILE__
). '/../maintenance/commandLine.inc' );
5 $wgHooks['BeforeParserFetchTemplateAndtitle'][] = 'PPFuzzTester::templateHook';
9 '[[', ']]', '{{', '}}', '{{{', '}}}',
10 '<', '>', '<nowiki', '<gallery', '</nowiki>', '</gallery>', '<nOwIkI>', '</NoWiKi>',
13 '|', '=', "\n", ' ', "\t", "\x7f",
14 '~~', '~~~', '~~~~', 'subst:',
15 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
16 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
19 //'<ref>', '</ref>', '<references/>',
23 var $maxTemplates = 5;
24 //var $outputTypes = array( 'OT_HTML', 'OT_WIKI', 'OT_PREPROCESS' );
25 var $entryPoints = array( 'testSrvus', 'testPst', 'testPreprocess' );
26 static $currentTest = false;
29 if ( !file_exists( 'results' ) ) {
32 if ( !is_dir( 'results' ) ) {
33 echo "Unable to create 'results' directory\n";
36 for ( $i = 0; true; $i++
) {
38 self
::$currentTest = new PPFuzzTest( $this );
39 self
::$currentTest->execute();
40 } catch ( MWException
$e ) {
41 $testReport = self
::$currentTest->getReport();
42 $exceptionReport = $e->getText();
43 $hash = md5( $testReport );
44 file_put_contents( "results/ppft-$hash.in", serialize( self
::$currentTest ) );
45 file_put_contents( "results/ppft-$hash.fail",
46 "Input:\n$testReport\n\nException report:\n$exceptionReport\n" );
47 print "Test $hash failed\n";
49 if ( $i %
1000 == 0 ) {
50 print "$i tests done\n";
52 $testReport = self::$currentTest->getReport();
53 $filename = 'results/ppft-' . md5( $testReport ) . '.pass';
54 file_put_contents( $filename, "Input:\n$testReport\n" );*/
59 function makeInputText() {
60 $length = mt_rand( $this->minLength
, $this->maxLength
);
62 for ( $i = 0; $i < $length; $i++
) {
63 $hairIndex = mt_rand( 0, count( $this->hairs
) - 1 );
64 $s .= $this->hairs
[$hairIndex];
66 // Send through the UTF-8 normaliser
67 // This resolves a few differences between the old preprocessor and the
68 // XML-based one, which doesn't like illegals and converts line endings.
69 // It's done by the MW UI, so it's a reasonably legitimate thing to do.
70 $s = UtfNormal
::cleanUp( $s );
74 function makeTitle() {
75 return Title
::newFromText( mt_rand( 0, 1000000 ), mt_rand( 0, 10 ) );
79 function pickOutputType() {
80 $count = count( $this->outputTypes );
81 return $this->outputTypes[ mt_rand( 0, $count - 1 ) ];
84 function pickEntryPoint() {
85 $count = count( $this->entryPoints
);
86 return $this->entryPoints
[ mt_rand( 0, $count - 1 ) ];
91 var $templates, $mainText, $title, $entryPoint;
93 function __construct( $tester ) {
94 $this->parent
= $tester;
95 $this->mainText
= $tester->makeInputText();
96 $this->title
= $tester->makeTitle();
97 //$this->outputType = $tester->pickOutputType();
98 $this->entryPoint
= $tester->pickEntryPoint();
99 $this->nickname
= $tester->makeInputText();
100 $this->fancySig
= (bool)mt_rand( 0, 1 );
101 $this->templates
= array();
104 function templateHook( $title ) {
105 $titleText = $title->getPrefixedDBkey();
107 if ( !isset( $this->templates
[$titleText] ) ) {
108 $finalTitle = $title;
109 if ( count( $this->templates
) >= $this->parent
->maxTemplates
) {
110 // Too many templates
113 if ( !mt_rand( 0, 1 ) ) {
115 $finalTitle = $this->parent
->makeTitle();
117 if ( !mt_rand( 0, 5 ) ) {
121 $text = $this->parent
->makeInputText();
124 $this->templates
[$titleText] = array(
126 'finalTitle' => $finalTitle );
128 return $this->templates
[$titleText];
132 global $wgParser, $wgUser;
134 $wgUser = new PPFuzzUser
;
135 $wgUser->mName
= 'Fuzz';
136 $wgUser->mFrom
= 'name';
137 $wgUser->ppfz_test
= $this;
139 $options = new ParserOptions
;
140 $options->setTemplateCallback( array( $this, 'templateHook' ) );
141 //$wgParser->startExternalParse( $this->title, $options, constant( $this->outputType ) );
142 return call_user_func( array( $wgParser, $this->entryPoint
), $this->mainText
, $this->title
, $options );
145 function getReport() {
146 $s = "Title: " . $this->title
->getPrefixedDBkey() . "\n" .
147 // "Output type: {$this->outputType}\n" .
148 "Entry point: {$this->entryPoint}\n" .
149 "User: " . ( $this->fancySig ?
'fancy' : 'no-fancy' ) . ' ' . var_export( $this->nickname
, true ) . "\n" .
150 "Main text: " . var_export( $this->mainText
, true ) . "\n";
151 foreach ( $this->templates
as $titleText => $template ) {
152 $finalTitle = $template['finalTitle'];
153 if ( $finalTitle != $titleText ) {
154 $s .= "[[$titleText]] -> [[$finalTitle]]: " . var_export( $template['text'], true ) . "\n";
156 $s .= "[[$titleText]]: " . var_export( $template['text'], true ) . "\n";
163 class PPFuzzUser
extends User
{
166 function getOption( $option, $defaultOverride = '' ) {
167 if ( $option === 'fancysig' ) {
168 return $this->ppfz_test
->fancySig
;
169 } elseif ( $option === 'nickname' ) {
170 return $this->ppfz_test
->nickname
;
172 return parent
::getOption( $option, $defaultOverride );
177 ini_set( 'memory_limit', '50M' );
178 if ( isset( $args[0] ) ) {
179 $testText = file_get_contents( $args[0] );
181 print "File not found\n";
184 $test = unserialize( $testText );
185 print $test->getReport();
186 $result = $test->execute();
187 print "Test passed.\nResult: $result\n";
189 $tester = new PPFuzzTester
;