WMDE-leszek has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/397854 )

Change subject: [DNM] Use change op to add a form in AddForm API
......................................................................

[DNM] Use change op to add a form in AddForm API

TODO:
 - getting a data of form added to be included in the API
   response is really nasty
 - test new stuff in the ChangeOp class

Change-Id: I0b12018d9c93ff79e0f3abcc4f6405038ce205f8
---
M src/Api/AddForm.php
M src/Api/AddFormRequest.php
M src/ChangeOp/ChangeOpAddForm.php
3 files changed, 41 insertions(+), 44 deletions(-)


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

diff --git a/src/Api/AddForm.php b/src/Api/AddForm.php
index 0cec37a..ecca98c 100644
--- a/src/Api/AddForm.php
+++ b/src/Api/AddForm.php
@@ -6,11 +6,13 @@
 use ApiMain;
 use Wikibase\EditEntityFactory;
 use Wikibase\Lexeme\Api\Error\LexemeNotFound;
+use Wikibase\Lexeme\DataModel\FormId;
 use Wikibase\Lexeme\DataModel\Lexeme;
 use Wikibase\Lexeme\DataModel\Serialization\FormSerializer;
 use Wikibase\Lib\Store\EntityRevisionLookup;
 use Wikibase\Lib\Store\StorageException;
 use Wikibase\Repo\Api\ApiErrorReporter;
+use Wikibase\Summary;
 use Wikibase\SummaryFormatter;
 
 /**
@@ -104,26 +106,6 @@
         * @throws \ApiUsageException
         */
        public function execute() {
-               /*
-                * {
-                         "representations": [
-                               {
-                                 "representation": "",
-                                 "language": ""
-                               },
-                               {
-                                 "representation": "",
-                                 "language": ""
-                               }
-                         ],
-                         "grammaticalFeatures": [
-                               "Q1",
-                               "Q2"
-                         ]
-                       }
-                *
-                */
-
                //FIXME: Response structure? - Added form
                //FIXME: Representation text normalization
 
@@ -167,16 +149,20 @@
                }
                /** @var Lexeme $lexeme */
                $lexeme = $lexemeRevision->getEntity();
-               $newForm = $request->addFormTo( $lexeme );
+
+               $changeOp = $request->getChangeOp();
+
+               $summary = new Summary();
+
+               $changeOp->apply( $lexeme, $summary );
 
                $editEntity = $this->editEntityFactory->newEditEntity(
                        $this->getUser(),
                        $request->getLexemeId(),
                        $lexemeRevision->getRevisionId()
                );
-               $summaryString = $this->summaryFormatter->formatSummary(
-                       new AddFormSummary( $lexeme->getId(), $newForm )
-               );
+
+               $summaryString = $this->summaryFormatter->formatSummary( 
$summary );
                $flags = EDIT_UPDATE;
                if ( isset( $params['bot'] ) && $params['bot'] && 
$this->getUser()->isAllowed( 'bot' ) ) {
                        $flags |= EDIT_FORCE_BOT;
@@ -195,6 +181,10 @@
                        $this->dieStatus( $status ); //Seems like it is good 
enough
                }
 
+               // TODO: NASTY! How to do it nicer?
+               $newFormId = $lexemeId . '-F' . 
$lexeme->getForms()->maxFormIdNumber();
+               $newForm = $lexeme->getForm( new FormId( $newFormId ) );
+
                $apiResult = $this->getResult();
 
                $statusValue = $status->getValue();
diff --git a/src/Api/AddFormRequest.php b/src/Api/AddFormRequest.php
index 175af14..76d5545 100644
--- a/src/Api/AddFormRequest.php
+++ b/src/Api/AddFormRequest.php
@@ -62,15 +62,4 @@
                return $this->lexemeId;
        }
 
-       /**
-        * @param Lexeme $lexeme
-        *
-        * @return Form
-        */
-       public function addFormTo( Lexeme $lexeme ) {
-               //FIXME Test it
-               //FIXME Assert on ID equality
-               return $lexeme->addForm( $this->representations, 
$this->grammaticalFeatures );
-       }
-
 }
diff --git a/src/ChangeOp/ChangeOpAddForm.php b/src/ChangeOp/ChangeOpAddForm.php
index c0f23e0..cfc39df 100644
--- a/src/ChangeOp/ChangeOpAddForm.php
+++ b/src/ChangeOp/ChangeOpAddForm.php
@@ -6,15 +6,17 @@
 use Wikibase\DataModel\Entity\EntityDocument;
 use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\DataModel\Term\TermList;
+use Wikibase\Lexeme\DataModel\Form;
 use Wikibase\Lexeme\DataModel\Lexeme;
-use Wikibase\Repo\ChangeOp\ChangeOpBase;
+use Wikibase\Repo\ChangeOp\ChangeOp;
+use Wikibase\Repo\Store\EntityPermissionChecker;
 use Wikibase\Summary;
 use Wikimedia\Assert\Assert;
 
 /**
- * TODO: Is this class actually needed? Could probably be removed
+ * @license GPL-2.0+
  */
-class ChangeOpAddForm extends ChangeOpBase {
+class ChangeOpAddForm implements ChangeOp {
 
        /**
         * @var TermList
@@ -43,15 +45,31 @@
        public function apply( EntityDocument $entity, Summary $summary = null 
) {
                Assert::parameterType( Lexeme::class, $entity, '$entity' );
 
+               //FIXME Assert on ID equality
                /** @var Lexeme $entity */
                $form = $entity->addForm( $this->representations, 
$this->grammaticalFeatures );
 
-               $this->updateSummary(
-                       $summary,
-                       'add-form',
-                       '',
-                       array_values( 
$form->getRepresentations()->toTextArray() )
-               );
+               $this->updateSummary( $summary, $form );
+       }
+
+       private function updateSummary( Summary $summary, Form $form ) {
+               if ( $summary === null ) {
+                       return;
+               }
+
+               $summary->setAction( 'add-form' );
+               $summary->setLanguage( null );
+               $summary->addAutoCommentArgs( 
$form->getId()->getSerialization() );
+               $summary->addAutoSummaryArgs( array_values( 
$form->getRepresentations()->toTextArray() ) );
+       }
+
+       /**
+        * @see ChangeOp::getActions
+        *
+        * @return string[]
+        */
+       public function getActions() {
+               return [ EntityPermissionChecker::ACTION_EDIT ];
        }
 
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0b12018d9c93ff79e0f3abcc4f6405038ce205f8
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseLexeme
Gerrit-Branch: master
Gerrit-Owner: WMDE-leszek <[email protected]>

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

Reply via email to