jenkins-bot has submitted this change and it was merged.
Change subject: Moving entity operations into separate ChangeOps
......................................................................
Moving entity operations into separate ChangeOps
- part of EditEntity (API) refactoring
Bug: 48137
Change-Id: Id9146d20893f300a66b377f96bf85dc709e3d022
---
M repo/Wikibase.classes.php
M repo/Wikibase.hooks.php
M repo/includes/api/EditEntity.php
A repo/includes/changeop/ChangeOp.php
A repo/includes/changeop/ChangeOpDescription.php
A repo/includes/changeop/ChangeOpLabel.php
A repo/includes/changeop/ChangeOps.php
A repo/tests/phpunit/includes/changeop/ChangeOpDescriptionTest.php
A repo/tests/phpunit/includes/changeop/ChangeOpLabelTest.php
A repo/tests/phpunit/includes/changeop/ChangeOpsTest.php
10 files changed, 551 insertions(+), 7 deletions(-)
Approvals:
Daniel Kinzler: Looks good to me, approved
jenkins-bot: Verified
diff --git a/repo/Wikibase.classes.php b/repo/Wikibase.classes.php
index f970d19..ecf4632 100644
--- a/repo/Wikibase.classes.php
+++ b/repo/Wikibase.classes.php
@@ -49,6 +49,10 @@
'Wikibase\PropertyView' => 'includes/PropertyView.php',
'Wikibase\Summary' => 'includes/Summary.php',
'Wikibase\Repo\WikibaseRepo' => 'includes/WikibaseRepo.php',
+ 'Wikibase\ChangeOps' => 'includes/changeop/ChangeOps.php',
+ 'Wikibase\ChangeOp' => 'includes/changeop/ChangeOp.php',
+ 'Wikibase\ChangeOpLabel' =>
'includes/changeop/ChangeOpLabel.php',
+ 'Wikibase\ChangeOpDescription' =>
'includes/changeop/ChangeOpDescription.php',
// includes/actions
'Wikibase\HistoryEntityAction' =>
'includes/actions/HistoryEntityAction.php',
diff --git a/repo/Wikibase.hooks.php b/repo/Wikibase.hooks.php
index bd8c6ae..adb40cc 100755
--- a/repo/Wikibase.hooks.php
+++ b/repo/Wikibase.hooks.php
@@ -187,6 +187,10 @@
'api/RemoveQualifiers',
'api/SetQualifier',
+ 'changeop/ChangeOps',
+ 'changeop/ChangeOpLabel',
+ 'changeop/ChangeOpDescription',
+
'content/EntityContentFactory',
'content/EntityHandler',
'content/ItemContent',
diff --git a/repo/includes/api/EditEntity.php b/repo/includes/api/EditEntity.php
index 5dd2e45..65049e6 100644
--- a/repo/includes/api/EditEntity.php
+++ b/repo/includes/api/EditEntity.php
@@ -2,8 +2,11 @@
namespace Wikibase\Api;
-use ApiBase, User, Status, SiteList;
+use Wikibase\ChangeOps;
+use Wikibase\ChangeOpLabel;
+use Wikibase\ChangeOpDescription;
+use ApiBase, User, Status, SiteList;
use Wikibase\SiteLink;
use Wikibase\Entity;
use Wikibase\EntityContent;
@@ -95,6 +98,8 @@
wfProfileIn( __METHOD__ );
$status = Status::newGood();
$summary = $this->createSummary( $params );
+ $entity = $entityContent->getEntity();
+ $changeOps = new ChangeOps();
if ( isset( $params['id'] ) XOR ( isset( $params['site'] ) &&
isset( $params['title'] ) ) ) {
$summary->setAction( $params['clear'] === false ?
'update' : 'override' );
@@ -181,11 +186,16 @@
foreach ( $list as $langCode => $arg ) {
$status->merge(
$this->checkMultilangArgs( $arg, $langCode, $languages ) );
- if ( array_key_exists(
'remove', $arg ) || $arg['value'] === "" ) {
-
$entityContent->getEntity()->removeLabel( $arg['language'] );
+
+ $language = $arg['language'];
+ $newLabel = Utils::trimToNFC(
$arg['value'] );
+ $oldLabel = $entity->getLabel(
$language );
+
+ if ( array_key_exists(
'remove', $arg ) || $newLabel === "" ) {
+ $changeOps->add( new
ChangeOpLabel( $language, null ) );
}
else {
-
$entityContent->getEntity()->setLabel( $arg['language'], Utils::trimToNFC(
$arg['value'] ) );
+ $changeOps->add( new
ChangeOpLabel( $language, $newLabel ) );
}
}
@@ -204,11 +214,16 @@
foreach ( $list as $langCode => $arg ) {
$status->merge(
$this->checkMultilangArgs( $arg, $langCode, $languages ) );
- if ( array_key_exists(
'remove', $arg ) || $arg['value'] === "" ) {
-
$entityContent->getEntity()->removeDescription( $arg['language'] );
+
+ $language = $arg['language'];
+ $newDescription =
Utils::trimToNFC( $arg['value'] );
+ $oldDescription =
$entity->getDescription( $language );
+
+ if ( array_key_exists(
'remove', $arg ) || $newDescription === "" ) {
+ $changeOps->add( new
ChangeOpDescription( $language, null ) );
}
else {
-
$entityContent->getEntity()->setDescription( $arg['language'],
Utils::trimToNFC( $arg['value'] ) );
+ $changeOps->add( new
ChangeOpDescription( $language, $newDescription ) );
}
}
@@ -335,6 +350,8 @@
}
}
+ $changeOps->apply( $entity );
+
// This is already done in createEntity
if ( $entityContent->isNew() ) {
// if the entity doesn't exist yet, create it
diff --git a/repo/includes/changeop/ChangeOp.php
b/repo/includes/changeop/ChangeOp.php
new file mode 100644
index 0000000..3eed5a2
--- /dev/null
+++ b/repo/includes/changeop/ChangeOp.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace Wikibase;
+
+/**
+ * Base class for change operations.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 0.4
+ *
+ * @file
+ * @ingroup WikibaseRepo
+ *
+ * @licence GNU GPL v2+
+ * @author Tobias Gritschacher < [email protected] >
+ */
+interface ChangeOp {
+
+ public function apply( Entity $entity );
+
+}
diff --git a/repo/includes/changeop/ChangeOpDescription.php
b/repo/includes/changeop/ChangeOpDescription.php
new file mode 100644
index 0000000..e02ad9c
--- /dev/null
+++ b/repo/includes/changeop/ChangeOpDescription.php
@@ -0,0 +1,80 @@
+<?php
+
+namespace Wikibase;
+
+use InvalidArgumentException;
+
+/**
+ * Class for description change operation
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 0.4
+ *
+ * @ingroup WikibaseRepo
+ *
+ * @licence GNU GPL v2+
+ * @author Tobias Gritschacher < [email protected] >
+ */
+class ChangeOpDescription implements ChangeOp {
+
+ /**
+ * @since 0.4
+ *
+ * @var string
+ */
+ protected $language;
+
+ /**
+ * @since 0.4
+ *
+ * @var string|null
+ */
+ protected $description;
+
+ /**
+ * @since 0.4
+ *
+ * @param string $language
+ * @param string|null $description
+ *
+ * @throws InvalidArgumentException
+ */
+ public function __construct( $language, $description ) {
+ if ( !is_string( $language ) ) {
+ throw new InvalidArgumentException( '$language needs to
be a string' );
+ }
+
+ $this->language = $language;
+ $this->description = $description;
+ }
+
+ /**
+ * Applies the change to the given entity
+ *
+ * @since 0.4
+ *
+ * @param Entity $entity
+ */
+ public function apply( Entity $entity ) {
+ if ( $this->description === null ) {
+ $entity->removeDescription( $this->language );
+ } else {
+ $entity->setDescription( $this->language,
$this->description );
+ }
+ }
+
+}
diff --git a/repo/includes/changeop/ChangeOpLabel.php
b/repo/includes/changeop/ChangeOpLabel.php
new file mode 100644
index 0000000..200d955
--- /dev/null
+++ b/repo/includes/changeop/ChangeOpLabel.php
@@ -0,0 +1,80 @@
+<?php
+
+namespace Wikibase;
+
+use InvalidArgumentException;
+
+/**
+ * Class for label change operation
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 0.4
+ *
+ * @ingroup WikibaseRepo
+ *
+ * @licence GNU GPL v2+
+ * @author Tobias Gritschacher < [email protected] >
+ */
+class ChangeOpLabel implements ChangeOp {
+
+ /**
+ * @since 0.4
+ *
+ * @var string
+ */
+ protected $language;
+
+ /**
+ * @since 0.4
+ *
+ * @var string|null
+ */
+ protected $label;
+
+ /**
+ * @since 0.4
+ *
+ * @param string $language
+ * @param string|null $label
+ *
+ * @throws InvalidArgumentException
+ */
+ public function __construct( $language, $label ) {
+ if ( !is_string( $language ) ) {
+ throw new InvalidArgumentException( '$language needs to
be a string' );
+ }
+
+ $this->language = $language;
+ $this->label = $label;
+ }
+
+ /**
+ * Applies the change to the given entity
+ *
+ * @since 0.4
+ *
+ * @param Entity $entity
+ */
+ public function apply( Entity $entity ) {
+ if ( $this->label === null ) {
+ $entity->removeLabel( $this->language );
+ } else {
+ $entity->setLabel( $this->language, $this->label );
+ }
+ }
+
+}
diff --git a/repo/includes/changeop/ChangeOps.php
b/repo/includes/changeop/ChangeOps.php
new file mode 100644
index 0000000..47c1591
--- /dev/null
+++ b/repo/includes/changeop/ChangeOps.php
@@ -0,0 +1,81 @@
+<?php
+
+namespace Wikibase;
+
+/**
+ * Class for holding a batch of change operations
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 0.4
+ *
+ * @ingroup WikibaseRepo
+ *
+ * @licence GNU GPL v2+
+ * @author Tobias Gritschacher < [email protected] >
+ */
+class ChangeOps {
+
+ /**
+ * @since 0.4
+ *
+ * @var ChangeOp[]
+ */
+ protected $ops;
+
+ /**
+ * @since 0.4
+ *
+ */
+ public function __construct() {
+ $this->ops = array();
+ }
+
+ /**
+ * Adds a changeOp
+ *
+ * @since 0.4
+ *
+ * @param ChangeOp $changeOp
+ */
+ public function add( ChangeOp $changeOp ) {
+ $this->ops[] = $changeOp;
+ }
+
+ /**
+ * Get the array of changeOps
+ *
+ * @since 0.4
+ *
+ * @return ChangeOp[]
+ */
+ public function getChangeOps() {
+ return $this->ops;
+ }
+
+ /**
+ * Applies all changes to the given entity
+ * @since 0.4
+ *
+ * @param Entity $entity
+ */
+ public function apply( Entity $entity ) {
+ foreach ( $this->ops as $op ) {
+ $op->apply( $entity );
+ }
+ }
+
+}
diff --git a/repo/tests/phpunit/includes/changeop/ChangeOpDescriptionTest.php
b/repo/tests/phpunit/includes/changeop/ChangeOpDescriptionTest.php
new file mode 100644
index 0000000..add402c
--- /dev/null
+++ b/repo/tests/phpunit/includes/changeop/ChangeOpDescriptionTest.php
@@ -0,0 +1,72 @@
+<?php
+
+namespace Wikibase\Test;
+
+use Wikibase\ChangeOpDescription;
+use Wikibase\ItemContent;
+use InvalidArgumentException;
+
+/**
+ * @covers Wikibase\ChangeOpDescription
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
USA
+ *
+ * @file
+ * @since 0.4
+ *
+ * @ingroup Wikibase
+ * @ingroup Test
+ *
+ * @group Wikibase
+ * @group WikibaseRepo
+ * @group ChangeOp
+ *
+ * @licence GNU GPL v2+
+ * @author Tobias Gritschacher < [email protected] >
+ */
+class ChangeOpDescriptionTest extends \PHPUnit_Framework_TestCase {
+
+ /**
+ * @expectedException InvalidArgumentException
+ */
+ public function testInvalidConstruct() {
+ $changeOpDescription = new ChangeOpDescription( 42, 'myOld' );
+ }
+
+ public function changeOpDescriptionProvider() {
+ $args = array();
+ $args[] = array ( new ChangeOpDescription( 'en', 'myNew' ),
'myNew' );
+ $args[] = array ( new ChangeOpDescription( 'en', null ), '' );
+
+ return $args;
+ }
+
+ /**
+ * @dataProvider changeOpDescriptionProvider
+ *
+ * @param ChangeOpDescription $changeOpDescription
+ * @param string $expectedDescription
+ */
+ public function testApply( $changeOpDescription, $expectedDescription )
{
+ $item = ItemContent::newEmpty();
+ $entity = $item->getEntity();
+ $entity->setDescription( 'en', 'test' );
+
+ $changeOpDescription->apply( $entity );
+
+ $this->assertEquals( $expectedDescription,
$entity->getDescription( 'en' ) );
+ }
+
+}
diff --git a/repo/tests/phpunit/includes/changeop/ChangeOpLabelTest.php
b/repo/tests/phpunit/includes/changeop/ChangeOpLabelTest.php
new file mode 100644
index 0000000..27bb205
--- /dev/null
+++ b/repo/tests/phpunit/includes/changeop/ChangeOpLabelTest.php
@@ -0,0 +1,72 @@
+<?php
+
+namespace Wikibase\Test;
+
+use Wikibase\ChangeOpLabel;
+use Wikibase\ItemContent;
+use InvalidArgumentException;
+
+/**
+ * @covers Wikibase\ChangeOpLabel
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
USA
+ *
+ * @file
+ * @since 0.4
+ *
+ * @ingroup Wikibase
+ * @ingroup Test
+ *
+ * @group Wikibase
+ * @group WikibaseRepo
+ * @group ChangeOp
+ *
+ * @licence GNU GPL v2+
+ * @author Tobias Gritschacher < [email protected] >
+ */
+class ChangeOpLabelTest extends \PHPUnit_Framework_TestCase {
+
+ /**
+ * @expectedException InvalidArgumentException
+ */
+ public function testInvalidConstruct() {
+ $changeOpLabel = new ChangeOpLabel( 42, 'myNew' );
+ }
+
+ public function changeOpLabelProvider() {
+ $args = array();
+ $args[] = array ( new ChangeOpLabel( 'en', 'myNew' ), 'myNew' );
+ $args[] = array ( new ChangeOpLabel( 'en', null ), '' );
+
+ return $args;
+ }
+
+ /**
+ * @dataProvider changeOpLabelProvider
+ *
+ * @param ChangeOpLabel $changeOpLabel
+ * @param string $expectedLabel
+ */
+ public function testApply( $changeOpLabel, $expectedLabel ) {
+ $item = ItemContent::newEmpty();
+ $entity = $item->getEntity();
+ $entity->setLabel( 'en', 'test' );
+
+ $changeOpLabel->apply( $entity );
+
+ $this->assertEquals( $expectedLabel, $entity->getLabel( 'en' )
);
+ }
+
+}
diff --git a/repo/tests/phpunit/includes/changeop/ChangeOpsTest.php
b/repo/tests/phpunit/includes/changeop/ChangeOpsTest.php
new file mode 100644
index 0000000..c0f4016
--- /dev/null
+++ b/repo/tests/phpunit/includes/changeop/ChangeOpsTest.php
@@ -0,0 +1,99 @@
+<?php
+
+namespace Wikibase\Test;
+
+use Wikibase\ChangeOpLabel;
+use Wikibase\ChangeOpDescription;
+use Wikibase\ChangeOps;
+use Wikibase\ItemContent;
+
+/**
+ * @covers Wikibase\ChangeOps
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
USA
+ *
+ * @file
+ * @since 0.4
+ *
+ * @ingroup Wikibase
+ * @ingroup Test
+ *
+ * @group Wikibase
+ * @group WikibaseRepo
+ * @group ChangeOp
+ *
+ * @licence GNU GPL v2+
+ * @author Tobias Gritschacher < [email protected] >
+ */
+class ChangeOpsTest extends \PHPUnit_Framework_TestCase {
+
+ public function testEmptyChangeOps() {
+ $changeOps = new ChangeOps();
+ $this->assertEmpty( $changeOps->getChangeOps() );
+ }
+
+ /**
+ * @return \Wikibase\ChangeOp[]
+ */
+ public function changeOpProvider() {
+ $ops = array();
+ $ops[] = array ( new ChangeOpLabel( 'en', 'myNewLabel' ) );
+ $ops[] = array ( new ChangeOpDescription( 'de',
'myNewDescription' ) );
+ $ops[] = array ( new ChangeOpLabel( 'en', null ) );
+
+ return $ops;
+ }
+
+ /**
+ * @dataProvider changeOpProvider
+ *
+ * @param ChangeOp $changeOp
+ */
+ public function testAdd( $changeOp ) {
+ $changeOps = new ChangeOps();
+ $changeOps->add( $changeOp );
+ $this->assertEquals( array( $changeOp ),
$changeOps->getChangeOps() );
+ }
+
+ public function changeOpsProvider() {
+ $args = array();
+
+ $language = 'en';
+ $changeOps = new ChangeOps();
+ $changeOps->add( new ChangeOpLabel( $language, 'newLabel' ) );
+ $changeOps->add( new ChangeOpDescription( $language,
'newDescription' ) );
+ $args[] = array( $changeOps, $language, 'newLabel',
'newDescription' );
+
+ return $args;
+ }
+
+ /**
+ * @dataProvider changeOpsProvider
+ *
+ * @param ChangeOps $changeOps
+ * @param string $language
+ * @param string $expectedLabel
+ * @param string $expectedDescription
+ */
+ public function testApply( $changeOps, $language, $expectedLabel,
$expectedDescription ) {
+ $item = ItemContent::newEmpty();
+ $entity = $item->getEntity();
+
+ $changeOps->apply( $entity );
+ $this->assertEquals( $expectedLabel, $entity->getLabel(
$language ) );
+ $this->assertEquals( $expectedDescription,
$entity->getDescription( $language ) );
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/66392
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Id9146d20893f300a66b377f96bf85dc709e3d022
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits