Aleksey Bekh-Ivanov (WMDE) has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/362236 )

Change subject: Make Form representations multilingual
......................................................................

Make Form representations multilingual

Change-Id: I9a852e3f353c28f82140ac9bf7389e78cb1276d4
---
M WikibaseLexeme.entitytypes.php
M src/Content/LexemeContent.php
M src/DataModel/Form.php
M src/DataModel/Serialization/LexemeDeserializer.php
M src/DataModel/Serialization/LexemeSerializer.php
M src/View/LexemeFormsView.php
A tests/phpunit/composer/DataModel/FormTest.php
M tests/phpunit/composer/DataModel/NewForm.php
M tests/phpunit/composer/DataModel/Serialization/LexemeDeserializerTest.php
M tests/phpunit/composer/DataModel/Serialization/LexemeSerializerTest.php
M tests/phpunit/mediawiki/View/LexemeFormsViewTest.php
11 files changed, 106 insertions(+), 55 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseLexeme 
refs/changes/36/362236/1

diff --git a/WikibaseLexeme.entitytypes.php b/WikibaseLexeme.entitytypes.php
index 55db0e6..06a5230 100644
--- a/WikibaseLexeme.entitytypes.php
+++ b/WikibaseLexeme.entitytypes.php
@@ -52,7 +52,6 @@
                'deserializer-factory-callback' => function( 
DeserializerFactory $deserializerFactory ) {
                        return new LexemeDeserializer(
                                $deserializerFactory->newEntityIdDeserializer(),
-                               $deserializerFactory->newTermListDeserializer(),
                                
$deserializerFactory->newStatementListDeserializer()
                        );
                },
diff --git a/src/Content/LexemeContent.php b/src/Content/LexemeContent.php
index 1f03a79..8c97975 100644
--- a/src/Content/LexemeContent.php
+++ b/src/Content/LexemeContent.php
@@ -91,9 +91,23 @@
                );
 
                $forms = [
-                       new Form( new FormId( 'F1' ), 'A', [] ),
-                       new Form( new FormId( 'F2' ), 'B', 
$grammaticalFeatures1, $statements1 ),
-                       new Form( new FormId( 'F3' ), 'C', 
$grammaticalFeatures2, $statements2 ),
+                       new Form(
+                               new FormId( 'F1' ),
+                               new TermList( [ new Term( 'en', 'A' ) ] ),
+                               []
+                       ),
+                       new Form(
+                               new FormId( 'F2' ),
+                               new TermList( [ new Term( 'en', 'B' ) ] ),
+                               $grammaticalFeatures1,
+                               $statements1
+                       ),
+                       new Form(
+                               new FormId( 'F3' ),
+                               new TermList( [ new Term( 'en', 'C' ) ] ),
+                               $grammaticalFeatures2,
+                               $statements2
+                       ),
                ];
 
                $lexeme->setForms( $forms );
diff --git a/src/DataModel/Form.php b/src/DataModel/Form.php
index 06ac5a0..785a28c 100644
--- a/src/DataModel/Form.php
+++ b/src/DataModel/Form.php
@@ -5,6 +5,7 @@
 use Wikibase\DataModel\Statement\StatementList;
 use Wikibase\DataModel\Statement\StatementListProvider;
 use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Term\TermList;
 
 /**
  * @license GPL-2.0+
@@ -20,7 +21,7 @@
        /**
         * @var string
         */
-       private $representation;
+       private $representations;
 
        /**
         * @var ItemId[]
@@ -39,13 +40,17 @@
         * @param StatementList|null $statementList
         */
        public function __construct(
-               FormId $id = null,
-               $representation,
+               FormId $id,
+               TermList $representations,
                array $grammaticalFeatures,
                StatementList $statementList = null
        ) {
+               if ( $representations->count() === 0 ) {
+                       throw new \InvalidArgumentException( 'Form must have at 
least one representation' );
+               }
+
                $this->id = $id;
-               $this->representation = $representation;
+               $this->representations = $representations;
                $this->grammaticalFeatures = $grammaticalFeatures;
                $this->statementList = $statementList ?: new StatementList();
        }
@@ -58,10 +63,10 @@
        }
 
        /**
-        * @return string
+        * @return TermList
         */
