Aude has uploaded a new change for review.

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

Change subject: Get labels via ParserOutput in ViewEntityAction
......................................................................

Get labels via ParserOutput in ViewEntityAction

With this change, we no longer need EntityContent
in ViewEntityAction.

Content is only accessed if the page is being
purged or otherwise ParserOutput is not available
via ParserCache.

@todo - provide a nicer way to stash EntityId in
ParserOutput.

Change-Id: Ie5f26a7e9bbe2592429bfd9b4324e832a5a9b372
---
M repo/Wikibase.hooks.php
M repo/includes/EntityParserOutputGenerator.php
M repo/includes/actions/EditEntityAction.php
M repo/includes/actions/ViewEntityAction.php
M repo/tests/phpunit/includes/EntityParserOutputGeneratorTest.php
5 files changed, 63 insertions(+), 42 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/40/168840/4

diff --git a/repo/Wikibase.hooks.php b/repo/Wikibase.hooks.php
index e02daaa..fb44ffd 100644
--- a/repo/Wikibase.hooks.php
+++ b/repo/Wikibase.hooks.php
@@ -1081,6 +1081,12 @@
                        $out->setProperty( 'wikibase-view-chunks', 
$placeholders );
                }
 
+               $labels = $parserOutput->getExtensionData( 
'wikibase-entity-labels' );
+
+               if ( $labels ) {
+                       $out->setProperty( 'wikibase-entity-labels', $labels );
+               }
+
                return true;
        }
 
diff --git a/repo/includes/EntityParserOutputGenerator.php 
b/repo/includes/EntityParserOutputGenerator.php
index 2cecb92..379b689 100644
--- a/repo/includes/EntityParserOutputGenerator.php
+++ b/repo/includes/EntityParserOutputGenerator.php
@@ -3,6 +3,7 @@
 namespace Wikibase;
 
 use ParserOutput;
+use Wikibase\DataModel\Entity\Entity;
 use Wikibase\DataModel\Entity\Item;
 use Wikibase\DataModel\Entity\PropertyDataTypeLookup;
 use Wikibase\DataModel\SiteLinkList;
@@ -82,6 +83,7 @@
                $pout->addJsConfigVars( $configVars );
 
                $this->addSnaksToParserOutput( $pout, $entity->getAllSnaks() );
+               $this->addLabelsToParserOutput( $pout, $entity );
 
                if ( $entity instanceof Item ) {
                        $this->addBadgesToParserOutput( $pout, 
$entity->getSiteLinkList() );
@@ -137,6 +139,11 @@
                }
        }
 
