jenkins-bot has submitted this change and it was merged.

Change subject: Make Lemma work with LexemeView
......................................................................


Make Lemma work with LexemeView

And other parts:
 - Registering Special:NewLexeme
 - Add lemma support in NewLexeme
 - Add lemma support in Patcher

Bug: T148827
Change-Id: I48782b2d7a7f4630d16322175db30458c0e13c57
---
M WikibaseLexeme.entitytypes.php
M src/Content/LexemeHandler.php
M src/DataModel/Services/Diff/LexemePatcher.php
M src/Specials/SpecialNewLexeme.php
M src/View/LexemeView.php
M tests/phpunit/composer/DataModel/Services/Diff/LexemePatcherTest.php
M tests/phpunit/mediawiki/View/LexemeViewTest.php
7 files changed, 129 insertions(+), 25 deletions(-)

Approvals:
  WMDE-leszek: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/WikibaseLexeme.entitytypes.php b/WikibaseLexeme.entitytypes.php
index af50c2a..e0bec9f 100644
--- a/WikibaseLexeme.entitytypes.php
+++ b/WikibaseLexeme.entitytypes.php
@@ -12,10 +12,12 @@
  * @author Amir Sarabadani <ladsgr...@gmail.com>
  */
 use Wikibase\Repo\MediaWikiLanguageDirectionalityLookup;
+use Wikibase\Repo\ParserOutput\FallbackHintHtmlTermRenderer;
 use Wikibase\Repo\WikibaseRepo;
 use Wikibase\DataModel\DeserializerFactory;
 use Wikibase\DataModel\SerializerFactory;
 use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup;
+use Wikibase\Lib\LanguageNameLookup;
 use Wikibase\LanguageFallbackChain;
 use Wikibase\Lexeme\Content\LexemeContent;
 use Wikibase\Lexeme\Content\LexemeHandler;
@@ -52,6 +54,11 @@
                        EntityTermsView $entityTermsView
                ) {
                        $viewFactory = 
WikibaseRepo::getDefaultInstance()->getViewFactory();
+                       $languageDirectionalityLookup = new 
MediaWikiLanguageDirectionalityLookup();
+                       $htmlTermRenderer = new FallbackHintHtmlTermRenderer(
+                               $languageDirectionalityLookup,
+                               new LanguageNameLookup( $languageCode )
+                       );
 
                        return new LexemeView(
                                TemplateFactory::getDefaultInstance(),
@@ -62,7 +69,8 @@
                                        $fallbackChain,
                                        $editSectionGenerator
                                ),
-                               new MediaWikiLanguageDirectionalityLookup(),
+                               $languageDirectionalityLookup,
+                               $htmlTermRenderer,
                                $languageCode
                        );
                },
diff --git a/src/Content/LexemeHandler.php b/src/Content/LexemeHandler.php
index 5bd0ab2..2e3d4c1 100644
--- a/src/Content/LexemeHandler.php
+++ b/src/Content/LexemeHandler.php
@@ -122,8 +122,11 @@
                return Lexeme::ENTITY_TYPE;
        }
 
+       /**
+        * @return string
+        */
        public function getSpecialPageForCreation() {
-               // TODO: Implement getSpecialPageForCreation() method.
+               return 'NewLexeme';
        }
 
 }
diff --git a/src/DataModel/Services/Diff/LexemePatcher.php 
b/src/DataModel/Services/Diff/LexemePatcher.php
index 3d91351..63a9c96 100644
--- a/src/DataModel/Services/Diff/LexemePatcher.php
+++ b/src/DataModel/Services/Diff/LexemePatcher.php
@@ -8,6 +8,7 @@
 use Wikibase\DataModel\Services\Diff\EntityPatcherStrategy;
 use Wikibase\DataModel\Services\Diff\StatementListPatcher;
 use Wikibase\DataModel\Services\Diff\TermListPatcher;
+use Wikibase\DataModel\Term\TermList;
 use Wikibase\Lexeme\DataModel\Lexeme;
 use Wikimedia\Assert\Assert;
 
@@ -51,6 +52,14 @@
                Assert::parameterType( Lexeme::class, $entity, '$entity' );
 
                /** @var Lexeme $entity */
