Hoo man has uploaded a new change for review.

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

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 so up.

Bug: T96298
Change-Id: Ie570beee9ab172bf3473e8c24efc930a9538fa89
---
M repo/Wikibase.hooks.php
M repo/includes/EntityParserOutputGenerator.php
M repo/includes/EntityParserOutputGeneratorFactory.php
M repo/includes/Hooks/OutputPageBeforeHTMLHookHandler.php
M repo/includes/WikibaseRepo.php
M repo/tests/phpunit/includes/EntityParserOutputGeneratorTest.php
M repo/tests/phpunit/includes/Hooks/OutputPageBeforeHTMLHookHandlerTest.php
7 files changed, 128 insertions(+), 7 deletions(-)


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

diff --git a/repo/Wikibase.hooks.php b/repo/Wikibase.hooks.php
index b1c2b40..6b68e8b 100644
--- a/repo/Wikibase.hooks.php
+++ b/repo/Wikibase.hooks.php
@@ -897,6 +897,12 @@
                        $out->setProperty( 'wikibase-titletext', $titleText );
                }
 
+               // Array with <link rel="alternate"> tags for the page HEAD.
+               $alternateLinks = $parserOutput->getExtensionData( 
'wikibase-alternate-links' );
+               if ( $alternateLinks !== null ) {
+                       $out->setProperty( 'wikibase-alternate-links', 
$alternateLinks );
+               }
+
                return true;
        }
 
diff --git a/repo/includes/EntityParserOutputGenerator.php 
b/repo/includes/EntityParserOutputGenerator.php
index 60e34db..be67ee0 100644
--- a/repo/includes/EntityParserOutputGenerator.php
+++ b/repo/includes/EntityParserOutputGenerator.php
@@ -20,6 +20,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 +86,16 @@
         */
        private $templateFactory;
 
+       /**
+        * @var EntityDataFormatProvider
+        */
+       private $entityDataFormatProvider;
+
+       /**
+        * @var string
+        */
+       private $conceptBaseUri;
+
        public function __construct(
                EntityViewFactory $entityViewFactory,
                ParserOutputJsConfigBuilder $configBuilder,
@@ -94,7 +105,9 @@
                LanguageFallbackChain $languageFallbackChain,
                $languageCode,
                ReferencedEntitiesFinder $referencedEntitiesFinder,
-               TemplateFactory $templateFactory
+               TemplateFactory $templateFactory,
+               EntityDataFormatProvider $entityDataFormatProvider,
+               $conceptBaseUri
        ) {
                $this->entityViewFactory = $entityViewFactory;
                $this->configBuilder = $configBuilder;
@@ -105,6 +118,8 @@
                $this->languageCode = $languageCode;
                $this->referencedEntitiesFinder = $referencedEntitiesFinder;
                $this->templateFactory = $templateFactory;
+               $this->entityDataFormatProvider = $entityDataFormatProvider;
+               $this->conceptBaseUri = $conceptBaseUri;
        }
 
        /**
@@ -190,6 +205,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 +414,30 @@
                $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;
+               $baseUri = $this->conceptBaseUri;
+               $links = array();
+
+               foreach ( $entityDataFormatProvider->getSupportedFormats() as 
$format ) {
+                       $ext = $entityDataFormatProvider->getExtension( $format 
);
+
+                       if ( $ext !== null ) {
+                               $links[] = array(
+                                       'rel' => 'alternate',
+                                       'href' => $baseUri . 
$entityId->getSerialization() . '.' . $ext,
+                                       'type' => 
$entityDataFormatProvider->getMimeType( $format )
+                               );
+                       }
+               }
+
+               $parserOutput->setExtensionData( 'wikibase-alternate-links', 
$links );
+       }
 }
diff --git a/repo/includes/EntityParserOutputGeneratorFactory.php 
b/repo/includes/EntityParserOutputGeneratorFactory.php
index fe3b5e9..9e013bb 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,16 @@
         */
        private $languageFallbackChainFactory;
 
