Merge "Remove 6 unused watchlist messages"
[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 __DIR__ . '/commandLine.inc';
25
26 $wgHooks['BeforeParserFetchTemplateAndtitle'][] = 'PPFuzzTester::templateHook';
27
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',
38
39 // extensions
40 // '<ref>', '</ref>', '<references/>',
41 );
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
49 private static $currentTest = false;
50
51 function execute() {
52 if ( !file_exists( 'results' ) ) {
53 mkdir( 'results' );
54 }
55 if ( !is_dir( 'results' ) ) {
56 echo "Unable to create 'results' directory\n";
57 exit( 1 );
58 }
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';
76 }
77 $t += microtime( true );
78
79 if ( $this->verbose ) {
80 printf( "Test $passed in %.3f seconds\n", $t );
81 print self::$currentTest->getReport();
82 }
83
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;
90 }
91 } elseif ( $reportMetric < 4 ) {
92 if ( substr( $reportInterval, 0, 1 ) === '1' ) {
93 $reportInterval *= 5;
94 } else {
95 $reportInterval *= 2;
96 }
97 }
98 if ( $i % $reportInterval == 0 ) {
99 print "$i tests done\n";
100 /*
101 $testReport = self::$currentTest->getReport();
102 $filename = 'results/ppft-' . md5( $testReport ) . '.pass';
103 file_put_contents( $filename, "Input:\n$testReport\n" );*/
104 }
105 }
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 public $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 /**
159 * @param Title $title
160 */
161 function templateHook( $title ) {
162 $titleText = $title->getPrefixedDBkey();
163
164 if ( !isset( $this->templates[$titleText] ) ) {
165 $finalTitle = $title;
166 if ( count( $this->templates ) >= $this->parent->maxTemplates ) {
167 // Too many templates
168 $text = false;
169 } else {
170 if ( !mt_rand( 0, 1 ) ) {
171 // Redirect
172 $finalTitle = $this->parent->makeTitle();
173 }
174 if ( !mt_rand( 0, 5 ) ) {
175 // Doesn't exist
176 $text = false;
177 } else {
178 $text = $this->parent->makeInputText();
179 }
180 }
181 $this->templates[$titleText] = array(
182 'text' => $text,
183 'finalTitle' => $finalTitle );
184 }
185 return $this->templates[$titleText];
186 }
187
188 function execute() {
189 global $wgParser, $wgUser;
190
191 $wgUser = new PPFuzzUser;
192 $wgUser->mName = 'Fuzz';
193 $wgUser->mFrom = 'name';
194 $wgUser->ppfz_test = $this;
195
196 $options = ParserOptions::newFromUser( $wgUser );
197 $options->setTemplateCallback( array( $this, 'templateHook' ) );
198 $options->setTimestamp( wfTimestampNow() );
199 $this->output = call_user_func(
200 array( $wgParser, $this->entryPoint ),
201 $this->mainText,
202 $this->title,
203 $options
204 );
205
206 return $this->output;
207 }
208
209 function getReport() {
210 $s = "Title: " . $this->title->getPrefixedDBkey() . "\n" .
211 // "Output type: {$this->outputType}\n" .
212 "Entry point: {$this->entryPoint}\n" .
213 "User: " . ( $this->fancySig ? 'fancy' : 'no-fancy' ) .
214 ' ' . var_export( $this->nickname, true ) . "\n" .
215 "Main text: " . var_export( $this->mainText, true ) . "\n";
216 foreach ( $this->templates as $titleText => $template ) {
217 $finalTitle = $template['finalTitle'];
218 if ( $finalTitle != $titleText ) {
219 $s .= "[[$titleText]] -> [[$finalTitle]]: " . var_export( $template['text'], true ) . "\n";
220 } else {
221 $s .= "[[$titleText]]: " . var_export( $template['text'], true ) . "\n";
222 }
223 }
224 $s .= "Output: " . var_export( $this->output, true ) . "\n";
225
226 return $s;
227 }
228 }
229
230 class PPFuzzUser extends User {
231 public $ppfz_test, $mDataLoaded;
232
233 function load() {
234 if ( $this->mDataLoaded ) {
235 return;
236 }
237 $this->mDataLoaded = true;
238 $this->loadDefaults( $this->mName );
239 }
240
241 function getOption( $oname, $defaultOverride = null, $ignoreHidden = false ) {
242 if ( $oname === 'fancysig' ) {
243 return $this->ppfz_test->fancySig;
244 } elseif ( $oname === 'nickname' ) {
245 return $this->ppfz_test->nickname;
246 } else {
247 return parent::getOption( $oname, $defaultOverride, $ignoreHidden );
248 }
249 }
250 }
251
252 ini_set( 'memory_limit', '50M' );
253 if ( isset( $args[0] ) ) {
254 $testText = file_get_contents( $args[0] );
255 if ( !$testText ) {
256 print "File not found\n";
257 exit( 1 );
258 }
259 $test = unserialize( $testText );
260 $result = $test->execute();
261 print "Test passed.\n";
262 } else {
263 $tester = new PPFuzzTester;
264 $tester->verbose = isset( $options['verbose'] );
265 $tester->execute();
266 }