WMDE-leszek has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/356573 )

Change subject: Display senses of the lexeme on the Lexeme page
......................................................................

Display senses of the lexeme on the Lexeme page

Currently senses are hard coded for demonstration purposes.

Bug: T165566
Change-Id: I6b2d44231718eac87e95a791df3179c3a7074fea
---
M i18n/en.json
M i18n/qqq.json
M resources/lexeme.css
M resources/templates.php
M src/Content/LexemeContent.php
M src/DataModel/Lexeme.php
M src/View/LexemeView.php
M src/View/LexemeViewFactory.php
M src/View/SensesView.php
M tests/phpunit/mediawiki/Content/LexemeContentTest.php
M tests/phpunit/mediawiki/View/SensesViewTest.php
11 files changed, 204 insertions(+), 10 deletions(-)


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

diff --git a/i18n/en.json b/i18n/en.json
index 3a42f9f..0415fe4 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -15,5 +15,6 @@
        "wikibase-lexeme-enter-form-representation": "Enter new form 
representation",
        "wikibase-lexeme-empty-form-representation": "No form representation 
defined",
        "wikibase-lexeme-view-senses": "Senses",
+       "wikibase-lexeme-gloss-empty": "No gloss defined",
        "wikibase-lexeme-summary-wbeditentity-update": "Changed a lexeme"
 }
diff --git a/i18n/qqq.json b/i18n/qqq.json
index 62a8270..9bfebed 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -20,5 +20,6 @@
        "wikibase-lexeme-enter-form-representation": "Placeholder for the 
lexeme form representation input field.",
        "wikibase-lexeme-empty-form-representation": "Placeholder message 
displayed instead of the lexeme form representation in case no representation 
has been specified yet.",
        "wikibase-lexeme-view-senses": "Subheading for the senses section on a 
lexeme page.\n{{Identical|Sense}}",
+       "wikibase-lexeme-gloss-empty": "Placeholder message displayed instead 
of the gloss of the sense in case the gloss in the display language has not 
been specified yet.",
        "wikibase-lexeme-summary-wbeditentity-update": "Auto-generated edit 
summary when editing a lexeme"
 }
diff --git a/resources/lexeme.css b/resources/lexeme.css
index f29e5d6..8e7ad63 100644
--- a/resources/lexeme.css
+++ b/resources/lexeme.css
@@ -65,3 +65,20 @@
        display: table;
        clear: both;
 }
+
+.wikibase-lexeme-senses .wikibase-lexeme-sense {
+       position: relative;
+       clear: left;
+}
+
+.wikibase-lexeme-senses .wikibase-lexeme-sense-gloss {
+       clear: left;
+       margin-left: 10px; /* same as .wb-section-heading */
+       position: relative;
+       border-bottom: 1px solid #a2a9b1;
+       font-weight: normal;
+}
+
+.wikibase-lexeme-sense-gloss .wikibase-lexeme-sense-id {
+       color: #72777d;
+}
diff --git a/resources/templates.php b/resources/templates.php
index 0c86db1..a904476 100644
--- a/resources/templates.php
+++ b/resources/templates.php
@@ -32,5 +32,15 @@
 </div>
 HTML;
 
+       // TODO: i18n space before id?
+       $templates['wikibase-lexeme-sense'] = <<< 'HTML'
+<div class="wikibase-lexeme-sense">
+       <h3 class="wikibase-lexeme-sense-gloss" dir="$1" lang="$2">
+               <span class="wikibase-lexeme-sense-gloss-text $3">$4</span>
+               <span class="wikibase-lexeme-sense-id wikibase-title-id"> 
$5</span>
+       </h3>
+</div>
+HTML;
+
        return $templates;
 } );
diff --git a/src/Content/LexemeContent.php b/src/Content/LexemeContent.php
index 0e881f9..2f49643 100644
--- a/src/Content/LexemeContent.php
+++ b/src/Content/LexemeContent.php
@@ -4,8 +4,12 @@
 
 use InvalidArgumentException;
 use Wikibase\Content\EntityHolder;
+use Wikibase\DataModel\Term\Term;
+use Wikibase\DataModel\Term\TermList;
 use Wikibase\EntityContent;
 use Wikibase\Lexeme\DataModel\Lexeme;
