MediaWiki REL1_32
ParserTestRunner.php
Go to the documentation of this file.
1<?php
31use Wikimedia\ScopedCallback;
32use Wikimedia\TestingAccessWrapper;
33
38
45 private static $coreTestFiles = [
46 'parserTests.txt',
47 'extraParserTests.txt',
48 ];
49
53 private $useTemporaryTables = true;
54
58 private $setupDone = [
59 'staticSetup' => false,
60 'perTestSetup' => false,
61 'setupDatabase' => false,
62 'setDatabase' => false,
63 'setupUploads' => false,
64 ];
65
70 private $db;
71
76 private $dbClone;
77
81 private $tidySupport;
82
86 private $tidyDriver = null;
87
91 private $recorder;
92
98 private $uploadDir = null;
99
105
110 private $regex;
111
118
124
129 private $runParsoid;
130
136
142
147 public function __construct( TestRecorder $recorder, $options = [] ) {
148 $this->recorder = $recorder;
149
150 if ( isset( $options['norm'] ) ) {
151 foreach ( $options['norm'] as $func ) {
152 if ( in_array( $func, [ 'removeTbody', 'trimWhitespace' ] ) ) {
153 $this->normalizationFunctions[] = $func;
154 } else {
155 $this->recorder->warning(
156 "Warning: unknown normalization option \"$func\"\n" );
157 }
158 }
159 }
160
161 if ( isset( $options['regex'] ) && $options['regex'] !== false ) {
162 $this->regex = $options['regex'];
163 } else {
164 # Matches anything
165 $this->regex = '//';
166 }
167
168 $this->keepUploads = !empty( $options['keep-uploads'] );
169
170 $this->fileBackendName = $options['file-backend'] ?? false;
171
172 $this->runDisabled = !empty( $options['run-disabled'] );
173 $this->runParsoid = !empty( $options['run-parsoid'] );
174
175 $this->disableSaveParse = !empty( $options['disable-save-parse'] );
176
177 $this->tidySupport = new TidySupport( !empty( $options['use-tidy-config'] ) );
178 if ( !$this->tidySupport->isEnabled() ) {
179 $this->recorder->warning(
180 "Warning: tidy is not installed, skipping some tests\n" );
181 }
182
183 if ( isset( $options['upload-dir'] ) ) {
184 $this->uploadDir = $options['upload-dir'];
185 }
186 }
187
193 public static function getParserTestFiles() {
194 global $wgParserTestFiles;
195
196 // Add core test files
197 $files = array_map( function ( $item ) {
198 return __DIR__ . "/$item";
200
201 // Plus legacy global files
202 $files = array_merge( $files, $wgParserTestFiles );
203
204 // Auto-discover extension parser tests
205 $registry = ExtensionRegistry::getInstance();
206 foreach ( $registry->getAllThings() as $info ) {
207 $dir = dirname( $info['path'] ) . '/tests/parser';
208 if ( !file_exists( $dir ) ) {
209 continue;
210 }
211 $counter = 1;
212 $dirIterator = new RecursiveIteratorIterator(
213 new RecursiveDirectoryIterator( $dir )
214 );
215 foreach ( $dirIterator as $fileInfo ) {
217 if ( substr( $fileInfo->getFilename(), -4 ) === '.txt' ) {
218 $name = $info['name'] . $counter;
219 while ( isset( $files[$name] ) ) {
220 $name = $info['name'] . '_' . $counter++;
221 }
222 $files[$name] = $fileInfo->getPathname();
223 }
224 }
225 }
226
227 return array_unique( $files );
228 }
229
230 public function getRecorder() {
231 return $this->recorder;
232 }
233
253 public function staticSetup( $nextTeardown = null ) {
254 // A note on coding style:
255
256 // The general idea here is to keep setup code together with
257 // corresponding teardown code, in a fine-grained manner. We have two
258 // arrays: $setup and $teardown. The code snippets in the $setup array
259 // are executed at the end of the method, before it returns, and the
260 // code snippets in the $teardown array are executed in reverse order
261 // when the Wikimedia\ScopedCallback object is consumed.
262
263 // Because it is a common operation to save, set and restore global
264 // variables, we have an additional convention: when the array key of
265 // $setup is a string, the string is taken to be the name of the global
266 // variable, and the element value is taken to be the desired new value.
267
268 // It's acceptable to just do the setup immediately, instead of adding
269 // a closure to $setup, except when the setup action depends on global
270 // variable initialisation being done first. In this case, you have to
271 // append a closure to $setup after the global variable is appended.
272
273 // When you add to setup functions in this class, please keep associated
274 // setup and teardown actions together in the source code, and please
275 // add comments explaining why the setup action is necessary.
276
277 $setup = [];
278 $teardown = [];
279
280 $teardown[] = $this->markSetupDone( 'staticSetup' );
281
282 // Some settings which influence HTML output
283 $setup['wgSitename'] = 'MediaWiki';
284 $setup['wgServer'] = 'http://example.org';
285 $setup['wgServerName'] = 'example.org';
286 $setup['wgScriptPath'] = '';
287 $setup['wgScript'] = '/index.php';
288 $setup['wgResourceBasePath'] = '';
289 $setup['wgStylePath'] = '/skins';
290 $setup['wgExtensionAssetsPath'] = '/extensions';
291 $setup['wgArticlePath'] = '/wiki/$1';
292 $setup['wgActionPaths'] = [];
293 $setup['wgVariantArticlePath'] = false;
294 $setup['wgUploadNavigationUrl'] = false;
295 $setup['wgCapitalLinks'] = true;
296 $setup['wgNoFollowLinks'] = true;
297 $setup['wgNoFollowDomainExceptions'] = [ 'no-nofollow.org' ];
298 $setup['wgExternalLinkTarget'] = false;
299 $setup['wgLocaltimezone'] = 'UTC';
300 $setup['wgHtml5'] = true;
301 $setup['wgDisableLangConversion'] = false;
302 $setup['wgDisableTitleConversion'] = false;
303
304 // "extra language links"
305 // see https://gerrit.wikimedia.org/r/111390
306 $setup['wgExtraInterlanguageLinkPrefixes'] = [ 'mul' ];
307
308 // All FileRepo changes should be done here by injecting services,
309 // there should be no need to change global variables.
311 $teardown[] = function () {
313 };
314
315 // Set up null lock managers
316 $setup['wgLockManagers'] = [ [
317 'name' => 'fsLockManager',
318 'class' => NullLockManager::class,
319 ], [
320 'name' => 'nullLockManager',
321 'class' => NullLockManager::class,
322 ] ];
323 $reset = function () {
325 };
326 $setup[] = $reset;
327 $teardown[] = $reset;
328
329 // This allows article insertion into the prefixed DB
330 $setup['wgDefaultExternalStore'] = false;
331
332 // This might slightly reduce memory usage
333 $setup['wgAdaptiveMessageCache'] = true;
334
335 // This is essential and overrides disabling of database messages in TestSetup
336 $setup['wgUseDatabaseMessages'] = true;
337 $reset = function () {
338 MessageCache::destroyInstance();
339 };
340 $setup[] = $reset;
341 $teardown[] = $reset;
342
343 // It's not necessary to actually convert any files
344 $setup['wgSVGConverter'] = 'null';
345 $setup['wgSVGConverters'] = [ 'null' => 'echo "1">$output' ];
346
347 // Fake constant timestamp
348 Hooks::register( 'ParserGetVariableValueTs', function ( &$parser, &$ts ) {
349 $ts = $this->getFakeTimestamp();
350 return true;
351 } );
352 $teardown[] = function () {
353 Hooks::clear( 'ParserGetVariableValueTs' );
354 };
355
356 $this->appendNamespaceSetup( $setup, $teardown );
357
358 // Set up interwikis and append teardown function
359 $teardown[] = $this->setupInterwikis();
360
361 // This affects title normalization in links. It invalidates
362 // MediaWikiTitleCodec objects.
363 $setup['wgLocalInterwikis'] = [ 'local', 'mi' ];
364 $reset = function () {
365 $this->resetTitleServices();
366 };
367 $setup[] = $reset;
368 $teardown[] = $reset;
369
370 // Set up a mock MediaHandlerFactory
371 MediaWikiServices::getInstance()->disableService( 'MediaHandlerFactory' );
372 MediaWikiServices::getInstance()->redefineService(
373 'MediaHandlerFactory',
374 function ( MediaWikiServices $services ) {
375 $handlers = $services->getMainConfig()->get( 'ParserTestMediaHandlers' );
376 return new MediaHandlerFactory( $handlers );
377 }
378 );
379 $teardown[] = function () {
380 MediaWikiServices::getInstance()->resetServiceForTesting( 'MediaHandlerFactory' );
381 };
382
383 // SqlBagOStuff broke when using temporary tables on r40209 (T17892).
384 // It seems to have been fixed since (r55079?), but regressed at some point before r85701.
385 // This works around it for now...
386 global $wgObjectCaches;
387 $setup['wgObjectCaches'] = [ CACHE_DB => $wgObjectCaches['hash'] ] + $wgObjectCaches;
388 if ( isset( ObjectCache::$instances[CACHE_DB] ) ) {
389 $savedCache = ObjectCache::$instances[CACHE_DB];
390 ObjectCache::$instances[CACHE_DB] = new HashBagOStuff;
391 $teardown[] = function () use ( $savedCache ) {
392 ObjectCache::$instances[CACHE_DB] = $savedCache;
393 };
394 }
395
396 $teardown[] = $this->executeSetupSnippets( $setup );
397
398 // Schedule teardown snippets in reverse order
399 return $this->createTeardownObject( $teardown, $nextTeardown );
400 }
401
402 private function appendNamespaceSetup( &$setup, &$teardown ) {
403 // Add a namespace shadowing a interwiki link, to test
404 // proper precedence when resolving links. (T53680)
405 $setup['wgExtraNamespaces'] = [
406 100 => 'MemoryAlpha',
407 101 => 'MemoryAlpha_talk'
408 ];
409 // Changing wgExtraNamespaces invalidates caches in MWNamespace and
410 // any live Language object, both on setup and teardown
411 $reset = function () {
412 MWNamespace::clearCaches();
413 MediaWikiServices::getInstance()->getContentLanguage()->resetNamespaces();
414 };
415 $setup[] = $reset;
416 $teardown[] = $reset;
417 }
418
423 protected function createRepoGroup() {
424 if ( $this->uploadDir ) {
425 if ( $this->fileBackendName ) {
426 throw new MWException( 'You cannot specify both use-filebackend and upload-dir' );
427 }
428 $backend = new FSFileBackend( [
429 'name' => 'local-backend',
430 'wikiId' => wfWikiID(),
431 'basePath' => $this->uploadDir,
432 'tmpDirectory' => wfTempDir()
433 ] );
434 } elseif ( $this->fileBackendName ) {
435 global $wgFileBackends;
437 $useConfig = false;
438 foreach ( $wgFileBackends as $conf ) {
439 if ( $conf['name'] === $name ) {
440 $useConfig = $conf;
441 }
442 }
443 if ( $useConfig === false ) {
444 throw new MWException( "Unable to find file backend \"$name\"" );
445 }
446 $useConfig['name'] = 'local-backend'; // swap name
447 unset( $useConfig['lockManager'] );
448 unset( $useConfig['fileJournal'] );
449 $class = $useConfig['class'];
450 $backend = new $class( $useConfig );
451 } else {
452 # Replace with a mock. We do not care about generating real
453 # files on the filesystem, just need to expose the file
454 # informations.
455 $backend = new MockFileBackend( [
456 'name' => 'local-backend',
457 'wikiId' => wfWikiID()
458 ] );
459 }
460
461 return new RepoGroup(
462 [
463 'class' => MockLocalRepo::class,
464 'name' => 'local',
465 'url' => 'http://example.com/images',
466 'hashLevels' => 2,
467 'transformVia404' => false,
468 'backend' => $backend
469 ],
470 []
471 );
472 }
473
487 protected function executeSetupSnippets( $setup ) {
488 $saved = [];
489 foreach ( $setup as $name => $value ) {
490 if ( is_int( $name ) ) {
491 $value();
492 } else {
493 $saved[$name] = $GLOBALS[$name] ?? null;
495 }
496 }
497 return function () use ( $saved ) {
498 $this->executeSetupSnippets( $saved );
499 };
500 }
501
514 protected function createTeardownObject( $teardown, $nextTeardown = null ) {
515 return new ScopedCallback( function () use ( $teardown, $nextTeardown ) {
516 // Schedule teardown snippets in reverse order
517 $teardown = array_reverse( $teardown );
518
519 $this->executeSetupSnippets( $teardown );
520 if ( $nextTeardown ) {
521 ScopedCallback::consume( $nextTeardown );
522 }
523 } );
524 }
525
533 protected function markSetupDone( $funcName ) {
534 if ( $this->setupDone[$funcName] ) {
535 throw new MWException( "$funcName is already done" );
536 }
537 $this->setupDone[$funcName] = true;
538 return function () use ( $funcName ) {
539 $this->setupDone[$funcName] = false;
540 };
541 }
542
549 protected function checkSetupDone( $funcName, $funcName2 = null ) {
550 if ( !$this->setupDone[$funcName]
551 && ( $funcName === null || !$this->setupDone[$funcName2] )
552 ) {
553 throw new MWException( "$funcName must be called before calling " .
554 wfGetCaller() );
555 }
556 }
557
564 public function isSetupDone( $funcName ) {
565 return $this->setupDone[$funcName] ?? false;
566 }
567
579 private function setupInterwikis() {
580 # Hack: insert a few Wikipedia in-project interwiki prefixes,
581 # for testing inter-language links
582 Hooks::register( 'InterwikiLoadPrefix', function ( $prefix, &$iwData ) {
583 static $testInterwikis = [
584 'local' => [
585 'iw_url' => 'http://doesnt.matter.org/$1',
586 'iw_api' => '',
587 'iw_wikiid' => '',
588 'iw_local' => 0 ],
589 'wikipedia' => [
590 'iw_url' => 'http://en.wikipedia.org/wiki/$1',
591 'iw_api' => '',
592 'iw_wikiid' => '',
593 'iw_local' => 0 ],
594 'meatball' => [
595 'iw_url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1',
596 'iw_api' => '',
597 'iw_wikiid' => '',
598 'iw_local' => 0 ],
599 'memoryalpha' => [
600 'iw_url' => 'http://www.memory-alpha.org/en/index.php/$1',
601 'iw_api' => '',
602 'iw_wikiid' => '',
603 'iw_local' => 0 ],
604 'zh' => [
605 'iw_url' => 'http://zh.wikipedia.org/wiki/$1',
606 'iw_api' => '',
607 'iw_wikiid' => '',
608 'iw_local' => 1 ],
609 'es' => [
610 'iw_url' => 'http://es.wikipedia.org/wiki/$1',
611 'iw_api' => '',
612 'iw_wikiid' => '',
613 'iw_local' => 1 ],
614 'fr' => [
615 'iw_url' => 'http://fr.wikipedia.org/wiki/$1',
616 'iw_api' => '',
617 'iw_wikiid' => '',
618 'iw_local' => 1 ],
619 'ru' => [
620 'iw_url' => 'http://ru.wikipedia.org/wiki/$1',
621 'iw_api' => '',
622 'iw_wikiid' => '',
623 'iw_local' => 1 ],
624 'mi' => [
625 'iw_url' => 'http://mi.wikipedia.org/wiki/$1',
626 'iw_api' => '',
627 'iw_wikiid' => '',
628 'iw_local' => 1 ],
629 'mul' => [
630 'iw_url' => 'http://wikisource.org/wiki/$1',
631 'iw_api' => '',
632 'iw_wikiid' => '',
633 'iw_local' => 1 ],
634 ];
635 if ( array_key_exists( $prefix, $testInterwikis ) ) {
636 $iwData = $testInterwikis[$prefix];
637 }
638
639 // We only want to rely on the above fixtures
640 return false;
641 } );// hooks::register
642
643 // Reset the service in case any other tests already cached some prefixes.
644 MediaWikiServices::getInstance()->resetServiceForTesting( 'InterwikiLookup' );
645
646 return function () {
647 // Tear down
648 Hooks::clear( 'InterwikiLoadPrefix' );
649 MediaWikiServices::getInstance()->resetServiceForTesting( 'InterwikiLookup' );
650 };
651 }
652
657 private function resetTitleServices() {
658 $services = MediaWikiServices::getInstance();
659 $services->resetServiceForTesting( 'TitleFormatter' );
660 $services->resetServiceForTesting( 'TitleParser' );
661 $services->resetServiceForTesting( '_MediaWikiTitleCodec' );
662 $services->resetServiceForTesting( 'LinkRenderer' );
663 $services->resetServiceForTesting( 'LinkRendererFactory' );
664 }
665
671 public static function chomp( $s ) {
672 if ( substr( $s, -1 ) === "\n" ) {
673 return substr( $s, 0, -1 );
674 } else {
675 return $s;
676 }
677 }
678
692 public function runTestsFromFiles( $filenames ) {
693 $ok = false;
694
695 $teardownGuard = $this->staticSetup();
696 $teardownGuard = $this->setupDatabase( $teardownGuard );
697 $teardownGuard = $this->setupUploads( $teardownGuard );
698
699 $this->recorder->start();
700 try {
701 $ok = true;
702
703 foreach ( $filenames as $filename ) {
704 $testFileInfo = TestFileReader::read( $filename, [
705 'runDisabled' => $this->runDisabled,
706 'runParsoid' => $this->runParsoid,
707 'regex' => $this->regex ] );
708
709 // Don't start the suite if there are no enabled tests in the file
710 if ( !$testFileInfo['tests'] ) {
711 continue;
712 }
713
714 $this->recorder->startSuite( $filename );
715 $ok = $this->runTests( $testFileInfo ) && $ok;
716 $this->recorder->endSuite( $filename );
717 }
718
719 $this->recorder->report();
720 } catch ( DBError $e ) {
721 $this->recorder->warning( $e->getMessage() );
722 }
723 $this->recorder->end();
724
725 ScopedCallback::consume( $teardownGuard );
726
727 return $ok;
728 }
729
736 public function meetsRequirements( $requirements ) {
737 foreach ( $requirements as $requirement ) {
738 switch ( $requirement['type'] ) {
739 case 'hook':
740 $ok = $this->requireHook( $requirement['name'] );
741 break;
742 case 'functionHook':
743 $ok = $this->requireFunctionHook( $requirement['name'] );
744 break;
745 case 'transparentHook':
746 $ok = $this->requireTransparentHook( $requirement['name'] );
747 break;
748 }
749 if ( !$ok ) {
750 return false;
751 }
752 }
753 return true;
754 }
755
763 public function runTests( $testFileInfo ) {
764 $ok = true;
765
766 $this->checkSetupDone( 'staticSetup' );
767
768 // Don't add articles from the file if there are no enabled tests from the file
769 if ( !$testFileInfo['tests'] ) {
770 return true;
771 }
772
773 // If any requirements are not met, mark all tests from the file as skipped
774 if ( !$this->meetsRequirements( $testFileInfo['requirements'] ) ) {
775 foreach ( $testFileInfo['tests'] as $test ) {
776 $this->recorder->startTest( $test );
777 $this->recorder->skipped( $test, 'required extension not enabled' );
778 }
779 return true;
780 }
781
782 // Add articles
783 $this->addArticles( $testFileInfo['articles'] );
784
785 // Run tests
786 foreach ( $testFileInfo['tests'] as $test ) {
787 $this->recorder->startTest( $test );
788 $result =
789 $this->runTest( $test );
790 if ( $result !== false ) {
791 $ok = $ok && $result->isSuccess();
792 $this->recorder->record( $test, $result );
793 }
794 }
795
796 return $ok;
797 }
798
805 function getParser( $preprocessor = null ) {
806 global $wgParserConf;
807
808 $class = $wgParserConf['class'];
809 $parser = new $class( [ 'preprocessorClass' => $preprocessor ] + $wgParserConf );
811
812 return $parser;
813 }
814
832 public function runTest( $test ) {
833 wfDebug( __METHOD__ . ": running {$test['desc']}" );
834 $opts = $this->parseOptions( $test['options'] );
835 $teardownGuard = $this->perTestSetup( $test );
836
837 $context = RequestContext::getMain();
838 $user = $context->getUser();
839 $options = ParserOptions::newFromContext( $context );
840 $options->setTimestamp( $this->getFakeTimestamp() );
841
842 if ( isset( $opts['tidy'] ) ) {
843 if ( !$this->tidySupport->isEnabled() ) {
844 $this->recorder->skipped( $test, 'tidy extension is not installed' );
845 return false;
846 } else {
847 $options->setTidy( true );
848 }
849 }
850
851 if ( isset( $opts['title'] ) ) {
852 $titleText = $opts['title'];
853 } else {
854 $titleText = 'Parser test';
855 }
856
857 if ( isset( $opts['maxincludesize'] ) ) {
858 $options->setMaxIncludeSize( $opts['maxincludesize'] );
859 }
860 if ( isset( $opts['maxtemplatedepth'] ) ) {
861 $options->setMaxTemplateDepth( $opts['maxtemplatedepth'] );
862 }
863
864 $local = isset( $opts['local'] );
865 $preprocessor = $opts['preprocessor'] ?? null;
866 $parser = $this->getParser( $preprocessor );
867 $title = Title::newFromText( $titleText );
868
869 if ( isset( $opts['styletag'] ) ) {
870 // For testing the behavior of <style> (including those deduplicated
871 // into <link> tags), add tag hooks to allow them to be generated.
872 $parser->setHook( 'style', function ( $content, $attributes, $parser ) {
873 $marker = Parser::MARKER_PREFIX . '-style-' . md5( $content ) . Parser::MARKER_SUFFIX;
874 $parser->mStripState->addNoWiki( $marker, $content );
875 return Html::inlineStyle( $marker, 'all', $attributes );
876 } );
877 $parser->setHook( 'link', function ( $content, $attributes, $parser ) {
878 return Html::element( 'link', $attributes );
879 } );
880 }
881
882 if ( isset( $opts['pst'] ) ) {
883 $out = $parser->preSaveTransform( $test['input'], $title, $user, $options );
884 $output = $parser->getOutput();
885 } elseif ( isset( $opts['msg'] ) ) {
886 $out = $parser->transformMsg( $test['input'], $options, $title );
887 } elseif ( isset( $opts['section'] ) ) {
888 $section = $opts['section'];
889 $out = $parser->getSection( $test['input'], $section );
890 } elseif ( isset( $opts['replace'] ) ) {
891 $section = $opts['replace'][0];
892 $replace = $opts['replace'][1];
893 $out = $parser->replaceSection( $test['input'], $section, $replace );
894 } elseif ( isset( $opts['comment'] ) ) {
895 $out = Linker::formatComment( $test['input'], $title, $local );
896 } elseif ( isset( $opts['preload'] ) ) {
897 $out = $parser->getPreloadText( $test['input'], $title, $options );
898 } else {
899 $output = $parser->parse( $test['input'], $title, $options, true, true, 1337 );
900 $out = $output->getText( [
901 'allowTOC' => !isset( $opts['notoc'] ),
902 'unwrap' => !isset( $opts['wrap'] ),
903 ] );
904 if ( isset( $opts['tidy'] ) ) {
905 $out = preg_replace( '/\s+$/', '', $out );
906 }
907
908 if ( isset( $opts['showtitle'] ) ) {
909 if ( $output->getTitleText() ) {
910 $title = $output->getTitleText();
911 }
912
913 $out = "$title\n$out";
914 }
915
916 if ( isset( $opts['showindicators'] ) ) {
917 $indicators = '';
918 foreach ( $output->getIndicators() as $id => $content ) {
919 $indicators .= "$id=$content\n";
920 }
921 $out = $indicators . $out;
922 }
923
924 if ( isset( $opts['ill'] ) ) {
925 $out = implode( ' ', $output->getLanguageLinks() );
926 } elseif ( isset( $opts['cat'] ) ) {
927 $out = '';
928 foreach ( $output->getCategories() as $name => $sortkey ) {
929 if ( $out !== '' ) {
930 $out .= "\n";
931 }
932 $out .= "cat=$name sort=$sortkey";
933 }
934 }
935 }
936
937 if ( isset( $output ) && isset( $opts['showflags'] ) ) {
938 $actualFlags = array_keys( TestingAccessWrapper::newFromObject( $output )->mFlags );
939 sort( $actualFlags );
940 $out .= "\nflags=" . implode( ', ', $actualFlags );
941 }
942
943 ScopedCallback::consume( $teardownGuard );
944
945 $expected = $test['result'];
946 if ( count( $this->normalizationFunctions ) ) {
948 $test['expected'], $this->normalizationFunctions );
949 $out = ParserTestResultNormalizer::normalize( $out, $this->normalizationFunctions );
950 }
951
952 $testResult = new ParserTestResult( $test, $expected, $out );
953 return $testResult;
954 }
955
963 private static function getOptionValue( $key, $opts, $default ) {
964 $key = strtolower( $key );
965
966 if ( isset( $opts[$key] ) ) {
967 return $opts[$key];
968 } else {
969 return $default;
970 }
971 }
972
980 private function parseOptions( $instring ) {
981 $opts = [];
982 // foo
983 // foo=bar
984 // foo="bar baz"
985 // foo=[[bar baz]]
986 // foo=bar,"baz quux"
987 // foo={...json...}
988 $defs = '(?(DEFINE)
989 (?<qstr> # Quoted string
990 "
991 (?:[^\\\\"] | \\\\.)*
992 "
993 )
994 (?<json>
995 \{ # Open bracket
996 (?:
997 [^"{}] | # Not a quoted string or object, or
998 (?&qstr) | # A quoted string, or
999 (?&json) # A json object (recursively)
1000 )*
1001 \} # Close bracket
1002 )
1003 (?<value>
1004 (?:
1005 (?&qstr) # Quoted val
1006 |
1007 \[\[
1008 [^]]* # Link target
1009 \]\]
1010 |
1011 [\w-]+ # Plain word
1012 |
1013 (?&json) # JSON object
1014 )
1015 )
1016 )';
1017 $regex = '/' . $defs . '\b
1018 (?<k>[\w-]+) # Key
1019 \b
1020 (?:\s*
1021 = # First sub-value
1022 \s*
1023 (?<v>
1024 (?&value)
1025 (?:\s*
1026 , # Sub-vals 1..N
1027 \s*
1028 (?&value)
1029 )*
1030 )
1031 )?
1032 /x';
1033 $valueregex = '/' . $defs . '(?&value)/x';
1034
1035 if ( preg_match_all( $regex, $instring, $matches, PREG_SET_ORDER ) ) {
1036 foreach ( $matches as $bits ) {
1037 $key = strtolower( $bits['k'] );
1038 if ( !isset( $bits['v'] ) ) {
1039 $opts[$key] = true;
1040 } else {
1041 preg_match_all( $valueregex, $bits['v'], $vmatches );
1042 $opts[$key] = array_map( [ $this, 'cleanupOption' ], $vmatches[0] );
1043 if ( count( $opts[$key] ) == 1 ) {
1044 $opts[$key] = $opts[$key][0];
1045 }
1046 }
1047 }
1048 }
1049 return $opts;
1050 }
1051
1052 private function cleanupOption( $opt ) {
1053 if ( substr( $opt, 0, 1 ) == '"' ) {
1054 return stripcslashes( substr( $opt, 1, -1 ) );
1055 }
1056
1057 if ( substr( $opt, 0, 2 ) == '[[' ) {
1058 return substr( $opt, 2, -2 );
1059 }
1060
1061 if ( substr( $opt, 0, 1 ) == '{' ) {
1062 return FormatJson::decode( $opt, true );
1063 }
1064 return $opt;
1065 }
1066
1076 public function perTestSetup( $test, $nextTeardown = null ) {
1077 $teardown = [];
1078
1079 $this->checkSetupDone( 'setupDatabase', 'setDatabase' );
1080 $teardown[] = $this->markSetupDone( 'perTestSetup' );
1081
1082 $opts = $this->parseOptions( $test['options'] );
1083 $config = $test['config'];
1084
1085 // Find out values for some special options.
1086 $langCode =
1087 self::getOptionValue( 'language', $opts, 'en' );
1088 $variant =
1089 self::getOptionValue( 'variant', $opts, false );
1090 $maxtoclevel =
1091 self::getOptionValue( 'wgMaxTocLevel', $opts, 999 );
1092 $linkHolderBatchSize =
1093 self::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 );
1094
1095 // Default to fallback skin, but allow it to be overridden
1096 $skin = self::getOptionValue( 'skin', $opts, 'fallback' );
1097
1098 $setup = [
1099 'wgEnableUploads' => self::getOptionValue( 'wgEnableUploads', $opts, true ),
1100 'wgLanguageCode' => $langCode,
1101 'wgRawHtml' => self::getOptionValue( 'wgRawHtml', $opts, false ),
1102 'wgNamespacesWithSubpages' => array_fill_keys(
1103 MWNamespace::getValidNamespaces(), isset( $opts['subpage'] )
1104 ),
1105 'wgMaxTocLevel' => $maxtoclevel,
1106 'wgAllowExternalImages' => self::getOptionValue( 'wgAllowExternalImages', $opts, true ),
1107 'wgThumbLimits' => [ self::getOptionValue( 'thumbsize', $opts, 180 ) ],
1108 'wgDefaultLanguageVariant' => $variant,
1109 'wgLinkHolderBatchSize' => $linkHolderBatchSize,
1110 // Set as a JSON object like:
1111 // wgEnableMagicLinks={"ISBN":false, "PMID":false, "RFC":false}
1112 'wgEnableMagicLinks' => self::getOptionValue( 'wgEnableMagicLinks', $opts, [] )
1113 + [ 'ISBN' => true, 'PMID' => true, 'RFC' => true ],
1114 // Test with legacy encoding by default until HTML5 is very stable and default
1115 'wgFragmentMode' => [ 'legacy' ],
1116 ];
1117
1118 $nonIncludable = self::getOptionValue( 'wgNonincludableNamespaces', $opts, false );
1119 if ( $nonIncludable !== false ) {
1120 $setup['wgNonincludableNamespaces'] = [ $nonIncludable ];
1121 }
1122
1123 if ( $config ) {
1124 $configLines = explode( "\n", $config );
1125
1126 foreach ( $configLines as $line ) {
1127 list( $var, $value ) = explode( '=', $line, 2 );
1128 $setup[$var] = eval( "return $value;" );
1129 }
1130 }
1131
1133 Hooks::run( 'ParserTestGlobals', [ &$setup ] );
1134
1135 // Create tidy driver
1136 if ( isset( $opts['tidy'] ) ) {
1137 // Cache a driver instance
1138 if ( $this->tidyDriver === null ) {
1139 $this->tidyDriver = MWTidy::factory( $this->tidySupport->getConfig() );
1140 }
1141 $tidy = $this->tidyDriver;
1142 } else {
1143 $tidy = false;
1144 }
1145 MWTidy::setInstance( $tidy );
1146 $teardown[] = function () {
1148 };
1149
1150 // Set content language. This invalidates the magic word cache and title services
1151 $lang = Language::factory( $langCode );
1152 $lang->resetNamespaces();
1153 $setup['wgContLang'] = $lang;
1154 $setup[] = function () use ( $lang ) {
1155 MediaWikiServices::getInstance()->disableService( 'ContentLanguage' );
1156 MediaWikiServices::getInstance()->redefineService(
1157 'ContentLanguage',
1158 function () use ( $lang ) {
1159 return $lang;
1160 }
1161 );
1162 };
1163 $teardown[] = function () {
1164 MediaWikiServices::getInstance()->resetServiceForTesting( 'ContentLanguage' );
1165 };
1166 $reset = function () {
1167 MediaWikiServices::getInstance()->resetServiceForTesting( 'MagicWordFactory' );
1168 $this->resetTitleServices();
1169 };
1170 $setup[] = $reset;
1171 $teardown[] = $reset;
1172
1173 // Make a user object with the same language
1174 $user = new User;
1175 $user->setOption( 'language', $langCode );
1176 $setup['wgLang'] = $lang;
1177
1178 // We (re)set $wgThumbLimits to a single-element array above.
1179 $user->setOption( 'thumbsize', 0 );
1180
1181 $setup['wgUser'] = $user;
1182
1183 // And put both user and language into the context
1184 $context = RequestContext::getMain();
1185 $context->setUser( $user );
1186 $context->setLanguage( $lang );
1187 // And the skin!
1188 $oldSkin = $context->getSkin();
1189 $skinFactory = MediaWikiServices::getInstance()->getSkinFactory();
1190 $context->setSkin( $skinFactory->makeSkin( $skin ) );
1191 $context->setOutput( new OutputPage( $context ) );
1192 $setup['wgOut'] = $context->getOutput();
1193 $teardown[] = function () use ( $context, $oldSkin ) {
1194 // Clear language conversion tables
1195 $wrapper = TestingAccessWrapper::newFromObject(
1196 $context->getLanguage()->getConverter()
1197 );
1198 $wrapper->reloadTables();
1199 // Reset context to the restored globals
1200 $context->setUser( $GLOBALS['wgUser'] );
1201 $context->setLanguage( $GLOBALS['wgContLang'] );
1202 $context->setSkin( $oldSkin );
1203 $context->setOutput( $GLOBALS['wgOut'] );
1204 };
1205
1206 $teardown[] = $this->executeSetupSnippets( $setup );
1207
1208 return $this->createTeardownObject( $teardown, $nextTeardown );
1209 }
1210
1216 private function listTables() {
1218
1219 $tables = [ 'user', 'user_properties', 'user_former_groups', 'page', 'page_restrictions',
1220 'protected_titles', 'revision', 'ip_changes', 'text', 'pagelinks', 'imagelinks',
1221 'categorylinks', 'templatelinks', 'externallinks', 'langlinks', 'iwlinks',
1222 'site_stats', 'ipblocks', 'image', 'oldimage',
1223 'recentchanges', 'watchlist', 'interwiki', 'logging', 'log_search',
1224 'querycache', 'objectcache', 'job', 'l10n_cache', 'redirect', 'querycachetwo',
1225 'archive', 'user_groups', 'page_props', 'category',
1226 'slots', 'content', 'slot_roles', 'content_models',
1227 ];
1228
1230 // The new tables for comments are in use
1231 $tables[] = 'comment';
1232 $tables[] = 'revision_comment_temp';
1233 }
1234
1236 // The new tables for actors are in use
1237 $tables[] = 'actor';
1238 $tables[] = 'revision_actor_temp';
1239 }
1240
1241 if ( in_array( $this->db->getType(), [ 'mysql', 'sqlite', 'oracle' ] ) ) {
1242 array_push( $tables, 'searchindex' );
1243 }
1244
1245 // Allow extensions to add to the list of tables to duplicate;
1246 // may be necessary if they hook into page save or other code
1247 // which will require them while running tests.
1248 Hooks::run( 'ParserTestTables', [ &$tables ] );
1249
1250 return $tables;
1251 }
1252
1253 public function setDatabase( IDatabase $db ) {
1254 $this->db = $db;
1255 $this->setupDone['setDatabase'] = true;
1256 }
1257
1275 public function setupDatabase( $nextTeardown = null ) {
1276 global $wgDBprefix;
1277
1278 $this->db = wfGetDB( DB_MASTER );
1279 $dbType = $this->db->getType();
1280
1281 if ( $dbType == 'oracle' ) {
1282 $suspiciousPrefixes = [ 'pt_', MediaWikiTestCase::ORA_DB_PREFIX ];
1283 } else {
1284 $suspiciousPrefixes = [ 'parsertest_', MediaWikiTestCase::DB_PREFIX ];
1285 }
1286 if ( in_array( $wgDBprefix, $suspiciousPrefixes ) ) {
1287 throw new MWException( "\$wgDBprefix=$wgDBprefix suggests DB setup is already done" );
1288 }
1289
1290 $teardown = [];
1291
1292 $teardown[] = $this->markSetupDone( 'setupDatabase' );
1293
1294 # CREATE TEMPORARY TABLE breaks if there is more than one server
1295 if ( MediaWikiServices::getInstance()->getDBLoadBalancer()->getServerCount() != 1 ) {
1296 $this->useTemporaryTables = false;
1297 }
1298
1299 $temporary = $this->useTemporaryTables || $dbType == 'postgres';
1300 $prefix = $dbType != 'oracle' ? 'parsertest_' : 'pt_';
1301
1302 $this->dbClone = new CloneDatabase( $this->db, $this->listTables(), $prefix );
1303 $this->dbClone->useTemporaryTables( $temporary );
1304 $this->dbClone->cloneTableStructure();
1305 CloneDatabase::changePrefix( $prefix );
1306
1307 if ( $dbType == 'oracle' ) {
1308 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
1309 # Insert 0 user to prevent FK violations
1310
1311 # Anonymous user
1312 $this->db->insert( 'user', [
1313 'user_id' => 0,
1314 'user_name' => 'Anonymous' ] );
1315 }
1316
1317 $teardown[] = function () {
1318 $this->teardownDatabase();
1319 };
1320
1321 // Wipe some DB query result caches on setup and teardown
1322 $reset = function () {
1323 MediaWikiServices::getInstance()->getLinkCache()->clear();
1324
1325 // Clear the message cache
1326 MessageCache::singleton()->clear();
1327 };
1328 $reset();
1329 $teardown[] = $reset;
1330 return $this->createTeardownObject( $teardown, $nextTeardown );
1331 }
1332
1341 public function setupUploads( $nextTeardown = null ) {
1342 $teardown = [];
1343
1344 $this->checkSetupDone( 'setupDatabase', 'setDatabase' );
1345 $teardown[] = $this->markSetupDone( 'setupUploads' );
1346
1347 // Create the files in the upload directory (or pretend to create them
1348 // in a MockFileBackend). Append teardown callback.
1349 $teardown[] = $this->setupUploadBackend();
1350
1351 // Create a user
1352 $user = User::createNew( 'WikiSysop' );
1353
1354 // Register the uploads in the database
1355
1356 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.jpg' ) );
1357 # note that the size/width/height/bits/etc of the file
1358 # are actually set by inspecting the file itself; the arguments
1359 # to recordUpload2 have no effect. That said, we try to make things
1360 # match up so it is less confusing to readers of the code & tests.
1361 $image->recordUpload2( '', 'Upload of some lame file', 'Some lame file', [
1362 'size' => 7881,
1363 'width' => 1941,
1364 'height' => 220,
1365 'bits' => 8,
1366 'media_type' => MEDIATYPE_BITMAP,
1367 'mime' => 'image/jpeg',
1368 'metadata' => serialize( [] ),
1369 'sha1' => Wikimedia\base_convert( '1', 16, 36, 31 ),
1370 'fileExists' => true
1371 ], $this->db->timestamp( '20010115123500' ), $user );
1372
1373 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Thumb.png' ) );
1374 # again, note that size/width/height below are ignored; see above.
1375 $image->recordUpload2( '', 'Upload of some lame thumbnail', 'Some lame thumbnail', [
1376 'size' => 22589,
1377 'width' => 135,
1378 'height' => 135,
1379 'bits' => 8,
1380 'media_type' => MEDIATYPE_BITMAP,
1381 'mime' => 'image/png',
1382 'metadata' => serialize( [] ),
1383 'sha1' => Wikimedia\base_convert( '2', 16, 36, 31 ),
1384 'fileExists' => true
1385 ], $this->db->timestamp( '20130225203040' ), $user );
1386
1387 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.svg' ) );
1388 $image->recordUpload2( '', 'Upload of some lame SVG', 'Some lame SVG', [
1389 'size' => 12345,
1390 'width' => 240,
1391 'height' => 180,
1392 'bits' => 0,
1393 'media_type' => MEDIATYPE_DRAWING,
1394 'mime' => 'image/svg+xml',
1395 'metadata' => serialize( [] ),
1396 'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
1397 'fileExists' => true
1398 ], $this->db->timestamp( '20010115123500' ), $user );
1399
1400 # This image will be blacklisted in [[MediaWiki:Bad image list]]
1401 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Bad.jpg' ) );
1402 $image->recordUpload2( '', 'zomgnotcensored', 'Borderline image', [
1403 'size' => 12345,
1404 'width' => 320,
1405 'height' => 240,
1406 'bits' => 24,
1407 'media_type' => MEDIATYPE_BITMAP,
1408 'mime' => 'image/jpeg',
1409 'metadata' => serialize( [] ),
1410 'sha1' => Wikimedia\base_convert( '3', 16, 36, 31 ),
1411 'fileExists' => true
1412 ], $this->db->timestamp( '20010115123500' ), $user );
1413
1414 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Video.ogv' ) );
1415 $image->recordUpload2( '', 'A pretty movie', 'Will it play', [
1416 'size' => 12345,
1417 'width' => 320,
1418 'height' => 240,
1419 'bits' => 0,
1420 'media_type' => MEDIATYPE_VIDEO,
1421 'mime' => 'application/ogg',
1422 'metadata' => serialize( [] ),
1423 'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
1424 'fileExists' => true
1425 ], $this->db->timestamp( '20010115123500' ), $user );
1426
1427 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Audio.oga' ) );
1428 $image->recordUpload2( '', 'An awesome hitsong', 'Will it play', [
1429 'size' => 12345,
1430 'width' => 0,
1431 'height' => 0,
1432 'bits' => 0,
1433 'media_type' => MEDIATYPE_AUDIO,
1434 'mime' => 'application/ogg',
1435 'metadata' => serialize( [] ),
1436 'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
1437 'fileExists' => true
1438 ], $this->db->timestamp( '20010115123500' ), $user );
1439
1440 # A DjVu file
1441 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'LoremIpsum.djvu' ) );
1442 $image->recordUpload2( '', 'Upload a DjVu', 'A DjVu', [
1443 'size' => 3249,
1444 'width' => 2480,
1445 'height' => 3508,
1446 'bits' => 0,
1447 'media_type' => MEDIATYPE_BITMAP,
1448 'mime' => 'image/vnd.djvu',
1449 'metadata' => '<?xml version="1.0" ?>
1450<!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
1451<DjVuXML>
1452<HEAD></HEAD>
1453<BODY><OBJECT height="3508" width="2480">
1454<PARAM name="DPI" value="300" />
1455<PARAM name="GAMMA" value="2.2" />
1456</OBJECT>
1457<OBJECT height="3508" width="2480">
1458<PARAM name="DPI" value="300" />
1459<PARAM name="GAMMA" value="2.2" />
1460</OBJECT>
1461<OBJECT height="3508" width="2480">
1462<PARAM name="DPI" value="300" />
1463<PARAM name="GAMMA" value="2.2" />
1464</OBJECT>
1465<OBJECT height="3508" width="2480">
1466<PARAM name="DPI" value="300" />
1467<PARAM name="GAMMA" value="2.2" />
1468</OBJECT>
1469<OBJECT height="3508" width="2480">
1470<PARAM name="DPI" value="300" />
1471<PARAM name="GAMMA" value="2.2" />
1472</OBJECT>
1473</BODY>
1474</DjVuXML>',
1475 'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
1476 'fileExists' => true
1477 ], $this->db->timestamp( '20010115123600' ), $user );
1478
1479 return $this->createTeardownObject( $teardown, $nextTeardown );
1480 }
1481
1488 private function teardownDatabase() {
1489 $this->checkSetupDone( 'setupDatabase' );
1490
1491 $this->dbClone->destroy();
1492
1493 if ( $this->useTemporaryTables ) {
1494 if ( $this->db->getType() == 'sqlite' ) {
1495 # Under SQLite the searchindex table is virtual and need
1496 # to be explicitly destroyed. See T31912
1497 # See also MediaWikiTestCase::destroyDB()
1498 wfDebug( __METHOD__ . " explicitly destroying sqlite virtual table parsertest_searchindex\n" );
1499 $this->db->query( "DROP TABLE `parsertest_searchindex`" );
1500 }
1501 # Don't need to do anything
1502 return;
1503 }
1504
1505 $tables = $this->listTables();
1506
1507 foreach ( $tables as $table ) {
1508 if ( $this->db->getType() == 'oracle' ) {
1509 $this->db->query( "DROP TABLE pt_$table DROP CONSTRAINTS" );
1510 } else {
1511 $this->db->query( "DROP TABLE `parsertest_$table`" );
1512 }
1513 }
1514
1515 if ( $this->db->getType() == 'oracle' ) {
1516 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
1517 }
1518 }
1519
1525 private function setupUploadBackend() {
1526 global $IP;
1527
1528 $repo = RepoGroup::singleton()->getLocalRepo();
1529 $base = $repo->getZonePath( 'public' );
1530 $backend = $repo->getBackend();
1531 $backend->prepare( [ 'dir' => "$base/3/3a" ] );
1532 $backend->store( [
1533 'src' => "$IP/tests/phpunit/data/parser/headbg.jpg",
1534 'dst' => "$base/3/3a/Foobar.jpg"
1535 ] );
1536 $backend->prepare( [ 'dir' => "$base/e/ea" ] );
1537 $backend->store( [
1538 'src' => "$IP/tests/phpunit/data/parser/wiki.png",
1539 'dst' => "$base/e/ea/Thumb.png"
1540 ] );
1541 $backend->prepare( [ 'dir' => "$base/0/09" ] );
1542 $backend->store( [
1543 'src' => "$IP/tests/phpunit/data/parser/headbg.jpg",
1544 'dst' => "$base/0/09/Bad.jpg"
1545 ] );
1546 $backend->prepare( [ 'dir' => "$base/5/5f" ] );
1547 $backend->store( [
1548 'src' => "$IP/tests/phpunit/data/parser/LoremIpsum.djvu",
1549 'dst' => "$base/5/5f/LoremIpsum.djvu"
1550 ] );
1551
1552 // No helpful SVG file to copy, so make one ourselves
1553 $data = '<?xml version="1.0" encoding="utf-8"?>' .
1554 '<svg xmlns="http://www.w3.org/2000/svg"' .
1555 ' version="1.1" width="240" height="180"/>';
1556
1557 $backend->prepare( [ 'dir' => "$base/f/ff" ] );
1558 $backend->quickCreate( [
1559 'content' => $data, 'dst' => "$base/f/ff/Foobar.svg"
1560 ] );
1561
1562 return function () use ( $backend ) {
1563 if ( $backend instanceof MockFileBackend ) {
1564 // In memory backend, so dont bother cleaning them up.
1565 return;
1566 }
1567 $this->teardownUploadBackend();
1568 };
1569 }
1570
1574 private function teardownUploadBackend() {
1575 if ( $this->keepUploads ) {
1576 return;
1577 }
1578
1579 $repo = RepoGroup::singleton()->getLocalRepo();
1580 $public = $repo->getZonePath( 'public' );
1581
1582 $this->deleteFiles(
1583 [
1584 "$public/3/3a/Foobar.jpg",
1585 "$public/e/ea/Thumb.png",
1586 "$public/0/09/Bad.jpg",
1587 "$public/5/5f/LoremIpsum.djvu",
1588 "$public/f/ff/Foobar.svg",
1589 "$public/0/00/Video.ogv",
1590 "$public/4/41/Audio.oga",
1591 ]
1592 );
1593 }
1594
1599 private function deleteFiles( $files ) {
1600 // Delete the files
1601 $backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
1602 foreach ( $files as $file ) {
1603 $backend->delete( [ 'src' => $file ], [ 'force' => 1 ] );
1604 }
1605
1606 // Delete the parent directories
1607 foreach ( $files as $file ) {
1608 $tmp = FileBackend::parentStoragePath( $file );
1609 while ( $tmp ) {
1610 if ( !$backend->clean( [ 'dir' => $tmp ] )->isOK() ) {
1611 break;
1612 }
1613 $tmp = FileBackend::parentStoragePath( $tmp );
1614 }
1615 }
1616 }
1617
1623 public function addArticles( $articles ) {
1624 $setup = [];
1625 $teardown = [];
1626
1627 // Be sure ParserTestRunner::addArticle has correct language set,
1628 // so that system messages get into the right language cache
1629 if ( MediaWikiServices::getInstance()->getContentLanguage()->getCode() !== 'en' ) {
1630 $setup['wgLanguageCode'] = 'en';
1631 $lang = Language::factory( 'en' );
1632 $setup['wgContLang'] = $lang;
1633 $setup[] = function () use ( $lang ) {
1634 $services = MediaWikiServices::getInstance();
1635 $services->disableService( 'ContentLanguage' );
1636 $services->redefineService( 'ContentLanguage', function () use ( $lang ) {
1637 return $lang;
1638 } );
1639 };
1640 $teardown[] = function () {
1641 MediaWikiServices::getInstance()->resetServiceForTesting( 'ContentLanguage' );
1642 };
1643 }
1644
1645 // Add special namespaces, in case that hasn't been done by staticSetup() yet
1646 $this->appendNamespaceSetup( $setup, $teardown );
1647
1648 // wgCapitalLinks obviously needs initialisation
1649 $setup['wgCapitalLinks'] = true;
1650
1651 $teardown[] = $this->executeSetupSnippets( $setup );
1652
1653 foreach ( $articles as $info ) {
1654 $this->addArticle( $info['name'], $info['text'], $info['file'], $info['line'] );
1655 }
1656
1657 // Wipe WANObjectCache process cache, which is invalidated by article insertion
1658 // due to T144706
1659 ObjectCache::getMainWANInstance()->clearProcessCache();
1660
1661 $this->executeSetupSnippets( $teardown );
1662 }
1663
1673 private function addArticle( $name, $text, $file, $line ) {
1674 $text = self::chomp( $text );
1675 $name = self::chomp( $name );
1676
1677 $title = Title::newFromText( $name );
1678 wfDebug( __METHOD__ . ": adding $name" );
1679
1680 if ( is_null( $title ) ) {
1681 throw new MWException( "invalid title '$name' at $file:$line\n" );
1682 }
1683
1684 $newContent = ContentHandler::makeContent( $text, $title );
1685
1686 $page = WikiPage::factory( $title );
1687 $page->loadPageData( 'fromdbmaster' );
1688
1689 if ( $page->exists() ) {
1690 $content = $page->getContent( Revision::RAW );
1691 // Only reject the title, if the content/content model is different.
1692 // This makes it easier to create Template:(( or Template:)) in different extensions
1693 if ( $newContent->equals( $content ) ) {
1694 return;
1695 }
1696 throw new MWException(
1697 "duplicate article '$name' with different content at $file:$line\n"
1698 );
1699 }
1700
1701 // Optionally use mock parser, to make debugging of actual parser tests simpler.
1702 // But initialise the MessageCache clone first, don't let MessageCache
1703 // get a reference to the mock object.
1704 if ( $this->disableSaveParse ) {
1705 MessageCache::singleton()->getParser();
1706 $restore = $this->executeSetupSnippets( [ 'wgParser' => new ParserTestMockParser ] );
1707 } else {
1708 $restore = false;
1709 }
1710 try {
1711 $status = $page->doEditContent(
1712 $newContent,
1713 '',
1715 );
1716 } finally {
1717 if ( $restore ) {
1718 $restore();
1719 }
1720 }
1721
1722 if ( !$status->isOK() ) {
1723 throw new MWException( $status->getWikiText( false, false, 'en' ) );
1724 }
1725
1726 // The RepoGroup cache is invalidated by the creation of file redirects
1727 if ( $title->inNamespace( NS_FILE ) ) {
1728 RepoGroup::singleton()->clearCache( $title );
1729 }
1730 }
1731
1738 public function requireHook( $name ) {
1739 global $wgParser;
1740
1741 $wgParser->firstCallInit(); // make sure hooks are loaded.
1742 if ( isset( $wgParser->mTagHooks[$name] ) ) {
1743 return true;
1744 } else {
1745 $this->recorder->warning( " This test suite requires the '$name' hook " .
1746 "extension, skipping." );
1747 return false;
1748 }
1749 }
1750
1757 public function requireFunctionHook( $name ) {
1758 global $wgParser;
1759
1760 $wgParser->firstCallInit(); // make sure hooks are loaded.
1761
1762 if ( isset( $wgParser->mFunctionHooks[$name] ) ) {
1763 return true;
1764 } else {
1765 $this->recorder->warning( " This test suite requires the '$name' function " .
1766 "hook extension, skipping." );
1767 return false;
1768 }
1769 }
1770
1777 public function requireTransparentHook( $name ) {
1778 global $wgParser;
1779
1780 $wgParser->firstCallInit(); // make sure hooks are loaded.
1781
1782 if ( isset( $wgParser->mTransparentTagHooks[$name] ) ) {
1783 return true;
1784 } else {
1785 $this->recorder->warning( " This test suite requires the '$name' transparent " .
1786 "hook extension, skipping.\n" );
1787 return false;
1788 }
1789 }
1790
1798 private function getFakeTimestamp() {
1799 // parsed as '1970-01-01T00:02:03Z'
1800 return 123;
1801 }
1802}
serialize()
$GLOBALS['IP']
$wgObjectCaches
Advanced object cache configuration.
$wgFileBackends
File backend structure configuration.
$wgDBprefix
Table name prefix.
$wgParserTestFiles
Parser test suite files to be run by parserTests.php when no specific filename is passed to it.
$wgParserConf
Parser configuration.
int $wgCommentTableSchemaMigrationStage
Comment table schema migration stage.
int $wgActorTableSchemaMigrationStage
Actor table schema migration stage.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfTempDir()
Tries to get the system directory for temporary files.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfGetCaller( $level=2)
Get the name of the function which called this function wfGetCaller( 1 ) is the function with the wfG...
wfLocalFile( $title)
Get an object referring to a locally registered file.
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
$wgParser
Definition Setup.php:921
$line
Definition cdb.php:59
static changePrefix( $prefix)
Change the table prefix on all open DB connections/.
Class for a file system (FS) based file backend.
static parentStoragePath( $storagePath)
Get the parent storage directory of a storage path.
Simple store for keeping values in an associative array for the current process.
static formatComment( $comment, $title=null, $local=false, $wikiId=null)
This function is called by all recent changes variants, by the page history, and by the user contribu...
Definition Linker.php:1088
static destroySingletons()
Destroy the singleton instances.
MediaWiki exception.
static factory(array $config)
Create a new Tidy driver object from configuration.
Definition MWTidy.php:105
static setInstance( $instance)
Set the driver to be used.
Definition MWTidy.php:132
static destroySingleton()
Destroy the current singleton instance.
Definition MWTidy.php:139
Class to construct MediaHandler objects.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Base class for HTML cleanup utilities.
Class simulating a backend store.
This class should be covered by a general architecture document which does not exist as of January 20...
A parser used during article insertion which does nothing, to avoid unnecessary log noise and other i...
Represent the result of a parser test.
TidySupport $tidySupport
listTables()
List of temporary tables to create, without prefix.
runTestsFromFiles( $filenames)
Run a series of tests listed in the given text files.
bool $keepUploads
Reuse upload directory.
bool $runParsoid
Run tests intended only for parsoid.
staticSetup( $nextTeardown=null)
Do any setup which can be done once for all tests, independent of test options, except for database s...
markSetupDone( $funcName)
Set a setupDone flag to indicate that setup has been done, and return the teardown closure.
string $regex
A complete regex for filtering tests.
runTest( $test)
Run a given wikitext input through a freshly-constructed wiki parser, and compare the output against ...
setupUploads( $nextTeardown=null)
Add data about uploads to the new test DB, and set up the upload directory.
bool $disableSaveParse
Disable parse on article insertion.
createRepoGroup()
Create a RepoGroup object appropriate for the current configuration.
createTeardownObject( $teardown, $nextTeardown=null)
Take a setup array in the same format as the one given to executeSetupSnippets(), and return a Scoped...
teardownUploadBackend()
Remove the dummy uploads directory.
static array $coreTestFiles
MediaWiki core parser test files, paths will be prefixed with DIR .
TestRecorder $recorder
string null $uploadDir
The upload directory, or null to not set up an upload directory.
setupDatabase( $nextTeardown=null)
Set up temporary DB tables.
checkSetupDone( $funcName, $funcName2=null)
Ensure a given setup stage has been done, throw an exception if it has not.
static getParserTestFiles()
Get list of filenames to extension and core parser tests.
requireHook( $name)
Check if a hook is installed.
setDatabase(IDatabase $db)
array $normalizationFunctions
A list of normalization functions to apply to the expected and actual output.
isSetupDone( $funcName)
Determine whether a particular setup function has been run.
resetTitleServices()
Reset the Title-related services that need resetting for each test.
string null $fileBackendName
The name of the file backend to use, or null to use MockFileBackend.
bool $runDisabled
Run disabled parser tests.
deleteFiles( $files)
Delete the specified files and their parent directories.
static getOptionValue( $key, $opts, $default)
Use a regex to find out the value of an option.
perTestSetup( $test, $nextTeardown=null)
Do any required setup which is dependent on test options.
Database $db
Our connection to the database.
setupInterwikis()
Insert hardcoded interwiki in the lookup table.
__construct(TestRecorder $recorder, $options=[])
requireTransparentHook( $name)
Check if a transparent tag hook is installed.
CloneDatabase $dbClone
Database clone helper.
runTests( $testFileInfo)
Run the tests from a single file.
teardownDatabase()
Helper for database teardown, called from the teardown closure.
requireFunctionHook( $name)
Check if a function hook is installed.
meetsRequirements( $requirements)
Determine whether the current parser has the hooks registered in it that are required by a file read ...
setupUploadBackend()
Upload test files to the backend created by createRepoGroup().
executeSetupSnippets( $setup)
Execute an array in which elements with integer keys are taken to be callable objects,...
addArticles( $articles)
Add articles to the test DB.
getFakeTimestamp()
Fake constant timestamp to make sure time-related parser functions give a persistent value.
static chomp( $s)
Remove last character if it is a newline.
TidyDriverBase $tidyDriver
parseOptions( $instring)
Given the options string, return an associative array of options.
addArticle( $name, $text, $file, $line)
Insert a temporary test article.
getParser( $preprocessor=null)
Get a Parser object.
appendNamespaceSetup(&$setup, &$teardown)
Prioritized list of file repositories.
Definition RepoGroup.php:29
static singleton()
Get a RepoGroup instance.
Definition RepoGroup.php:59
static setSingleton( $instance)
Set the singleton instance to a given object Used by extensions which hook into the Repo chain.
Definition RepoGroup.php:85
static destroySingleton()
Destroy the singleton instance, so that a new one will be created next time singleton() is called.
Definition RepoGroup.php:73
const RAW
Definition Revision.php:57
static read( $file, array $options=[])
Interface to record parser test results.
warning( $message)
Show a warning to the user.
Initialize and detect the tidy support.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:47
setOption( $oname, $val)
Set the given option for a user.
Definition User.php:3258
static createNew( $name, $params=[])
Add a user to the database, return the user object.
Definition User.php:4301
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
see documentation in includes Linker php for Linker::makeImageLink or false for current used if you return false $parser
Definition hooks.txt:1873
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check $image
Definition hooks.txt:925
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:2050
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition hooks.txt:2885
this hook is for auditing only RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist & $tables
Definition hooks.txt:1035
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2055
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place or wrap services the preferred way to define a new service is the $wgServiceWiringFiles array $services
Definition hooks.txt:2335
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:894
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:302
Status::newGood()` to allow deletion, and then `return false` from the hook function. Ensure you consume the 'ChangeTagAfterDelete' hook to carry out custom deletion actions. $tag:name of the tag $user:user initiating the action & $status:Status object. See above. 'ChangeTagsListActive':Allows you to nominate which of the tags your extension uses are in active use. & $tags:list of all active tags. Append to this array. 'ChangeTagsAfterUpdateTags':Called after tags have been updated with the ChangeTags::updateTags function. Params:$addedTags:tags effectively added in the update $removedTags:tags effectively removed in the update $prevTags:tags that were present prior to the update $rc_id:recentchanges table id $rev_id:revision table id $log_id:logging table id $params:tag params $rc:RecentChange being tagged when the tagging accompanies the action, or null $user:User who performed the tagging when the tagging is subsequent to the action, or null 'ChangeTagsAllowedAdd':Called when checking if a user can add tags to a change. & $allowedTags:List of all the tags the user is allowed to add. Any tags the user wants to add( $addTags) that are not in this array will cause it to fail. You may add or remove tags to this array as required. $addTags:List of tags user intends to add. $user:User who is adding the tags. 'ChangeUserGroups':Called before user groups are changed. $performer:The User who will perform the change $user:The User whose groups will be changed & $add:The groups that will be added & $remove:The groups that will be removed 'Collation::factory':Called if $wgCategoryCollation is an unknown collation. $collationName:Name of the collation in question & $collationObject:Null. Replace with a subclass of the Collation class that implements the collation given in $collationName. 'ConfirmEmailComplete':Called after a user 's email has been confirmed successfully. $user:user(object) whose email is being confirmed 'ContentAlterParserOutput':Modify parser output for a given content object. Called by Content::getParserOutput after parsing has finished. Can be used for changes that depend on the result of the parsing but have to be done before LinksUpdate is called(such as adding tracking categories based on the rendered HTML). $content:The Content to render $title:Title of the page, as context $parserOutput:ParserOutput to manipulate 'ContentGetParserOutput':Customize parser output for a given content object, called by AbstractContent::getParserOutput. May be used to override the normal model-specific rendering of page content. $content:The Content to render $title:Title of the page, as context $revId:The revision ID, as context $options:ParserOptions for rendering. To avoid confusing the parser cache, the output can only depend on parameters provided to this hook function, not on global state. $generateHtml:boolean, indicating whether full HTML should be generated. If false, generation of HTML may be skipped, but other information should still be present in the ParserOutput object. & $output:ParserOutput, to manipulate or replace 'ContentHandlerDefaultModelFor':Called when the default content model is determined for a given title. May be used to assign a different model for that title. $title:the Title in question & $model:the model name. Use with CONTENT_MODEL_XXX constants. 'ContentHandlerForModelID':Called when a ContentHandler is requested for a given content model name, but no entry for that model exists in $wgContentHandlers. Note:if your extension implements additional models via this hook, please use GetContentModels hook to make them known to core. $modeName:the requested content model name & $handler:set this to a ContentHandler object, if desired. 'ContentModelCanBeUsedOn':Called to determine whether that content model can be used on a given page. This is especially useful to prevent some content models to be used in some special location. $contentModel:ID of the content model in question $title:the Title in question. & $ok:Output parameter, whether it is OK to use $contentModel on $title. Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok. 'ContribsPager::getQueryInfo':Before the contributions query is about to run & $pager:Pager object for contributions & $queryInfo:The query for the contribs Pager 'ContribsPager::reallyDoQuery':Called before really executing the query for My Contributions & $data:an array of results of all contribs queries $pager:The ContribsPager object hooked into $offset:Index offset, inclusive $limit:Exact query limit $descending:Query direction, false for ascending, true for descending 'ContributionsLineEnding':Called before a contributions HTML line is finished $page:SpecialPage object for contributions & $ret:the HTML line $row:the DB row for this line & $classes:the classes to add to the surrounding< li > & $attribs:associative array of other HTML attributes for the< li > element. Currently only data attributes reserved to MediaWiki are allowed(see Sanitizer::isReservedDataAttribute). 'ContributionsToolLinks':Change tool links above Special:Contributions $id:User identifier $title:User page title & $tools:Array of tool links $specialPage:SpecialPage instance for context and services. Can be either SpecialContributions or DeletedContributionsPage. Extensions should type hint against a generic SpecialPage though. 'ConvertContent':Called by AbstractContent::convert when a conversion to another content model is requested. Handler functions that modify $result should generally return false to disable further attempts at conversion. $content:The Content object to be converted. $toModel:The ID of the content model to convert to. $lossy: boolean indicating whether lossy conversion is allowed. & $result:Output parameter, in case the handler function wants to provide a converted Content object. Note that $result->getContentModel() must return $toModel. 'ContentSecurityPolicyDefaultSource':Modify the allowed CSP load sources. This affects all directives except for the script directive. If you want to add a script source, see ContentSecurityPolicyScriptSource hook. & $defaultSrc:Array of Content-Security-Policy allowed sources $policyConfig:Current configuration for the Content-Security-Policy header $mode:ContentSecurityPolicy::REPORT_ONLY_MODE or ContentSecurityPolicy::FULL_MODE depending on type of header 'ContentSecurityPolicyDirectives':Modify the content security policy directives. Use this only if ContentSecurityPolicyDefaultSource and ContentSecurityPolicyScriptSource do not meet your needs. & $directives:Array of CSP directives $policyConfig:Current configuration for the CSP header $mode:ContentSecurityPolicy::REPORT_ONLY_MODE or ContentSecurityPolicy::FULL_MODE depending on type of header 'ContentSecurityPolicyScriptSource':Modify the allowed CSP script sources. Note that you also have to use ContentSecurityPolicyDefaultSource if you want non-script sources to be loaded from whatever you add. & $scriptSrc:Array of CSP directives $policyConfig:Current configuration for the CSP header $mode:ContentSecurityPolicy::REPORT_ONLY_MODE or ContentSecurityPolicy::FULL_MODE depending on type of header 'CustomEditor':When invoking the page editor Return true to allow the normal editor to be used, or false if implementing a custom editor, e.g. for a special namespace, etc. $article:Article being edited $user:User performing the edit 'DatabaseOraclePostInit':Called after initialising an Oracle database $db:the DatabaseOracle object 'DeletedContribsPager::reallyDoQuery':Called before really executing the query for Special:DeletedContributions Similar to ContribsPager::reallyDoQuery & $data:an array of results of all contribs queries $pager:The DeletedContribsPager object hooked into $offset:Index offset, inclusive $limit:Exact query limit $descending:Query direction, false for ascending, true for descending 'DeletedContributionsLineEnding':Called before a DeletedContributions HTML line is finished. Similar to ContributionsLineEnding $page:SpecialPage object for DeletedContributions & $ret:the HTML line $row:the DB row for this line & $classes:the classes to add to the surrounding< li > & $attribs:associative array of other HTML attributes for the< li > element. Currently only data attributes reserved to MediaWiki are allowed(see Sanitizer::isReservedDataAttribute). 'DeleteUnknownPreferences':Called by the cleanupPreferences.php maintenance script to build a WHERE clause with which to delete preferences that are not known about. This hook is used by extensions that have dynamically-named preferences that should not be deleted in the usual cleanup process. For example, the Gadgets extension creates preferences prefixed with 'gadget-', and so anything with that prefix is excluded from the deletion. &where:An array that will be passed as the $cond parameter to IDatabase::select() to determine what will be deleted from the user_properties table. $db:The IDatabase object, useful for accessing $db->buildLike() etc. 'DifferenceEngineAfterLoadNewText':called in DifferenceEngine::loadNewText() after the new revision 's content has been loaded into the class member variable $differenceEngine->mNewContent but before returning true from this function. $differenceEngine:DifferenceEngine object 'DifferenceEngineLoadTextAfterNewContentIsLoaded':called in DifferenceEngine::loadText() after the new revision 's content has been loaded into the class member variable $differenceEngine->mNewContent but before checking if the variable 's value is null. This hook can be used to inject content into said class member variable. $differenceEngine:DifferenceEngine object 'DifferenceEngineMarkPatrolledLink':Allows extensions to change the "mark as patrolled" link which is shown both on the diff header as well as on the bottom of a page, usually wrapped in a span element which has class="patrollink". $differenceEngine:DifferenceEngine object & $markAsPatrolledLink:The "mark as patrolled" link HTML(string) $rcid:Recent change ID(rc_id) for this change(int) 'DifferenceEngineMarkPatrolledRCID':Allows extensions to possibly change the rcid parameter. For example the rcid might be set to zero due to the user being the same as the performer of the change but an extension might still want to show it under certain conditions. & $rcid:rc_id(int) of the change or 0 $differenceEngine:DifferenceEngine object $change:RecentChange object $user:User object representing the current user 'DifferenceEngineNewHeader':Allows extensions to change the $newHeader variable, which contains information about the new revision, such as the revision 's author, whether the revision was marked as a minor edit or not, etc. $differenceEngine:DifferenceEngine object & $newHeader:The string containing the various #mw-diff-otitle[1-5] divs, which include things like revision author info, revision comment, RevisionDelete link and more $formattedRevisionTools:Array containing revision tools, some of which may have been injected with the DiffRevisionTools hook $nextlink:String containing the link to the next revision(if any) $status
Definition hooks.txt:1071
usually copyright or history_copyright This message must be in HTML not wikitext if the section is included from a template $section
Definition hooks.txt:3107
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place $output
Definition hooks.txt:2317
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition hooks.txt:247
returning false will NOT prevent logging $e
Definition hooks.txt:2226
$IP
Definition update.php:3
const EDIT_INTERNAL
Definition Defines.php:159
const NS_FILE
Definition Defines.php:70
const SCHEMA_COMPAT_WRITE_NEW
Definition Defines.php:286
const CACHE_DB
Definition Defines.php:103
const MIGRATION_WRITE_BOTH
Definition Defines.php:316
const EDIT_NEW
Definition Defines.php:152
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
const MEDIATYPE_DRAWING
Definition defines.php:30
const MEDIATYPE_VIDEO
Definition defines.php:35
const MEDIATYPE_AUDIO
Definition defines.php:32
const MEDIATYPE_BITMAP
Definition defines.php:28
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
$content
const DB_MASTER
Definition defines.php:26
if(!isset( $args[0])) $lang