Adrian Heine has uploaded a new change for review.

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

Change subject: Introduce LanguageDirectionalityLookup
......................................................................

Introduce LanguageDirectionalityLookup

Change-Id: Icbf9b6c7b0dca72bb1aff49c3db1e9de0c50ec03
---
A repo/includes/MediaWikiLanguageDirectionalityLookup.php
M repo/includes/WikibaseRepo.php
M view/src/EntityView.php
M view/src/EntityViewPlaceholderExpander.php
M view/src/ItemView.php
A view/src/LanguageDirectionalityLookup.php
M view/src/PropertyView.php
M view/src/ViewFactory.php
M view/tests/phpunit/EntityViewPlaceholderExpanderTest.php
M view/tests/phpunit/ItemViewTest.php
M view/tests/phpunit/PropertyViewTest.php
M view/tests/phpunit/ViewFactoryTest.php
12 files changed, 102 insertions(+), 43 deletions(-)


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

diff --git a/repo/includes/MediaWikiLanguageDirectionalityLookup.php 
b/repo/includes/MediaWikiLanguageDirectionalityLookup.php
new file mode 100644
index 0000000..cf582f8
--- /dev/null
+++ b/repo/includes/MediaWikiLanguageDirectionalityLookup.php
@@ -0,0 +1,26 @@
+<?php
+
+namespace Wikibase\Repo;
+
+use Language;
+use Wikibase\View\LanguageDirectionalityLookup;
+
+/**
+ * Service for looking up language directionalities based on MediaWiki's 
Language
+ * class.
+ *
+ * @since 0.5
+ *
+ * @license GPL-2.0+
+ * @author Adrian Heine <adrian.he...@wikimedia.de>
+ */
+class MediaWikiLanguageDirectionalityLookup implements 
LanguageDirectionalityLookup {
+
+       /**
+        * @see LanguageDirectionalityLookup::getDirectionality
+        */
+       public function getDirectionality( $languageCode ) {
+               return Language::factory( $languageCode )->getDir();
+       }
+
+}
diff --git a/repo/includes/WikibaseRepo.php b/repo/includes/WikibaseRepo.php
index c912ea8..b4acc90 100644
--- a/repo/includes/WikibaseRepo.php
+++ b/repo/includes/WikibaseRepo.php
@@ -1536,6 +1536,7 @@
                        $this->getDataTypeFactory(),
                        TemplateFactory::getDefaultInstance(),
                        new LanguageNameLookup( $wgLang->getCode() ),
+                       new MediaWikiLanguageDirectionalityLookup(),
                        $this->settings->getSetting( 'siteLinkGroups' ),
                        $this->settings->getSetting( 'specialSiteLinkGroups' ),
                        $this->settings->getSetting( 'badgeItems' )
diff --git a/view/src/EntityView.php b/view/src/EntityView.php
index a7e10d7..33e901e 100644
--- a/view/src/EntityView.php
+++ b/view/src/EntityView.php
@@ -2,7 +2,6 @@
 
 namespace Wikibase\View;
 
-use Language;
 use Wikibase\DataModel\Entity\EntityDocument;
 use Wikibase\DataModel\Entity\EntityId;
 use Wikibase\DataModel\Term\FingerprintProvider;
@@ -38,9 +37,14 @@
        private $entityTermsView;
 
        /**
-        * @var Language
+        * @var LanguageDirectionalityLookup
         */
