5a029f6ae176099e0e9bc1fa7f6c3d9357141a5c
[lhc/web/wiklou.git] / maintenance / preprocessorFuzzTest.php
1 <?php
2
3 require_once( dirname( __FILE__ ). '/../maintenance/commandLine.inc' );
4
5 $wgHooks['BeforeParserFetchTemplateAndtitle'][] = 'PPFuzzTester::templateHook';
6
7 class PPFuzzTester {
8 var $hairs = array(
9 '[[', ']]', '{{', '}}', '{{{', '}}}',
10 '<', '>', '<nowiki', '<gallery', '</nowiki>', '</gallery>', '<nOwIkI>', '</NoWiKi>',
11 //'<!--' , '-->',
12 //'<ref>', '</ref>', '<references/>',
13 "\n==", "==\n",
14 '|', '=', "\n", ' ', "\t", "\x7f",
15 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
16 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
17 );
18 var $minLength = 0;
19 var $maxLength = 20;
20 var $maxTemplates = 5;
21 var $outputTypes = array( 'OT_HTML', 'OT_WIKI', 'OT_MSG', 'OT_PREPROCESS' );
22 static $currentTest = false;
23
24 function execute() {
25 if ( !file_exists( 'results' ) ) {
26 mkdir( 'results' );
27 }
28 if ( !is_dir( 'results' ) ) {
29 echo "Unable to create 'results' directory\n";
30 exit( 1 );
31 }
32 for ( $i = 0; true; $i++ ) {
33 try {
34 self::$currentTest = new PPFuzzTest( $this );
35 self::$currentTest->execute();
36 } catch ( MWException $e ) {
37 $testReport = self::$currentTest->getReport();
38 $exceptionReport = $e->getText();
39 $hash = md5( $testReport );
40 file_put_contents( "results/ppft-$hash.in", serialize( self::$currentTest ) );
41 file_put_contents( "results/ppft-$hash.fail",
42 "Input:\n$testReport\n\nException report:\n$exceptionReport\n" );
43 print "Test $hash failed\n";
44 }
45 if ( $i % 1000 == 0 ) {
46 print "$i tests done\n";
47 /*
48 $testReport = self::$currentTest->getReport();
49 $filename = 'results/ppft-' . md5( $testReport ) . '.pass';
50 file_put_contents( $filename, "Input:\n$testReport\n" );*/
51 }
52 }
53 }
54
55 function makeInputText() {
56 $length = mt_rand( $this->minLength, $this->maxLength );
57 $s = '';
58 for ( $i = 0; $i < $length; $i++ ) {
59 $hairIndex = mt_rand( 0, count( $this->hairs ) - 1 );
60 $s .= $this->hairs[$hairIndex];
61 }
62 // Send through the UTF-8 normaliser
63 // This resolves a few differences between the old preprocessor and the
64 // XML-based one, which doesn't like illegals and converts line endings.
65 // It's done by the MW UI, so it's a reasonably legitimate thing to do.
66 $s = UtfNormal::cleanUp( $s );
67 return $s;
68 }
69
70 function makeTitle() {
71 return Title::newFromText( mt_rand( 0, 1000000 ), mt_rand( 0, 10 ) );
72 }
73
74 function pickOutputType() {
75 $count = count( $this->outputTypes );
76 return $this->outputTypes[ mt_rand( 0, $count - 1 ) ];
77 }
78 }
79
80 class PPFuzzTest {
81 var $templates, $mainText, $title;
82
83 function __construct( $tester ) {
84 $this->parent = $tester;
85 $this->mainText = $tester->makeInputText();
86 $this->title = $tester->makeTitle();
87 $this->outputType = $tester->pickOutputType();
88 $this->templates = array();
89 }
90
91 function templateHook( $title ) {
92 $titleText = $title->getPrefixedDBkey();
93
94 if ( !isset( $this->templates[$titleText] ) ) {
95 $finalTitle = $title;
96 if ( count( $this->templates ) >= $this->parent->maxTemplates ) {
97 // Too many templates
98 $text = false;
99 } else {
100 if ( !mt_rand( 0, 1 ) ) {
101 // Redirect
102 $finalTitle = $this->parent->makeTitle();
103 }
104 if ( !mt_rand( 0, 5 ) ) {
105 // Doesn't exist
106 $text = false;
107 } else {
108 $text = $this->parent->makeInputText();
109 }
110 }
111 $this->templates[$titleText] = array(
112 'text' => $text,
113 'finalTitle' => $finalTitle );
114 }
115 return $this->templates[$titleText];
116 }
117
118 function execute() {
119 global $wgParser;
120 $options = new ParserOptions;
121 $options->setTemplateCallback( array( $this, 'templateHook' ) );
122 $wgParser->startExternalParse( $this->title, $options, constant( $this->outputType ) );
123 return $wgParser->srvus( $this->mainText );
124 }
125
126 function getReport() {
127 $s = "Title: " . $this->title->getPrefixedDBkey() . "\n" .
128 "Output type: {$this->outputType}\n" .
129 "Main text: " . var_export( $this->mainText, true ) . "\n";
130 foreach ( $this->templates as $titleText => $template ) {
131 $finalTitle = $template['finalTitle'];
132 if ( $finalTitle != $titleText ) {
133 $s .= "[[$titleText]] -> [[$finalTitle]]: " . var_export( $template['text'], true ) . "\n";
134 } else {
135 $s .= "[[$titleText]]: " . var_export( $template['text'], true ) . "\n";
136 }
137 }
138 return $s;
139 }
140 }
141
142 ini_set( 'memory_limit', '50M' );
143 if ( isset( $args[0] ) ) {
144 $testText = file_get_contents( $args[0] );
145 if ( !$testText ) {
146 print "File not found\n";
147 exit( 1 );
148 }
149 $test = unserialize( $testText );
150 print $test->getReport();
151 $result = $test->execute();
152 print "Test passed.\nResult: $result\n";
153 } else {
154 $tester = new PPFuzzTester;
155 $tester->execute();
156 }