Merge "Minor CSS cleanup for Vector and Monobook skins"
[mediawiki.git] / maintenance / preprocessorFuzzTest.php
blob0a1a9f6f117ba8924d4fbc41c3f5bba876120ae3
1 <?php
2 /**
3 * Performs fuzz-style testing of MediaWiki's preprocessor.
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.
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.
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
20 * @file
21 * @ingroup Maintenance
24 require_once( __DIR__ . '/commandLine.inc' );
26 $wgHooks['BeforeParserFetchTemplateAndtitle'][] = 'PPFuzzTester::templateHook';
28 class PPFuzzTester {
29 public $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',
39 // extensions
40 // '<ref>', '</ref>', '<references/>',
42 public $minLength = 0;
43 public $maxLength = 20;
44 public $maxTemplates = 5;
45 // public $outputTypes = array( 'OT_HTML', 'OT_WIKI', 'OT_PREPROCESS' );
46 public $entryPoints = array( 'testSrvus', 'testPst', 'testPreprocess' );
47 public $verbose = false;
48 static $currentTest = false;
50 function execute() {
51 if ( !file_exists( 'results' ) ) {
52 mkdir( 'results' );
54 if ( !is_dir( 'results' ) ) {
55 echo "Unable to create 'results' directory\n";
56 exit( 1 );
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';
76 $t += microtime( true );
78 if ( $this->verbose ) {
79 printf( "Test $passed in %.3f seconds\n", $t );
80 print self::$currentTest->getReport();
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;
90 } elseif ( $reportMetric < 4 ) {
91 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
92 $reportInterval *= 5;
93 } else {
94 $reportInterval *= 2;
97 if ( $i % $reportInterval == 0 ) {
98 print "$i tests done\n";
100 $testReport = self::$currentTest->getReport();
101 $filename = 'results/ppft-' . md5( $testReport ) . '.pass';
102 file_put_contents( $filename, "Input:\n$testReport\n" );*/
107 function makeInputText( $max = false ) {
108 if ( $max === false ) {
109 $max = $this->maxLength;
111 $length = mt_rand( $this->minLength, $max );
112 $s = '';
113 for ( $i = 0; $i < $length; $i++ ) {
114 $hairIndex = mt_rand( 0, count( $this->hairs ) - 1 );
115 $s .= $this->hairs[$hairIndex];
117 // Send through the UTF-8 normaliser
118 // This resolves a few differences between the old preprocessor and the
119 // XML-based one, which doesn't like illegals and converts line endings.
120 // It's done by the MW UI, so it's a reasonably legitimate thing to do.
121 global $wgContLang;
122 $s = $wgContLang->normalize( $s );
123 return $s;
126 function makeTitle() {
127 return Title::newFromText( mt_rand( 0, 1000000 ), mt_rand( 0, 10 ) );
131 function pickOutputType() {
132 $count = count( $this->outputTypes );
133 return $this->outputTypes[ mt_rand( 0, $count - 1 ) ];
136 function pickEntryPoint() {
137 $count = count( $this->entryPoints );
138 return $this->entryPoints[ mt_rand( 0, $count - 1 ) ];
142 class PPFuzzTest {
143 public $templates, $mainText, $title, $entryPoint, $output;
145 function __construct( $tester ) {
146 global $wgMaxSigChars;
147 $this->parent = $tester;
148 $this->mainText = $tester->makeInputText();
149 $this->title = $tester->makeTitle();
150 // $this->outputType = $tester->pickOutputType();
151 $this->entryPoint = $tester->pickEntryPoint();
152 $this->nickname = $tester->makeInputText( $wgMaxSigChars + 10 );
153 $this->fancySig = (bool)mt_rand( 0, 1 );
154 $this->templates = array();
158 * @param $title Title
160 function templateHook( $title ) {
161 $titleText = $title->getPrefixedDBkey();
163 if ( !isset( $this->templates[$titleText] ) ) {
164 $finalTitle = $title;
165 if ( count( $this->templates ) >= $this->parent->maxTemplates ) {
166 // Too many templates
167 $text = false;
168 } else {
169 if ( !mt_rand( 0, 1 ) ) {
170 // Redirect
171 $finalTitle = $this->parent->makeTitle();
173 if ( !mt_rand( 0, 5 ) ) {
174 // Doesn't exist
175 $text = false;
176 } else {
177 $text = $this->parent->makeInputText();
180 $this->templates[$titleText] = array(
181 'text' => $text,
182 'finalTitle' => $finalTitle );
184 return $this->templates[$titleText];
187 function execute() {
188 global $wgParser, $wgUser;
190 $wgUser = new PPFuzzUser;
191 $wgUser->mName = 'Fuzz';
192 $wgUser->mFrom = 'name';
193 $wgUser->ppfz_test = $this;
195 $options = ParserOptions::newFromUser( $wgUser );
196 $options->setTemplateCallback( array( $this, 'templateHook' ) );
197 $options->setTimestamp( wfTimestampNow() );
198 $this->output = call_user_func( array( $wgParser, $this->entryPoint ), $this->mainText, $this->title, $options );
199 return $this->output;
202 function getReport() {
203 $s = "Title: " . $this->title->getPrefixedDBkey() . "\n" .
204 // "Output type: {$this->outputType}\n" .
205 "Entry point: {$this->entryPoint}\n" .
206 "User: " . ( $this->fancySig ? 'fancy' : 'no-fancy' ) . ' ' . var_export( $this->nickname, true ) . "\n" .
207 "Main text: " . var_export( $this->mainText, true ) . "\n";
208 foreach ( $this->templates as $titleText => $template ) {
209 $finalTitle = $template['finalTitle'];
210 if ( $finalTitle != $titleText ) {
211 $s .= "[[$titleText]] -> [[$finalTitle]]: " . var_export( $template['text'], true ) . "\n";
212 } else {
213 $s .= "[[$titleText]]: " . var_export( $template['text'], true ) . "\n";
216 $s .= "Output: " . var_export( $this->output, true ) . "\n";
217 return $s;
221 class PPFuzzUser extends User {
222 public $ppfz_test, $mDataLoaded;
224 function load() {
225 if ( $this->mDataLoaded ) {
226 return;
228 $this->mDataLoaded = true;
229 $this->loadDefaults( $this->mName );
232 function getOption( $oname, $defaultOverride = null, $ignoreHidden = false ) {
233 if ( $oname === 'fancysig' ) {
234 return $this->ppfz_test->fancySig;
235 } elseif ( $oname === 'nickname' ) {
236 return $this->ppfz_test->nickname;
237 } else {
238 return parent::getOption( $oname, $defaultOverride, $ignoreHidden );
243 ini_set( 'memory_limit', '50M' );
244 if ( isset( $args[0] ) ) {
245 $testText = file_get_contents( $args[0] );
246 if ( !$testText ) {
247 print "File not found\n";
248 exit( 1 );
250 $test = unserialize( $testText );
251 $result = $test->execute();
252 print "Test passed.\n";
253 } else {
254 $tester = new PPFuzzTester;
255 $tester->verbose = isset( $options['verbose'] );
256 $tester->execute();