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

Change subject: Don't use services in EntityContent
......................................................................


Don't use services in EntityContent

Content objects should ideally be value objects. This moves
any code that relies on services into the associated ContentHandler,
instead of having it in the Content object itself. This allows
such services to be injected when constructing the handler.

Note: getEntityView still needs to be factored out, see bug 66486.

Change-Id: I99753f806fa37bd21cc122a68dd47c4565484b25
---
M repo/Wikibase.hooks.php
M repo/includes/WikibaseRepo.php
M repo/includes/content/EntityContent.php
M repo/includes/content/EntityHandler.php
M repo/includes/content/ItemHandler.php
M repo/includes/content/PropertyHandler.php
6 files changed, 96 insertions(+), 91 deletions(-)

Approvals:
  WikidataJenkins: Verified
  Thiemo Mättig (WMDE): Looks good to me, approved
  jenkins-bot: Verified



diff --git a/repo/Wikibase.hooks.php b/repo/Wikibase.hooks.php
index 4c5274e..14432c5 100644
--- a/repo/Wikibase.hooks.php
+++ b/repo/Wikibase.hooks.php
@@ -1245,9 +1245,10 @@
                $codec = $repo->getEntityContentDataCodec();
                $entityPerPage = $repo->getStore()->newEntityPerPage();
                $termIndex = $repo->getStore()->getTermIndex();
+               $errorLocalizer = $repo->getValidatorErrorLocalizer();
                $siteLinkStore = $repo->getStore()->newSiteLinkCache();
 
