1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
|
<?php
/**
* Although marked as a stub, can work independently.
*
* @group Database
* @group Parser
* @group Stub
*/
class NewParserTest extends MediaWikiTestCase {
static protected $articles = array(); // Array of test articles defined by the tests
/* The dataProvider is run on a different instance than the test, so it must be static
* When running tests from several files, all tests will see all articles.
*/
static protected $backendToUse;
public $keepUploads = false;
public $runDisabled = false;
public $regex = '';
public $showProgress = true;
public $savedInitialGlobals = array();
public $savedWeirdGlobals = array();
public $savedGlobals = array();
public $hooks = array();
public $functionHooks = array();
//Fuzz test
public $maxFuzzTestLength = 300;
public $fuzzSeed = 0;
public $memoryLimit = 50;
protected $file = false;
function setUp() {
global $wgContLang, $wgNamespaceProtection, $wgNamespaceAliases;
global $wgHooks, $IP;
$wgContLang = Language::factory( 'en' );
//Setup CLI arguments
if ( $this->getCliArg( 'regex=' ) ) {
$this->regex = $this->getCliArg( 'regex=' );
} else {
# Matches anything
$this->regex = '';
}
$this->keepUploads = $this->getCliArg( 'keep-uploads' );
$tmpGlobals = array();
$tmpGlobals['wgScript'] = '/index.php';
$tmpGlobals['wgScriptPath'] = '/';
$tmpGlobals['wgArticlePath'] = '/wiki/$1';
$tmpGlobals['wgStyleSheetPath'] = '/skins';
$tmpGlobals['wgStylePath'] = '/skins';
$tmpGlobals['wgThumbnailScriptPath'] = false;
$tmpGlobals['wgLocalFileRepo'] = array(
'class' => 'LocalRepo',
'name' => 'local',
'url' => 'http://example.com/images',
'hashLevels' => 2,
'transformVia404' => false,
'backend' => 'local-backend'
);
$tmpGlobals['wgForeignFileRepos'] = array();
$tmpGlobals['wgEnableParserCache'] = false;
$tmpGlobals['wgHooks'] = $wgHooks;
$tmpGlobals['wgDeferredUpdateList'] = array();
$tmpGlobals['wgMemc'] = wfGetMainCache();
$tmpGlobals['messageMemc'] = wfGetMessageCacheStorage();
$tmpGlobals['parserMemc'] = wfGetParserCacheStorage();
// $tmpGlobals['wgContLang'] = new StubContLang;
$tmpGlobals['wgUser'] = new User;
$context = new RequestContext();
$tmpGlobals['wgLang'] = $context->getLanguage();
$tmpGlobals['wgOut'] = $context->getOutput();
$tmpGlobals['wgParser'] = new StubObject( 'wgParser', $GLOBALS['wgParserConf']['class'], array( $GLOBALS['wgParserConf'] ) );
$tmpGlobals['wgRequest'] = $context->getRequest();
if ( $GLOBALS['wgStyleDirectory'] === false ) {
$tmpGlobals['wgStyleDirectory'] = "$IP/skins";
}
foreach ( $tmpGlobals as $var => $val ) {
if ( array_key_exists( $var, $GLOBALS ) ) {
$this->savedInitialGlobals[$var] = $GLOBALS[$var];
}
$GLOBALS[$var] = $val;
}
$this->savedWeirdGlobals['mw_namespace_protection'] = $wgNamespaceProtection[NS_MEDIAWIKI];
$this->savedWeirdGlobals['image_alias'] = $wgNamespaceAliases['Image'];
$this->savedWeirdGlobals['image_talk_alias'] = $wgNamespaceAliases['Image_talk'];
$wgNamespaceProtection[NS_MEDIAWIKI] = 'editinterface';
$wgNamespaceAliases['Image'] = NS_FILE;
$wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
}
public function tearDown() {
foreach ( $this->savedInitialGlobals as $var => $val ) {
$GLOBALS[$var] = $val;
}
global $wgNamespaceProtection, $wgNamespaceAliases;
$wgNamespaceProtection[NS_MEDIAWIKI] = $this->savedWeirdGlobals['mw_namespace_protection'];
$wgNamespaceAliases['Image'] = $this->savedWeirdGlobals['image_alias'];
$wgNamespaceAliases['Image_talk'] = $this->savedWeirdGlobals['image_talk_alias'];
// Restore backends
RepoGroup::destroySingleton();
FileBackendGroup::destroySingleton();
}
function addDBData() {
$this->tablesUsed[] = 'site_stats';
$this->tablesUsed[] = 'interwiki';
# disabled for performance
#$this->tablesUsed[] = 'image';
# Hack: insert a few Wikipedia in-project interwiki prefixes,
# for testing inter-language links
$this->db->insert( 'interwiki', array(
array( 'iw_prefix' => 'wikipedia',
'iw_url' => 'http://en.wikipedia.org/wiki/$1',
'iw_api' => '',
'iw_wikiid' => '',
'iw_local' => 0 ),
array( 'iw_prefix' => 'meatball',
'iw_url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1',
'iw_api' => '',
'iw_wikiid' => '',
'iw_local' => 0 ),
array( 'iw_prefix' => 'zh',
'iw_url' => 'http://zh.wikipedia.org/wiki/$1',
'iw_api' => '',
'iw_wikiid' => '',
'iw_local' => 1 ),
array( 'iw_prefix' => 'es',
'iw_url' => 'http://es.wikipedia.org/wiki/$1',
'iw_api' => '',
'iw_wikiid' => '',
'iw_local' => 1 ),
array( 'iw_prefix' => 'fr',
'iw_url' => 'http://fr.wikipedia.org/wiki/$1',
'iw_api' => '',
'iw_wikiid' => '',
'iw_local' => 1 ),
array( 'iw_prefix' => 'ru',
'iw_url' => 'http://ru.wikipedia.org/wiki/$1',
'iw_api' => '',
'iw_wikiid' => '',
'iw_local' => 1 ),
/**
* @todo Fixme! Why are we inserting duplicate data here? Shouldn't
* need this IGNORE or shouldn't need the insert at all.
*/
), __METHOD__, array( 'IGNORE' )
);
# Update certain things in site_stats
$this->db->insert( 'site_stats',
array( 'ss_row_id' => 1, 'ss_images' => 2, 'ss_good_articles' => 1 ),
__METHOD__
);
# Reinitialise the LocalisationCache to match the database state
Language::getLocalisationCache()->unloadAll();
# Clear the message cache
MessageCache::singleton()->clear();
$user = User::newFromId( 0 );
LinkCache::singleton()->clear(); # Avoids the odd failure at creating the nullRevision
# Upload DB table entries for files.
# We will upload the actual files later. Note that if anything causes LocalFile::load()
# to be triggered before then, it will break via maybeUpgrade() setting the fileExists
# member to false and storing it in cache.
$image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.jpg' ) );
if ( !$this->db->selectField( 'image', '1', array( 'img_name' => $image->getName() ) ) ) {
$image->recordUpload2(
'', // archive name
'Upload of some lame file',
'Some lame file',
array(
'size' => 12345,
'width' => 1941,
'height' => 220,
'bits' => 24,
'media_type' => MEDIATYPE_BITMAP,
'mime' => 'image/jpeg',
'metadata' => serialize( array() ),
'sha1' => wfBaseConvert( '', 16, 36, 31 ),
'fileExists' => true ),
$this->db->timestamp( '20010115123500' ), $user
);
}
# This image will be blacklisted in [[MediaWiki:Bad image list]]
$image = wfLocalFile( Title::makeTitle( NS_FILE, 'Bad.jpg' ) );
if ( !$this->db->selectField( 'image', '1', array( 'img_name' => $image->getName() ) ) ) {
$image->recordUpload2(
'', // archive name
'zomgnotcensored',
'Borderline image',
array(
'size' => 12345,
'width' => 320,
'height' => 240,
'bits' => 24,
'media_type' => MEDIATYPE_BITMAP,
'mime' => 'image/jpeg',
'metadata' => serialize( array() ),
'sha1' => wfBaseConvert( '', 16, 36, 31 ),
'fileExists' => true ),
$this->db->timestamp( '20010115123500' ), $user
);
}
}
//ParserTest setup/teardown functions
/**
* Set up the global variables for a consistent environment for each test.
* Ideally this should replace the global configuration entirely.
*/
protected function setupGlobals( $opts = '', $config = '' ) {
global $wgFileBackends;
# Find out values for some special options.
$lang =
self::getOptionValue( 'language', $opts, 'en' );
$variant =
self::getOptionValue( 'variant', $opts, false );
$maxtoclevel =
self::getOptionValue( 'wgMaxTocLevel', $opts, 999 );
$linkHolderBatchSize =
self::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 );
$uploadDir = $this->getUploadDir();
if ( $this->getCliArg( 'use-filebackend=' ) ) {
if ( self::$backendToUse ) {
$backend = self::$backendToUse;
} else {
$name = $this->getCliArg( 'use-filebackend=' );
$useConfig = array();
foreach ( $wgFileBackends as $conf ) {
if ( $conf['name'] == $name ) {
$useConfig = $conf;
}
}
$useConfig['name'] = 'local-backend'; // swap name
$class = $conf['class'];
self::$backendToUse = new $class( $useConfig );
$backend = self::$backendToUse;
}
} else {
$backend = new FSFileBackend( array(
'name' => 'local-backend',
'lockManager' => 'nullLockManager',
'containerPaths' => array(
'local-public' => "$uploadDir",
'local-thumb' => "$uploadDir/thumb",
)
) );
}
$settings = array(
'wgServer' => 'http://Britney-Spears',
'wgScript' => '/index.php',
'wgScriptPath' => '/',
'wgArticlePath' => '/wiki/$1',
'wgExtensionAssetsPath' => '/extensions',
'wgActionPaths' => array(),
'wgLocalFileRepo' => array(
'class' => 'LocalRepo',
'name' => 'local',
'url' => 'http://example.com/images',
'hashLevels' => 2,
'transformVia404' => false,
'backend' => $backend
),
'wgEnableUploads' => self::getOptionValue( 'wgEnableUploads', $opts, true ),
'wgStylePath' => '/skins',
'wgStyleSheetPath' => '/skins',
'wgSitename' => 'MediaWiki',
'wgLanguageCode' => $lang,
'wgDBprefix' => $this->db->getType() != 'oracle' ? 'unittest_' : 'ut_',
'wgRawHtml' => isset( $opts['rawhtml'] ),
'wgLang' => null,
'wgContLang' => null,
'wgNamespacesWithSubpages' => array( 0 => isset( $opts['subpage'] ) ),
'wgMaxTocLevel' => $maxtoclevel,
'wgCapitalLinks' => true,
'wgNoFollowLinks' => true,
'wgNoFollowDomainExceptions' => array(),
'wgThumbnailScriptPath' => false,
'wgUseImageResize' => false,
'wgUseTeX' => isset( $opts['math'] ),
'wgMathDirectory' => $uploadDir . '/math',
'wgLocaltimezone' => 'UTC',
'wgAllowExternalImages' => true,
'wgUseTidy' => false,
'wgDefaultLanguageVariant' => $variant,
'wgVariantArticlePath' => false,
'wgGroupPermissions' => array( '*' => array(
'createaccount' => true,
'read' => true,
'edit' => true,
'createpage' => true,
'createtalk' => true,
) ),
'wgNamespaceProtection' => array( NS_MEDIAWIKI => 'editinterface' ),
'wgDefaultExternalStore' => array(),
'wgForeignFileRepos' => array(),
'wgLinkHolderBatchSize' => $linkHolderBatchSize,
'wgExperimentalHtmlIds' => false,
'wgExternalLinkTarget' => false,
'wgAlwaysUseTidy' => false,
'wgHtml5' => true,
'wgCleanupPresentationalAttributes' => true,
'wgWellFormedXml' => true,
'wgAllowMicrodataAttributes' => true,
'wgAdaptiveMessageCache' => true,
'wgUseDatabaseMessages' => true,
);
if ( $config ) {
$configLines = explode( "\n", $config );
foreach ( $configLines as $line ) {
list( $var, $value ) = explode( '=', $line, 2 );
$settings[$var] = eval( "return $value;" ); //???
}
}
$this->savedGlobals = array();
foreach ( $settings as $var => $val ) {
if ( array_key_exists( $var, $GLOBALS ) ) {
$this->savedGlobals[$var] = $GLOBALS[$var];
}
$GLOBALS[$var] = $val;
}
$langObj = Language::factory( $lang );
$GLOBALS['wgContLang'] = $langObj;
$context = new RequestContext();
$GLOBALS['wgLang'] = $context->getLanguage();
$GLOBALS['wgMemc'] = new EmptyBagOStuff;
$GLOBALS['wgOut'] = $context->getOutput();
$GLOBALS['wgUser'] = $context->getUser();
global $wgHooks;
$wgHooks['ParserTestParser'][] = 'ParserTestParserHook::setup';
$wgHooks['ParserGetVariableValueTs'][] = 'ParserTest::getFakeTimestamp';
MagicWord::clearCache();
RepoGroup::destroySingleton();
FileBackendGroup::destroySingleton();
# Create dummy files in storage
$this->setupUploads();
# Publish the articles after we have the final language set
$this->publishTestArticles();
# The entries saved into RepoGroup cache with previous globals will be wrong.
RepoGroup::destroySingleton();
FileBackendGroup::destroySingleton();
MessageCache::singleton()->destroyInstance();
return $context;
}
/**
* Get an FS upload directory (only applies to FSFileBackend)
*
* @return String: the directory
*/
protected function getUploadDir() {
if ( $this->keepUploads ) {
$dir = wfTempDir() . '/mwParser-images';
if ( is_dir( $dir ) ) {
return $dir;
}
} else {
$dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
}
// wfDebug( "Creating upload directory $dir\n" );
if ( file_exists( $dir ) ) {
wfDebug( "Already exists!\n" );
return $dir;
}
return $dir;
}
/**
* Create a dummy uploads directory which will contain a couple
* of files in order to pass existence tests.
*
* @return String: the directory
*/
protected function setupUploads() {
global $IP;
$base = $this->getBaseDir();
$backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
$backend->prepare( array( 'dir' => "$base/local-public/3/3a" ) );
$backend->store( array(
'src' => "$IP/skins/monobook/headbg.jpg", 'dst' => "$base/local-public/3/3a/Foobar.jpg"
) );
$backend->prepare( array( 'dir' => "$base/local-public/0/09" ) );
$backend->store( array(
'src' => "$IP/skins/monobook/headbg.jpg", 'dst' => "$base/local-public/0/09/Bad.jpg"
) );
}
/**
* Restore default values and perform any necessary clean-up
* after each test runs.
*/
protected function teardownGlobals() {
$this->teardownUploads();
foreach ( $this->savedGlobals as $var => $val ) {
$GLOBALS[$var] = $val;
}
RepoGroup::destroySingleton();
LinkCache::singleton()->clear();
}
/**
* Remove the dummy uploads directory
*/
private function teardownUploads() {
if ( $this->keepUploads ) {
return;
}
$base = $this->getBaseDir();
// delete the files first, then the dirs.
self::deleteFiles(
array (
"$base/local-public/3/3a/Foobar.jpg",
"$base/local-thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
"$base/local-thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
"$base/local-thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
"$base/local-thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
"$base/local-public/0/09/Bad.jpg",
"$base/local-thumb/0/09/Bad.jpg",
"$base/local-public/math/f/a/5/fa50b8b616463173474302ca3e63586b.png",
)
);
}
/**
* Delete the specified files, if they exist.
* @param $files Array: full paths to files to delete.
*/
private static function deleteFiles( $files ) {
$backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
foreach ( $files as $file ) {
$backend->delete( array( 'src' => $file ), array( 'force' => 1 ) );
}
foreach ( $files as $file ) {
$tmp = $file;
while ( $tmp = FileBackend::parentStoragePath( $tmp ) ) {
if ( !$backend->clean( array( 'dir' => $tmp ) )->isOK() ) {
break;
}
}
}
}
protected function getBaseDir() {
return 'mwstore://local-backend';
}
public function parserTestProvider() {
if ( $this->file === false ) {
global $wgParserTestFiles;
$this->file = $wgParserTestFiles[0];
}
return new TestFileIterator( $this->file, $this );
}
/**
* Set the file from whose tests will be run by this instance
*/
public function setParserTestFile( $filename ) {
$this->file = $filename;
}
/** @dataProvider parserTestProvider */
public function testParserTest( $desc, $input, $result, $opts, $config ) {
if ( $this->regex != '' && !preg_match( '/' . $this->regex . '/', $desc ) ) {
$this->assertTrue( true ); // XXX: don't flood output with "test made no assertions"
//$this->markTestSkipped( 'Filtered out by the user' );
return;
}
wfDebug( "Running parser test: $desc\n" );
$opts = $this->parseOptions( $opts );
$context = $this->setupGlobals( $opts, $config );
$user = $context->getUser();
$options = ParserOptions::newFromContext( $context );
if ( isset( $opts['title'] ) ) {
$titleText = $opts['title'];
}
else {
$titleText = 'Parser test';
}
$local = isset( $opts['local'] );
$preprocessor = isset( $opts['preprocessor'] ) ? $opts['preprocessor'] : null;
$parser = $this->getParser( $preprocessor );
$title = Title::newFromText( $titleText );
if ( isset( $opts['pst'] ) ) {
$out = $parser->preSaveTransform( $input, $title, $user, $options );
} elseif ( isset( $opts['msg'] ) ) {
$out = $parser->transformMsg( $input, $options, $title );
} elseif ( isset( $opts['section'] ) ) {
$section = $opts['section'];
$out = $parser->getSection( $input, $section );
} elseif ( isset( $opts['replace'] ) ) {
$section = $opts['replace'][0];
$replace = $opts['replace'][1];
$out = $parser->replaceSection( $input, $section, $replace );
} elseif ( isset( $opts['comment'] ) ) {
$out = Linker::formatComment( $input, $title, $local );
} elseif ( isset( $opts['preload'] ) ) {
$out = $parser->getpreloadText( $input, $title, $options );
} else {
$output = $parser->parse( $input, $title, $options, true, true, 1337 );
$out = $output->getText();
if ( isset( $opts['showtitle'] ) ) {
if ( $output->getTitleText() ) {
$title = $output->getTitleText();
}
$out = "$title\n$out";
}
if ( isset( $opts['ill'] ) ) {
$out = $this->tidy( implode( ' ', $output->getLanguageLinks() ) );
} elseif ( isset( $opts['cat'] ) ) {
$outputPage = $context->getOutput();
$outputPage->addCategoryLinks( $output->getCategories() );
$cats = $outputPage->getCategoryLinks();
if ( isset( $cats['normal'] ) ) {
$out = $this->tidy( implode( ' ', $cats['normal'] ) );
} else {
$out = '';
}
}
$parser->mPreprocessor = null;
$result = $this->tidy( $result );
}
$this->teardownGlobals();
$this->assertEquals( $result, $out, $desc );
}
/**
* Run a fuzz test series
* Draw input from a set of test files
*
* @todo @fixme Needs some work to not eat memory until the world explodes
*
* @group ParserFuzz
*/
function testFuzzTests() {
global $wgParserTestFiles;
$files = $wgParserTestFiles;
if( $this->getCliArg( 'file=' ) ) {
$files = array( $this->getCliArg( 'file=' ) );
}
$dict = $this->getFuzzInput( $files );
$dictSize = strlen( $dict );
$logMaxLength = log( $this->maxFuzzTestLength );
ini_set( 'memory_limit', $this->memoryLimit * 1048576 );
$user = new User;
$opts = ParserOptions::newFromUser( $user );
$title = Title::makeTitle( NS_MAIN, 'Parser_test' );
$id = 1;
while ( true ) {
// Generate test input
mt_srand( ++$this->fuzzSeed );
$totalLength = mt_rand( 1, $this->maxFuzzTestLength );
$input = '';
while ( strlen( $input ) < $totalLength ) {
$logHairLength = mt_rand( 0, 1000000 ) / 1000000 * $logMaxLength;
$hairLength = min( intval( exp( $logHairLength ) ), $dictSize );
$offset = mt_rand( 0, $dictSize - $hairLength );
$input .= substr( $dict, $offset, $hairLength );
}
$this->setupGlobals();
$parser = $this->getParser();
// Run the test
try {
$parser->parse( $input, $title, $opts );
$this->assertTrue( true, "Test $id, fuzz seed {$this->fuzzSeed}" );
} catch ( Exception $exception ) {
$input_dump = sprintf( "string(%d) \"%s\"\n", strlen( $input ), $input );
$this->assertTrue( false, "Test $id, fuzz seed {$this->fuzzSeed}. \n\nInput: $input_dump\n\nError: {$exception->getMessage()}\n\nBacktrace: {$exception->getTraceAsString()}" );
}
$this->teardownGlobals();
$parser->__destruct();
if ( $id % 100 == 0 ) {
$usage = intval( memory_get_usage( true ) / $this->memoryLimit / 1048576 * 100 );
//echo "{$this->fuzzSeed}: $numSuccess/$numTotal (mem: $usage%)\n";
if ( $usage > 90 ) {
$ret = "Out of memory:\n";
$memStats = $this->getMemoryBreakdown();
foreach ( $memStats as $name => $usage ) {
$ret .= "$name: $usage\n";
}
throw new MWException( $ret );
}
}
$id++;
}
}
//Various getter functions
/**
* Get an input dictionary from a set of parser test files
*/
function getFuzzInput( $filenames ) {
$dict = '';
foreach ( $filenames as $filename ) {
$contents = file_get_contents( $filename );
preg_match_all( '/!!\s*input\n(.*?)\n!!\s*result/s', $contents, $matches );
foreach ( $matches[1] as $match ) {
$dict .= $match . "\n";
}
}
return $dict;
}
/**
* Get a memory usage breakdown
*/
function getMemoryBreakdown() {
$memStats = array();
foreach ( $GLOBALS as $name => $value ) {
$memStats['$' . $name] = strlen( serialize( $value ) );
}
$classes = get_declared_classes();
foreach ( $classes as $class ) {
$rc = new ReflectionClass( $class );
$props = $rc->getStaticProperties();
$memStats[$class] = strlen( serialize( $props ) );
$methods = $rc->getMethods();
foreach ( $methods as $method ) {
$memStats[$class] += strlen( serialize( $method->getStaticVariables() ) );
}
}
$functions = get_defined_functions();
foreach ( $functions['user'] as $function ) {
$rf = new ReflectionFunction( $function );
$memStats["$function()"] = strlen( serialize( $rf->getStaticVariables() ) );
}
asort( $memStats );
return $memStats;
}
/**
* Get a Parser object
*/
function getParser( $preprocessor = null ) {
global $wgParserConf;
$class = $wgParserConf['class'];
$parser = new $class( array( 'preprocessorClass' => $preprocessor ) + $wgParserConf );
wfRunHooks( 'ParserTestParser', array( &$parser ) );
return $parser;
}
//Various action functions
public function addArticle( $name, $text, $line ) {
self::$articles[$name] = array( $text, $line );
}
public function publishTestArticles() {
if ( empty( self::$articles ) ) {
return;
}
foreach ( self::$articles as $name => $info ) {
list( $text, $line ) = $info;
ParserTest::addArticle( $name, $text, $line, 'ignoreduplicate' );
}
}
/**
* Steal a callback function from the primary parser, save it for
* application to our scary parser. If the hook is not installed,
* abort processing of this file.
*
* @param $name String
* @return Bool true if tag hook is present
*/
public function requireHook( $name ) {
global $wgParser;
$wgParser->firstCallInit( ); // make sure hooks are loaded.
return isset( $wgParser->mTagHooks[$name] );
}
public function requireFunctionHook( $name ) {
global $wgParser;
$wgParser->firstCallInit( ); // make sure hooks are loaded.
return isset( $wgParser->mFunctionHooks[$name] );
}
//Various "cleanup" functions
/**
* Run the "tidy" command on text if the $wgUseTidy
* global is true
*
* @param $text String: the text to tidy
* @return String
*/
protected function tidy( $text ) {
global $wgUseTidy;
if ( $wgUseTidy ) {
$text = MWTidy::tidy( $text );
}
return $text;
}
/**
* Remove last character if it is a newline
*/
public function removeEndingNewline( $s ) {
if ( substr( $s, -1 ) === "\n" ) {
return substr( $s, 0, -1 );
}
else {
return $s;
}
}
//Test options parser functions
protected function parseOptions( $instring ) {
$opts = array();
// foo
// foo=bar
// foo="bar baz"
// foo=[[bar baz]]
// foo=bar,"baz quux"
$regex = '/\b
([\w-]+) # Key
\b
(?:\s*
= # First sub-value
\s*
(
"
[^"]* # Quoted val
"
|
\[\[
[^]]* # Link target
\]\]
|
[\w-]+ # Plain word
)
(?:\s*
, # Sub-vals 1..N
\s*
(
"[^"]*" # Quoted val
|
\[\[[^]]*\]\] # Link target
|
[\w-]+ # Plain word
)
)*
)?
/x';
if ( preg_match_all( $regex, $instring, $matches, PREG_SET_ORDER ) ) {
foreach ( $matches as $bits ) {
array_shift( $bits );
$key = strtolower( array_shift( $bits ) );
if ( count( $bits ) == 0 ) {
$opts[$key] = true;
} elseif ( count( $bits ) == 1 ) {
$opts[$key] = $this->cleanupOption( array_shift( $bits ) );
} else {
// Array!
$opts[$key] = array_map( array( $this, 'cleanupOption' ), $bits );
}
}
}
return $opts;
}
protected function cleanupOption( $opt ) {
if ( substr( $opt, 0, 1 ) == '"' ) {
return substr( $opt, 1, -1 );
}
if ( substr( $opt, 0, 2 ) == '[[' ) {
return substr( $opt, 2, -2 );
}
return $opt;
}
/**
* Use a regex to find out the value of an option
* @param $key String: name of option val to retrieve
* @param $opts Options array to look in
* @param $default Mixed: default value returned if not found
*/
protected static function getOptionValue( $key, $opts, $default ) {
$key = strtolower( $key );
if ( isset( $opts[$key] ) ) {
return $opts[$key];
} else {
return $default;
}
}
}
|