Bene has uploaded a new change for review.

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

Change subject: Change special pages for terms to work with narrower interfaces
......................................................................

Change special pages for terms to work with narrower interfaces

This patch changes the special pages from only working with entities that
implement FingerprintProvider to accept narrower interfaces like
LabelsProvider, DescriptionsProvider and AliasesProvider.

Change-Id: If456229e3605f769b21f8b5cb5d7cb8d1478a575
---
M repo/includes/Specials/SpecialModifyTerm.php
M repo/includes/Specials/SpecialSetAliases.php
M repo/includes/Specials/SpecialSetDescription.php
M repo/includes/Specials/SpecialSetLabel.php
4 files changed, 92 insertions(+), 34 deletions(-)


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

diff --git a/repo/includes/Specials/SpecialModifyTerm.php 
b/repo/includes/Specials/SpecialModifyTerm.php
index 467d893..2a808d2 100644
--- a/repo/includes/Specials/SpecialModifyTerm.php
+++ b/repo/includes/Specials/SpecialModifyTerm.php
@@ -126,10 +126,6 @@
                        return false;
                }
 
-               if ( !( $this->entityRevision->getEntity() instanceof 
FingerprintProvider ) ) {
-                       return false;
-               }
-
                try {
                        $this->checkTermChangePermissions( 
$this->entityRevision->getEntity() );
                } catch ( PermissionsError $e ) {
@@ -175,14 +171,8 @@
         * @throws InvalidArgumentException
         */
        private function checkTermChangePermissions( EntityDocument $entity ) {
-               if ( $entity instanceof Item ) {
-                       $type = 'item';
-               } elseif ( $entity instanceof Property ) {
-                       $type = 'property';
-               } else {
-                       throw new InvalidArgumentException( 'Unexpected Entity 
type when checking special page term change permissions' );
-               }
-               $restriction = $type . '-term';
+               $restriction = $entity->getType() . '-term';
+
                if ( !$this->getUser()->isAllowed( $restriction ) ) {
                        throw new PermissionsError( $restriction );
                }
@@ -273,12 +263,12 @@
                }
        }
 
-       private function setValueIfNull( FingerprintProvider 
$fingerprintProvider = null ) {
+       private function setValueIfNull( EntityDocument $entity = null ) {
                if ( $this->value === null ) {
-                       if ( $fingerprintProvider === null ) {
+                       if ( $entity === null ) {
                                $this->value = '';
                        } else {
-                               $this->value = $this->getValue( 
$fingerprintProvider->getFingerprint(), $this->languageCode );
+                               $this->value = $this->getValue( $entity, 
$this->languageCode );
                        }
                }
        }
@@ -297,12 +287,12 @@
         *
         * @since 0.5
         *
-        * @param Fingerprint $fingerprint
+        * @param EntityDocument $entity
         * @param string $languageCode
         *
         * @return string
         */
-       abstract protected function getValue( Fingerprint $fingerprint, 
$languageCode );
+       abstract protected function getValue( EntityDocument $entity, 
$languageCode );
 
        /**
         * Setting the value of the entity name by the given language
diff --git a/repo/includes/Specials/SpecialSetAliases.php 
b/repo/includes/Specials/SpecialSetAliases.php
index 1142794..c8103ad 100644
--- a/repo/includes/Specials/SpecialSetAliases.php
+++ b/repo/includes/Specials/SpecialSetAliases.php
@@ -4,6 +4,7 @@
 
 use InvalidArgumentException;
 use Wikibase\DataModel\Entity\EntityDocument;
+use Wikibase\DataModel\Term\AliasesProvider;
 use Wikibase\DataModel\Term\Fingerprint;
 use Wikibase\DataModel\Term\FingerprintProvider;
 use Wikibase\Summary;
@@ -30,6 +31,19 @@
        }
 
        /**
+        * @see SpecialModifyTerm::validateInput
+        *
+        * @return bool
+        */
+       protected function validateInput() {
+               if ( !parent::validateInput() ) {
+                       return false;
+               }
+
+               return $this->entityRevision->getEntity() instanceof 
AliasesProvider;
+       }
+
+       /**
         * @see SpecialSetEntity::getPostedValue()
         *
         * @since 0.4
@@ -45,14 +59,21 @@
         *
         * @since 0.4
         *
-        * @param Fingerprint $fingerprint
+        * @param EntityDocument $entity
         * @param string $languageCode
         *
+        * @throws InvalidArgumentException
         * @return string
         */
-       protected function getValue( Fingerprint $fingerprint, $languageCode ) {
-               if ( $fingerprint->hasAliasGroup( $languageCode ) ) {
-                       return implode( '|', $fingerprint->getAliasGroup( 
$languageCode )->getAliases() );
+       protected function getValue( EntityDocument $entity, $languageCode ) {
+               if ( !( $entity instanceof AliasesProvider ) ) {
+                       throw new InvalidArgumentException( '$entity must be an 
AliasesProvider' );
+               }
+
+               $aliases = $entity->getAliasGroups();
+
+               if ( $aliases->hasGroupForLanguage( $languageCode ) ) {
+                       return implode( '|', $aliases->getByLanguage( 
$languageCode )->getAliases() );
                }
 
                return '';
@@ -71,14 +92,14 @@
         * @return Summary
         */
        protected function setValue( EntityDocument $entity, $languageCode, 
$value ) {
-               if ( !( $entity instanceof FingerprintProvider ) ) {
-                       throw new InvalidArgumentException( '$entity must be a 
FingerprintProvider' );
+               if ( !( $entity instanceof AliasesProvider ) ) {
+                       throw new InvalidArgumentException( '$entity must be an 
AliasesProvider' );
                }
 
                $summary = new Summary( 'wbsetaliases' );
 
                if ( $value === '' ) {
-                       $aliases = $entity->getFingerprint()->getAliasGroup( 
$languageCode )->getAliases();
+                       $aliases = $entity->getAliasGroups()->getByLanguage( 
$languageCode )->getAliases();
                        $changeOp = 
$this->termChangeOpFactory->newRemoveAliasesOp( $languageCode, $aliases );
                } else {
                        $changeOp = 
$this->termChangeOpFactory->newSetAliasesOp( $languageCode, explode( '|', 
$value ) );
diff --git a/repo/includes/Specials/SpecialSetDescription.php 
b/repo/includes/Specials/SpecialSetDescription.php
index 16acb9b..02ff784 100644
--- a/repo/includes/Specials/SpecialSetDescription.php
+++ b/repo/includes/Specials/SpecialSetDescription.php
@@ -2,7 +2,9 @@
 
 namespace Wikibase\Repo\Specials;
 
+use InvalidArgumentException;
 use Wikibase\DataModel\Entity\EntityDocument;
+use Wikibase\DataModel\Term\DescriptionsProvider;
 use Wikibase\DataModel\Term\Fingerprint;
 use Wikibase\Summary;
 
@@ -28,6 +30,19 @@
        }
 
        /**
+        * @see SpecialModifyTerm::validateInput
+        *
+        * @return bool
+        */
+       protected function validateInput() {
+               if ( !parent::validateInput() ) {
+                       return false;
+               }
+
+               return $this->entityRevision->getEntity() instanceof 
DescriptionsProvider;
+       }
+
+       /**
         * @see SpecialSetEntity::getPostedValue()
         *
         * @since 0.4
@@ -43,15 +58,24 @@
         *
         * @since 0.4
         *
-        * @param Fingerprint $fingerprint
+        * @param EntityDocument $entity
         * @param string $languageCode
         *
+        * @throws InvalidArgumentException
         * @return string
         */
-       protected function getValue( Fingerprint $fingerprint, $languageCode ) {
-               return $fingerprint->hasDescription( $languageCode ) ?
-                       $fingerprint->getDescription( $languageCode )->getText()
-                       : '';
+       protected function getValue( EntityDocument $entity, $languageCode ) {
+               if ( !( $entity instanceof DescriptionsProvider ) ) {
+                       throw new InvalidArgumentException( '$entity must be a 
DescriptionsProvider' );
+               }
+
+               $descriptions = $entity->getDescriptions();
+
+               if ( $descriptions->hasTermForLanguage( $languageCode ) ) {
+                       return $descriptions->getByLanguage( $languageCode 
)->getText();
+               }
+
+               return '';
        }
 
        /**
diff --git a/repo/includes/Specials/SpecialSetLabel.php 
b/repo/includes/Specials/SpecialSetLabel.php
index ef69969..c581950 100644
--- a/repo/includes/Specials/SpecialSetLabel.php
+++ b/repo/includes/Specials/SpecialSetLabel.php
@@ -2,8 +2,10 @@
 
 namespace Wikibase\Repo\Specials;
 
+use InvalidArgumentException;
 use Wikibase\DataModel\Entity\EntityDocument;
 use Wikibase\DataModel\Term\Fingerprint;
+use Wikibase\DataModel\Term\LabelsProvider;
 use Wikibase\Summary;
 
 /**
@@ -28,6 +30,19 @@
        }
 
        /**
+        * @see SpecialModifyTerm::validateInput
+        *
+        * @return bool
+        */
+       protected function validateInput() {
+               if ( !parent::validateInput() ) {
+                       return false;
+               }
+
+               return $this->entityRevision->getEntity() instanceof 
LabelsProvider;
+       }
+
+       /**
         * @see SpecialSetEntity::getPostedValue()
         *
         * @since 0.4
@@ -43,15 +58,23 @@
         *
         * @since 0.4
         *
-        * @param Fingerprint $fingerprint
+        * @param EntityDocument $entity
         * @param string $languageCode
         *
         * @return string
         */
-       protected function getValue( Fingerprint $fingerprint, $languageCode ) {
-               return $fingerprint->hasLabel( $languageCode )
-                       ? $fingerprint->getLabel( $languageCode )->getText()
-                       : '';
+       protected function getValue( EntityDocument $entity, $languageCode ) {
+               if ( !( $entity instanceof LabelsProvider ) ) {
+                       throw new InvalidArgumentException( '$entity must be a 
LabelsProvider' );
+               }
+
+               $labels = $entity->getLabels();
+
+               if ( $labels->hasTermForLanguage( $languageCode ) ) {
+                       return $labels->getByLanguage( $languageCode 
)->getText();
+               }
+
+               return '';
        }
 
        /**

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: If456229e3605f769b21f8b5cb5d7cb8d1478a575
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Bene <[email protected]>

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

Reply via email to