+               /** @var LexemeDiff $patch */
+               $lemmata = !is_null( $entity->getLemmata() ) ? 
$entity->getLemmata() : new TermList();
+               $this->termListPatcher->patchTermList(
+                       $lemmata,
+                       $patch->getLemmataDiff()
+               );
+
+               /** @var Lexeme $entity */
                $this->statementListPatcher->patchStatementList(
                        $entity->getStatements(),
                        $patch->getClaimsDiff()
diff --git a/src/Specials/SpecialNewLexeme.php 
b/src/Specials/SpecialNewLexeme.php
index 57425c7..876ecfc 100644
--- a/src/Specials/SpecialNewLexeme.php
+++ b/src/Specials/SpecialNewLexeme.php
@@ -8,7 +8,8 @@
 use Status;
 use Wikibase\CopyrightMessageBuilder;
 use Wikibase\DataModel\Entity\EntityDocument;
-use Wikibase\DataModel\Term\FingerprintProvider;
+use Wikibase\DataModel\Term\Term;
+use Wikibase\DataModel\Term\TermList;
 use Wikibase\Lexeme\DataModel\Lexeme;
 use Wikibase\Lib\LanguageNameLookup;
 use Wikibase\Repo\WikibaseRepo;
@@ -16,6 +17,7 @@
 use Wikibase\Repo\Specials\SpecialPageCopyrightView;
 use Wikibase\Summary;
 use Wikibase\View\LanguageDirectionalityLookup;
+use Wikimedia\Assert\Assert;
 
 /**
  * Page for creating new Lexeme entities that contain a Fingerprint.
@@ -191,11 +193,9 @@
         * @return Status
         */
        protected function modifyEntity( EntityDocument &$entity ) {
-               if ( !( $entity instanceof FingerprintProvider ) ) {
-                       throw new InvalidArgumentException( '$entity must be a 
FingerprintProvider' );
-               }
+               /** @var Lexeme $entity */
+               Assert::parameterType( Lexeme::class, $entity, '$entity' );
 
-               $fingerprint = $entity->getFingerprint();
                $status = Status::newGood();
 
                $languageCode = $this->contentLanguageCode;
@@ -204,7 +204,8 @@
                        return $status;
                }
 
-               $fingerprint->setLabel( $languageCode, $this->lemma );
+               $lemmata = new TermList( [ new Term( $languageCode, 
$this->lemma ) ] );
+               $entity->setLemmata( $lemmata );
 
                return $status;
        }
diff --git a/src/View/LexemeView.php b/src/View/LexemeView.php
index 66fa6a1..e4570d9 100644
--- a/src/View/LexemeView.php
+++ b/src/View/LexemeView.php
@@ -3,12 +3,15 @@
 namespace Wikibase\Lexeme\View;
 
 use InvalidArgumentException;
-use MediaWiki\Linker\LinkRenderer;
+use Language;
+use Message;
 use Wikibase\DataModel\Entity\EntityDocument;
+use Wikibase\DataModel\Term\Term;
+use Wikibase\DataModel\Term\TermList;
 use Wikibase\Lexeme\DataModel\Lexeme;
-use Wikibase\Lexeme\DataModel\LexemeId;
 use Wikibase\View\EntityTermsView;
 use Wikibase\View\EntityView;
+use Wikibase\View\HtmlTermRenderer;
 use Wikibase\View\LanguageDirectionalityLookup;
 use Wikibase\View\StatementSectionsView;
 use Wikibase\View\Template\TemplateFactory;
@@ -28,10 +31,26 @@
        private $statementSectionsView;
 
        /**
+        * @var TemplateFactory
+        */
+       protected $templateFactory;
+
+       /**
+        * @var string
+        */
+       protected $languageCode;
+
+       /**
+        * @var HtmlTermRenderer
+        */
+       private $htmlTermRenderer;
+
+       /**
         * @param TemplateFactory $templateFactory
         * @param EntityTermsView $entityTermsView
         * @param StatementSectionsView $statementSectionsView
         * @param LanguageDirectionalityLookup $languageDirectionalityLookup
+        * @param HtmlTermRenderer $htmlTermRenderer
         * @param string $languageCode
         */
        public function __construct(
@@ -39,6 +58,7 @@
                EntityTermsView $entityTermsView,
                StatementSectionsView $statementSectionsView,
                LanguageDirectionalityLookup $languageDirectionalityLookup,
+               HtmlTermRenderer $htmlTermRenderer,
                $languageCode
        ) {
                parent::__construct(
@@ -49,6 +69,9 @@
                );
 
                $this->statementSectionsView = $statementSectionsView;
+               $this->templateFactory = $templateFactory;
+               $this->languageCode = $languageCode;
+               $this->htmlTermRenderer = $htmlTermRenderer;
        }
 
        /**
@@ -60,11 +83,12 @@
         * @return string HTML
         */
        protected function getMainHtml( EntityDocument $entity ) {
+               /** @var Lexeme $entity */
                Assert::parameterType( Lexeme::class, $entity, '$entity' );
 
                $html = $this->getHtmlForLexicalCategoryAndLanguage( $entity )
-                        . $this->templateFactory->render( 'wikibase-toc' )
-                        . $this->statementSectionsView->getHtml( 
$entity->getStatements() );
+                       . $this->templateFactory->render( 'wikibase-toc' )
+                       . $this->statementSectionsView->getHtml( 
$entity->getStatements() );
 
                return $html;
        }
@@ -80,9 +104,64 @@
                return '';
        }
 
