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

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

Change subject: Introduce and start using StatementSectionsView
......................................................................

Introduce and start using StatementSectionsView

This patch introduces a new view. This new view renders the "Statements"
heading and the statements below via the existing StatementGroupListView.
StatementGroupListView does *not* render the heading any more.

Note that there was no test for the heading before. We changed the
StatementGroupListView tests along with the new section view tests.

This patch is "incomplete" on purpose to make it small and easy to review.
We will continue working on the TODOs in the next patch.

Bug: T118949
Change-Id: Iac913191cdd980ed8f9659a3bcb075add6cdef39
---
M repo/i18n/en.json
M repo/i18n/qqq.json
M view/src/ItemView.php
M view/src/PropertyView.php
M view/src/StatementGroupListView.php
A view/src/StatementSectionsView.php
M view/tests/phpunit/StatementGroupListViewTest.php
A view/tests/phpunit/StatementSectionsViewTest.php
8 files changed, 217 insertions(+), 47 deletions(-)


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

diff --git a/repo/i18n/en.json b/repo/i18n/en.json
index cea37cd..1d0cafb 100644
--- a/repo/i18n/en.json
+++ b/repo/i18n/en.json
@@ -60,7 +60,7 @@
        "wikibase-description-input-help-message": "Enter a short description 
for this entity in $1.",
        "wikibase-entitytermsview-input-help-message": "Enter a label of this 
entity, a short description and aliases per language.",
        "wikibase-entitytermsforlanguageview-input-help-message": "Enter the 
label of this entity, a short description and aliases in $1.",
-       "wikibase-statements": "Statements",
+       "wikibase-statementsection-statements": "Statements",
        "wikibase-sitelinks": "Sitelinks",
        "wikibase-sitelinkgroupview-input-help-message": "Add a sitelink by 
specifying a site and a page of that site, edit or remove existing sitelinks.",
        "wikibase-sitelinks-empty": "No page is linked to this item.",
diff --git a/repo/i18n/qqq.json b/repo/i18n/qqq.json
index c815d0d..9434f73 100644
--- a/repo/i18n/qqq.json
+++ b/repo/i18n/qqq.json
@@ -89,7 +89,7 @@
        "wikibase-description-input-help-message": "[[File:Screenshot 