+       /**
+        * @var EntityDataFormatProvider
+        */
+       private $entityDataFormatProvider;
+
+       /**
+        * @var string
+        */
+       private $conceptBaseUri;
+
        public function __construct(
                EntityViewFactory $entityViewFactory,
                EntityInfoBuilderFactory $entityInfoBuilderFactory,
@@ -60,7 +71,9 @@
                ValuesFinder $valuesFinder,
                LanguageFallbackChainFactory $languageFallbackChainFactory,
                ReferencedEntitiesFinder $referencedEntitiesFinder,
-               TemplateFactory $templateFactory
+               TemplateFactory $templateFactory,
+               EntityDataFormatProvider $entityDataFormatProvider,
+               $conceptBaseUri
        ) {
                $this->entityViewFactory = $entityViewFactory;
                $this->entityInfoBuilderFactory = $entityInfoBuilderFactory;
@@ -69,6 +82,8 @@
                $this->languageFallbackChainFactory = 
$languageFallbackChainFactory;
                $this->referencedEntitiesFinder = $referencedEntitiesFinder;
                $this->templateFactory = $templateFactory;
+               $this->entityDataFormatProvider = $entityDataFormatProvider;
+               $this->conceptBaseUri = $conceptBaseUri;
        }
 
        /**
@@ -90,7 +105,9 @@
                        $this->getLanguageFallbackChain( $languageCode ),
                        $languageCode,
                        $this->referencedEntitiesFinder,
-                       $this->templateFactory
+                       $this->templateFactory,
+                       $this->entityDataFormatProvider,
+                       $this->conceptBaseUri
                );
        }
 
diff --git a/repo/includes/Hooks/OutputPageBeforeHTMLHookHandler.php 
b/repo/includes/Hooks/OutputPageBeforeHTMLHookHandler.php
index c0eae09..ad4fe1f 100644
--- a/repo/includes/Hooks/OutputPageBeforeHTMLHookHandler.php
+++ b/repo/includes/Hooks/OutputPageBeforeHTMLHookHandler.php
@@ -149,6 +149,13 @@
                                ) )
                        );
                }
+
+               $alternateLinks = $out->getProperty( 'wikibase-alternate-links' 
);
+               if ( !empty( $alternateLinks ) ) {
+                       foreach ( $alternateLinks as $link ) {
+                               $out->addLink( $link );
+                       }
+               }
        }
 
        /**
diff --git a/repo/includes/WikibaseRepo.php b/repo/includes/WikibaseRepo.php
index 4194411..6adec25 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,9 @@
                        new ValuesFinder( $this->getPropertyDataTypeLookup() ),
                        $this->getLanguageFallbackChainFactory(),
                        new ReferencedEntitiesFinder( 
$this->getLocalEntityUriParser() ),
-                       $templateFactory
+                       $templateFactory,
+                       $entityDataFormatProvider,
+                       $this->getSettings()->getSetting( 'conceptBaseUri' )
                );
        }
 
diff --git a/repo/tests/phpunit/includes/EntityParserOutputGeneratorTest.php 
b/repo/tests/phpunit/includes/EntityParserOutputGeneratorTest.php
index 38624e1..e5148b7 100644
--- a/repo/tests/phpunit/includes/EntityParserOutputGeneratorTest.php
+++ b/repo/tests/phpunit/includes/EntityParserOutputGeneratorTest.php
@@ -17,6 +17,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 +90,23 @@
                        $missingOptions,
                        'Missing cache-split flags: ' . join( '|', 
$missingOptions ) . '. Options: ' . join( '|', $actualOptions )
                );
+
+               $this->assertEquals(
+                       array(
+                               array(
+                                       'rel' => 'alternate',
+                                       'href' => 'conceptURI/' . 
$item->getId()->getSerialization() . '.json',
+                                       'type' => 'application/json'
+                               ),
+                               array(
+                                       'rel' => 'alternate',
+                                       'href' => 'conceptURI/' . 
$item->getId()->getSerialization() . '.nt',
+                                       'type' => 'application/n-triples'
+                               )
+                       ),
+                       $parserOutput->getExtensionData( 
'wikibase-alternate-links' ),
+                       'alternate links (extension data)'
+               );
        }
 
        public function testTitleText_ItemHasNolabel() {
@@ -112,6 +130,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 +144,9 @@
                        $this->newLanguageFallbackChain(),
                        'en',
                        $referencedEntitiesFinder,
-                       $templateFactory
+                       $templateFactory,
+                       $entityDataFormatProvider,
+                       'conceptURI/'
                );
        }
 
diff --git 
a/repo/tests/phpunit/includes/Hooks/OutputPageBeforeHTMLHookHandlerTest.php 
b/repo/tests/phpunit/includes/Hooks/OutputPageBeforeHTMLHookHandlerTest.php
index c771bd8..004e36d 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 );
@@ -59,6 +68,12 @@
                        array( array( 
'entityViewPlaceholder-entitytermsview-entitytermsforlanguagelistview-class' ) )
                );
 
+               $alternateLinks = array( array( 'a' => 'b' ), array( 'c', 'd' ) 
);
+               $out->setProperty(
+                       'wikibase-alternate-links',
+                       $alternateLinks
+               );
+
                $outputPageBeforeHTMLHookHandler->doOutputPageBeforeHTML( $out, 
$html );
 
                // Verify the wbUserSpecifiedLanguages JS variable
@@ -66,5 +81,7 @@
                $wbUserSpecifiedLanguages = 
$jsConfigVars['wbUserSpecifiedLanguages'];
 
                $this->assertSame( array( 'es', 'ru' ), 
$wbUserSpecifiedLanguages );
+
+               $this->assertSame( $alternateLinks, $out->getLinkTags() );
        }
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie570beee9ab172bf3473e8c24efc930a9538fa89
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Hoo man <[email protected]>

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

Reply via email to