+       /**
+        * @param EntityDocument $entity
+        * @return string
+        */
+       public function getTitleHtml( EntityDocument $entity ) {
+               /** @var Lexeme $entity */
+               Assert::parameterType( Lexeme::class, $entity, '$entity' );
+               $isEmpty = true;
+               $idInParenthesesHtml = '';
+               $labelHtml = '';
+
+               if ( $entity->getId() !== null ) {
+                       $id = $entity->getId()->getSerialization();
+                       $idInParenthesesHtml = htmlspecialchars(
+                               $this->getLocalizedMessage( 'parentheses', [ 
$id ] )
+                       );
+
+                       $label = $this->getMainTerm( $entity->getLemmata() );
+                       if ( $label !== null ) {
+                               $labelHtml = 
$this->htmlTermRenderer->renderTerm( $label );
+                               $isEmpty = false;
+                       }
+               }
+               $title = $isEmpty ? htmlspecialchars(
+                       $this->getLocalizedMessage( 'wikibase-label-empty' ) ) 
: $labelHtml;
+               return $this->templateFactory->render(
+                       'wikibase-title',
+                       $isEmpty ? 'wb-empty' : '',
+                       $title,
+                       $idInParenthesesHtml
+               );
+       }
+
        private function getHtmlForLexicalCategoryAndLanguage( $entity ) {
                // TODO: Implement when building LexicalCategory and Language
                return '';
        }
 
+       /**
+        * @param TermList|null $lemmata
+        * @return Term|null
+        */
+       private function getMainTerm( $lemmata ) {
+
+               if ( is_null( $lemmata ) || $lemmata->isEmpty() ) {
+                       return null;
+               }
+
+               // Return the first term, until we build a proper UI
+               foreach ( $lemmata->getIterator() as $term ) {
+                       return $term;
+               }
+
+               return null;
+       }
+
+       private function getLocalizedMessage( $key, array $params = [] ) {
+               return ( new Message( $key, $params, Language::factory( 
$this->languageCode ) ) )->text();
+       }
+
 }
diff --git 
a/tests/phpunit/composer/DataModel/Services/Diff/LexemePatcherTest.php 
b/tests/phpunit/composer/DataModel/Services/Diff/LexemePatcherTest.php
index d4be3a8..8f38d90 100644
--- a/tests/phpunit/composer/DataModel/Services/Diff/LexemePatcherTest.php
+++ b/tests/phpunit/composer/DataModel/Services/Diff/LexemePatcherTest.php
@@ -12,6 +12,7 @@
 use Wikibase\DataModel\Snak\PropertyNoValueSnak;
 use Wikibase\DataModel\Statement\Statement;
 use Wikibase\Lexeme\DataModel\Lexeme;
