jenkins-bot has submitted this change and it was merged. Change subject: Inject language links on ContentAlterParserOutput ......................................................................
Inject language links on ContentAlterParserOutput Using ContentAlterParserOutput instead of ParserAfterParse has two major advantages: * it is not triggered when parsing system message or other interface text. * it works for all content models, not just wikitext. Note that ContentAlterParserOutput is triggered on Edit, Preview, LinksUpdate, and stashedit. Change-Id: I5ee84362debadf97ea4bd256ae7db5fba663a876 --- M client/WikibaseClient.php M client/includes/Hooks/ParserOutputUpdateHookHandlers.php M client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php 3 files changed, 28 insertions(+), 73 deletions(-) Approvals: Hoo man: Looks good to me, approved jenkins-bot: Verified diff --git a/client/WikibaseClient.php b/client/WikibaseClient.php index e9a8b45..94fc071 100644 --- a/client/WikibaseClient.php +++ b/client/WikibaseClient.php @@ -92,7 +92,7 @@ $wgHooks['OldChangesListRecentChangesLine'][] = '\Wikibase\ClientHooks::onOldChangesListRecentChangesLine'; $wgHooks['OutputPageParserOutput'][] = '\Wikibase\Client\Hooks\SidebarHookHandlers::onOutputPageParserOutput'; $wgHooks['SkinTemplateGetLanguageLink'][] = '\Wikibase\Client\Hooks\SidebarHookHandlers::onSkinTemplateGetLanguageLink'; - $wgHooks['ParserAfterParse'][] = '\Wikibase\Client\Hooks\ParserOutputUpdateHookHandlers::onParserAfterParse'; + $wgHooks['ContentAlterParserOutput'][] = '\Wikibase\Client\Hooks\ParserOutputUpdateHookHandlers::onContentAlterParserOutput'; $wgHooks['SidebarBeforeOutput'][] = '\Wikibase\Client\Hooks\SidebarHookHandlers::onSidebarBeforeOutput'; $wgHooks['ArticleEditUpdates'][] = '\Wikibase\Client\Hooks\DataUpdateHookHandlers::onArticleEditUpdates'; $wgHooks['ArticleDeleteComplete'][] = '\Wikibase\Client\Hooks\DataUpdateHookHandlers::onArticleDeleteComplete'; diff --git a/client/includes/Hooks/ParserOutputUpdateHookHandlers.php b/client/includes/Hooks/ParserOutputUpdateHookHandlers.php index 89c3c6d..d705124 100644 --- a/client/includes/Hooks/ParserOutputUpdateHookHandlers.php +++ b/client/includes/Hooks/ParserOutputUpdateHookHandlers.php @@ -2,9 +2,10 @@ namespace Wikibase\Client\Hooks; -use Parser; -use StripState; +use Content; +use ParserOutput; use StubUserLang; +use Title; use Wikibase\Client\WikibaseClient; use Wikibase\InterwikiSorter; use Wikibase\LangLinkHandler; @@ -63,31 +64,21 @@ } /** - * Static handler for the ParserAfterParse hook. + * Static handler for the ContentAlterParserOutput hook. * - * @param Parser|null &$parser - * @param string|null &$text Unused. - * @param StripState|null $stripState Unused. - * - * @return bool + * @param Content $content + * @param Title $title + * @param ParserOutput $parserOutput */ - public static function onParserAfterParse( Parser &$parser = null, &$text = null, StripState $stripState = null ) { + public static function onContentAlterParserOutput( Content $content, Title $title, ParserOutput $parserOutput ) { // this hook tries to access repo SiteLinkTable // it interferes with any test that parses something, like a page or a message - if ( $parser === null || defined( 'MW_PHPUNIT_TEST' ) ) { - return true; - } - - // Only run this once, for the article content and not interface stuff - - // This check needs to be here as this method is being invoked a lot, - // thus calling self::newFromGlobalState would be quite heavy - if ( $parser->getOptions()->getInterfaceMessage() ) { - return true; + if ( defined( 'MW_PHPUNIT_TEST' ) ) { + return; } $handler = self::newFromGlobalState(); - return $handler->doParserAfterParse( $parser ); + $handler->doContentAlterParserOutput( $title, $parserOutput ); } /** @@ -111,32 +102,19 @@ /** * Hook runs after internal parsing - * @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserAfterParse + * @see https://www.mediawiki.org/wiki/Manual:Hooks/ContentAlterParserOutput * - * @param Parser &$parser + * @param Title $title + * @param ParserOutput $parserOutput * * @return bool */ - public function doParserAfterParse( Parser &$parser ) { - $title = $parser->getTitle(); - - // Doing this only makes sense when actually creating html for page views, not when - // for example substing a template. - // Please note: While all cases where this matches don't need to go through this many - // that don't match (have OT_HTML) still actually wouldn't need to go through this... - // for example message parses, but we don't have a good way to identify those. - if ( $parser->OutputType() !== Parser::OT_HTML ) { - return true; - } - + public function doContentAlterParserOutput( Title $title, ParserOutput $parserOutput ) { if ( !$this->namespaceChecker->isWikibaseEnabled( $title->getNamespace() ) ) { // shorten out return true; } - // @todo split up the multiple responsibilities here and in lang link handler - - $parserOutput = $parser->getOutput(); $useRepoLinks = $this->langLinkHandler->useRepoLinks( $title, $parserOutput ); if ( $useRepoLinks ) { diff --git a/client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php b/client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php index 6997607..956c3bb 100644 --- a/client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php +++ b/client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php @@ -5,12 +5,9 @@ use Language; use MediaWikiSite; use MediaWikiTestCase; -use Parser; -use ParserOptions; use ParserOutput; use Site; use SiteStore; -use StripState; use Title; use Wikibase\Client\Hooks\LanguageLinkBadgeDisplay; use Wikibase\Client\Hooks\OtherProjectsSidebarGeneratorFactory; @@ -209,23 +206,13 @@ return new OtherProjectsSidebarGeneratorFactory( $settings, $siteLinkLookup, - $this->getSiteStore() - ); + $this->getSiteStore() + ); } - private function newParser( Title $title, array $pageProps, array $extensionData ) { - $popt = new ParserOptions(); - $parser = new Parser(); + private function newParserOutput( array $pageProps, array $extensionData ) { + $parserOutput = new ParserOutput(); - $parser->startExternalParse( $title, $popt, Parser::OT_HTML ); - - $parserOutput = $parser->getOutput(); - $this->primeParserOutput( $parserOutput, $pageProps, $extensionData ); - - return $parser; - } - - private function primeParserOutput( ParserOutput $parserOutput, array $pageProps, array $extensionData ) { foreach ( $pageProps as $name => $value ) { $parserOutput->setProperty( $name, $value ); } @@ -233,6 +220,8 @@ foreach ( $extensionData as $key => $value ) { $parserOutput->setExtensionData( $key, $value ); } + + return $parserOutput; } public function testNewFromGlobalState() { @@ -302,7 +291,7 @@ /** * @dataProvider parserAfterParseProvider */ - public function testDoParserAfterParse( + public function testDoContentAlterParserOutput( Title $title, $expectedItem, $pagePropsBefore, @@ -310,10 +299,10 @@ $expectedSisterLinks, $expectedBadges ) { - $parser = $this->newParser( $title, $pagePropsBefore, array() ); + $parserOutput = $this->newParserOutput( $pagePropsBefore, array() ); $handler = $this->newParserOutputUpdateHookHandlers(); - $handler->doParserAfterParse( $parser ); + $handler->doContentAlterParserOutput( $title, $parserOutput ); $expectedUsage = array( new EntityUsage( @@ -321,8 +310,6 @@ EntityUsage::SITELINK_USAGE ) ); - - $parserOutput = $parser->getOutput(); $this->assertEquals( $expectedItem, $parserOutput->getProperty( 'wikibase_item' ) ); $this->assertLanguageLinks( $expectedLanguageLinks, $parserOutput ); @@ -336,13 +323,6 @@ // $actualBadges contains info arrays, these are checked by LanguageLinkBadgeDisplayTest and LangLinkHandlerTest $this->assertSame( $expectedBadges, $actualBadges ); - } - - /** - * @see https://bugzilla.wikimedia.org/show_bug.cgi?id=71772 - */ - public function testOnParserAfterParse_withoutParameters() { - $this->assertTrue( ParserOutputUpdateHookHandlers::onParserAfterParse() ); } public function parserAfterParseProvider_noItem() { @@ -359,15 +339,12 @@ /** * @dataProvider parserAfterParseProvider_noItem */ - public function testDoParserAfterParse_noItem( Title $title ) { - $parser = $this->newParser( $title, array(), array() ); + public function testDoContentAlterParserOutput_noItem( Title $title ) { + $parserOutput = $this->newParserOutput( array(), array() ); $handler = $this->newParserOutputUpdateHookHandlers(); - $text = ''; - $stripState = new StripState( 'x' ); - $handler->doParserAfterParse( $parser, $text, $stripState ); + $handler->doContentAlterParserOutput( $title, $parserOutput ); - $parserOutput = $parser->getOutput(); $this->assertSame( false, $parserOutput->getProperty( 'wikibase_item' ) ); $this->assertEmpty( $parserOutput->getLanguageLinks() ); -- To view, visit https://gerrit.wikimedia.org/r/212273 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I5ee84362debadf97ea4bd256ae7db5fba663a876 Gerrit-PatchSet: 4 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: Aude <aude.w...@gmail.com> Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: Hoo man <h...@online.de> Gerrit-Reviewer: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits