Jeroen De Dauw has submitted this change and it was merged.

Change subject: Refactor Fingerprint code out of EntityView
......................................................................


Refactor Fingerprint code out of EntityView

All code related to fingerprint is moved into a separate FingerprintView
which only knows about a fingerprint and an entity id. This helps us
getting rid of EntityView at one point.

Change-Id: Ib932282be071c20455045f32e60f0244c4623980
---
M repo/includes/EntityView.php
M repo/includes/PropertyView.php
A repo/includes/View/FingerprintView.php
M repo/includes/View/TermBoxView.php
M repo/tests/phpunit/includes/EntityViewTest.php
A repo/tests/phpunit/includes/View/FingerprintViewTest.php
6 files changed, 313 insertions(+), 184 deletions(-)

Approvals:
  WikidataJenkins: Verified
  Jeroen De Dauw: Looks good to me, approved



diff --git a/repo/includes/EntityView.php b/repo/includes/EntityView.php
index d048b95..14509a4 100644
--- a/repo/includes/EntityView.php
+++ b/repo/includes/EntityView.php
@@ -12,6 +12,7 @@
 use Wikibase\Lib\Serializers\SerializationOptions;
 use Wikibase\Lib\SnakFormatter;
 use Wikibase\Lib\Store\EntityInfoBuilderFactory;
+use Wikibase\Repo\View\FingerprintView;
 use Wikibase\Repo\View\SectionEditLinkGenerator;
 use Wikibase\Repo\View\SnakHtmlGenerator;
 use Wikibase\Repo\View\TextInjector;
@@ -29,6 +30,7 @@
  * @author H. Snater < mediawiki at snater.com >
  * @author Daniel Werner
  * @author Daniel Kinzler
+ * @author Bene* < benestar.wikime...@gmail.com >
  */
 abstract class EntityView extends ContextSource {
 
@@ -230,12 +232,7 @@
 
                $html = '';
 
-               $html .= $this->getHtmlForLabel( $entity, $editable );
-               $html .= $this->getHtmlForDescription( $entity, $editable );
-
-               $html .= wfTemplate( 'wb-entity-header-separator' );
-
-               $html .= $this->getHtmlForAliases( $entity, $editable );
+               $html .= $this->getHtmlForFingerprint( $entity, $editable );
                $html .= $this->getHtmlForToc();
                $html .= $this->getHtmlForTermBox( $entityRevision, $editable );
                $html .= $this->getHtmlForClaims( $entity, $editable );
@@ -245,7 +242,19 @@
        }
 
        /**
-        * Builds and returns the html for the toc.
+        * Builds and returns the HTML for the entity's fingerprint.
+        *
+        * @param Entity $entity
+        * @param bool $editable
+        * @return string
+        */
+       protected function getHtmlForFingerprint( Entity $entity, $editable = 
true ) {
+               $fingerprintView = new FingerprintView( 
$this->sectionEditLinkGenerator, $this->getLanguage()->getCode() );
+               return $fingerprintView->getHtml( $entity->getFingerprint(), 
$entity->getId(), $editable );
+       }
+
+       /**
+        * Builds and returns the HTML for the toc.
         *
         * @return string
         */
@@ -383,128 +392,6 @@
 
                wfProfileOut( __METHOD__ );
                return $pout;