+use Wikibase\Lexeme\DataModel\Services\Diff\LexemeDiff;
 use Wikibase\Lexeme\DataModel\Services\Diff\LexemePatcher;
 
 /**
@@ -29,7 +30,7 @@
 
                $patched = $lexeme->copy();
                $patcher = new LexemePatcher();
-               $patcher->patchEntity( $patched, new EntityDiff() );
+               $patcher->patchEntity( $patched, new LexemeDiff() );
 
                $this->assertInstanceOf( Lexeme::class, $patched );
                $this->assertTrue( $lexeme->equals( $patched ) );
@@ -47,7 +48,7 @@
                $patcher = new LexemePatcher();
 
                $this->setExpectedException( InvalidArgumentException::class );
-               $patcher->patchEntity( new Item(), new EntityDiff() );
+               $patcher->patchEntity( new Item(), new LexemeDiff() );
        }
 
        public function testStatementsArePatched() {
@@ -57,7 +58,7 @@
                $lexeme = new Lexeme();
                $lexeme->getStatements()->addStatement( $removedStatement );
 
-               $patch = new EntityDiff( [
+               $patch = new LexemeDiff( [
                        'claim' => new Diff( [
                                's1' => new DiffOpRemove( $removedStatement ),
                                's2' => new DiffOpAdd( $addedStatement ),
diff --git a/tests/phpunit/mediawiki/View/LexemeViewTest.php 
b/tests/phpunit/mediawiki/View/LexemeViewTest.php
index 8230632..fadbd36 100644
--- a/tests/phpunit/mediawiki/View/LexemeViewTest.php
+++ b/tests/phpunit/mediawiki/View/LexemeViewTest.php
@@ -3,7 +3,6 @@
 namespace Wikibase\Lexeme\Tests\MediaWiki\View;
 
 use InvalidArgumentException;
-use MediaWiki\Linker\LinkRenderer;
 use PHPUnit_Framework_TestCase;
 use Wikibase\DataModel\Entity\EntityDocument;
 use Wikibase\DataModel\Entity\PropertyId;
@@ -13,11 +12,14 @@
 use Wikibase\Lexeme\DataModel\Lexeme;
 use Wikibase\Lexeme\DataModel\LexemeId;
 use Wikibase\Lexeme\View\LexemeView;
+use Wikibase\Lib\LanguageNameLookup;
+use Wikibase\Repo\ParserOutput\FallbackHintHtmlTermRenderer;
 use Wikibase\View\EntityTermsView;
 use Wikibase\View\EntityView;
 use Wikibase\View\LanguageDirectionalityLookup;
 use Wikibase\View\StatementSectionsView;
 use Wikibase\View\Template\TemplateFactory;
+use Wikimedia\Assert\ParameterTypeException;
 
 /**
  * @covers Wikibase\Lexeme\View\LexemeView
@@ -73,18 +75,19 @@
                        $statementSectionsView = 
$this->newStatementSectionsViewMock();
                }
 
-               /** @var LinkRenderer $linkRenderer */
-               $linkRenderer = $this->getMockBuilder( LinkRenderer::class )
-                       ->disableOriginalConstructor()
-                       ->getMock();
+               $languageDirectionalityLookup = 
$this->newLanguageDirectionalityLookupMock();
+               $htmlTermRenderer = new FallbackHintHtmlTermRenderer(
+                       $languageDirectionalityLookup,
+                       new LanguageNameLookup( $contentLanguageCode )
+               );
 
                return new LexemeView(
                        $templateFactory,
                        $entityTermsView,
                        $statementSectionsView,
-                       $this->newLanguageDirectionalityLookupMock(),
-                       $contentLanguageCode,
-                       $linkRenderer
+                       $languageDirectionalityLookup,
+                       $htmlTermRenderer,
+                       $contentLanguageCode
                );
        }
 
@@ -190,8 +193,8 @@
 
                /** @var EntityDocument $entity */
                $entity = $this->getMock( EntityDocument::class );
-               $html = $view->getTitleHtml( $entity );
-               $this->assertSame( $html, '' );
+               $this->setExpectedException( ParameterTypeException::class );
+               $view->getTitleHtml( $entity );
        }
 
 }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I48782b2d7a7f4630d16322175db30458c0e13c57
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/WikibaseLexeme
Gerrit-Branch: master
Gerrit-Owner: Ladsgroup <ladsgr...@gmail.com>
Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de>
Gerrit-Reviewer: Ladsgroup <ladsgr...@gmail.com>
Gerrit-Reviewer: WMDE-leszek <leszek.mani...@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