Applying live hack from Wikimedia code base: tweaks to runJobs.php. Not clear that...
[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 "\n==", "==\n",
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',
17
18 // extensions
19 //'<ref>', '</ref>', '<references/>',
20 );
21 var $minLength = 0;
22 var $maxLength = 20;
23 var $maxTemplates = 5;
24 //var $outputTypes = array( 'OT_HTML', 'OT_WIKI', 'OT_PREPROCESS' );
25 var $entryPoints = array( 'testSrvus', 'testPst', 'testPreprocess' );
26 var $verbose = false;
27 static $currentTest = false;
28
29 function execute() {
30 if ( !file_exists( 'results' ) ) {
31 mkdir( 'results' );
32 }
33 if ( !is_dir( 'results' ) ) {
34 echo "Unable to create 'results' directory\n";
35 exit( 1 );
36 }
37 $overallStart = microtime( true );
38 $reportInterval = 1000;
39 for ( $i = 1; true; $i++ ) {
40 $t = -microtime( true );
41 try {
42 self::$currentTest = new PPFuzzTest( $this );
43 self::$currentTest->execute();
44 $passed = 'passed';
45 } catch ( MWException $e ) {
46 $testReport = self::$currentTest->getReport();
47 $exceptionReport = $e->getText();
48 $hash = md5( $testReport );
49 file_put_contents( "results/ppft-$hash.in", serialize( self::$currentTest ) );
50 file_put_contents( "results/ppft-$hash.fail",
51 "Input:\n$testReport\n\nException report:\n$exceptionReport\n" );
52 print "Test $hash failed\n";
53 $passed = 'failed';
54 }
55 $t += microtime( true );
56
57 if ( $this->verbose ) {
58 printf( "Test $passed in %.3f seconds\n", $t );
59 print self::$currentTest->getReport();
60 }
61
62 $reportMetric = ( microtime( true ) - $overallStart ) / $i * $reportInterval;
63 if ( $reportMetric > 25 ) {
64 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
65 $reportInterval /= 2;
66 } else {
67 $reportInterval /= 5;
68 }
69 } elseif ( $reportMetric < 4 ) {
70 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
71 $reportInterval *= 5;
72 } else {
73 $reportInterval *= 2;
74 }
75 }
76 if ( $i % $reportInterval == 0 ) {
77 print "$i tests done\n";
78 /*
79 $testReport = self::$currentTest->getReport();
80 $filename = 'results/ppft-' . md5( $testReport ) . '.pass';
81 file_put_contents( $filename, "Input:\n$testReport\n" );*/
82 }
83 }
84 wfLogProfilingData();
85 }
86
87 function makeInputText( $max = false ) {
88 if ( $max === false ) {
89 $max = $this->maxLength;
90 }
91 $length = mt_rand( $this->minLength, $max );
92 $s = '';
93 for ( $i = 0; $i < $length; $i++ ) {
94 $hairIndex = mt_rand( 0, count( $this->hairs ) - 1 );
95 $s .= $this->hairs[$hairIndex];
96 }
97 // Send through the UTF-8 normaliser
98 // This resolves a few differences between the old preprocessor and the
99 // XML-based one, which doesn't like illegals and converts line endings.
100 // It's done by the MW UI, so it's a reasonably legitimate thing to do.
101 $s = UtfNormal::cleanUp( $s );
102 return $s;
103 }
104
105 function makeTitle() {
106 return Title::newFromText( mt_rand( 0, 1000000 ), mt_rand( 0, 10 ) );
107 }
108
109 /*
110 function pickOutputType() {
111 $count = count( $this->outputTypes );
112 return $this->outputTypes[ mt_rand( 0, $count - 1 ) ];
113 }*/
114
115 function pickEntryPoint() {
116 $count = count( $this->entryPoints );
117 return $this->entryPoints[ mt_rand( 0, $count - 1 ) ];
118 }
119 }
120
121 class PPFuzzTest {
122 var $templates, $mainText, $title, $entryPoint, $output;
123
124 function __construct( $tester ) {
125 global $wgMaxSigChars;
126 $this->parent = $tester;
127 $this->mainText = $tester->makeInputText();
128 $this->title = $tester->makeTitle();
129 //$this->outputType = $tester->pickOutputType();
130 $this->entryPoint = $tester->pickEntryPoint();
131 $this->nickname = $tester->makeInputText( $wgMaxSigChars + 10);
132 $this->fancySig = (bool)mt_rand( 0, 1 );
133 $this->templates = array();
134 }
135
136 function templateHook( $title ) {
137 $titleText = $title->getPrefixedDBkey();
138
139 if ( !isset( $this->templates[$titleText] ) ) {
140 $finalTitle = $title;
141 if ( count( $this->templates ) >= $this->parent->maxTemplates ) {
142 // Too many templates
143 $text = false;
144 } else {
145 if ( !mt_rand( 0, 1 ) ) {
146 // Redirect
147 $finalTitle = $this->parent->makeTitle();
148 }
149 if ( !mt_rand( 0, 5 ) ) {
150 // Doesn't exist
151 $text = false;
152 } else {
153 $text = $this->parent->makeInputText();
154 }
155 }
156 $this->templates[$titleText] = array(
157 'text' => $text,
158 'finalTitle' => $finalTitle );
159 }
160 return $this->templates[$titleText];
161 }
162
163 function execute() {
164 global $wgParser, $wgUser;
165
166 $wgUser = new PPFuzzUser;
167 $wgUser->mName = 'Fuzz';
168 $wgUser->mFrom = 'name';
169 $wgUser->ppfz_test = $this;
170
171 $options = new ParserOptions;
172 $options->setTemplateCallback( array( $this, 'templateHook' ) );
173 $options->setTimestamp( wfTimestampNow() );
174 $this->output = call_user_func( array( $wgParser, $this->entryPoint ), $this->mainText, $this->title->getPrefixedText(), $options );
175 return $this->output;
176 }
177
178 function getReport() {
179 $s = "Title: " . $this->title->getPrefixedDBkey() . "\n" .
180 // "Output type: {$this->outputType}\n" .
181 "Entry point: {$this->entryPoint}\n" .
182 "User: " . ( $this->fancySig ? 'fancy' : 'no-fancy' ) . ' ' . var_export( $this->nickname, true ) . "\n" .
183 "Main text: " . var_export( $this->mainText, true ) . "\n";
184 foreach ( $this->templates as $titleText => $template ) {
185 $finalTitle = $template['finalTitle'];
186 if ( $finalTitle != $titleText ) {
187 $s .= "[[$titleText]] -> [[$finalTitle]]: " . var_export( $template['text'], true ) . "\n";
188 } else {
189 $s .= "[[$titleText]]: " . var_export( $template['text'], true ) . "\n";
190 }
191 }
192 $s .= "Output: " . var_export( $this->output, true ) . "\n";
193 return $s;
194 }
195 }
196
197 class PPFuzzUser extends User {
198 var $ppfz_test;
199
200 function load() {
201 if ( $this->mDataLoaded ) {
202 return;
203 }
204 $this->mDataLoaded = true;
205 $this->loadDefaults( $this->mName );
206 }
207
208 function getOption( $option, $defaultOverride = '' ) {
209 if ( $option === 'fancysig' ) {
210 return $this->ppfz_test->fancySig;
211 } elseif ( $option === 'nickname' ) {
212 return $this->ppfz_test->nickname;
213 } else {
214 return parent::getOption( $option, $defaultOverride );
215 }
216 }
217 }
218
219 ini_set( 'memory_limit', '50M' );
220 if ( isset( $args[0] ) ) {
221 $testText = file_get_contents( $args[0] );
222 if ( !$testText ) {
223 print "File not found\n";
224 exit( 1 );
225 }
226 $test = unserialize( $testText );
227 $result = $test->execute();
228 print "Test passed.\n";
229 } else {
230 $tester = new PPFuzzTester;
231 $tester->verbose = isset( $options['verbose'] );
232 $tester->execute();
233 }