Thiemo Mättig (WMDE) has uploaded a new change for review.

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

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

Revert "Refactor Fingerprint code out of EntityView"

This reverts commit 4ec25c6a4adaf8936156672e6f4e7a14d4134899.

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


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

diff --git a/repo/includes/EntityView.php b/repo/includes/EntityView.php
index 14509a4..d048b95 100644
--- a/repo/includes/EntityView.php
+++ b/repo/includes/EntityView.php
@@ -12,7 +12,6 @@
 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;
@@ -30,7 +29,6 @@
  * @author H. Snater < mediawiki at snater.com >
  * @author Daniel Werner
  * @author Daniel Kinzler
- * @author Bene* < [email protected] >
  */
 abstract class EntityView extends ContextSource {
 
@@ -232,7 +230,12 @@
 
                $html = '';
 
-               $html .= $this->getHtmlForFingerprint( $entity, $editable );
+               $html .= $this->getHtmlForLabel( $entity, $editable );
+               $html .= $this->getHtmlForDescription( $entity, $editable );
+
+               $html .= wfTemplate( 'wb-entity-header-separator' );
+
+               $html .= $this->getHtmlForAliases( $entity, $editable );
                $html .= $this->getHtmlForToc();
                $html .= $this->getHtmlForTermBox( $entityRevision, $editable );
                $html .= $this->getHtmlForClaims( $entity, $editable );
@@ -242,19 +245,7 @@
        }
 
        /**
-        * 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.
+        * Builds and returns the html for the toc.
         *
         * @return string
         */
@@ -395,6 +386,128 @@
        }
 
        /**
+        * 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;
+       }
+
+       /**
         * Returns the HTML for the heading of the claims section
         *
         * @since 0.5
diff --git a/repo/includes/PropertyView.php b/repo/includes/PropertyView.php
index 8ceb012..1060bed 100644
--- a/repo/includes/PropertyView.php
+++ b/repo/includes/PropertyView.php
@@ -39,7 +39,12 @@
 
                $html = '';
 
-               $html .= $this->getHtmlForFingerprint( $property, $editable );
+               $html .= $this->getHtmlForLabel( $property, $editable );
+               $html .= $this->getHtmlForDescription( $property, $editable );
+
+               $html .= wfTemplate( 'wb-entity-header-separator' );
+
+               $html .= $this->getHtmlForAliases( $property, $editable );
                $html .= $this->getHtmlForToc();
                $html .= $this->getHtmlForTermBox( $entityRevision, $editable );
 
diff --git a/repo/includes/View/FingerprintView.php 
b/repo/includes/View/FingerprintView.php
deleted file mode 100644
index 446e002..0000000
--- a/repo/includes/View/FingerprintView.php
+++ /dev/null
@@ -1,206 +0,0 @@
-<?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* < [email protected] >
- */
-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 1f30162..bd3f1d4 100644
--- a/repo/includes/View/TermBoxView.php
+++ b/repo/includes/View/TermBoxView.php
@@ -4,6 +4,7 @@
 
 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 f275039..a3ef4d2 100644
--- a/repo/tests/phpunit/includes/EntityViewTest.php
+++ b/repo/tests/phpunit/includes/EntityViewTest.php
@@ -506,6 +506,54 @@
                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
deleted file mode 100644
index fb64673..0000000
--- a/repo/tests/phpunit/includes/View/FingerprintViewTest.php
+++ /dev/null
@@ -1,90 +0,0 @@
-<?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* < [email protected] >
- */
-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/154241
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id7e37044f460860d0aa35c47ab1e0b6ab8295a23
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>

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

Reply via email to