+use Wikibase\Lexeme\DataModel\Sense;
+use Wikibase\Lexeme\DataModel\SenseId;
 use Wikimedia\Assert\Assert;
 
 use DataValues\StringValue;
@@ -87,6 +91,30 @@
 
                $lexeme->setForms( $forms );
 
+               $senses = [
+                       new Sense(
+                               new SenseId( 
$lexeme->getId()->getSerialization() . '-S1' ),
+                               new TermList( [
+                                       new Term( 'en', 'A mammal, Capra 
aegagrus hircus, and similar species of the genus Capra.' )
+                               ] ),
+                               new StatementList()
+                       ),
+                       new Sense(
+                               new SenseId( 
$lexeme->getId()->getSerialization() . '-S2' ),
+                               new TermList( [ new Term( 'en', 'A scapegoat.' 
) ] ),
+                               new StatementList( [
+                                       new Statement(
+                                               new PropertyValueSnak( new 
PropertyId( 'P900' ), new StringValue( 'informal' ) ),
+                                               null,
+                                               null,
+                                               'guid900'
+                                       ),
+                               ] )
+                       )
+               ];
+
+               $lexeme->setSenses( $senses );
+
                return $lexeme;
        }
 
diff --git a/src/DataModel/Lexeme.php b/src/DataModel/Lexeme.php
index ea802a7..430ffde 100644
--- a/src/DataModel/Lexeme.php
+++ b/src/DataModel/Lexeme.php
@@ -256,6 +256,14 @@
        }
 
        /**
+        * @param Sense[] $senses
+        * @deprecated Only for demonstration purposes. Do not use otherwise!
+        */
+       public function setSenses( array $senses ) {
+               $this->senses = $senses;
+       }
+
+       /**
         * @return bool False if a non-optional field was never initialized, 
true otherwise.
         */
        public function isSufficientlyInitialized() {
diff --git a/src/View/LexemeView.php b/src/View/LexemeView.php
index 4bdb94e..0c2ee58 100644
--- a/src/View/LexemeView.php
+++ b/src/View/LexemeView.php
@@ -105,7 +105,7 @@
                        . $this->templateFactory->render( 'wikibase-toc' )
                        . $this->statementSectionsView->getHtml( 
$entity->getStatements() )
                        . $this->formsView->getHtml( $entity->getForms() )
-                       . $this->sensesView->getHtml();
+                       . $this->sensesView->getHtml( $entity->getSenses() );
        }
 
        /**
diff --git a/src/View/LexemeViewFactory.php b/src/View/LexemeViewFactory.php
index 4ff5abe..c39c8b2 100644
--- a/src/View/LexemeViewFactory.php
+++ b/src/View/LexemeViewFactory.php
@@ -77,10 +77,17 @@
 
        public function newLexemeView() {
                $templates = include __DIR__ . '/../../resources/templates.php';
+               $templateFactory = new LexemeTemplateFactory( $templates );
+
                $languageDirectionalityLookup = new 
MediaWikiLanguageDirectionalityLookup();
                $localizedTextProvider = new MediaWikiLocalizedTextProvider( 
$this->languageCode );
 
-               $sensesView = new SensesView( $localizedTextProvider );
+               $sensesView = new SensesView(
+                       $localizedTextProvider,
+                       $languageDirectionalityLookup,
+                       $templateFactory,
+                       $this->languageCode
+               );
 
                $wikibaseRepo = WikibaseRepo::getDefaultInstance();
                $statementSectionsView = 
$wikibaseRepo->getViewFactory()->newStatementSectionsView(
@@ -105,7 +112,7 @@
 
                $formsView = new LexemeFormsView(
                        $localizedTextProvider,
-                       new LexemeTemplateFactory( $templates ),
+                       $templateFactory,
                        $this->entityIdHtmlLinkFormatterFactory
                                ->getEntityIdFormatter( 
$retrievingLabelDescriptionLookup ),
                        $statementSectionsView
diff --git a/src/View/SensesView.php b/src/View/SensesView.php
index f0f5d74..3d2dc9d 100644
--- a/src/View/SensesView.php
+++ b/src/View/SensesView.php
@@ -2,6 +2,9 @@
 
 namespace Wikibase\Lexeme\View;
 
+use Wikibase\Lexeme\DataModel\Sense;
+use Wikibase\Lexeme\View\Template\LexemeTemplateFactory;
+use Wikibase\View\LanguageDirectionalityLookup;
 use Wikibase\View\LocalizedTextProvider;
 
 /**
@@ -14,24 +17,87 @@
         */
        private $textProvider;
 
