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