getServiceContainer(); return new CommentParser( $services->getLinkRenderer(), $services->getLinkBatchFactory(), $services->getLinkCache(), $services->getRepoGroup(), $services->getContentLanguage(), $services->getContentLanguage(), $services->getTitleParser(), $services->getNamespaceInfo(), $services->getHookContainer() ); } private function setupInterwiki() { $this->overrideMwServices( null, [ 'InterwikiLookup' => function () { return $this->getDummyInterwikiLookup( [ 'interwiki' => [ 'iw_prefix' => 'interwiki', 'iw_url' => 'https://interwiki/$1', ] ] ); } ] ); } private function setupConf() { $conf = new SiteConfiguration(); $conf->settings = [ 'wgServer' => [ 'enwiki' => '//en.example.org' ], 'wgArticlePath' => [ 'enwiki' => '/w/$1', ], ]; $conf->suffixes = [ 'wiki' ]; $this->setMwGlobals( [ 'wgScript' => '/wiki/index.php', 'wgArticlePath' => '/wiki/$1', 'wgCapitalLinks' => true, 'wgConf' => $conf, ] ); } /** * Copied from LinkerTest so that LinkerTest can be deleted once deprecation * and removal of Linker::formatComment() is complete. * * @return array[] */ public function provideFormatComment() { $wikiId = 'enwiki'; // $wgConf has a fake entry for this return [ // Linker::formatComment [ 'a<script>b', 'a */" ], [ '→‎autocomment', "/* autocomment */", false, true ], [ 'autocomment', "/* autocomment */", null ], [ '', "/* */", false, true ], [ '', "/* */", null ], [ '[[', "/* [[ */", false, true ], [ '[[', "/* [[ */", null ], [ "foo →‎[[#_\t_]]", "foo /* [[#_\t_]] */", false, true ], [ "foo #_\t_", "foo /* [[#_\t_]] */", null ], [ '→‎autocomment', "/* autocomment */", false, false ], [ '→‎autocomment', "/* autocomment */", false, false, $wikiId ], // Linker::formatLinksInComment [ 'abc link def', "abc [[link]] def", ], [ 'abc text def', "abc [[link|text]] def", ], [ 'abc Special:BlankPage def', "abc [[Special:BlankPage|]] def", ], [ 'abc ąśż def', "abc [[%C4%85%C5%9B%C5%BC]] def", ], [ 'abc #section def', "abc [[#section]] def", ], [ 'abc /subpage def', "abc [[/subpage]] def", ], [ 'abc "evil!" def', "abc [[\"evil!\"]] def", ], [ 'abc [[<script>very evil</script>]] def', "abc [[]] def", ], [ 'abc [[|]] def', "abc [[|]] def", ], [ 'abc link def', "abc [[link]] def", false, false ], [ 'abc link def', "abc [[link]] def", false, false, $wikiId ], [ 'Media:LinkerTest.jpg', '[[Media:LinkerTest.jpg]]' ], [ 'Special:BlankPage', '[[:Special:BlankPage]]' ], [ 'linktrail...', '[[link]]trail...' ], [ 'Present', '[[Present]]', ], [ 'interwiki:Some page', '[[interwiki:Some page]]', ], [ 'interwiki:Present Present', '[[interwiki:Present]] [[Present]]' ] ]; // phpcs:enable } /** * Adapted from LinkerTest * * @dataProvider provideFormatComment */ public function testFormatComment( $expected, $comment, $title = false, $local = false, $wikiId = null ) { $conf = new SiteConfiguration(); $conf->settings = [ 'wgServer' => [ 'enwiki' => '//en.example.org', 'dewiki' => '//de.example.org', ], 'wgArticlePath' => [ 'enwiki' => '/w/$1', 'dewiki' => '/w/$1', ], ]; $conf->suffixes = [ 'wiki' ]; $this->setMwGlobals( [ 'wgScript' => '/wiki/index.php', 'wgArticlePath' => '/wiki/$1', 'wgCapitalLinks' => true, 'wgConf' => $conf, // TODO: update tests when the default changes 'wgFragmentMode' => [ 'legacy' ], ] ); $this->setupInterwiki(); $this->addGoodLinkObject( 1, Title::newFromText( 'Present' ) ); if ( $title === false ) { // We need a page title that exists $title = Title::newFromText( 'Special:BlankPage' ); } $parser = $this->getParser(); $result = $parser->finalize( $parser->preprocess( $comment, $title, $local, $wikiId ) ); $this->assertEquals( $expected, $result ); } /** * Adapted from LinkerTest */ public static function provideFormatLinksInComment() { return [ [ 'foo bar Special:BlankPage', 'foo bar [[Special:BlankPage]]', null, ], [ 'Special:BlankPage', '[[ :Special:BlankPage]]', null, ], [ '[[FooSpecial:BlankPage', '[[Foo[[Special:BlankPage]]', null, ], [ 'Foo'bar', "[[Foo'bar]]", 'enwiki', ], [ 'foo bar Special:BlankPage', 'foo bar [[Special:BlankPage]]', 'enwiki', ], [ 'foo bar Image:Example', 'foo bar [[Image:Example]]', 'enwiki', ], ]; // phpcs:enable } /** * Adapted from LinkerTest. Note that we test the new HTML escaping variant. * * @dataProvider provideFormatLinksInComment */ public function testFormatLinksInComment( $expected, $input, $wiki ) { $this->setupConf(); $parser = $this->getParser(); $title = Title::newFromText( 'Special:BlankPage' ); $result = $parser->finalize( $parser->preprocess( $input, $title, false, $wiki, false ) ); $this->assertEquals( $expected, $result ); } public function testLinkCacheInteraction() { $this->tablesUsed[] = 'page'; $services = $this->getServiceContainer(); $present = Title::newFromText( 'Present' ); $absent = Title::newFromText( 'Absent' ); $this->editPage( $present, 'content' ); $parser = $this->getParser(); $linkCache = $services->getLinkCache(); $result = $parser->finalize( [ $parser->preprocess( "[[$present]]" ), $parser->preprocess( "[[$absent]]" ) ] ); $expected = [ 'Present', 'Absent' ]; $this->assertSame( $expected, $result ); $this->assertGreaterThan( 0, $linkCache->getGoodLinkID( $present ) ); $this->assertTrue( $linkCache->isBadLink( $absent ) ); // Run the comment batch again and confirm that LinkBatch does not need // to execute a query. This is a CommentParser responsibility since // LinkBatch does not provide a transparent read-through cache. // TODO: Generic $this->assertQueryCount() would do the job. $lbf = $services->getDBLoadBalancerFactory(); $fakeLB = $lbf->newMainLB(); $fakeLB->disable( __METHOD__ ); $linkBatchFactory = new LinkBatchFactory( $services->getLinkCache(), $services->getTitleFormatter(), $services->getContentLanguage(), $services->getGenderCache(), $fakeLB, LoggerFactory::getInstance( 'LinkBatch' ) ); $parser = new CommentParser( $services->getLinkRenderer(), $linkBatchFactory, $linkCache, $services->getRepoGroup(), $services->getContentLanguage(), $services->getContentLanguage(), $services->getTitleParser(), $services->getNamespaceInfo(), $services->getHookContainer() ); $result = $parser->finalize( [ $parser->preprocess( "[[$present]]" ), $parser->preprocess( "[[$absent]]" ) ] ); $this->assertSame( $expected, $result ); } /** * Regression test for T300311 */ public function testInterwikiLinkCachePollution() { $this->tablesUsed[] = 'page'; $this->setupConf(); $this->setupInterwiki(); $present = Title::newFromText( 'Template:Present' ); $this->editPage( $present, 'content' ); $this->getServiceContainer()->getLinkCache()->clear(); $parser = $this->getParser(); $result = $parser->finalize( $parser->preprocess( "[[interwiki:$present]] [[$present]]" ) ); $this->assertSame( // phpcs:ignore Generic.Files.LineLength "interwiki:$present $present", $result ); } }