-       public function getRepresentation() {
-               return $this->representation;
+       public function getRepresentations() {
+               return $this->representations;
        }
 
        public function getGrammaticalFeatures() {
diff --git a/src/DataModel/Serialization/LexemeDeserializer.php 
b/src/DataModel/Serialization/LexemeDeserializer.php
index 6c1fe9f..7c4bb30 100644
--- a/src/DataModel/Serialization/LexemeDeserializer.php
+++ b/src/DataModel/Serialization/LexemeDeserializer.php
@@ -5,6 +5,8 @@
 use Deserializers\Deserializer;
 use Deserializers\Exceptions\DeserializationException;
 use Deserializers\TypedObjectDeserializer;
+use Wikibase\DataModel\Deserializers\TermDeserializer;
+use Wikibase\DataModel\Deserializers\TermListDeserializer;
 use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\DataModel\Statement\StatementList;
 use Wikibase\DataModel\Term\TermList;
@@ -36,13 +38,12 @@
 
        public function __construct(
                Deserializer $entityIdDeserializer,
-               Deserializer $termListDeserializer,
                Deserializer $statementListDeserializer
        ) {
                parent::__construct( 'lexeme', 'type' );
 
                $this->entityIdDeserializer = $entityIdDeserializer;
-               $this->termListDeserializer = $termListDeserializer;
+               $this->termListDeserializer = new TermListDeserializer( new 
TermDeserializer() );
                $this->statementListDeserializer = $statementListDeserializer;
        }
 
@@ -161,8 +162,11 @@
                        $id = new FormId( $serialization['id'] );
                }
 
-               // TODO: Throw proper exception if array key does not exist
-               return new Form( $id, $serialization['representation'], [] );
+               $representations = $this->termListDeserializer->deserialize(
+                       $serialization['representations']
+               );
+
+               return new Form( $id, $representations, [] );
        }
 
 }
diff --git a/src/DataModel/Serialization/LexemeSerializer.php 
b/src/DataModel/Serialization/LexemeSerializer.php
index f9971d8..4b446d0 100644
--- a/src/DataModel/Serialization/LexemeSerializer.php
+++ b/src/DataModel/Serialization/LexemeSerializer.php
@@ -137,7 +137,7 @@
                        $serialization['id'] = $id->getSerialization();
                }
 
