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

Change subject: Fix lexeme data type labels
......................................................................

Fix lexeme data type labels

Created temporary LexemeId HTML formatter

Bug: T170918
Change-Id: I844e0acbf73224a395f6dc7050a8419ff37c6e73
---
M WikibaseLexeme.datatypes.php
A src/PropertyType/LexemeIdHtmlFormatter.php
2 files changed, 137 insertions(+), 0 deletions(-)


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

diff --git a/WikibaseLexeme.datatypes.php b/WikibaseLexeme.datatypes.php
index 77fb280..7a5115c 100644
--- a/WikibaseLexeme.datatypes.php
+++ b/WikibaseLexeme.datatypes.php
@@ -21,7 +21,9 @@
 use Wikibase\Lexeme\DataModel\Lexeme;
 use Wikibase\Lexeme\PropertyType\FormIdFormatter;
 use Wikibase\Lexeme\PropertyType\FormIdParser;
+use Wikibase\Lexeme\PropertyType\LexemeIdHtmlFormatter;
 use Wikibase\Lexeme\PropertyType\SenseIdFormatter;
+use Wikibase\Lib\SnakFormatter;
 use Wikibase\Repo\WikibaseRepo;
 
 return [
@@ -31,6 +33,18 @@
                        $factory = WikibaseRepo::getDefaultValidatorBuilders();
                        return $factory->getEntityValidators( 
Lexeme::ENTITY_TYPE );
                },
+               'formatter-factory-callback' => function ( $format, 
FormatterOptions $options ) {
+                       if ( $format === SnakFormatter::FORMAT_HTML_WIDGET ||
+                               $format === SnakFormatter::FORMAT_HTML ) {
+                               return new LexemeIdHtmlFormatter(
+                                       
WikibaseRepo::getDefaultInstance()->getEntityLookup(),
+                                       
WikibaseRepo::getDefaultInstance()->getEntityTitleLookup()
+                               );
+                       }
+
+                       return WikibaseRepo::getDefaultValueFormatterBuilders()
+                               ->newEntityIdFormatter( $format, $options );
+               },
                'value-type' => 'wikibase-entityid',
        ],
        'PT:wikibase-lexeme-form' => [
diff --git a/src/PropertyType/LexemeIdHtmlFormatter.php 
b/src/PropertyType/LexemeIdHtmlFormatter.php
new file mode 100644
index 0000000..34a251d
--- /dev/null
+++ b/src/PropertyType/LexemeIdHtmlFormatter.php
@@ -0,0 +1,123 @@
+<?php
+
+namespace Wikibase\Lexeme\PropertyType;
+
+use Html;
+use InvalidArgumentException;
+use ValueFormatters\FormattingException;
+use ValueFormatters\ValueFormatter;
+use Wikibase\DataModel\Entity\EntityIdValue;
+use Wikibase\DataModel\Entity\Item;
+use Wikibase\DataModel\Services\Lookup\EntityLookup;
+use Wikibase\Lexeme\DataModel\Lexeme;
+use Wikibase\Lexeme\DataModel\LexemeId;
+use Wikibase\Lib\Store\EntityTitleLookup;
+
+class LexemeIdHtmlFormatter  implements ValueFormatter {
+
+       /**
+        * @var EntityLookup
+        */
+       private $entityLookup;
+
+       /**
+        * @var EntityTitleLookup
+        */
+       private $entityTitleLookup;
+
+       public function __construct(
+               EntityLookup $entityLookup,
+               EntityTitleLookup $entityTitleLookup
+       ) {
+               $this->entityLookup = $entityLookup;
+               $this->entityTitleLookup = $entityTitleLookup;
+       }
+
+       /**
+        * @param mixed $value
+        *
+        * @return string
+        * @throws FormattingException
+        */
+       public function format( $value ) {
+               if ( !( $value instanceof EntityIdValue ) ) {
+                       throw new InvalidArgumentException(
+                               'Data value type mismatch. Expected an 
EntityIdValue.'
+                       );
+               }
+
+               if ( !( $value->getEntityId() instanceof LexemeId ) ) {
+                       throw new InvalidArgumentException(
+                               'Data value type mismatch. Expected an 
EntityIdValue referencing Lexeme.'
+                       );
+               }
+
+               /** @var LexemeId $lexemeId */
+               $lexemeId = $value->getEntityId();
+               /** @var Lexeme $lexeme */
+               $lexeme = $this->entityLookup->getEntity( $lexemeId );
+
+               $title = $this->entityTitleLookup->getTitleForId( $lexemeId );
+
+               if ( $title === null ) {
+                       return $this->getHtmlForNonExistent( $lexemeId );
+               }
+
+               $url = $title->isLocal() ? $title->getLocalURL() : 
$title->getFullURL();
+
+               $label = $this->buildLabel( $lexeme );
+               $attributes = [
+                       'href' => $url
+               ];
+
+               return Html::element( 'a', $attributes, $label );
+       }
+
+       /**
+        * @param LexemeId $lexemeId
+        *
+        * @return string HTML
+        */
+       private function getHtmlForNonExistent( LexemeId $lexemeId ) {
+               $attributes = [ 'class' => 'wb-entity-undefinedinfo' ];
+
+               $message = wfMessage( 'parentheses',
+                                                         wfMessage( 
'wikibase-deletedentity-lexeme' )->text()
+               );
+
+               $undefinedInfo = Html::element( 'span', $attributes, $message );
+
+               $separator = wfMessage( 'word-separator' )->text();
+               return $lexemeId->getSerialization() . $separator . 
$undefinedInfo;
+       }
+
+       private function buildLabel( Lexeme $lexeme ) {
+               $label = '';
+
+               $label .= wfMessage( 'parentheses',
+                                                        
$lexeme->getId()->getSerialization()
+               );
+               $label .= ' ';
+
+               $glossTexts = [];
+               foreach ( $lexeme->getLemmas() as $lemma ) {
+                       $glossTexts[] = $lemma->getText();
+               }
+
+               $label .= implode( ', ', $glossTexts );
+               $label .= ': ';
+
+               /** @var Item $languageItem */
+               $languageItem = $this->entityLookup->getEntity( 
$lexeme->getLanguage() );
+               /** @var Item $lexicalCategoryItem */
+               $lexicalCategoryItem = $this->entityLookup->getEntity( 
$lexeme->getLexicalCategory() );
+
+               // 'noun in English'
+               $label .= $lexicalCategoryItem->getLabels()->getByLanguage( 
'en' )->getText();
+               $label .= ' in ';
+               $label .= $languageItem->getLabels()->getByLanguage( 'en' 
)->getText();
+
+               return $label;
+       }
+
+}

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

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