-       protected $language;
+       private $languageDirectionalityLookup;
+
+       /**
+        * @var string
+        */
+       protected $languageCode;
 
        /**
         * @var TextInjector
@@ -50,15 +54,18 @@
        /**
         * @param TemplateFactory $templateFactory
         * @param EntityTermsView $entityTermsView
-        * @param Language $language
+        * @param LanguageDirectionalityLookup $languageDirectionalityLookup
+        * @param string $languageCode
         */
        public function __construct(
                TemplateFactory $templateFactory,
                EntityTermsView $entityTermsView,
-               Language $language
+               LanguageDirectionalityLookup $languageDirectionalityLookup,
+               string $languageCode
        ) {
                $this->entityTermsView = $entityTermsView;
-               $this->language = $language;
+               $this->languageDirectionalityLookup = 
$languageDirectionalityLookup;
+               $this->languageCode = $languageCode;
 
                $this->textInjector = new TextInjector();
                $this->templateFactory = $templateFactory;
@@ -96,8 +103,8 @@
                $html = $this->templateFactory->render( 'wikibase-entityview',
                        $entity->getType(),
                        $entityId,
-                       $this->language->getCode(),
-                       $this->language->getDir(),
+                       $this->languageCode,
+                       $this->languageDirectionalityLookup->getDirectionality( 
$this->languageCode ) ?: 'auto',
                        $this->getMainHtml( $entityRevision ),
                        $this->getSideHtml( $entity )
                );
diff --git a/view/src/EntityViewPlaceholderExpander.php 
b/view/src/EntityViewPlaceholderExpander.php
index 25d9efe..8a68bf0 100644
--- a/view/src/EntityViewPlaceholderExpander.php
+++ b/view/src/EntityViewPlaceholderExpander.php
@@ -3,7 +3,6 @@
 namespace Wikibase\View;
 
 use InvalidArgumentException;
-use Language;
 use MWException;
 use RuntimeException;
 use Title;
@@ -51,9 +50,9 @@
        private $user;
 
        /**
-        * @var Language
+        * @var string
         */
-       private $uiLanguage;
+       private $uiLanguageCode;
 
        /**
         * @var EntityIdParser
@@ -89,7 +88,7 @@
         * @param TemplateFactory $templateFactory
         * @param Title $targetPage the page for which this expander is 
supposed to handle expansion.
         * @param User $user the current user
-        * @param Language $uiLanguage the user's current UI language (as per 
the present request)
+        * @param string $uiLanguageCode the user's current UI language (as per 
the present request)
         * @param EntityIdParser $entityIdParser
         * @param EntityRevisionLookup $entityRevisionLookup
         * @param UserLanguageLookup $userLanguageLookup
@@ -100,7 +99,7 @@
                TemplateFactory $templateFactory,
                Title $targetPage,
                User $user,
-               Language $uiLanguage,
+               $uiLanguageCode,
                EntityIdParser $entityIdParser,
                EntityRevisionLookup $entityRevisionLookup,
                UserLanguageLookup $userLanguageLookup,
@@ -109,7 +108,7 @@
        ) {
                $this->targetPage = $targetPage;
                $this->user = $user;
-               $this->uiLanguage = $uiLanguage;
+               $this->uiLanguageCode = $uiLanguageCode;
                $this->entityIdParser = $entityIdParser;
                $this->entityRevisionLookup = $entityRevisionLookup;
                $this->userLanguageLookup = $userLanguageLookup;
@@ -132,7 +131,7 @@
                                $this->extraLanguages = array();
                        } else {
                                // ignore current interface language
-                               $skip = array( $this->uiLanguage->getCode() );
+                               $skip = array( $this->uiLanguageCode );
                                $langs = array_diff(
                                        
$this->userLanguageLookup->getAllUserLanguages( $this->user ),
                                        $skip
@@ -241,7 +240,7 @@
         */
        public function renderTermBox( EntityId $entityId, $revisionId ) {
                $languages = array_merge(
-                       array( $this->uiLanguage->getCode() ),
+                       array( $this->uiLanguageCode ),
                        $this->getExtraUserLanguages()
                );
 
@@ -264,7 +263,7 @@
                        $this->templateFactory,
                        null,
                        $this->languageNameLookup,
-                       $this->uiLanguage->getCode()
+                       $this->uiLanguageCode
                );
 
                // FIXME: assumes all entities have a fingerprint
diff --git a/view/src/ItemView.php b/view/src/ItemView.php
index 90bbad9..eab9dc1 100644
--- a/view/src/ItemView.php
+++ b/view/src/ItemView.php
@@ -3,7 +3,6 @@
 namespace Wikibase\View;
 
 use InvalidArgumentException;
-use Language;
 use Wikibase\DataModel\Entity\EntityDocument;
 use Wikibase\DataModel\Entity\Item;
 use Wikibase\EntityRevision;
@@ -41,20 +40,22 @@
         *
         * @param TemplateFactory $templateFactory
         * @param EntityTermsView $entityTermsView
+        * @param LanguageDirectionalityLookup $languageDirectionalityLookup
         * @param StatementSectionsView $statementSectionsView
-        * @param Language $language
+        * @param string $languageCode
         * @param SiteLinksView $siteLinksView
         * @param string[] $siteLinkGroups
         */
        public function __construct(
                TemplateFactory $templateFactory,
                EntityTermsView $entityTermsView,
+               LanguageDirectionalityLookup $languageDirectionalityLookup,
                StatementSectionsView $statementSectionsView,
-               Language $language,
+               string $languageCode,
                SiteLinksView $siteLinksView,
                array $siteLinkGroups
        ) {
-               parent::__construct( $templateFactory, $entityTermsView, 
$language );
+               parent::__construct( $templateFactory, $entityTermsView, 
$languageDirectionalityLookup, $languageCode );
 
                $this->statementSectionsView = $statementSectionsView;
                $this->siteLinksView = $siteLinksView;
diff --git a/view/src/LanguageDirectionalityLookup.php 
b/view/src/LanguageDirectionalityLookup.php
new file mode 100644
index 0000000..d84901b
--- /dev/null
+++ b/view/src/LanguageDirectionalityLookup.php
@@ -0,0 +1,20 @@
+<?php
+
+namespace Wikibase\View;
+
+/**
+ * Returns the directionality of a language
+ *
+ * @since 0.5
+ *
+ * @license GPL-2.0+
+ * @author Adrian Heine < adrian.he...@wikimedia.de >
+ */
+interface LanguageDirectionalityLookup {
+       /**
+        * @param string $languageCode
+        *
+        * @return string|null 'ltr', 'rtl' or null if unknown
+        */
+       public function getDirectionality( $languageCode );
+}
diff --git a/view/src/PropertyView.php b/view/src/PropertyView.php
index 9a7bc9a..27d9a33 100644
--- a/view/src/PropertyView.php
+++ b/view/src/PropertyView.php
@@ -5,7 +5,6 @@
 use DataTypes\DataTypeFactory;
 use Html;
 use InvalidArgumentException;
-use Language;
 use Wikibase\DataModel\Entity\EntityDocument;
 use OutOfBoundsException;
 use Wikibase\DataModel\Entity\Property;
@@ -37,18 +36,20 @@
        /**
         * @param TemplateFactory $templateFactory
         * @param EntityTermsView $entityTermsView
+        * @param LanguageDirectionalityLookup $languageDirectionalityLookup
         * @param StatementSectionsView $statementSectionsView
         * @param DataTypeFactory $dataTypeFactory
-        * @param Language $language
+        * @param string $languageCode
         */
        public function __construct(
                TemplateFactory $templateFactory,
                EntityTermsView $entityTermsView,
+               LanguageDirectionalityLookup $languageDirectionalityLookup,
                StatementSectionsView $statementSectionsView,
                DataTypeFactory $dataTypeFactory,
-               Language $language
+               $languageCode
        ) {
-               parent::__construct( $templateFactory, $entityTermsView, 
$language );
+               parent::__construct( $templateFactory, $entityTermsView, 
$languageDirectionalityLookup, $languageCode );
 
                $this->statementSectionsView = $statementSectionsView;
                $this->dataTypeFactory = $dataTypeFactory;
@@ -101,7 +102,7 @@
                try {
                        $dataType = $this->dataTypeFactory->getType( 
$propertyType );
                        $html .= $this->templateFactory->render( 
'wikibase-propertyview-datatype',
-                               htmlspecialchars( $dataType->getLabel( 
$this->language->getCode() ) )
+                               htmlspecialchars( $dataType->getLabel( 
$this->languageCode ) )
                        );
                } catch ( OutOfBoundsException $ex ) {
                        $html .= Html::rawElement( 'span', array( 'class' => 
'error' ),
diff --git a/view/src/ViewFactory.php b/view/src/ViewFactory.php
index 3f62f74..77d5aa1 100644
--- a/view/src/ViewFactory.php
+++ b/view/src/ViewFactory.php
@@ -4,7 +4,6 @@
 
 use DataTypes\DataTypeFactory;
 use InvalidArgumentException;
-use Language;
 use SiteStore;
 use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup;
 use Wikibase\DataModel\Services\Statement\Grouper\StatementGrouper;
@@ -67,6 +66,11 @@
        private $languageNameLookup;
 
        /**
+        * @var LanguageDirectionalityLookup
+        */
+       private $languageDirectionalityLookup;
+
+       /**
         * @var string[]
         */
        private $siteLinkGroups;
@@ -90,6 +94,7 @@
         * @param DataTypeFactory $dataTypeFactory
         * @param TemplateFactory $templateFactory
         * @param LanguageNameLookup $languageNameLookup
+        * @param LanguageDirectionalityLookup $languageDirectionalityLookup
         * @param string[] $siteLinkGroups
         * @param string[] $specialSiteLinkGroups
         * @param string[] $badgeItems
@@ -105,6 +110,7 @@
                DataTypeFactory $dataTypeFactory,
                TemplateFactory $templateFactory,
                LanguageNameLookup $languageNameLookup,
+               LanguageDirectionalityLookup $languageDirectionalityLookup,
                array $siteLinkGroups = array(),
                array $specialSiteLinkGroups = array(),
                array $badgeItems = array()
@@ -123,6 +129,7 @@
                $this->dataTypeFactory = $dataTypeFactory;
                $this->templateFactory = $templateFactory;
                $this->languageNameLookup = $languageNameLookup;
+               $this->languageDirectionalityLookup = 
$languageDirectionalityLookup;
                $this->siteLinkGroups = $siteLinkGroups;
                $this->specialSiteLinkGroups = $specialSiteLinkGroups;
                $this->badgeItems = $badgeItems;
@@ -173,9 +180,6 @@
                        $editSectionGenerator
                );
 
-               // @fixme all that seems needed in ItemView is language code 
and dir.
-               $language = Language::factory( $languageCode );
-
                $siteLinksView = new SiteLinksView(
                        $this->templateFactory,
                        $this->siteStore->getSites(),
@@ -189,8 +193,9 @@
                return new ItemView(
                        $this->templateFactory,
                        $entityTermsView,
+                       $this->languageDirectionalityLookup,
                        $statementSectionsView,
-                       $language,
+                       $languageCode,
                        $siteLinksView,
                        $this->siteLinkGroups
                );
@@ -221,15 +226,13 @@
                        $editSectionGenerator
                );
 
-               // @fixme all that seems needed in PropertyView is language 
code and dir.
-               $language = Language::factory( $languageCode );
-
                return new PropertyView(
                        $this->templateFactory,
                        $entityTermsView,
+                       $this->languageDirectionalityLookup,
                        $statementSectionsView,
                        $this->dataTypeFactory,
-                       $language
+                       $languageCode
                );
        }
 
diff --git a/view/tests/phpunit/EntityViewPlaceholderExpanderTest.php 
b/view/tests/phpunit/EntityViewPlaceholderExpanderTest.php
index 995a2af..132bc1a 100644
--- a/view/tests/phpunit/EntityViewPlaceholderExpanderTest.php
+++ b/view/tests/phpunit/EntityViewPlaceholderExpanderTest.php
@@ -2,7 +2,6 @@
 
 namespace Wikibase\View\Tests;
 
-use Language;
 use MediaWikiTestCase;
 use Title;
 use User;
@@ -48,8 +47,6 @@
                        ->disableOriginalConstructor()
                        ->getMock();
 
-               $language = Language::factory( 'en' );
-
                $idParser = $this->getMockBuilder( EntityIdParser::class )
                        ->disableOriginalConstructor()
                        ->getMock();
@@ -68,7 +65,7 @@
                        $templateFactory,
                        $title,
                        $user,
-                       $language,
+                       'en',
                        $idParser,
                        $entityRevisionLookup,
                        $userLanguages,
diff --git a/view/tests/phpunit/ItemViewTest.php 
b/view/tests/phpunit/ItemViewTest.php
index c9ca4db..f41f6f2 100644
--- a/view/tests/phpunit/ItemViewTest.php
+++ b/view/tests/phpunit/ItemViewTest.php
@@ -2,13 +2,13 @@
 
 namespace Wikibase\View\Tests;
 
-use Language;
 use Wikibase\DataModel\Entity\EntityId;
 use Wikibase\DataModel\Entity\Item;
 use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\DataModel\Statement\StatementList;
 use Wikibase\View\EntityTermsView;
 use Wikibase\View\ItemView;
+use Wikibase\View\LanguageDirectionalityLookup;
 use Wikibase\View\SiteLinksView;
 use Wikibase\View\StatementSectionsView;
 use Wikibase\View\Template\TemplateFactory;
@@ -59,10 +59,11 @@
                        $this->getMockBuilder( EntityTermsView::class )
                                ->disableOriginalConstructor()
                                ->getMock(),
+                       $this->getMock( LanguageDirectionalityLookup::class ),
                        $this->getMockBuilder( StatementSectionsView::class )
                                ->disableOriginalConstructor()
                                ->getMock(),
-                       $this->getMock( Language::class ),
+                       'en',
                        $this->getMockBuilder( SiteLinksView::class )
                                ->disableOriginalConstructor()
                                ->getMock(),
diff --git a/view/tests/phpunit/PropertyViewTest.php 
b/view/tests/phpunit/PropertyViewTest.php
index 266e43b..3f56ae2 100644
--- a/view/tests/phpunit/PropertyViewTest.php
+++ b/view/tests/phpunit/PropertyViewTest.php
@@ -3,7 +3,6 @@
 namespace Wikibase\View\Tests;
 
 use DataTypes\DataTypeFactory;
-use Language;
 use Wikibase\DataModel\Entity\EntityDocument;
 use Wikibase\DataModel\Entity\EntityId;
 use Wikibase\DataModel\Entity\Property;
@@ -11,6 +10,7 @@
 use Wikibase\DataModel\Statement\Statement;
 use Wikibase\DataModel\Statement\StatementList;
 use Wikibase\View\EntityTermsView;
+use Wikibase\View\LanguageDirectionalityLookup;
 use Wikibase\View\PropertyView;
 use Wikibase\View\StatementSectionsView;
 use Wikibase\View\Template\TemplateFactory;
@@ -80,11 +80,12 @@
                        $this->getMockBuilder( EntityTermsView::class )
                                ->disableOriginalConstructor()
                                ->getMock(),
+                       $this->getMock( LanguageDirectionalityLookup::class ),
                        $this->getMockBuilder( StatementSectionsView::class )
                                ->disableOriginalConstructor()
                                ->getMock(),
                        $this->getDataTypeFactory(),
-                       Language::factory( 'en' )
+                       'en'
                );
 
                return array(
diff --git a/view/tests/phpunit/ViewFactoryTest.php 
b/view/tests/phpunit/ViewFactoryTest.php
index e53d4cb..2597c80 100644
--- a/view/tests/phpunit/ViewFactoryTest.php
+++ b/view/tests/phpunit/ViewFactoryTest.php
@@ -15,6 +15,7 @@
 use Wikibase\View\EditSectionGenerator;
 use Wikibase\View\HtmlSnakFormatterFactory;
 use Wikibase\View\ItemView;
+use Wikibase\View\LanguageDirectionalityLookup;
 use Wikibase\View\PropertyView;
 use Wikibase\View\ViewFactory;
 use Wikibase\View\EntityIdFormatterFactory;
@@ -67,6 +68,7 @@
                        new DataTypeFactory( array() ),
                        $templateFactory,
                        $languageNameLookup,
+                       $this->getMock( LanguageDirectionalityLookup::class ),
                        array(),
                        array(),
                        array()

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Icbf9b6c7b0dca72bb1aff49c3db1e9de0c50ec03
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Adrian Heine <adrian.l...@wikimedia.de>

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

Reply via email to