+       private function addLabelsToParserOutput( ParserOutput $pout, Entity 
$entity ) {
+               $labels = $entity->getFingerprint()->getLabels()->toTextArray();
+               $pout->setExtensionData( 'wikibase-entity-labels', $labels );
+       }
+
        private function addBadgesToParserOutput( ParserOutput $pout, 
SiteLinkList $siteLinkList ) {
                foreach ( $siteLinkList as $siteLink ) {
                        foreach ( $siteLink->getBadges() as $badge ) {
diff --git a/repo/includes/actions/EditEntityAction.php 
b/repo/includes/actions/EditEntityAction.php
index db69ee3..980f9fa 100644
--- a/repo/includes/actions/EditEntityAction.php
+++ b/repo/includes/actions/EditEntityAction.php
@@ -291,7 +291,7 @@
                $this->getOutput()->setPageTitle(
                        $this->msg(
                                $restore ? 'wikibase-restore-title' : 
'wikibase-undo-title',
-                               $this->getLabelText( $latestContent ),
+                               $this->getLabelTextForContent( $latestContent ),
                                $olderRevision->getId(),
                                $newerRevision->getId()
                        )
@@ -348,7 +348,7 @@
         *
         * @return String
         */
-       public function getLabelText( EntityContent $content ) {
+       private function getLabelTextForContent( EntityContent $content ) {
                $labelData = null;
 
                // TODO: use a message like <autoredircomment> to represent the 
redirect.
diff --git a/repo/includes/actions/ViewEntityAction.php 
b/repo/includes/actions/ViewEntityAction.php
index febfd04..8fa777e 100644
--- a/repo/includes/actions/ViewEntityAction.php
+++ b/repo/includes/actions/ViewEntityAction.php
@@ -89,13 +89,7 @@
                if ( !$this->getArticle()->getPage()->exists() ) {
                        $this->displayMissingEntity();
                } else {
-                       $contentRetriever = new ContentRetriever();
-                       $content = $contentRetriever->getContentForRequest(
-                               $this->getRequest(),
-                               $this->getArticle()
-                       );
-
-                       $this->displayEntityContent( $content );
+                       $this->displayEntityPage();
                }
        }
 
@@ -119,38 +113,50 @@
        }
 
        /**
-        * Displays the entity content.
+        * Displays the entity page.
         *
         * @since 0.1
-        *
-        * @param EntityContent $content
         */
-       protected function displayEntityContent( EntityContent $content ) {
-               $outputPage = $this->getOutput();
+       protected function displayEntityPage() {
+               $this->setIsEditViewJsConfigVariable();
+
+               $this->setParserOptions();
+               $this->getArticle()->view();
+
+               $this->applyLabelTextToTitle();
+       }
+
+       private function setIsEditViewJsConfigVariable() {
                $editable = $this->isEditable();
 
                // NOTE: page-wide property, independent of user permissions
-               $outputPage->addJsConfigVars( 'wbIsEditView', $editable );
-
-               $parserOptions = 
$this->getArticle()->getPage()->makeParserOptions( 
$this->getContext()->getUser() );
-
-               $this->getArticle()->setParserOptions( $parserOptions );
-               $this->getArticle()->view();
-
-               $this->applyLabelToTitleText( $outputPage, $content );
+               $this->getOutput()->addJsConfigVars( 'wbIsEditView', $editable 
);
        }
 
-       /**
-        * @param OutputPage $outputPage
-        */
-       private function applyLabelToTitleText( OutputPage $outputPage, 
EntityContent $content ) {
-               // Figure out which label to use for title.
-               $labelText = $this->getLabelText( $content );
+       private function setParserOptions() {
+               $user = $this->getContext()->getUser();
+               $parserOptions = 
$this->getArticle()->getPage()->makeParserOptions( $user );
+
+               $this->getArticle()->setParserOptions( $parserOptions );
+       }
+
+       private function applyLabelTextToTitle() {
+               $outputPage = $this->getOutput();
+
+               // @todo store EntityId in a nice way in ParserOutput
+               $configVars = $outputPage->getJsConfigVars();
+               $titleText = isset( $configVars['wbEntityId'] ) ?: '';
+
+               $labels = $outputPage->getProperty( 'wikibase-entity-labels' );
+
+               if ( $labels ) {
+                       $titleText = $this->getLabelText( $labels ) ?: 
$titleText;
+               }
 
                if ( $this->isDiff() ) {
-                       $this->setPageTitle( $outputPage, $labelText );
+                       $this->setPageTitle( $outputPage, $titleText );
                } else {
-                       $this->setHTMLTitle( $outputPage, $labelText );
+                       $this->setHTMLTitle( $outputPage, $titleText );
                }
        }
 
@@ -184,25 +190,20 @@
        }
 
        /**
-        * @param EntityContent $content
+        * @param string[] $labels
         *
-        * @return string
+        * @return string|null
         */
-       private function getLabelText( EntityContent $content ) {
+       private function getLabelText( array $labels ) {
                // Figure out which label to use for title.
                $languageFallbackChain = $this->getLanguageFallbackChain();
-               $labelData = null;
-
-               if ( !$content->isRedirect() ) {
-                       $labels = $content->getEntity()->getLabels();
-                       $labelData = 
$languageFallbackChain->extractPreferredValueOrAny( $labels );
-               }
+               $labelData = 
$languageFallbackChain->extractPreferredValueOrAny( $labels );
 
                if ( $labelData ) {
                        return $labelData['value'];
-               } else {
-                       return $content->getEntityId()->getSerialization();
                }
+
+               return null;
        }
 
        /**
diff --git a/repo/tests/phpunit/includes/EntityParserOutputGeneratorTest.php 
b/repo/tests/phpunit/includes/EntityParserOutputGeneratorTest.php
index db4f62b..7c4b570 100644
--- a/repo/tests/phpunit/includes/EntityParserOutputGeneratorTest.php
+++ b/repo/tests/phpunit/includes/EntityParserOutputGeneratorTest.php
@@ -10,6 +10,7 @@
 use Wikibase\DataModel\Entity\PropertyId;
 use Wikibase\DataModel\Snak\PropertyValueSnak;
 use Wikibase\DataModel\Statement\StatementList;
+use Wikibase\DataModel\Term\Fingerprint;
 use Wikibase\EntityParserOutputGenerator;
 use Wikibase\EntityRevision;
 
@@ -40,6 +41,7 @@
                $this->assertEquals( self::$html, $parserOutput->getText() );
                $this->assertEquals( self::$placeholders, 
$parserOutput->getExtensionData( 'wikibase-view-chunks' ) );
                $this->assertEquals( self::$configVars, 
$parserOutput->getJsConfigVars() );
+               $this->assertEquals( array( 'en' => 'kittens' ), 
$parserOutput->getExtensionData( 'wikibase-entity-labels' ) );
 
                $this->assertEquals( array( 'http://an.url.com', 
'https://another.url.org' ), array_keys( $parserOutput->getExternalLinks() ) );
                $this->assertEquals( array( 'File:This_is_a_file.pdf', 
'File:Selfie.jpg' ), array_keys( $parserOutput->getImages() ) );
@@ -57,6 +59,11 @@
 
        private function newItem() {
                $item = Item::newEmpty();
+
+               $fingerprint = Fingerprint::newEmpty();
+               $fingerprint->setLabel( 'en', 'kittens' );
+               $item->setFingerprint( $fingerprint );
+
                $statements = new StatementList();
 
                $statements->addNewStatement( new PropertyValueSnak( 42, new 
StringValue( 'http://an.url.com' ) ) );
@@ -100,7 +107,7 @@
 
        private function getEntityTitleLookupMock() {
                $entityTitleLookup = $this->getMock( 
'Wikibase\Lib\Store\EntityTitleLookup' );
-               
+
                $entityTitleLookup->expects( $this->any() )
                        ->method( 'getTitleForId' )
                        ->will( $this->returnCallback( function( EntityId $id ) 
{

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie5f26a7e9bbe2592429bfd9b4324e832a5a9b372
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to