jenkins-bot has submitted this change and it was merged.

Change subject: Add rel=alternate links to the html head for entities
......................................................................


Add rel=alternate links to the html head for entities

Please note: Links are cached in the ParserCache, so a
null edit/ purge is needed to make the links show up.

Bug: T96298
Change-Id: Ie570beee9ab172bf3473e8c24efc930a9538fa89
---
M repo/Wikibase.hooks.php
M repo/includes/EntityParserOutputGenerator.php
M repo/includes/EntityParserOutputGeneratorFactory.php
M repo/includes/WikibaseRepo.php
M repo/tests/phpunit/includes/EntityParserOutputGeneratorTest.php
M repo/tests/phpunit/includes/Hooks/OutputPageBeforeHTMLHookHandlerTest.php
M repo/tests/phpunit/includes/RepoHooksTest.php
M repo/tests/phpunit/includes/content/EntityContentTest.php
M repo/tests/phpunit/includes/content/ItemContentTest.php
9 files changed, 146 insertions(+), 12 deletions(-)

Approvals:
  Daniel Kinzler: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/repo/Wikibase.hooks.php b/repo/Wikibase.hooks.php
index b1c2b40..3557c36 100644
--- a/repo/Wikibase.hooks.php
+++ b/repo/Wikibase.hooks.php
@@ -897,6 +897,14 @@
                        $out->setProperty( 'wikibase-titletext', $titleText );
                }
 
+               // Array with <link rel="alternate"> tags for the page HEAD.
+               $alternateLinks = $parserOutput->getExtensionData( 
'wikibase-alternate-links' );
+               if ( $alternateLinks !== null ) {
+                       foreach ( $alternateLinks as $link ) {
+                               $out->addLink( $link );
+                       }
+               }
+
                return true;
        }
 
diff --git a/repo/includes/EntityParserOutputGenerator.php 
b/repo/includes/EntityParserOutputGenerator.php
index 60e34db..75c1624 100644
--- a/repo/includes/EntityParserOutputGenerator.php
+++ b/repo/includes/EntityParserOutputGenerator.php
@@ -6,6 +6,7 @@
 use LinkBatch;
 use ParserOptions;
 use ParserOutput;
+use SpecialPage;
 use Title;
 use Wikibase\DataModel\Entity\EntityDocument;
 use Wikibase\DataModel\Entity\EntityId;
@@ -20,6 +21,7 @@
 use Wikibase\Lib\Store\EntityInfoTermLookup;
 use Wikibase\Lib\Store\EntityTitleLookup;
 use Wikibase\Lib\Store\LanguageFallbackLabelDescriptionLookup;
+use Wikibase\Repo\LinkedData\EntityDataFormatProvider;
 use Wikibase\Repo\View\RepoSpecialPageLinker;
 use Wikibase\View\EmptyEditSectionGenerator;
 use Wikibase\View\EntityViewFactory;
@@ -85,6 +87,11 @@
         */
        private $templateFactory;
 
+       /**
+        * @var EntityDataFormatProvider
+        */
+       private $entityDataFormatProvider;
+
        public function __construct(
                EntityViewFactory $entityViewFactory,
                ParserOutputJsConfigBuilder $configBuilder,
@@ -94,7 +101,8 @@
                LanguageFallbackChain $languageFallbackChain,
                $languageCode,
                ReferencedEntitiesFinder $referencedEntitiesFinder,
-               TemplateFactory $templateFactory
+               TemplateFactory $templateFactory,
+               EntityDataFormatProvider $entityDataFormatProvider
        ) {
                $this->entityViewFactory = $entityViewFactory;
                $this->configBuilder = $configBuilder;
@@ -105,6 +113,7 @@
                $this->languageCode = $languageCode;
                $this->referencedEntitiesFinder = $referencedEntitiesFinder;
                $this->templateFactory = $templateFactory;
+               $this->entityDataFormatProvider = $entityDataFormatProvider;
        }
 
        /**
@@ -190,6 +199,8 @@
                //       So, for now, we leave it to the caller to override the 
display title, if desired.
                // set the display title
                //$parserOutput->setTitleText( $entity>getLabel( $langCode ) );
+
+               $this->addAlternateLinks( $parserOutput, $entity->getId() );
 
                return $parserOutput;
        }
@@ -397,4 +408,33 @@
                $parserOutput->addModules( 'wikibase.ui.entityViewInit' );
        }
 
+       /**
+        * Add alternate links as extension data.
+        * OutputPageBeforeHTMLHookHandler will add these to the OutputPage.
+        *
+        * @param ParserOutput $parserOutput
+        * @param EntityId $entityId
+        */
+       private function addAlternateLinks( ParserOutput $parserOutput, 
EntityId $entityId ) {
+               $entityDataFormatProvider = $this->entityDataFormatProvider;
+               $subPagePrefix = $entityId->getSerialization() . '.';
+
+               $links = array();
+
+               foreach ( $entityDataFormatProvider->getSupportedFormats() as 
$format ) {
+                       $ext = $entityDataFormatProvider->getExtension( $format 
);
+
+                       if ( $ext !== null ) {
+                               $entityDataTitle = SpecialPage::getTitleFor( 
'EntityData', $subPagePrefix . $ext );
+
+                               $links[] = array(
+                                       'rel' => 'alternate',
+                                       'href' => 
$entityDataTitle->getCanonicalURL(),
+                                       'type' => 
$entityDataFormatProvider->getMimeType( $format )
+                               );
+                       }
+               }
+
+               $parserOutput->setExtensionData( 'wikibase-alternate-links', 
$links );
+       }
 }