-       }
-
-       /**
-        * Builds and returns the HTML representing a WikibaseEntity's label.
-        *
-        * @since 0.1
-        *
-        * @param Entity $entity the entity to render
-        * @param bool $editable whether editing is allowed (enabled edit links)
-        * @return string
-        */
-       public function getHtmlForLabel( Entity $entity, $editable = true ) {
-               wfProfileIn( __METHOD__ );
-
-               $languageCode = $this->getLanguage()->getCode();
-               $label = $entity->getLabel( $languageCode );
-               $entityId = $entity->getId();
-               $idString = 'new';
-               $supplement = '';
-
-               if ( $entityId !== null ) {
-                       $idString = $entityId->getSerialization();
-                       $supplement .= wfTemplate( 
'wb-property-value-supplement', wfMessage( 'parentheses', $idString ) );
-                       if ( $editable ) {
-                               $supplement .= $this->getHtmlForEditSection( 
'SetLabel', array( $idString, $languageCode ) );
-                       }
-               }
-
-               $html = wfTemplate( 'wb-label',
-                       $idString,
-                       wfTemplate( 'wb-property',
-                               $label === false ? 'wb-value-empty' : '',
-                               htmlspecialchars( $label === false ? wfMessage( 
'wikibase-label-empty' )->text() : $label ),
-                               $supplement
-                       )
-               );
-
-               wfProfileOut( __METHOD__ );
-               return $html;
-       }
-
-       /**
-        * Builds and returns the HTML representing a WikibaseEntity's 
description.
-        *
-        * @since 0.1
-        *
-        * @param Entity $entity the entity to render
-        * @param bool $editable whether editing is allowed (enabled edit links)
-        * @return string
-        */
-       public function getHtmlForDescription( Entity $entity, $editable = true 
) {
-               wfProfileIn( __METHOD__ );
-
-               $languageCode = $this->getLanguage()->getCode();
-               $description = $entity->getDescription( $languageCode );
-               $entityId = $entity->getId();
-               $editSection = '';
-
-               if ( $entityId !== null && $editable ) {
-                       $idString = $entityId->getSerialization();
-                       $editSection .= $this->getHtmlForEditSection( 
'SetDescription', array( $idString, $languageCode ) );
-               }
-
-               $html = wfTemplate( 'wb-description',
-                       wfTemplate( 'wb-property',
-                               $description === false ? 'wb-value-empty' : '',
-                               htmlspecialchars( $description === false ? 
wfMessage( 'wikibase-description-empty' )->text() : $description ),
-                               $editSection
-                       )
-               );
-
-               wfProfileOut( __METHOD__ );
-               return $html;
-       }
-
-       /**
-        * Builds and returns the HTML representing a WikibaseEntity's aliases.
-        *
-        * @since 0.1
-        *
-        * @param Entity $entity the entity to render
-        * @param bool $editable whether editing is allowed (enabled edit links)
-        * @return string
-        */
-       public function getHtmlForAliases( Entity $entity, $editable = true ) {
-               wfProfileIn( __METHOD__ );
-
-               $languageCode = $this->getLanguage()->getCode();
-               $aliases = $entity->getAliases( $languageCode );
-               $entityId = $entity->getId();
-               $editSection = '';
-
-               if ( $entityId !== null && $editable ) {
-                       $idString = $entityId->getSerialization();
-                       $action = empty( $aliases ) ? 'add' : 'edit';
-                       $editSection = $this->getHtmlForEditSection( 
'SetAliases', array( $idString, $languageCode ), $action );
-               }
-
-               if ( empty( $aliases ) ) {
-                       $html = wfTemplate( 'wb-aliases-wrapper',
-                               'wb-aliases-empty',
-                               'wb-value-empty',
-                               wfMessage( 'wikibase-aliases-empty' )->text(),
-                               $editSection
-                       );
-               } else {
-                       $aliasesHtml = '';
-                       foreach ( $aliases as $alias ) {
-                               $aliasesHtml .= wfTemplate( 'wb-alias', 
htmlspecialchars( $alias ) );
-                       }
-                       $aliasList = wfTemplate( 'wb-aliases', $aliasesHtml );
-
-                       $html = wfTemplate( 'wb-aliases-wrapper',
-                               '',
-                               '',
-                               wfMessage( 'wikibase-aliases-label' )->text(),
-                               $aliasList . $editSection
-                       );
-               }
-
-               wfProfileOut( __METHOD__ );
-               return $html;
        }
 
        /**
diff --git a/repo/includes/PropertyView.php b/repo/includes/PropertyView.php
index 1060bed..8ceb012 100644
--- a/repo/includes/PropertyView.php
+++ b/repo/includes/PropertyView.php
@@ -39,12 +39,7 @@
 
                $html = '';
 
-               $html .= $this->getHtmlForLabel( $property, $editable );
-               $html .= $this->getHtmlForDescription( $property, $editable );
-
-               $html .= wfTemplate( 'wb-entity-header-separator' );
-
-               $html .= $this->getHtmlForAliases( $property, $editable );
+               $html .= $this->getHtmlForFingerprint( $property, $editable );
                $html .= $this->getHtmlForToc();
                $html .= $this->getHtmlForTermBox( $entityRevision, $editable );
 
diff --git a/repo/includes/View/FingerprintView.php 
b/repo/includes/View/FingerprintView.php
new file mode 100644
index 0000000..446e002
--- /dev/null
+++ b/repo/includes/View/FingerprintView.php
@@ -0,0 +1,206 @@
+<?php
+
+namespace Wikibase\Repo\View;
+
+use Message;
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Term\Fingerprint;
+use Wikibase\DataModel\Term\AliasGroupList;
+use Wikibase\DataModel\Term\TermList;
+
+/**
+ * Generates HTML to display the fingerprint of an entity
+ * in the user's current language.
+ *
+ * @since 0.5
+ * @licence GNU GPL v2+
+ *
+ * @author Bene* < benestar.wikime...@gmail.com >
+ */
+class FingerprintView {
+
+       /**
+        * @var SectionEditLinkGenerator
+        */
+       private $sectionEditLinkGenerator;
+
+       /**
+        * @var string
+        */
+       private $languageCode;
+
+       /**
+        * @param SectionEditLinkGenerator $sectionEditLinkGenerator
+        * @param string $languageCode
+        */
+       public function __construct( SectionEditLinkGenerator 
$sectionEditLinkGenerator, $languageCode ) {
+               $this->sectionEditLinkGenerator = $sectionEditLinkGenerator;
+               $this->languageCode = $languageCode;
+       }
+
+       /**
+        * Builds and returns the HTML representing a fingerprint.
+        *
+        * @since 0.5
+        *
+        * @param Fingerprint $fingerprint the fingerprint to render
+        * @param EntityId|null $entityId the id of the fingerprint's entity
+        * @param bool $editable whether editing is allowed (enabled edit links)
+        * @return string
+        */
+       public function getHtml( Fingerprint $fingerprint, EntityId $entityId = 
null, $editable = true ) {
+               $labels = $fingerprint->getLabels();
+               $descriptions = $fingerprint->getDescriptions();
+               $aliasGroups = $fingerprint->getAliasGroups();
+
+               $html = '';
+
+               if ( $entityId !== null ) {
+                       $serializedId = $entityId->getSerialization();
+                       $html .= wfTemplate( 'wb-property-value-supplement', 
wfMessage( 'parentheses', $serializedId ) );
+               }
+
+               $html .= $this->getHtmlForLabel( $labels, $entityId, $editable 
);
+               $html .= $this->getHtmlForDescription( $descriptions, 
$entityId, $editable );
+               $html .= wfTemplate( 'wb-entity-header-separator' );
+               $html .= $this->getHtmlForAliases( $aliasGroups, $entityId, 
$editable );
+
+               return $html;
+       }
+
+       /**
+        * Builds and returns the HTML for the edit section.
+        *
+        * @param Message $message
+        * @param string $specialPageName
+        * @param EntityId|null $entityId
+        * @param bool $editable
+        * @return string
+        */
+       private function getHtmlForEditSection( Message $message, 
$specialPageName, EntityId $entityId = null, $editable = true ) {
+               if ( $entityId !== null && $editable ) {
+                       return 
$this->sectionEditLinkGenerator->getHtmlForEditSection(
+                               $specialPageName,
+                               array( $entityId->getSerialization(), 
$this->languageCode ),
+                               $message
+                       );
+               } else {
+                       return '';
+               }
+       }
+
+       /**
+        * Builds and returns the HTML representing a label.
+        *
+        * @param TermList $labels the list of labels to render
+        * @param EntityId|null $entityId the id of the fingerprint's entity
+        * @param bool $editable whether editing is allowed (enabled edit links)
+        * @return string
+        */
+       private function getHtmlForLabel( TermList $labels, EntityId $entityId 
= null, $editable = true ) {
+               $labelExists = $labels->hasTermForLanguage( $this->languageCode 
);
+               $editSection = $this->getHtmlForEditSection( wfMessage( 
'wikibase-edit' ), 'SetLabel', $entityId, $editable );
+               $idString = $entityId === null ? 'new' : 
$entityId->getSerialization();
+
+               if ( $labelExists ) {
+                       return $this->getLabelWrapperHTML(
+                               $idString,
+                               $labels->getByLanguage( $this->languageCode 
)->getText(),
+                               $editSection
+                       );
+               } else {
+                       return $this->getLabelWrapperHTML(
+                               $idString,
+                               wfMessage( 'wikibase-label-empty' )->text(),
+                               $editSection
+                       );
+               }
+       }
+
+       private function getLabelWrapperHTML( $idString, $content, $editSection 
) {
+               return wfTemplate( 'wb-label',
+                       $idString,
+                       wfTemplate( 'wb-property',
+                               '',
+                               htmlspecialchars( $content ),
+                               $editSection
+                       )
+               );
+       }
+
+       /**
+        * Builds and returns the HTML representing a description.
+        *
+        * @param TermList $descriptions the list of descriptions to render
+        * @param EntityId|null $entityId the id of the fingerprint's entity
+        * @param bool $editable whether editing is allowed (enabled edit links)
+        * @return string
+        */
+       private function getHtmlForDescription( TermList $descriptions, 
EntityId $entityId = null, $editable = true ) {
+               $descriptionExists = $descriptions->hasTermForLanguage( 
$this->languageCode );
+               $editSection = $this->getHtmlForEditSection( wfMessage( 
'wikibase-edit' ), 'SetDescription', $entityId, $editable );
+
+               if ( $descriptionExists ) {
+                       return $this->getDescriptionWrapperHTML(
+                               $descriptions->getByLanguage( 
$this->languageCode )->getText(),
+                               $editSection
+                       );
+               } else {
+                       return $this->getDescriptionWrapperHTML(
+                               wfMessage( 'wikibase-label-empty' )->text(),
+                               $editSection
+                       );
+               }
+       }
+
+       private function getDescriptionWrapperHTML( $content, $editSection ) {
+               return wfTemplate( 'wb-description',
+                       wfTemplate( 'wb-property',
+                               'wb-value-empty',
+                               htmlspecialchars( $content ),
+                               $editSection
+                       )
+               );
+       }
+
+       /**
+        * Builds and returns the HTML representing aliases.
+        *
+        * @param AliasGroupList $aliasGroups the list of alias groups to render
+        * @param EntityId|null $entityId the id of the fingerprint's entity
+        * @param bool $editable whether editing is allowed (enabled edit links)
+        * @return string
+        */
+       private function getHtmlForAliases( AliasGroupList $aliasGroups, 
EntityId $entityId = null, $editable = true ) {
+               $aliasesExist = $aliasGroups->hasGroupForLanguage( 
$this->languageCode );
+               $message = wfMessage( 'wikibase-' . $aliasesExist ? 'edit' : 
'add' );
+               $editSection = $this->getHtmlForEditSection( $message, 
'SetAliases', $entityId, $editable );
+
+               if ( $aliasesExist ) {
+                       $aliasList = $this->getAliasListHTML( 
$aliasGroups->getByLanguage( $this->languageCode )->getAliases() );
+
+                       return wfTemplate( 'wb-aliases-wrapper',
+                               '',
+                               '',
+                               wfMessage( 'wikibase-aliases-label' )->text(),
+                               $aliasList . $editSection
+                       );
+               } else {
+                       return wfTemplate( 'wb-aliases-wrapper',
+                               'wb-aliases-empty',
+                               'wb-value-empty',
+                               wfMessage( 'wikibase-aliases-empty' )->text(),
+                               $editSection
+                       );
+               }
+       }
+
+       private function getAliasListHTML( array $aliases ) {
+               $aliasesHtml = '';
+               foreach ( $aliases as $alias ) {
+                       $aliasesHtml .= wfTemplate( 'wb-alias', 
htmlspecialchars( $alias ) );
+               }
+               return wfTemplate( 'wb-aliases', $aliasesHtml );
+       }
+
+}
diff --git a/repo/includes/View/TermBoxView.php 
b/repo/includes/View/TermBoxView.php
index bd3f1d4..1f30162 100644
--- a/repo/includes/View/TermBoxView.php
+++ b/repo/includes/View/TermBoxView.php
@@ -4,7 +4,6 @@
 
 use Language;
 use Message;