-               $serialization['representation'] = $form->getRepresentation();
+               $serialization['representations'] = 
$this->termListSerializer->serialize( $form->getRepresentations() );
                $serialization['grammaticalFeatures'] = array_map(
                        function ( ItemId $itemId ) {
                                return $itemId->getSerialization();
diff --git a/src/View/LexemeFormsView.php b/src/View/LexemeFormsView.php
index 123e9db..fe45c97 100644
--- a/src/View/LexemeFormsView.php
+++ b/src/View/LexemeFormsView.php
@@ -76,7 +76,8 @@
         * @return string HTML
         */
        private function getFormHtml( Form $form ) {
-               $representation = $form->getRepresentation();
+               //TODO Change to rendering all the representations
+               $representation = 
$form->getRepresentations()->getIterator()->current()->getText();
 
                $grammaticalFeaturesHtml = $this->templateFactory->render(
                        'wikibase-lexeme-form-grammatical-features',
diff --git a/tests/phpunit/composer/DataModel/FormTest.php 
b/tests/phpunit/composer/DataModel/FormTest.php
new file mode 100644
index 0000000..f482a67
--- /dev/null
+++ b/tests/phpunit/composer/DataModel/FormTest.php
@@ -0,0 +1,26 @@
+<?php
+
+namespace Wikibase\Lexeme\Tests\DataModel;
+
+use PHPUnit_Framework_TestCase;
+use Wikibase\DataModel\Term\Term;
+use Wikibase\DataModel\Term\TermList;
+use Wikibase\Lexeme\DataModel\Form;
+use Wikibase\Lexeme\DataModel\FormId;
+
+class FormTest extends PHPUnit_Framework_TestCase {
+
+       public function 
testCreateFormWithoutRepresentations_ThrowsAnException() {
+               $this->setExpectedException( \Exception::class );
+               new Form( new FormId( 'F1' ), new TermList(), [] );
+       }
+
+       public function testCreateFormWithOneRepresentation_CreatesIt() {
+               new Form(
+                       new FormId( 'F1' ),
+                       new TermList( [ new Term( 'en', 'representation' ) ] ),
+                       []
+               );
+       }
+
+}
diff --git a/tests/phpunit/composer/DataModel/NewForm.php 
b/tests/phpunit/composer/DataModel/NewForm.php
index 8d812f7..08f5b69 100644
--- a/tests/phpunit/composer/DataModel/NewForm.php
+++ b/tests/phpunit/composer/DataModel/NewForm.php
@@ -6,12 +6,14 @@
 use Wikibase\DataModel\Snak\Snak;
 use Wikibase\DataModel\Statement\Statement;
 use Wikibase\DataModel\Statement\StatementList;
+use Wikibase\DataModel\Term\Term;
+use Wikibase\DataModel\Term\TermList;
 use Wikibase\Lexeme\DataModel\Form;
 use Wikibase\Lexeme\DataModel\FormId;
 use Wikibase\Repo\Tests\NewStatement;
 
 /**
- * @method static self havingRepresentation (string $representation)
+ * @method static self havingRepresentation (string $language, string 
$representation)
  * @method static self havingId (FormId | string $formId)
  * @method static self havingStatement (Statement | Snak | NewStatement 
$statement)
  * @method static self havingGrammaticalFeature (ItemId | string $itemId)
@@ -24,9 +26,9 @@
        private $formId;
 
        /**
-        * @var string
+        * @var string[] Indexed by language
         */
-       private $representation;
+       private $representations = [];
 
        /**
         * @var Statement[]
@@ -69,17 +71,18 @@
        }
 
        /**
+        * @param string $language
         * @param string $representation
-        * @return self
+        * @return NewForm
         */
-       public function andRepresentation( $representation ) {
-               if ( $this->representation ) {
+       public function andRepresentation( $language, $representation ) {
+               if ( $this->representations[$language] ) {
                        throw new \LogicException(
-                               'Representation is already set. You are not 
allowed to change it'
+                               "Representation for '{$language}' is already 
set. You are not allowed to change it"
                        );
                }
                $result = clone $this;
-               $result->representation = $representation;
+               $result->representations[$language] = $representation;
                return $result;
        }
 
@@ -118,10 +121,19 @@
         */
        public function build() {
                $formId = $this->formId ?: $this->newRandomFormId();
-               $representation = $this->representation ?: 'representation' . 
mt_rand( 0, mt_getrandmax() );
+               if ( empty( $this->representations ) ) {
+                       $representations = new TermList( [
+                               new Term( 'qqq', 'representation' . mt_rand( 0, 
mt_getrandmax() ) )
+                       ] );
+               } else {
+                       $representations = new TermList();
+                       foreach ( $this->representations as $language => 
$representation ) {
+                               $representations->setTextForLanguage( 
$language, $representation );
+                       }
+               }
                return new Form(
                        $formId,
-                       $representation,
+                       $representations,
                        $this->grammaticalFeatures,
                        new StatementList( $this->statements )
                );
diff --git 
a/tests/phpunit/composer/DataModel/Serialization/LexemeDeserializerTest.php 
b/tests/phpunit/composer/DataModel/Serialization/LexemeDeserializerTest.php
index 27a2af5..05ee587 100644
--- a/tests/phpunit/composer/DataModel/Serialization/LexemeDeserializerTest.php
+++ b/tests/phpunit/composer/DataModel/Serialization/LexemeDeserializerTest.php
@@ -32,8 +32,7 @@
                $entityIdDeserializer = $this->getMockBuilder( 
EntityIdDeserializer::class )
                        ->disableOriginalConstructor()
                        ->getMock();
-               $entityIdDeserializer->expects( $this->any() )
-                       ->method( 'deserialize' )
+               $entityIdDeserializer->method( 'deserialize' )
                        ->will( $this->returnCallback( function( $serialization 
) {
                                return new ItemId( $serialization );
                        } ) );
@@ -41,8 +40,7 @@
                $statementListDeserializer = $this->getMockBuilder( 
StatementListDeserializer::class )
                        ->disableOriginalConstructor()
                        ->getMock();
-               $statementListDeserializer->expects( $this->any() )
-                       ->method( 'deserialize' )
+               $statementListDeserializer->method( 'deserialize' )
                        ->will( $this->returnCallback( function( array 
$serialization ) {
                                $statementList = new StatementList();
 
@@ -53,22 +51,8 @@
                                return $statementList;
                        } ) );
 
-               $termListDeserializer = $this->getMockBuilder( 
TermListDeserializer::class )
-                       ->disableOriginalConstructor()
-                       ->getMock();
-               $termListDeserializer->expects( $this->any() )
-                       ->method( 'deserialize' )
-                       ->will( $this->returnCallback( function( array 
$serialization ) {
-                               $terms = [];
-                               foreach ( $serialization as $language => $value 
) {
-                                       $terms[] = new Term( $language, $value 
);
-                               }
-                               return new TermList( $terms );
-                       } ) );
-
                return new LexemeDeserializer(
                        $entityIdDeserializer,
-                       $termListDeserializer,
                        $statementListDeserializer
                );
        }
@@ -136,7 +120,7 @@
                        [
                                'type' => 'lexeme',
                                'id' => 'L2',
-                               'lemmas' => [ 'el'  => 'Hey' ],
+                               'lemmas' => [ 'el'  => [ 'language' => 'el', 
'value' => 'Hey' ] ],
                        ],
                        $lexeme
                ];
@@ -169,14 +153,21 @@
                                'id' => 'L1',
                                'lexicalCategory' => 'Q1',
                                'language' => 'Q2',
-                               'forms' => [ [ 'id' => 'F1', 'representation' 
=> 'form' ] ],
+                               'forms' => [
+                                       [
+                                               'id' => 'F1',
+                                               'representations' => [
+                                                       'en' => [ 'language' => 
'en', 'value' => 'form' ]
+                                               ]
+                                       ]
+                               ],
                        ],
                        NewLexeme::havingId( 'L1' )
                                ->withLexicalCategory( 'Q1' )
                                ->withLanguage( 'Q2' )
                                ->withForm(
                                        NewForm::havingId( 'F1' )
-                                               ->andRepresentation( 'form' )
+                                               ->andRepresentation( 'en', 
'form' )
                                )->build()
 
                ];
diff --git 
a/tests/phpunit/composer/DataModel/Serialization/LexemeSerializerTest.php 
b/tests/phpunit/composer/DataModel/Serialization/LexemeSerializerTest.php
index 52185c6..a1cbc2f 100644
--- a/tests/phpunit/composer/DataModel/Serialization/LexemeSerializerTest.php
+++ b/tests/phpunit/composer/DataModel/Serialization/LexemeSerializerTest.php
@@ -180,18 +180,17 @@
 
        public function 
testLexemeFormWithRepresentation_SerializesFromRepresentation() {
                $lexeme = NewLexeme::havingForm(
-                       NewForm::havingRepresentation( 'some representation' )
+                       NewForm::havingRepresentation( 'en', 'some 
representation' )
                )->build();
 
                $serialization = $this->newSerializer()->serialize( $lexeme );
 
+               $formSerialization = $serialization['forms'][0];
                assertThat(
-                       $serialization,
+                       $formSerialization,
                        hasKeyValuePair(
-                               'forms',
-                               hasItemInArray(
-                                       hasKeyValuePair( 'representation', 
'some representation' )
-                               )
+                               'representations',
+                               hasKeyValuePair( 'en', 'some representation' )
                        )
                );
        }
diff --git a/tests/phpunit/mediawiki/View/LexemeFormsViewTest.php 
b/tests/phpunit/mediawiki/View/LexemeFormsViewTest.php
index 5f81c4f..2a31861 100644
--- a/tests/phpunit/mediawiki/View/LexemeFormsViewTest.php
+++ b/tests/phpunit/mediawiki/View/LexemeFormsViewTest.php
@@ -61,7 +61,7 @@
                $view = $this->newFormsView();
                $html = $view->getHtml( [
                        NewForm::havingId( 'F1' )
-                               ->andRepresentation( 'FORM_REPRESENTATION' )
+                               ->andRepresentation( 'en', 
'FORM_REPRESENTATION' )
                                ->build()
                ] );
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9a852e3f353c28f82140ac9bf7389e78cb1276d4
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseLexeme
Gerrit-Branch: master
Gerrit-Owner: Aleksey Bekh-Ivanov (WMDE) <[email protected]>

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

Reply via email to