diff --git a/repo/includes/EntityParserOutputGeneratorFactory.php 
b/repo/includes/EntityParserOutputGeneratorFactory.php
index fe3b5e9..c3f4153 100644
--- a/repo/includes/EntityParserOutputGeneratorFactory.php
+++ b/repo/includes/EntityParserOutputGeneratorFactory.php
@@ -9,6 +9,7 @@
 use Wikibase\Lib\Store\EntityTitleLookup;
 use Wikibase\View\EntityViewFactory;
 use Wikibase\View\Template\TemplateFactory;
+use Wikibase\Repo\LinkedData\EntityDataFormatProvider;
 
 /**
  * @since 0.5
@@ -53,6 +54,11 @@
         */
        private $languageFallbackChainFactory;
 
+       /**
+        * @var EntityDataFormatProvider
+        */
+       private $entityDataFormatProvider;
+
        public function __construct(
                EntityViewFactory $entityViewFactory,
                EntityInfoBuilderFactory $entityInfoBuilderFactory,
@@ -60,7 +66,8 @@
                ValuesFinder $valuesFinder,
                LanguageFallbackChainFactory $languageFallbackChainFactory,
                ReferencedEntitiesFinder $referencedEntitiesFinder,
-               TemplateFactory $templateFactory
+               TemplateFactory $templateFactory,
+               EntityDataFormatProvider $entityDataFormatProvider
        ) {
                $this->entityViewFactory = $entityViewFactory;
                $this->entityInfoBuilderFactory = $entityInfoBuilderFactory;
@@ -69,6 +76,7 @@
                $this->languageFallbackChainFactory = 
$languageFallbackChainFactory;
                $this->referencedEntitiesFinder = $referencedEntitiesFinder;
                $this->templateFactory = $templateFactory;
+               $this->entityDataFormatProvider = $entityDataFormatProvider;
        }
 
        /**
@@ -90,7 +98,8 @@
                        $this->getLanguageFallbackChain( $languageCode ),
                        $languageCode,
                        $this->referencedEntitiesFinder,
-                       $this->templateFactory
+                       $this->templateFactory,
+                       $this->entityDataFormatProvider
                );
        }
 
diff --git a/repo/includes/WikibaseRepo.php b/repo/includes/WikibaseRepo.php
index 4194411..343ef60 100644
--- a/repo/includes/WikibaseRepo.php
+++ b/repo/includes/WikibaseRepo.php
@@ -71,6 +71,7 @@
 use Wikibase\Repo\Content\PropertyHandler;
 use Wikibase\Repo\Hooks\EditFilterHookRunner;
 use Wikibase\Repo\Interactors\RedirectCreationInteractor;
+use Wikibase\Repo\LinkedData\EntityDataFormatProvider;
 use Wikibase\Repo\Localizer\ChangeOpValidationExceptionLocalizer;
 use Wikibase\Repo\Localizer\MessageParameterFormatter;
 use Wikibase\Repo\Notifications\ChangeNotifier;
@@ -1070,6 +1071,10 @@
                        $this->getSettings()->getSetting( 'badgeItems' )
                );
 
+               $entityDataFormatProvider = new EntityDataFormatProvider();
+               $formats = $this->getSettings()->getSetting( 
'entityDataFormats' );
+               $entityDataFormatProvider->setFormatWhiteList( $formats );
+
                return new EntityParserOutputGeneratorFactory(
                        $entityViewFactory,
                        $this->getStore()->getEntityInfoBuilderFactory(),
@@ -1077,7 +1082,8 @@
                        new ValuesFinder( $this->getPropertyDataTypeLookup() ),
                        $this->getLanguageFallbackChainFactory(),
                        new ReferencedEntitiesFinder( 
$this->getLocalEntityUriParser() ),
-                       $templateFactory
+                       $templateFactory,
+                       $entityDataFormatProvider
                );
        }
 
diff --git a/repo/tests/phpunit/includes/EntityParserOutputGeneratorTest.php 
b/repo/tests/phpunit/includes/EntityParserOutputGeneratorTest.php
index 38624e1..1fa9f60 100644
--- a/repo/tests/phpunit/includes/EntityParserOutputGeneratorTest.php
+++ b/repo/tests/phpunit/includes/EntityParserOutputGeneratorTest.php
@@ -6,6 +6,7 @@
 use Language;
 use MediaWikiTestCase;
 use ParserOptions;
+use SpecialPage;
 use Title;
 use Wikibase\DataModel\Entity\BasicEntityIdParser;
 use Wikibase\DataModel\Entity\EntityId;
@@ -17,6 +18,7 @@
 use Wikibase\EntityParserOutputGenerator;
 use Wikibase\EntityRevision;
 use Wikibase\Lib\Store\Sql\SqlEntityInfoBuilderFactory;
+use Wikibase\Repo\LinkedData\EntityDataFormatProvider;
 use Wikibase\ReferencedEntitiesFinder;
 use Wikibase\ValuesFinder;
 use Wikibase\View\Template\TemplateFactory;
@@ -89,6 +91,26 @@
                        $missingOptions,
                        'Missing cache-split flags: ' . join( '|', 
$missingOptions ) . '. Options: ' . join( '|', $actualOptions )
                );
+
+               $jsonHref = SpecialPage::getTitleFor( 'EntityData', 
$item->getId()->getSerialization() . '.json' )->getCanonicalURL();
+               $ntHref = SpecialPage::getTitleFor( 'EntityData', 
$item->getId()->getSerialization() . '.nt' )->getCanonicalURL();
+
+               $this->assertEquals(
+                       array(
+                               array(
+                                       'rel' => 'alternate',
+                                       'href' => $jsonHref,
+                                       'type' => 'application/json'
+                               ),
+                               array(
+                                       'rel' => 'alternate',
+                                       'href' => $ntHref,
+                                       'type' => 'application/n-triples'
+                               )
+                       ),
+                       $parserOutput->getExtensionData( 
'wikibase-alternate-links' ),
+                       'alternate links (extension data)'
+               );
        }
 
        public function testTitleText_ItemHasNolabel() {
@@ -112,6 +134,10 @@
        private function newEntityParserOutputGenerator() {
                $templateFactory = TemplateFactory::getDefaultInstance();
                $referencedEntitiesFinder = new ReferencedEntitiesFinder( new 
BasicEntityIdParser() );
+               $entityDataFormatProvider = new EntityDataFormatProvider();
+
+               $formats = array( 'json', 'ntriples' );
+               $entityDataFormatProvider->setFormatWhiteList( $formats );
 
                return new EntityParserOutputGenerator(
                        $this->getEntityViewFactory(),
@@ -122,7 +148,8 @@
                        $this->newLanguageFallbackChain(),
                        'en',
                        $referencedEntitiesFinder,
-                       $templateFactory
+                       $templateFactory,
+                       $entityDataFormatProvider
                );
        }
 
diff --git 
a/repo/tests/phpunit/includes/Hooks/OutputPageBeforeHTMLHookHandlerTest.php 
b/repo/tests/phpunit/includes/Hooks/OutputPageBeforeHTMLHookHandlerTest.php
index c771bd8..9558d53 100644
--- a/repo/tests/phpunit/includes/Hooks/OutputPageBeforeHTMLHookHandlerTest.php
+++ b/repo/tests/phpunit/includes/Hooks/OutputPageBeforeHTMLHookHandlerTest.php
@@ -27,9 +27,9 @@
 class OutputPageBeforeHTMLHookHandlerTest extends PHPUnit_Framework_TestCase {
 
        /**
-        * Integration test mostly testing that things don't fatal/ throw.
+        * @return OutputPageBeforeHTMLHookHandler
         */