-use SpecialPage;
 use Title;
 use Wikibase\DataModel\Entity\Entity;
 use Wikibase\Utils;
diff --git a/repo/tests/phpunit/includes/EntityViewTest.php 
b/repo/tests/phpunit/includes/EntityViewTest.php
index a3ef4d2..f275039 100644
--- a/repo/tests/phpunit/includes/EntityViewTest.php
+++ b/repo/tests/phpunit/includes/EntityViewTest.php
@@ -506,54 +506,6 @@
                return $argLists;
        }
 
-       public function testGetHtmlForLabel_editable() {
-               $entity = $this->makeEntity( $this->makeEntityId( 1 ) );
-               $entityView = $this->newEntityView( $entity->getType() );
-               $html = $entityView->getHtmlForLabel( $entity );
-
-               $this->assertRegExp( '@<a 
href="[^"]*\bSpecial:SetLabel/\w1/en"[^>]*>\S+</a>@', $html );
-       }
-
-       public function testGetHtmlForLabel_notEditable() {
-               $entity = $this->makeEntity( $this->makeEntityId( 1 ) );
-               $entityView = $this->newEntityView( $entity->getType() );
-               $html = $entityView->getHtmlForLabel( $entity, false );
-
-               $this->assertNotContains( '<a ', $html );
-       }
-
-       public function testGetHtmlForDescription_editable() {
-               $entity = $this->makeEntity( $this->makeEntityId( 1 ) );
-               $entityView = $this->newEntityView( $entity->getType() );
-               $html = $entityView->getHtmlForDescription( $entity );
-
-               $this->assertRegExp( '@<a 
href="[^"]*\bSpecial:SetDescription/\w1/en"[^>]*>\S+</a>@', $html );
-       }
-
-       public function testGetHtmlForDescription_notEditable() {
-               $entity = $this->makeEntity( $this->makeEntityId( 1 ) );
-               $entityView = $this->newEntityView( $entity->getType() );
-               $html = $entityView->getHtmlForDescription( $entity, false );
-
-               $this->assertNotContains( '<a ', $html );
-       }
-
-       public function testGetHtmlForAliases_editable() {
-               $entity = $this->makeEntity( $this->makeEntityId( 1 ) );
-               $entityView = $this->newEntityView( $entity->getType() );
-               $html = $entityView->getHtmlForAliases( $entity );
-
-               $this->assertRegExp( '@<a 
href="[^"]*\bSpecial:SetAliases/\w1/en"[^>]*>\S+</a>@', $html );
-       }
-
-       public function testGetHtmlForAliases_notEditable() {
-               $entity = $this->makeEntity( $this->makeEntityId( 1 ) );
-               $entityView = $this->newEntityView( $entity->getType() );
-               $html = $entityView->getHtmlForAliases( $entity, false );
-
-               $this->assertNotContains( '<a ', $html );
-       }
-
        /**
         * @return Entity
         */
