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