-       public function testOutputPageBeforeHTMLHookHandler() {
+       private function getHookHandler() {
                $userLanguageLookup = $this->getMock( 
'Wikibase\Lib\UserLanguageLookup' );
                $userLanguageLookup->expects( $this->once() )
                        ->method( 'getUserSpecifiedLanguages' )
@@ -50,6 +50,15 @@
                        new EntityContentFactory( array() )
                );
 
+               return $outputPageBeforeHTMLHookHandler;
+       }
+
+       /**
+        * Integration test mostly testing that things don't fatal/ throw.
+        */
+       public function testOutputPageBeforeHTMLHookHandler() {
+               $outputPageBeforeHTMLHookHandler = $this->getHookHandler();
+
                $html = '';
                $context = new DerivativeContext( RequestContext::getMain() );
                $out = new OutputPage( $context );
diff --git a/repo/tests/phpunit/includes/RepoHooksTest.php 
b/repo/tests/phpunit/includes/RepoHooksTest.php
index 22f30a3..81b9211 100644
--- a/repo/tests/phpunit/includes/RepoHooksTest.php
+++ b/repo/tests/phpunit/includes/RepoHooksTest.php
@@ -2,9 +2,12 @@
 
 namespace Wikibase\Tests;
 
-use ImportStringSource;
 use ConfigFactory;
+use DerivativeContext;
 use Exception;
+use ImportStringSource;
+use OutputPage;
+use RequestContext;
 use Wikibase\Repo\WikibaseRepo;
 use Wikibase\RepoHooks;
 use WikiImporter;
@@ -163,4 +166,35 @@
                $this->assertTrue( true ); // make PHPUnit happy
        }
 