diff --git a/repo/tests/phpunit/includes/View/FingerprintViewTest.php 
b/repo/tests/phpunit/includes/View/FingerprintViewTest.php
new file mode 100644
index 0000000..fb64673
--- /dev/null
+++ b/repo/tests/phpunit/includes/View/FingerprintViewTest.php
@@ -0,0 +1,90 @@
+<?php
+
+namespace Wikibase\Test;
+
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Term\AliasGroup;
+use Wikibase\DataModel\Term\Fingerprint;
+use Wikibase\DataModel\Term\Term;
+use Wikibase\Repo\View\FingerprintView;
+use Wikibase\Repo\View\SectionEditLinkGenerator;
+
+/**
+ * @covers Wikibase\Repo\View\FingerprintView
+ *
+ * @group Wikibase
+ * @group WikibaseRepo
+ *
+ * @licence GNU GPL v2+
+ * @author Bene* < benestar.wikime...@gmail.com >
+ */
+class FingerprintViewTest extends \PHPUnit_Framework_TestCase {
+
+       public function provideTestGetHtml() {
+               $cases = array();
+
+               $fingerprint = Fingerprint::newEmpty();
+
+               $cases['empty fingerprint'] = array(
+                       $fingerprint,
+                       new ItemId( 'Q42' ),
+                       'en'
+               );
+
+               $fingerprint->setLabel( new Term( 'en', 'Foobar' ) );
+               $fingerprint->setDescription( new Term( 'en', 'This is a foo 
bar.' ) );
+               $fingerprint->setAliasGroup( new AliasGroup( 'en', array( 
'foo', 'bar' ) ) );
+
+               $cases['empty fingerprint'] = array(
+                       $fingerprint,
+                       new ItemId( 'Q42' ),
+                       'en'
+               );
+
+               $cases['other language'] = array(
+                       $fingerprint,
+                       new ItemId( 'Q42' ),
+                       'de'
+               );
+
+               $cases['other item id'] = array(
+                       $fingerprint,
+                       new ItemId( 'Q12' ),
+                       'en'
+               );
+
+               return $cases;
+       }
+
+       /**
+        * @dataProvider provideTestGetHtml
+        */
+       public function testGetHtmlEditable( Fingerprint $fingerprint, EntityId 
$entityId, $languageCode ) {
+               $fingerprintView = new FingerprintView( new 
SectionEditLinkGenerator(), $languageCode );
+               $html = $fingerprintView->getHtml( $fingerprint, $entityId, 
true );
+               $serializedId = $entityId->getSerialization();
+
+               $this->assertRegExp( '@<a href="[^"]*\bSpecial:SetLabel/' . 
$serializedId . '/' . $languageCode . '"[^>]*>\S+</a>@', $html );
+               $this->assertRegExp( '@<a href="[^"]*\bSpecial:SetDescription/' 
. $serializedId . '/' . $languageCode . '"[^>]*>\S+</a>@', $html );
+               $this->assertRegExp( '@<a href="[^"]*\bSpecial:SetAliases/' . 
$serializedId . '/' . $languageCode . '"[^>]*>\S+</a>@', $html );
+       }
+
+       /**
+        * @dataProvider provideTestGetHtml
+        */
+       public function testGetHtmlNotEditable( Fingerprint $fingerprint, 
EntityId $entityId, $languageCode ) {
+               $fingerprintView = new FingerprintView( new 
SectionEditLinkGenerator(), $languageCode );
+               $html = $fingerprintView->getHtml( $fingerprint, $entityId, 
false );
+
+               $this->assertNotContains( '<a ', $html );
+       }
+
+       public function testGetHtmlNoEntityId() {
+               $fingerprintView = new FingerprintView( new 
SectionEditLinkGenerator(), 'en' );
+               $html = $fingerprintView->getHtml( Fingerprint::newEmpty(), 
null, true );
+
+               $this->assertNotContains( '<a ', $html );
+       }
+
+}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ib932282be071c20455045f32e60f0244c4623980
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Bene <benestar.wikime...@gmail.com>
Gerrit-Reviewer: Addshore <addshorew...@gmail.com>
Gerrit-Reviewer: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Bene <benestar.wikime...@gmail.com>
Gerrit-Reviewer: Hoo man <h...@online.de>
Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de>
Gerrit-Reviewer: WikidataJenkins <wikidata-servi...@wikimedia.de>
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