-       public function __construct( LocalizedTextProvider $textProvider ) {
+       /**
+        * @var LanguageDirectionalityLookup
+        */
+       private $languageDirectionalityLookup;
+
+       /**
+        * @var LexemeTemplateFactory
+        */
+       private $templateFactory;
+
+       /**
+        * @var string
+        */
+       private $languageCode;
+
+       /**
+        * @param LocalizedTextProvider $textProvider
+        * @param LanguageDirectionalityLookup $languageDirectionalityLookup
+        * @param LexemeTemplateFactory $templateFactory
+        * @param string $languageCode
+        */
+       public function __construct(
+               LocalizedTextProvider $textProvider,
+               LanguageDirectionalityLookup $languageDirectionalityLookup,
+               LexemeTemplateFactory $templateFactory,
+               $languageCode
+       ) {
                $this->textProvider = $textProvider;
+               $this->languageDirectionalityLookup = 
$languageDirectionalityLookup;
+               $this->templateFactory = $templateFactory;
+               $this->languageCode = $languageCode;
        }
 
        /**
+        * @param Sense[] $senses
+        *
         * @return string HTML
         */
-       public function getHtml() {
-               $html = '<h2 class="wb-section-heading section-heading">'
+       public function getHtml( array $senses ) {
+               $html = '<div class="wikibase-lexeme-senses-section">';
+               $html .= '<h2 class="wb-section-heading section-heading">'
                        . '<span class="mw-headline" id="senses">'
                        . htmlspecialchars( $this->textProvider->get( 
'wikibase-lexeme-view-senses' ) )
                        . '</span>'
                        . '</h2>';
 
                $html .= '<div class="wikibase-lexeme-senses">';
+               foreach ( $senses as $sense ) {
+                       $html .= $this->getSenseHtml( $sense );
+               }
+               $html .= '</div>';
                $html .= '</div>';
 
                return $html;
        }
 
+       /**
+        * @param Sense $sense
+        *
+        * @return string HTML
+        */
+       private function getSenseHtml( Sense $sense ) {
+               $hasGloss = $sense->getGlosses()->hasTermForLanguage( 
$this->languageCode );
+               $emptyTextKey = 'wikibase-lexeme-gloss-empty';
+               $effectiveLanguage = $hasGloss
+                       ? $this->languageCode
+                       : $this->textProvider->getLanguageOf( $emptyTextKey );
+               return $this->templateFactory->render(
+                       'wikibase-lexeme-sense',
+                       [
+                               
$this->languageDirectionalityLookup->getDirectionality( $effectiveLanguage ) ?: 
'auto',
+                               $effectiveLanguage,
+                               $hasGloss ? '' : 'wb-empty',
+                               $hasGloss
+                                       ? $sense->getGlosses()->getByLanguage( 
$this->languageCode )->getText()
+                                       // TODO: should it rather fallback to 
gloss in language that exists?
+                                       : $this->textProvider->get( 
$emptyTextKey ),
+                               wfMessage( 'parentheses' )->rawParams( 
htmlspecialchars( $sense->getId()->getSerialization() ) )
+                                       ->text()
+                       ]
+               );
+       }
+
 }
diff --git a/tests/phpunit/mediawiki/Content/LexemeContentTest.php 
b/tests/phpunit/mediawiki/Content/LexemeContentTest.php
index 68d25d4..6eed097 100644
--- a/tests/phpunit/mediawiki/Content/LexemeContentTest.php
+++ b/tests/phpunit/mediawiki/Content/LexemeContentTest.php
@@ -27,7 +27,7 @@
        }
 
        public function testGetEntity() {
-               $lexeme = new Lexeme();
+               $lexeme = new Lexeme( new LexemeId( 'L1' ) );
                $lexemeContent = new LexemeContent( new EntityInstanceHolder( 
$lexeme ) );
 
                $this->assertSame( $lexeme, $lexemeContent->getEntity() );
@@ -44,7 +44,7 @@
        public function countableLexemeProvider() {
                $countable = [];
 
-               $lexeme = new Lexeme();
+               $lexeme = new Lexeme( new LexemeId( 'L1' ) );
                $lexeme->getStatements()->addNewStatement( new 
PropertyNoValueSnak( 42 ) );
                $countable[] = [ $lexeme ];
 
@@ -52,7 +52,9 @@
        }
 
        public function testNotCountable() {
-               $lexemeContent = new LexemeContent( new EntityInstanceHolder( 
new Lexeme() ) );
+               $lexemeContent = new LexemeContent( new EntityInstanceHolder(
+                       new Lexeme( new LexemeId( 'L1' ) )
+               ) );
                $this->assertFalse( $lexemeContent->isCountable() );
        }
 
diff --git a/tests/phpunit/mediawiki/View/SensesViewTest.php 
b/tests/phpunit/mediawiki/View/SensesViewTest.php
index b9523b7..b84d9e5 100644
--- a/tests/phpunit/mediawiki/View/SensesViewTest.php
+++ b/tests/phpunit/mediawiki/View/SensesViewTest.php
@@ -3,7 +3,14 @@
 namespace Wikibase\Lexeme\Tests\MediaWiki\View;
 
 use PHPUnit_Framework_TestCase;
+use Wikibase\DataModel\Statement\StatementList;
+use Wikibase\DataModel\Term\Term;
+use Wikibase\DataModel\Term\TermList;
+use Wikibase\Lexeme\DataModel\Sense;
+use Wikibase\Lexeme\DataModel\SenseId;
 use Wikibase\Lexeme\View\SensesView;
+use Wikibase\Lexeme\View\Template\LexemeTemplateFactory;
+use Wikibase\Repo\MediaWikiLanguageDirectionalityLookup;
 use Wikibase\View\DummyLocalizedTextProvider;
 
 /**
@@ -43,8 +50,55 @@
                );
        }
 
+       public function testHtmlContainsGlossWithId() {
+               $view = $this->newSensesView();
+               $html = $view->getHtml( [
+                       new Sense(
+                               new SenseId( 'L1-S1' ),
+                               new TermList( [ new Term( 'en', 'test gloss' ) 
] ),
+                               new StatementList()
+                       )
+               ] );
+
+               assertThat(
+                       $html,
+                       is( htmlPiece( havingChild(
+                               both( tagMatchingOutline( '<h3 dir="ltr" 
lang="en">' ) )
+                                       ->andAlso( havingTextContents( 
containsString( 'test gloss (L1-S1)' ) )
+                                       )
+                               ) ) )
+               );
+       }
+
+       public function 
testGivenNoGlossInDisplayLanguageHtmlContainsNoGlossMessage() {
+               $view = $this->newSensesView();
+               $html = $view->getHtml( [
+                       new Sense(
+                               new SenseId( 'L1-S1' ),
+                               new TermList( [ new Term( 'de', 'Testgloss' ) ] 
),
+                               new StatementList()
+                       )
+               ] );
+
+               assertThat(
+                       $html,
+                       is( htmlPiece( havingChild(
+                               both( tagMatchingOutline( '<h3 lang="qqx">' ) )
+                                       ->andAlso( havingTextContents( 
containsString( '(wikibase-lexeme-gloss-empty)' ) )
+                                       )
+                       ) ) )
+               );
+       }
+
        private function newSensesView() {
-               return new SensesView( new DummyLocalizedTextProvider() );
+               return new SensesView(
+                       new DummyLocalizedTextProvider(),
+                       new MediaWikiLanguageDirectionalityLookup(),
+                       new LexemeTemplateFactory( [
+                               'wikibase-lexeme-sense' => '<h3 dir="$1" 
lang="$2"><span class="$3">$4</span> $5</h3>',
+                       ] ),
+                       'en'
+               );
        }
 
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6b2d44231718eac87e95a791df3179c3a7074fea
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseLexeme
Gerrit-Branch: master
Gerrit-Owner: WMDE-leszek <leszek.mani...@wikimedia.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to