-               return new ItemHandler( $entityPerPage, $termIndex, $codec, 
array( $validator ), $siteLinkStore );
+               return new ItemHandler( $entityPerPage, $termIndex, $codec, 
array( $validator ), $errorLocalizer, $siteLinkStore );
        }
 
        private static function newPropertyHandler() {
@@ -1256,9 +1257,10 @@
                $codec = $repo->getEntityContentDataCodec();
                $entityPerPage = $repo->getStore()->newEntityPerPage();
                $termIndex = $repo->getStore()->getTermIndex();
+               $errorLocalizer = $repo->getValidatorErrorLocalizer();
                $propertyInfoStore = $repo->getStore()->getPropertyInfoStore();
 
-               return new PropertyHandler( $entityPerPage, $termIndex, $codec, 
array( $validator ), $propertyInfoStore );
+               return new PropertyHandler( $entityPerPage, $termIndex, $codec, 
array( $validator ), $errorLocalizer, $propertyInfoStore );
        }
 
        /**
diff --git a/repo/includes/WikibaseRepo.php b/repo/includes/WikibaseRepo.php
index dc670fd..8db9b9b 100644
--- a/repo/includes/WikibaseRepo.php
+++ b/repo/includes/WikibaseRepo.php
@@ -6,6 +6,7 @@
 use DataValues\DataValueFactory;
 use SiteSQLStore;
 use SiteStore;
+use StubObject;
 use ValueFormatters\FormatterOptions;
 use ValueFormatters\ValueFormatter;
 use Wikibase\ChangeNotifier;
@@ -672,6 +673,7 @@
         */
        protected function getMessageParameterFormatter() {
                global $wgLang;
+               StubObject::unstub( $wgLang );
 
                $formatterOptions = new FormatterOptions();
                $valueFormatterBuilders = $this->getValueFormatterBuilders();
diff --git a/repo/includes/content/EntityContent.php 
b/repo/includes/content/EntityContent.php
index e272add..e5bbe5a 100644
--- a/repo/includes/content/EntityContent.php
+++ b/repo/includes/content/EntityContent.php
@@ -4,8 +4,8 @@
 
 use AbstractContent;
 use Content;
-use Diff\DiffOp\Diff\Diff;
 use DataUpdate;
+use Diff\DiffOp\Diff\Diff;
 use IContextSource;
 use ParserOptions;
 use ParserOutput;
@@ -15,16 +15,14 @@
 use User;
 use ValueFormatters\FormatterOptions;
 use ValueFormatters\ValueFormatter;
-use ValueValidators\Result;
 use Wikibase\DataModel\Entity\BasicEntityIdParser;
-use Wikibase\Repo\Content\EntityContentDiff;
 use Wikibase\DataModel\Entity\EntityIdParser;
 use Wikibase\Lib\PropertyDataTypeLookup;
 use Wikibase\Lib\Serializers\SerializationOptions;
 use Wikibase\Lib\SnakFormatter;
+use Wikibase\Repo\Content\EntityContentDiff;
 use Wikibase\Repo\EntitySearchTextGenerator;
 use Wikibase\Repo\WikibaseRepo;
-use Wikibase\Validators\EntityValidator;
 use WikiPage;
 
 /**
@@ -37,7 +35,6 @@
  * @author Daniel Kinzler
  */
 abstract class EntityContent extends AbstractContent {
-
 
        /**
         * For use in the wb-status page property to indicate that the entity 
has no special
@@ -77,27 +74,6 @@
        }
 
        /**
-        * Returns the Title for the item or false if there is none.
-        *
-        * @since 0.1
-        * @deprecated use EntityTitleLookup instead
-        *
-        * @deprecated since 0.5, use EntityTitleLookup:.getTitleForId instead.
-        *
-        * @return Title|bool
-        */
-       public function getTitle() {
-               $id = $this->getEntity()->getId();
-
-               if ( !$id ) {
-                       return false;
-               }
-
-               $lookup = 
WikibaseRepo::getDefaultInstance()->getEntityTitleLookup();
-               return $lookup->getTitleForId( $id );
-       }
-
-       /**
         * Returns the entity contained by this entity content.
         * Deriving classes typically have a more specific get method as
         * for greater clarity and type hinting.
@@ -107,6 +83,18 @@
         * @return Entity
         */
        abstract public function getEntity();
+
+       /**
+        * Returns the ID of the entity represented by this EntityContent;
+        *
+        * @return null|EntityId
+        *
+        * @todo: Force an ID to be present; Entity objects without an ID may 
sense, EntityContent
+        * objects with no entity ID don't.
+        */
+       public function getEntityId() {
+               return $this->getEntity()->getId();
+       }
 
        /**
         * @see Content::getDeletionUpdates
@@ -499,13 +487,11 @@
         * @return ItemContent
         */
        public function copy() {
-               $array = array();
+               /* @var EntityHandler $handler */
+               $handler = $this->getContentHandler();
 
-               foreach ( $this->getEntity()->toArray() as $key => $value ) {
-                       $array[$key] = is_object( $value ) ? clone $value : 
$value;
-               }
-
-               return static::newFromArray( $array );
+               $entity = $this->getEntity()->copy();
+               return $handler->makeEntityContent( $entity );
        }
 
        /**
@@ -528,34 +514,12 @@
                        return $status;
                }
 
-               $status = $this->applyOnSaveValidators();
+               /* @var EntityHandler $handler */
+               $handler = $this->getContentHandler();
+               $status = $handler->applyOnSaveValidators( $this );
 
                wfProfileOut( __METHOD__ );
                return $status;
-       }
-
-       /**
-        * Apply all EntityValidators registered for on-save validation.
-        */
-       private function applyOnSaveValidators() {
-               /* @var EntityHandler $handler */
-               $handler = $this->getContentHandler();
-               $validators = $handler->getOnSaveValidators();
-
-               $entity = $this->getEntity();
-               $result = Result::newSuccess();
-
-               /* @var EntityValidator $validator */
-               foreach ( $validators as $validator ) {
-                       $result = $validator->validateEntity( $entity );
-
-                       if ( !$result->isValid() ) {
-                               break;
-                       }
-               }
-
-               $localizer = 
WikibaseRepo::getDefaultInstance()->getValidatorErrorLocalizer();
-               return $localizer->getResultStatus( $result );
        }
 
        /**
diff --git a/repo/includes/content/EntityHandler.php 
b/repo/includes/content/EntityHandler.php
index 7ea23df..595d9f8 100644
--- a/repo/includes/content/EntityHandler.php
+++ b/repo/includes/content/EntityHandler.php
@@ -14,10 +14,17 @@
 use Revision;
 use Title;
 use User;
+use ValueFormatters\FormatterOptions;
+use ValueFormatters\ValueFormatter;
+use ValueValidators\Result;
 use Wikibase\DataModel\Entity\BasicEntityIdParser;
+use Wikibase\Lib\Serializers\SerializationOptions;
+use Wikibase\Lib\SnakFormatter;
 use Wikibase\Lib\Store\EntityContentDataCodec;
+use Wikibase\Repo\WikibaseRepo;
 use Wikibase\Updates\DataUpdateClosure;
 use Wikibase\Validators\EntityValidator;
+use Wikibase\Validators\ValidatorErrorLocalizer;
 
 /**
  * Base handler class for Wikibase\Entity content classes.
@@ -51,18 +58,25 @@
        private $termIndex;
 
        /**
+        * @var ValidatorErrorLocalizer
+        */
+       private $errorLocalizer;
+
+       /**
         * @param string $modelId
         * @param EntityPerPage $entityPerPage
         * @param TermIndex $termIndex
         * @param EntityContentDataCodec $contentCodec
         * @param EntityValidator[] $preSaveValidators
+        * @param ValidatorErrorLocalizer $errorLocalizer
         */
        public function __construct(
                $modelId,
                EntityPerPage $entityPerPage,
                TermIndex $termIndex,
                EntityContentDataCodec $contentCodec,
-               array $preSaveValidators
+               array $preSaveValidators,
+               ValidatorErrorLocalizer $errorLocalizer
        ) {
                $formats = $contentCodec->getSupportedFormats();
 
@@ -72,6 +86,7 @@
                $this->preSaveValidators = $preSaveValidators;
                $this->entityPerPage = $entityPerPage;
                $this->termIndex = $termIndex;
+               $this->errorLocalizer = $errorLocalizer;
        }
 
        /**
@@ -95,13 +110,26 @@
        }
 
        /**
-        * Returns a set of validators for enforcing hard constraints on the 
content
-        * before saving. For soft constraints, see the TermValidatorFactory.
+        * Apply all EntityValidators registered for on-save validation
         *
-        * @return EntityValidator[]
+        * @param EntityContent $content
+        *
+        * @return \Status
         */
-       public function getOnSaveValidators() {
-               return $this->preSaveValidators;
+       public function applyOnSaveValidators( EntityContent $content ) {
+               $entity = $content->getEntity();
+               $result = Result::newSuccess();
+
+               /* @var EntityValidator $validator */
+               foreach ( $this->preSaveValidators as $validator ) {
+                       $result = $validator->validateEntity( $entity );
+
+                       if ( !$result->isValid() ) {
+                               break;
+                       }
+               }
+
+               return $this->errorLocalizer->getResultStatus( $result );
        }
 
        /**
diff --git a/repo/includes/content/ItemHandler.php 
b/repo/includes/content/ItemHandler.php
index 149179d..5da34e1 100644
--- a/repo/includes/content/ItemHandler.php
+++ b/repo/includes/content/ItemHandler.php
@@ -8,6 +8,7 @@
 use Wikibase\Lib\Store\SiteLinkCache;
 use Wikibase\Updates\DataUpdateClosure;
 use Wikibase\Validators\EntityValidator;
+use Wikibase\Validators\ValidatorErrorLocalizer;
 
 /**
  * Content handler for Wikibase items.
@@ -25,6 +26,34 @@
        private $siteLinkStore;
 
        /**
+        * @param EntityPerPage $entityPerPage
+        * @param TermIndex $termIndex
+        * @param EntityContentDataCodec $contentCodec
+        * @param EntityValidator[] $preSaveValidators
+        * @param ValidatorErrorLocalizer $errorLocalizer
+        * @param SiteLinkCache $siteLinkStore
+        */
+       public function __construct(
+               EntityPerPage $entityPerPage,
+               TermIndex $termIndex,
+               EntityContentDataCodec $contentCodec,
+               array $preSaveValidators,
+               ValidatorErrorLocalizer $errorLocalizer,
+               SiteLinkCache $siteLinkStore
+       ) {
+               parent::__construct(
+                       CONTENT_MODEL_WIKIBASE_ITEM,
+                       $entityPerPage,
+                       $termIndex,
+                       $contentCodec,
+                       $preSaveValidators,
+                       $errorLocalizer
+               );
+
+               $this->siteLinkStore = $siteLinkStore;
+       }
+
+       /**
         * @see EntityHandler::getContentClass
         *
         * @since 0.3
@@ -33,31 +62,6 @@
         */
        protected function getContentClass() {
                return '\Wikibase\ItemContent';
-       }
-
-       /**
-        * @param EntityPerPage $entityPerPage
-        * @param TermIndex $termIndex
-        * @param EntityContentDataCodec $contentCodec
-        * @param EntityValidator[] $preSaveValidators
-        * @param SiteLinkCache $siteLinkStore
-        */
-       public function __construct(
-               EntityPerPage $entityPerPage,
-               TermIndex $termIndex,
-               EntityContentDataCodec $contentCodec,
-               array $preSaveValidators,
-               SiteLinkCache $siteLinkStore
-       ) {
-               parent::__construct(
-                       CONTENT_MODEL_WIKIBASE_ITEM,
-                       $entityPerPage,
-                       $termIndex,
-                       $contentCodec,
-                       $preSaveValidators
-               );
-
-               $this->siteLinkStore = $siteLinkStore;
        }
 
        /**
@@ -142,4 +146,5 @@
                        parent::getEntityModificationUpdates( $content, $title )
                );
        }
+
 }
diff --git a/repo/includes/content/PropertyHandler.php 
b/repo/includes/content/PropertyHandler.php
index e697721..ce54d6b 100644
--- a/repo/includes/content/PropertyHandler.php
+++ b/repo/includes/content/PropertyHandler.php
@@ -7,6 +7,7 @@
 use Wikibase\Lib\Store\EntityContentDataCodec;
 use Wikibase\Updates\DataUpdateClosure;
 use Wikibase\Validators\EntityValidator;
+use Wikibase\Validators\ValidatorErrorLocalizer;
 
 /**
  * Content handler for Wikibase items.
@@ -40,6 +41,7 @@
         * @param TermIndex $termIndex
         * @param EntityContentDataCodec $contentCodec
         * @param EntityValidator[] $preSaveValidators
+        * @param ValidatorErrorLocalizer $errorLocalizer
         * @param PropertyInfoStore $infoStore
         */
        public function __construct(
@@ -47,6 +49,7 @@
                TermIndex $termIndex,
                EntityContentDataCodec $contentCodec,
                $preSaveValidators,
+               ValidatorErrorLocalizer $errorLocalizer,
                PropertyInfoStore $infoStore
        ) {
                parent::__construct(
@@ -54,7 +57,8 @@
                        $entityPerPage,
                        $termIndex,
                        $contentCodec,
-                       $preSaveValidators
+                       $preSaveValidators,
+                       $errorLocalizer
                );
 
                $this->infoStore = $infoStore;

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I99753f806fa37bd21cc122a68dd47c4565484b25
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Adrian Lang <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Hoo man <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: WikidataJenkins <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to