WikidataRepo 2012-05-13 H.png|right|0x150px]]\nBubble help message for entering 
the description of the data set used for a specific item. Takes on additional 
argument that is the language name, i.e. \"English\" in the nominative singular 
form. See also the Wikidata glossary for 
[[d:Wikidata:Glossary#Description|description]] and 
[[d:Wikidata:Glossary#Item|item]].\n\nParameters:\n* $1 - language 
name\n{{Related|Wikibase-input-help-message}}",
        "wikibase-entitytermsview-input-help-message": "Bubble help message for 
simultaneously editing label, description and aliases of the entity in multiple 
languages via the \"In other languages\" box.\n\nSee also the Wikidata glossary 
for [[d:Wikidata:Glossary#Language_attributes|language attributes]] and 
[[d:Wikidata:Glossary#Item|item]].\n\n{{Related|Wikibase-input-help-message}}",
        "wikibase-entitytermsforlanguageview-input-help-message": "Bubble help 
message for simultaneously editing label, description and aliases of the entity 
in another than the user interface language via the \"In other languages\" box. 
Takes on additional argument that is the language name, i.e. \"English\" in the 
nominative singular form. See also the Wikidata glossary for 
[[d:Wikidata:Glossary#Language_attributes|language attributes]] and 
[[d:Wikidata:Glossary#Item|item]].\n\nParameters:\n* $1 - language 
name\n{{Related|Wikibase-input-help-message}}",
-       "wikibase-statements": "Heading for the list of statements giving 
specific information about the currently viewed item. See also the Wikidata 
glossary for [[m:d:Wikidata:Glossary#Statement|statements]] and 
[[m:d:Wikidata:Glossary#Item|item]].\n{{Identical|Statement}}",
+       "wikibase-statementsection-statements": "Heading for the list of 
statements giving specific information about the currently viewed item. See 
also the Wikidata glossary for [[m:d:Wikidata:Glossary#Statement|statements]] 
and [[m:d:Wikidata:Glossary#Item|item]].\n{{Identical|Statement}}",
        "wikibase-sitelinks": "Heading for the list of sitelinks to Wikipedia 
and other sister projects. This heading is only shown on mobile devices.",
        "wikibase-sitelinkgroupview-input-help-message": "Bubble help message 
for editing a group of sitelinks which involves simultaneous editing and 
removing of existing sitelinks as well as adding new sitelinks to the group. 
See also the Wikidata glossary for 
[[d:Wikidata:Glossary#Sitelinks|sitelinks]].",
        "wikibase-sitelinks-empty": "There are no sitelinks for any of the 
language specific pages on the given cluster.  See also the Wikidata glossary 
for [[d:Wikidata:Glossary#sitelinks|sitelinks]] and 
[[d:Wikidata:Glossary#sitelinks-title|title]].",
diff --git a/view/src/ItemView.php b/view/src/ItemView.php
index a1b272c..07ac4d0 100644
--- a/view/src/ItemView.php
+++ b/view/src/ItemView.php
@@ -21,9 +21,9 @@
 class ItemView extends EntityView {
 
        /**
-        * @var StatementGroupListView
+        * @var StatementSectionsView
         */
-       private $statementGroupListView;
+       private $statementSectionsView;
 
        /**
         * @var string[]
@@ -55,7 +55,11 @@
        ) {
                parent::__construct( $templateFactory, $entityTermsView, 
$language );
 
-               $this->statementGroupListView = $statementGroupListView;
+               // TODO: Inject.
+               $this->statementSectionsView = new StatementSectionsView(
+                       $this->templateFactory,
+                       $statementGroupListView
+               );
                $this->siteLinkGroups = $siteLinkGroups;
                $this->siteLinksView = $siteLinksView;
        }
@@ -71,9 +75,10 @@
                }
 
                $html = parent::getMainHtml( $entityRevision );
-               $html .= $this->statementGroupListView->getHtml(
-                       $item->getStatements()->toArray()
-               );
+               // TODO: Group statements into sections.
+               $html .= $this->statementSectionsView->getHtml( array(
+                       'statements' => $item->getStatements(),
+               ) );
 
                return $html;
        }
diff --git a/view/src/PropertyView.php b/view/src/PropertyView.php
index 2c36375..694dc18 100644
--- a/view/src/PropertyView.php
+++ b/view/src/PropertyView.php
@@ -23,9 +23,9 @@
 class PropertyView extends EntityView {
 
        /**
-        * @var StatementGroupListView
+        * @var StatementSectionsView
         */
-       private $statementGroupListView;
+       private $statementSectionsView;
 
        /**
         * @var DataTypeFactory
@@ -48,7 +48,11 @@
        ) {
                parent::__construct( $templateFactory, $entityTermsView, 
$language );
 
-               $this->statementGroupListView = $statementGroupListView;
+               // TODO: Inject.
+               $this->statementSectionsView = new StatementSectionsView(
+                       $this->templateFactory,
+                       $statementGroupListView
+               );
                $this->dataTypeFactory = $dataTypeFactory;
        }
 
@@ -65,9 +69,10 @@
                $html = parent::getMainHtml( $entityRevision );
                $html .= $this->getHtmlForDataType( $this->getDataType( 
$property ) );
 
-               $html .= $this->statementGroupListView->getHtml(
-                       $property->getStatements()->toArray()
-               );
+               // TODO: Group statements into sections.
+               $html .= $this->statementSectionsView->getHtml( array(
+                       'statements' => $property->getStatements(),
+               ) );
 
                $footer = wfMessage( 'wikibase-property-footer' );
 
diff --git a/view/src/StatementGroupListView.php 
b/view/src/StatementGroupListView.php
index e138b20..5d7e91b 100644
--- a/view/src/StatementGroupListView.php
+++ b/view/src/StatementGroupListView.php
@@ -71,33 +71,10 @@
                        $statementsHtml .= $this->getHtmlForStatementGroupView( 
$statements );
                }
 
-               $html = $this->templateFactory->render(
+               return $this->templateFactory->render(
                        'wikibase-statementgrouplistview',
                        $this->templateFactory->render( 'wikibase-listview', 
$statementsHtml )
                );
-
-               // TODO: Add link to SpecialPage that allows adding a new 
statement.
-               $sectionHeading = $this->getHtmlForSectionHeading( 
'wikibase-statements' );
-
-               return $sectionHeading . $html;
-       }
-
-       /**
-        * Returns the HTML for the heading of the statements section
-        *
-        * @param string $heading message key of the heading
-        *
-        * @return string
-        */
-       private function getHtmlForSectionHeading( $heading ) {
-               $html = $this->templateFactory->render(
-                       'wb-section-heading',
-                       wfMessage( $heading )->escaped(),
-                       'claims', // ID - TODO: should not be added if output 
page is not the entity's page
-                       $heading
-               );
-
-               return $html;
        }
 
        /**
diff --git a/view/src/StatementSectionsView.php 
b/view/src/StatementSectionsView.php
new file mode 100644
index 0000000..3970c64
--- /dev/null
+++ b/view/src/StatementSectionsView.php
@@ -0,0 +1,89 @@
+<?php
+
+namespace Wikibase\View;
+
+use InvalidArgumentException;
+use Wikibase\DataModel\Statement\StatementList;
+use Wikibase\View\Template\TemplateFactory;
+
+/**
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Thiemo Mättig
+ */
+class StatementSectionsView {
+
+       /**
+        * @var TemplateFactory
+        */
+       private $templateFactory;
+
+       /**
+        * @var StatementGroupListView
+        */
+       private $statementListView;
+
+       public function __construct(
+               TemplateFactory $templateFactory,
+               StatementGroupListView $statementListView
+       ) {
+               $this->templateFactory = $templateFactory;
+               $this->statementListView = $statementListView;
+       }
+
+       /**
+        * @param StatementList[] $statementLists
+        *
+        * @throws InvalidArgumentException
+        * @return string HTML
+        */
+       public function getHtml( array $statementLists ) {
+               $html = '';
+
+               foreach ( $statementLists as $key => $statements ) {
+                       if ( !is_string( $key ) || !( $statements instanceof 
StatementList ) ) {
+                               throw new InvalidArgumentException(
+                                       '$statementLists must be an associative 
array of StatementList objects'
+                               );
+                       }
+
+                       $html .= $this->getHtmlForSectionHeading( $key );
+                       $html .= $this->statementListView->getHtml( 
$statements->toArray() );
+               }
+
+               return $html;
+       }
+
+       /**
+        * @param string $key
+        *
+        * @return string HTML
+        */
+       private function getHtmlForSectionHeading( $key ) {
+               /**
+                * Message keys:
+                * wikibase-statementsection-statements
+                * wikibase-statementsection-identifiers
+                */
+               $msg = wfMessage( 'wikibase-statementsection-' . $key );
+               $className = 'wikibase-statements';
+
+               if ( $key === 'statements' ) {
+                       $id = 'claims';
+               } else {
+                       $id = $key;
+                       $className .= ' wikibase-statements-' . $key;
+               }
+
+               // TODO: Add link to SpecialPage that allows adding a new 
statement.
+               return $this->templateFactory->render(
+                       'wb-section-heading',
+                       $msg->escaped(),
+                       // TODO: ID should not be added if output page is not 
the entity's page.
+                       $id,
+                       $className
+               );
+       }
+
+}
diff --git a/view/tests/phpunit/StatementGroupListViewTest.php 
b/view/tests/phpunit/StatementGroupListViewTest.php
index 5c125f2..c1d4e7a 100644
--- a/view/tests/phpunit/StatementGroupListViewTest.php
+++ b/view/tests/phpunit/StatementGroupListViewTest.php
@@ -16,9 +16,14 @@
 use Wikibase\View\ClaimHtmlGenerator;
 use Wikibase\View\StatementGroupListView;
 use Wikibase\View\Template\TemplateFactory;
+use Wikibase\View\Template\TemplateRegistry;
 
 /**
  * @covers Wikibase\View\StatementGroupListView
+ *
+ * @uses Wikibase\View\Template\Template
+ * @uses Wikibase\View\Template\TemplateFactory
+ * @uses Wikibase\View\Template\TemplateRegistry
  *
  * @group Wikibase
  * @group WikibaseView
@@ -37,12 +42,6 @@
                ) );
        }
 
-       /**
-        * @uses Wikibase\View\EditSectionGenerator
-        * @uses Wikibase\View\Template\Template
-        * @uses Wikibase\View\Template\TemplateFactory
-        * @uses Wikibase\View\Template\TemplateRegistry
-        */
        public function testGetHtml() {
                $propertyId = new PropertyId( 'P77' );
                $statements = $this->makeStatements( $propertyId );
@@ -53,10 +52,12 @@
 
                $html = $statementGroupListView->getHtml( $statements );
 
-               $this->assertContains( '<ID>', $html );
+               $this->assertContains( 'id="P77', $html );
+               $this->assertContains( '<PROPERTY><ID></PROPERTY>', $html );
                foreach ( $statements as $statement ) {
                        $this->assertContains( $statement->getGuid(), $html );
                }
+               $this->assertContains( '<TOOLBAR></TOOLBAR>', $html );
        }
 
        /**
@@ -117,7 +118,12 @@
         * @return StatementGroupListView
         */
        private function newStatementGroupListView( EntityIdFormatter 
$propertyIdFormatter ) {
-               $templateFactory = TemplateFactory::getDefaultInstance();
+               $templateFactory = new TemplateFactory( new TemplateRegistry( 
array(
+                       'wikibase-statementgrouplistview' => 
'<SGLIST>$1</SGLIST>',
+                       'wikibase-listview' => '<LIST>$1</LIST>',
+                       'wikibase-statementgroupview' => '<SGROUP 
id="$3"><PROPERTY>$1</PROPERTY>$2</SGROUP>',
+                       'wikibase-statementlistview' => 
'<SLIST>$1<TOOLBAR>$2</TOOLBAR></SLIST>',
+               ) ) );
 
                return new StatementGroupListView(
                        $templateFactory,
@@ -138,7 +144,7 @@
                $claimHtmlGenerator->expects( $this->any() )
                        ->method( 'getHtmlForClaim' )
                        ->will( $this->returnCallback( function( Statement 
$statement, $editSectionHtml = null ) {
-                               return $statement->getGuid();
+                               return $statement->getGuid() . "\n";
                        } ) );
 
                return $claimHtmlGenerator;
diff --git a/view/tests/phpunit/StatementSectionsViewTest.php 
b/view/tests/phpunit/StatementSectionsViewTest.php
new file mode 100644
index 0000000..dd1dc80
--- /dev/null
+++ b/view/tests/phpunit/StatementSectionsViewTest.php
@@ -0,0 +1,88 @@
+<?php
+
+namespace Wikibase\Test;
+
+use PHPUnit_Framework_TestCase;
+use Wikibase\DataModel\Statement\StatementList;
+use Wikibase\View\StatementSectionsView;
+use Wikibase\View\Template\TemplateFactory;
+use Wikibase\View\Template\TemplateRegistry;
+
+/**
+ * @covers Wikibase\View\StatementSectionsView
+ *
+ * @uses Wikibase\View\Template\Template
+ * @uses Wikibase\View\Template\TemplateFactory
+ * @uses Wikibase\View\Template\TemplateRegistry
+ *
+ * @group Wikibase
+ * @group WikibaseView
+ *
+ * @licence GNU GPL v2+
+ * @author Thiemo Mättig
+ */
+class StatementSectionsViewTest extends PHPUnit_Framework_TestCase {
+
+       private function newInstance() {
+               $templateFactory = new TemplateFactory( new TemplateRegistry( 
array(
+                       'wb-section-heading' => '<HEADING id="$2" 
class="$3">$1</HEADING>',
+               ) ) );
+
+               $statementListView = $this->getMockBuilder( 
'Wikibase\View\StatementGroupListView' )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+               $statementListView->expects( $this->any() )
+                       ->method( 'getHtml' )
+                       ->will( $this->returnValue( '<LIST>' ) );
+
+               return new StatementSectionsView( $templateFactory, 
$statementListView );
+       }
+
+       /**
+        * @dataProvider statementListsProvider
+        */
+       public function testGetHtml( array $statementLists, $expected ) {
+               $view = $this->newInstance();
+               $html = $view->getHtml( $statementLists );
+               $this->assertSame( $expected, $html );
+       }
+
+       public function statementListsProvider() {
+               $empty = new StatementList();
+
+               return array(
+                       array(
+                               array(),
+                               ''
+                       ),
+                       array(
+                               array( 'statements' => $empty ),
+                               '<HEADING id="claims" 
class="wikibase-statements">Statements</HEADING><LIST>'
+                       ),
+                       array(
+                               array( 'statements' => $empty, 'identifiers' => 
$empty ),
+                               '<HEADING id="claims" 
class="wikibase-statements">Statements</HEADING><LIST>'
+                               . '<HEADING id="identifiers" 
class="wikibase-statements'
+                               . ' 
wikibase-statements-identifiers">Identifiers</HEADING><LIST>'
+                       ),
+               );
+       }
+
+       /**
+        * @dataProvider invalidConstructorArgumentProvider
+        */
+       public function testGivenInvalidArray_getHtmlFails( $array ) {
+               $view = $this->newInstance();
+               $this->setExpectedException( 'InvalidArgumentException' );
+               $view->getHtml( $array );
+       }
+
+       public function invalidConstructorArgumentProvider() {
+               return array(
+                       array( array( 'statements' => array() ) ),
+                       array( array( array() ) ),
+                       array( array( new StatementList() ) ),
+               );
+       }
+
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iac913191cdd980ed8f9659a3bcb075add6cdef39
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