Merge "Make update.php file executable"
[mediawiki.git] / maintenance / preprocessorFuzzTest.php
blob9155623f100defd8b5520b140ef497a63c978c8c
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;
49 private static $currentTest = false;
51 function execute() {
52 if ( !file_exists( 'results' ) ) {
53 mkdir( 'results' );
55 if ( !is_dir( 'results' ) ) {
56 echo "Unable to create 'results' directory\n";
57 exit( 1 );
59 $overallStart = microtime( true );
60 $reportInterval = 1000;
61 for ( $i = 1; true; $i++ ) {
62 $t = -microtime( true );
63 try {
64 self::$currentTest = new PPFuzzTest( $this );
65 self::$currentTest->execute();
66 $passed = 'passed';
67 } catch ( MWException $e ) {
68 $testReport = self::$currentTest->getReport();
69 $exceptionReport = $e->getText();
70 $hash = md5( $testReport );
71 file_put_contents( "results/ppft-$hash.in", serialize( self::$currentTest ) );
72 file_put_contents( "results/ppft-$hash.fail",
73 "Input:\n$testReport\n\nException report:\n$exceptionReport\n" );
74 print "Test $hash failed\n";
75 $passed = 'failed';
77 $t += microtime( true );
79 if ( $this->verbose ) {
80 printf( "Test $passed in %.3f seconds\n", $t );
81 print self::$currentTest->getReport();
84 $reportMetric = ( microtime( true ) - $overallStart ) / $i * $reportInterval;
85 if ( $reportMetric > 25 ) {
86 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
87 $reportInterval /= 2;
88 } else {
89 $reportInterval /= 5;
91 } elseif ( $reportMetric < 4 ) {
92 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
93 $reportInterval *= 5;
94 } else {
95 $reportInterval *= 2;
98 if ( $i % $reportInterval == 0 ) {
99 print "$i tests done\n";
101 $testReport = self::$currentTest->getReport();
102 $filename = 'results/ppft-' . md5( $testReport ) . '.pass';
103 file_put_contents( $filename, "Input:\n$testReport\n" );*/
108 function makeInputText( $max = false ) {
109 if ( $max === false ) {
110 $max = $this->maxLength;
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];
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 );
125 return $s;
128 function makeTitle() {
129 return Title::newFromText( mt_rand( 0, 1000000 ), mt_rand( 0, 10 ) );
133 function pickOutputType() {
134 $count = count( $this->outputTypes );
135 return $this->outputTypes[ mt_rand( 0, $count - 1 ) ];
138 function pickEntryPoint() {
139 $count = count( $this->entryPoints );
141 return $this->entryPoints[mt_rand( 0, $count - 1 )];
145 class PPFuzzTest {
146 public $templates, $mainText, $title, $entryPoint, $output;
148 function __construct( $tester ) {
149 global $wgMaxSigChars;
150 $this->parent = $tester;
151 $this->mainText = $tester->makeInputText();
152 $this->title = $tester->makeTitle();
153 // $this->outputType = $tester->pickOutputType();
154 $this->entryPoint = $tester->pickEntryPoint();
155 $this->nickname = $tester->makeInputText( $wgMaxSigChars + 10 );
156 $this->fancySig = (bool)mt_rand( 0, 1 );
157 $this->templates = array();
161 * @param Title $title
163 function templateHook( $title ) {
164 $titleText = $title->getPrefixedDBkey();
166 if ( !isset( $this->templates[$titleText] ) ) {
167 $finalTitle = $title;
168 if ( count( $this->templates ) >= $this->parent->maxTemplates ) {
169 // Too many templates
170 $text = false;
171 } else {
172 if ( !mt_rand( 0, 1 ) ) {
173 // Redirect
174 $finalTitle = $this->parent->makeTitle();
176 if ( !mt_rand( 0, 5 ) ) {
177 // Doesn't exist
178 $text = false;
179 } else {
180 $text = $this->parent->makeInputText();
183 $this->templates[$titleText] = array(
184 'text' => $text,
185 'finalTitle' => $finalTitle );
188 return $this->templates[$titleText];
191 function execute() {
192 global $wgParser, $wgUser;
194 $wgUser = new PPFuzzUser;
195 $wgUser->mName = 'Fuzz';
196 $wgUser->mFrom = 'name';
197 $wgUser->ppfz_test = $this;
199 $options = ParserOptions::newFromUser( $wgUser );
200 $options->setTemplateCallback( array( $this, 'templateHook' ) );
201 $options->setTimestamp( wfTimestampNow() );
202 $this->output = call_user_func(
203 array( $wgParser, $this->entryPoint ),
204 $this->mainText,
205 $this->title,
206 $options
209 return $this->output;
212 function getReport() {
213 $s = "Title: " . $this->title->getPrefixedDBkey() . "\n" .
214 // "Output type: {$this->outputType}\n" .
215 "Entry point: {$this->entryPoint}\n" .
216 "User: " . ( $this->fancySig ? 'fancy' : 'no-fancy' ) .
217 ' ' . var_export( $this->nickname, true ) . "\n" .
218 "Main text: " . var_export( $this->mainText, true ) . "\n";
219 foreach ( $this->templates as $titleText => $template ) {
220 $finalTitle = $template['finalTitle'];
221 if ( $finalTitle != $titleText ) {
222 $s .= "[[$titleText]] -> [[$finalTitle]]: " . var_export( $template['text'], true ) . "\n";
223 } else {
224 $s .= "[[$titleText]]: " . var_export( $template['text'], true ) . "\n";
227 $s .= "Output: " . var_export( $this->output, true ) . "\n";
229 return $s;
233 class PPFuzzUser extends User {
234 public $ppfz_test, $mDataLoaded;
236 function load() {
237 if ( $this->mDataLoaded ) {
238 return;
240 $this->mDataLoaded = true;
241 $this->loadDefaults( $this->mName );
244 function getOption( $oname, $defaultOverride = null, $ignoreHidden = false ) {
245 if ( $oname === 'fancysig' ) {
246 return $this->ppfz_test->fancySig;
247 } elseif ( $oname === 'nickname' ) {
248 return $this->ppfz_test->nickname;
249 } else {
250 return parent::getOption( $oname, $defaultOverride, $ignoreHidden );
255 ini_set( 'memory_limit', '50M' );
256 if ( isset( $args[0] ) ) {
257 $testText = file_get_contents( $args[0] );
258 if ( !$testText ) {
259 print "File not found\n";
260 exit( 1 );
262 $test = unserialize( $testText );
263 $result = $test->execute();
264 print "Test passed.\n";
265 } else {
266 $tester = new PPFuzzTester;
267 $tester->verbose = isset( $options['verbose'] );
268 $tester->execute();