+       public function testOnOutputPageParserOutput() {
+               $altLinks = array( array( 'a' => 'b' ), array( 'c', 'd' ) );
+
+               $context = new DerivativeContext( RequestContext::getMain() );
+               $out = new OutputPage( $context );
+
+               $parserOutput = $this->getMock( 'ParserOutput' );
+               $parserOutput->expects( $this->exactly( 3 ) )
+                       ->method( 'getExtensionData' )
+                       ->will( $this->returnCallback( function ( $key ) use ( 
$altLinks ) {
+                               if ( $key === 'wikibase-alternate-links' ) {
+                                       return $altLinks;
+                               } else {
+                                       return $key;
+                               }
+                       } ) );
+
+               RepoHooks::onOutputPageParserOutput( $out, $parserOutput );
+
+               $this->assertSame(
+                       'wikibase-view-chunks',
+                       $out->getProperty( 'wikibase-view-chunks' )
+               );
+
+               $this->assertSame(
+                       'wikibase-titletext',
+                       $out->getProperty( 'wikibase-titletext' )
+               );
+
+               $this->assertSame( $altLinks, $out->getLinkTags() );
+       }
 }
diff --git a/repo/tests/phpunit/includes/content/EntityContentTest.php 
b/repo/tests/phpunit/includes/content/EntityContentTest.php
index 13e4d14..b6338a7 100644
--- a/repo/tests/phpunit/includes/content/EntityContentTest.php
+++ b/repo/tests/phpunit/includes/content/EntityContentTest.php
@@ -151,13 +151,14 @@
 
        public function providePageProperties() {
                $cases = array();
+               $emptyContent = $this->newEmpty( $this->getDummyId() );
 
                $cases['empty'] = array(
-                       $this->newEmpty(),
+                       $emptyContent,
                        array( 'wb-status' => EntityContent::STATUS_EMPTY, 
'wb-claims' => 0 )
                );
 
-               $contentWithLabel = $this->newEmpty();
+               $contentWithLabel = $this->newEmpty( $this->getDummyId() );
                $contentWithLabel->getEntity()->setLabel( 'en', 'Foo' );
 
                $cases['labels'] = array(
diff --git a/repo/tests/phpunit/includes/content/ItemContentTest.php 
b/repo/tests/phpunit/includes/content/ItemContentTest.php
index 2c00a6b..56190ef 100644
--- a/repo/tests/phpunit/includes/content/ItemContentTest.php
+++ b/repo/tests/phpunit/includes/content/ItemContentTest.php
@@ -99,7 +99,7 @@
        public function providePageProperties() {
                $cases = parent::providePageProperties();
 
-               $contentLinkStub = ItemContent::newEmpty();
+               $contentLinkStub = $this->newEmpty( $this->getDummyId() );
                
$contentLinkStub->getEntity()->getSiteLinkList()->addNewSiteLink( 'enwiki', 
'Foo' );
 
                $cases['sitelinks'] = array(
@@ -109,7 +109,7 @@
 
                // @todo this is needed in PropertyContentTest as well
                //       once we have statements in properties
-               $contentWithClaim = $this->newEmpty();
+               $contentWithClaim = $this->newEmpty( $this->getDummyId() );
                $snak = new PropertyNoValueSnak( 83 );
                $guid = '$testing$';
                
$contentWithClaim->getEntity()->getStatements()->addNewStatement( $snak, null, 
null, $guid );

-- 
To view, visit https://gerrit.wikimedia.org/r/219001
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie570beee9ab172bf3473e8c24efc930a9538fa89
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Hoo man <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Hoo man <[email protected]>
Gerrit-Reviewer: JanZerebecki <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to