26use Wikimedia\TestingAccessWrapper;
29require_once __DIR__ .
'/commandLine.inc';
31$wgHooks[
'BeforeParserFetchTemplateAndtitle'][] =
'PPFuzzTester::templateHook';
35 '[[',
']]',
'{{',
'{{',
'}}',
'}}',
'{{{',
'}}}',
36 '<',
'>',
'<nowiki',
'<gallery',
'</nowiki>',
'</gallery>',
'<nOwIkI>',
'</NoWiKi>',
39 '|',
'=',
"\n",
' ',
"\t",
"\x7f",
40 '~~',
'~~~',
'~~~~',
'subst:',
41 'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
42 'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
51 public $entryPoints = [
'fuzzTestSrvus',
'fuzzTestPst',
'fuzzTestPreprocess' ];
60 if ( !file_exists(
'results' ) ) {
63 if ( !is_dir(
'results' ) ) {
64 echo
"Unable to create 'results' directory\n";
67 $overallStart = microtime(
true );
68 $reportInterval = 1000;
69 for ( $i = 1;
true; $i++ ) {
70 $t = -microtime(
true );
73 self::$currentTest->execute();
75 }
catch ( Exception $e ) {
76 $testReport = self::$currentTest->getReport();
77 $exceptionReport = $e instanceof
MWException ? $e->getText() : (string)$e;
78 $hash = md5( $testReport );
79 file_put_contents(
"results/ppft-$hash.in",
serialize( self::$currentTest ) );
80 file_put_contents(
"results/ppft-$hash.fail",
81 "Input:\n$testReport\n\nException report:\n$exceptionReport\n" );
82 print "Test $hash failed\n";
85 $t += microtime(
true );
88 printf(
"Test $passed in %.3f seconds\n",
$t );
89 print self::$currentTest->getReport();
92 $reportMetric = ( microtime(
true ) - $overallStart ) / $i * $reportInterval;
93 if ( $reportMetric > 25 ) {
94 if ( substr( $reportInterval, 0, 1 ) ===
'1' ) {
99 } elseif ( $reportMetric < 4 ) {
100 if ( substr( $reportInterval, 0, 1 ) ===
'1' ) {
101 $reportInterval *= 5;
103 $reportInterval *= 2;
106 if ( $i % $reportInterval == 0 ) {
107 print "$i tests done\n";
117 if ( $max ===
false ) {
120 $length = mt_rand( $this->minLength, $max );
122 for ( $i = 0; $i < $length; $i++ ) {
123 $hairIndex = mt_rand( 0, count( $this->hairs ) - 1 );
124 $s .= $this->hairs[$hairIndex];
130 $s = MediaWikiServices::getInstance()->getContentLanguage()->normalize(
$s );
136 return Title::newFromText( mt_rand( 0, 1000000 ), mt_rand( 0, 10 ) );
146 $count = count( $this->entryPoints );
148 return $this->entryPoints[mt_rand( 0, $count - 1 )];
167 $this->parent = $tester;
168 $this->mainText = $tester->makeInputText();
169 $this->title = $tester->makeTitle();
171 $this->entryPoint = $tester->pickEntryPoint();
173 $this->fancySig = (bool)mt_rand( 0, 1 );
174 $this->templates = [];
182 $titleText =
$title->getPrefixedDBkey();
184 if ( !isset( $this->templates[$titleText] ) ) {
186 if ( count( $this->templates ) >= $this->parent->maxTemplates ) {
190 if ( !mt_rand( 0, 1 ) ) {
192 $finalTitle = $this->parent->makeTitle();
194 if ( !mt_rand( 0, 5 ) ) {
198 $text = $this->parent->makeInputText();
201 $this->templates[$titleText] = [
203 'finalTitle' => $finalTitle ];
206 return $this->templates[$titleText];
213 $wgUser->mName =
'Fuzz';
214 $wgUser->mFrom =
'name';
215 $wgUser->ppfz_test = $this;
217 $options = ParserOptions::newFromUser( $wgUser );
218 $options->setTemplateCallback( [ $this,
'templateHook' ] );
220 $this->output = call_user_func(
221 [ TestingAccessWrapper::newFromObject(
222 MediaWikiServices::getInstance()->getParser()
223 ), $this->entryPoint ],
233 $s =
"Title: " . $this->title->getPrefixedDBkey() .
"\n" .
235 "Entry point: {$this->entryPoint}\n" .
236 "User: " . ( $this->fancySig ?
'fancy' :
'no-fancy' ) .
237 ' ' . var_export( $this->nickname,
true ) .
"\n" .
238 "Main text: " . var_export( $this->mainText,
true ) .
"\n";
239 foreach ( $this->templates as $titleText => $template ) {
240 $finalTitle = $template[
'finalTitle'];
241 if ( $finalTitle != $titleText ) {
242 $s .=
"[[$titleText]] -> [[$finalTitle]]: " . var_export( $template[
'text'],
true ) .
"\n";
244 $s .=
"[[$titleText]]: " . var_export( $template[
'text'],
true ) .
"\n";
247 $s .=
"Output: " . var_export( $this->output,
true ) .
"\n";
256 function load( $flags =
null ) {
257 if ( $this->mDataLoaded ) {
260 $this->mDataLoaded =
true;
264 function getOption( $oname, $defaultOverride =
null, $ignoreHidden =
false ) {
265 if ( $oname ===
'fancysig' ) {
266 return $this->ppfz_test->fancySig;
267 } elseif ( $oname ===
'nickname' ) {
268 return $this->ppfz_test->nickname;
270 return parent::getOption( $oname, $defaultOverride, $ignoreHidden );
275ini_set(
'memory_limit',
'50M' );
276if ( isset(
$args[0] ) ) {
277 $testText = file_get_contents(
$args[0] );
279 print "File not found\n";
283 $result = $test->execute();
284 print "Test passed.\n";
287 $tester->verbose = isset( $options[
'verbose'] );
unserialize( $serialized)
$wgMaxSigChars
Maximum number of Unicode characters in signature.
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
static bool PPFuzzTest $currentTest
makeInputText( $max=false)
load( $flags=null)
Load the user table data for this object from the source given by mFrom.
getOption( $oname, $defaultOverride=null, $ignoreHidden=false)
Get the user's current setting for a given option.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
loadDefaults( $name=false, $actorId=null)
Set cached properties to default.
while(( $__line=Maintenance::readconsole()) !==false) print
$wgHooks['BeforeParserFetchTemplateAndtitle'][]