Ladsgroup has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/329118 )

Change subject: [Code experiment] Try to use api change op callbacks
......................................................................

[Code experiment] Try to use api change op callbacks

Change-Id: I4744774fe460b7421bb9d7060795d6fd99053673
---
A src/ChangeOp/ChangeOpsApiCallbacks.php
A src/ChangeOp/LexemeChangeOpFactory.php
2 files changed, 142 insertions(+), 0 deletions(-)


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

diff --git a/src/ChangeOp/ChangeOpsApiCallbacks.php 
b/src/ChangeOp/ChangeOpsApiCallbacks.php
new file mode 100644
index 0000000..8e363cc
--- /dev/null
+++ b/src/ChangeOp/ChangeOpsApiCallbacks.php
@@ -0,0 +1,96 @@
+<?php
+namespace Wikibase\Lexeme\ChangeOp;
+
+use Wikibase\ChangeOp\ChangeOp;
+use Wikibase\ChangeOp\ChangeOps;
+use Wikibase\DataModel\Entity\EntityDocument;
+use Wikibase\Repo\WikibaseRepo;
+use Wikibase\StringNormalizer;
+use Wikimedia\Assert\Assert;
+use Wikibase\Lexeme\DataModel\Lexeme;
+
+/**
+ * Class for creating ChangeOps for EditEntity API
+ *
+ * @license GPL-2.0+
+ * @author Amir Sarabadani <ladsgr...@gmail.com>
+ */
+class LexemeChangeOpsApiCallbacks {
+
+       /**
+        * @var StringNormalizer
+        */
+       protected $stringNormalizer;
+
+       /**
+        * @var LexemeChangeOpFactory
+        */
+       private $lexemeChangeOpFactory;
+
+       public function __construct() {
+               $wikibaseRepo = WikibaseRepo::getDefaultInstance();
+               $this->stringNormalizer = $wikibaseRepo->getStringNormalizer();
+
+               $changeOpFactoryProvider = 
$wikibaseRepo->getChangeOpFactoryProvider();
+               $this->termChangeOpFactory = 
$changeOpFactoryProvider->getFingerprintChangeOpFactory();
+       }
+
+       public function run( array $data, ChangeOps $changeOps, EntityDocument 
$entity ) {
+               Assert::parameterType( Lexeme::class, $entity, '$entity' );
+
+               if ( array_key_exists( 'lemmas', $data ) ) {
+                       $this->assertArray( $data['lemmas'], 'List of lemmas 
must be an array' );
+                       $changeOps->add( $this->getLemmasChangeOps( 
$data['lemmas'] ) );
+               }
+
+               return $changeOps;
+       }
+
+       /**
+        * @param array[] $lemmas
+        *
+        * @return ChangeOp[]
+        */
+       private function getLemmasChangeOps( array $lemmas ) {
+               $lemmasChangeOps = [];
+
+               foreach ( $lemmas as $langCode => $arg ) {
+                       // Add validateMultilangArgs
+
+                       $language = $arg['language'];
+                       $newLemma = ( array_key_exists( 'remove', $arg ) ? '' :
+                               $this->stringNormalizer->trimToNFC( 
$arg['value'] ) );
+
+                       if ( $newLemma === '' ) {
+                               $lemmasChangeOps[] = 
$this->lexemeChangeOpFactory->newRemoveLemmaOp( $language );
+                       } else {
+                               $lemmasChangeOps[] = 
$this->lexemeChangeOpFactory->newSetLemmaOp(
+                                       $language,
+                                       $newLemma
+                               );
+                       }
+               }
+
+               return $lemmasChangeOps;
+       }
+
+       /**
+        * @param mixed $value
+        * @param string $message
+        */
+       private function assertArray( $value, $message ) {
+               $this->assertType( 'array', $value, $message );
+       }
+
+       /**
+        * @param string $type
+        * @param mixed $value
+        * @param string $message
+        */
+       private function assertType( $type, $value, $message ) {
+               if ( gettype( $value ) !== $type ) {
+                       // TODO: This should return error back to EditEntity 
API not error out here
+                       $this->errorReporter->dieError( $message, 
'not-recognized-' . $type );
+               }
+       }
+}
\ No newline at end of file
diff --git a/src/ChangeOp/LexemeChangeOpFactory.php 
b/src/ChangeOp/LexemeChangeOpFactory.php
new file mode 100644
index 0000000..65232ea
--- /dev/null
+++ b/src/ChangeOp/LexemeChangeOpFactory.php
@@ -0,0 +1,46 @@
+<?php
+namespace Wikibase\Lexeme\ChangeOp;
+use Doctrine\Instantiator\Exception\InvalidArgumentException;
+use Wikibase\ChangeOp\ChangeOp;
+use Wikibase\Repo\Validators\TermValidatorFactory;
+
+/**
+ * Factory to return changeOps of Lexeme parts.
+ *
+ * @license GPL-2.0+
+ * @author Amir Sarabadani <ladsgr...@gmail.com>
+ */
+class LexemeChangeOpFactory {
+       /**
+        * @var TermValidatorFactory
+        */
+       private $termValidatorFactory;
+
+       /**
+        * @param TermValidatorFactory $termValidatorFactory
+        */
+       public function __construct( TermValidatorFactory $termValidatorFactory 
) {
+               $this->termValidatorFactory = $termValidatorFactory;
+       }
+
+       /**
+        * @param string $languageCode
+        * @param string $lemma
+        *
+        * @throws InvalidArgumentException
+        * @return ChangeOp
+        */
+       public function newSetLemmaOp( $languageCode, $lemma ) {
+               return new ChangeOpLemma( $languageCode, $lemma, 
$this->termValidatorFactory );
+       }
+
+       /**
+        * @param string $languageCode
+        *
+        * @throws InvalidArgumentException
+        * @return ChangeOp
+        */
+       public function newRemoveLemmaOp( $languageCode ) {
+               return new ChangeOpLemma( $languageCode, null, 
$this->termValidatorFactory );
+       }
+}
\ No newline at end of file

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4744774fe460b7421bb9d7060795d6fd99053673
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseLexeme
Gerrit-Branch: master
Gerrit-Owner: Ladsgroup <ladsgr...@gmail.com>

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

Reply via email to