Daniel Kinzler has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/212273

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/LinkInjectionHookHandlers.php
M client/tests/phpunit/includes/Hooks/LinkInjectionHookHandlersTest.php
3 files changed, 28 insertions(+), 73 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/73/212273/1

diff --git a/client/WikibaseClient.php b/client/WikibaseClient.php
index 114e7b3..61eafee 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\LinkInjectionHookHandlers::onParserAfterParse';
+       $wgHooks['ContentAlterParserOutput'][]                          = 
'\Wikibase\Client\Hooks\LinkInjectionHookHandlers::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/LinkInjectionHookHandlers.php 
b/client/includes/Hooks/LinkInjectionHookHandlers.php
index cacf6ef..3eca88f 100644
--- a/client/includes/Hooks/LinkInjectionHookHandlers.php
+++ b/client/includes/Hooks/LinkInjectionHookHandlers.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/LinkInjectionHookHandlersTest.php 
b/client/tests/phpunit/includes/Hooks/LinkInjectionHookHandlersTest.php
index 4f34ccf..d6707d1 100644
--- a/client/tests/phpunit/includes/Hooks/LinkInjectionHookHandlersTest.php
+++ b/client/tests/phpunit/includes/Hooks/LinkInjectionHookHandlersTest.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;
@@ -202,23 +199,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 );
                }
@@ -226,6 +213,8 @@
                foreach ( $extensionData as $key => $value ) {
                        $parserOutput->setExtensionData( $key, $value );
                }
+
+               return $parserOutput;
        }
 
        public function testNewFromGlobalState() {
@@ -295,7 +284,7 @@
        /**
         * @dataProvider parserAfterParseProvider
         */
-       public function testDoParserAfterParse(
+       public function testDoContentAlterParserOutput(
                Title $title,
                $expectedItem,
                $pagePropsBefore,
@@ -303,10 +292,10 @@
                $expectedSisterLinks,
                $expectedBadges
        ) {
-               $parser = $this->newParser( $title, $pagePropsBefore, array() );
+               $parserOutput = $this->newParserOutput( $pagePropsBefore, 
array() );
                $handler = $this->newLinkInjectionHookHandlers();
 
-               $handler->doParserAfterParse( $parser );
+               $handler->doContentAlterParserOutput( $title, $parserOutput );
 
                $expectedUsage = array(
                        new EntityUsage(
@@ -314,8 +303,6 @@
                                EntityUsage::SITELINK_USAGE
                        )
                );
-
-               $parserOutput = $parser->getOutput();
 
                $this->assertEquals( $expectedItem, $parserOutput->getProperty( 
'wikibase_item' ) );
                $this->assertLanguageLinks( $expectedLanguageLinks, 
$parserOutput );
@@ -329,13 +316,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( 
LinkInjectionHookHandlers::onParserAfterParse() );
        }
 
        public function parserAfterParseProvider_noItem() {
@@ -352,15 +332,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->newLinkInjectionHookHandlers();
 
-               $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: newchange
Gerrit-Change-Id: I5ee84362debadf97ea4bd256ae7db5fba663a876
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <daniel.kinz...@wikimedia.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to