Hoo man has uploaded a new change for review.
https://gerrit.wikimedia.org/r/246781
Change subject: Add a changePropertyDataType maintenance script
......................................................................
Add a changePropertyDataType maintenance script
This uses an English only edit summary and 127.0.0.1
as user for the edits being made. That may or may not
be good enough for such a one-off task.
Bug: T95686
Change-Id: I92ff59debf75096810d47045f11ff6906b2a7322
---
A repo/includes/PropertyDataTypeChanger.php
A repo/maintenance/changePropertyDataType.php
A repo/tests/phpunit/includes/PropertyDataTypeChangerTest.php
3 files changed, 245 insertions(+), 0 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/81/246781/1
diff --git a/repo/includes/PropertyDataTypeChanger.php
b/repo/includes/PropertyDataTypeChanger.php
new file mode 100644
index 0000000..fec07d9
--- /dev/null
+++ b/repo/includes/PropertyDataTypeChanger.php
@@ -0,0 +1,77 @@
+<?php
+
+namespace Wikibase\Repo;
+
+use Wikibase\DataModel\Entity\Property;
+use Wikibase\DataModel\Entity\PropertyId;
+use Wikibase\Lib\Store\EntityStore;
+use Wikibase\Lib\Store\EntityRevisionLookup;
+use Wikibase\Lib\Store\StorageException;
+use User;
+
+/**
+ * Class for changing a property's data type.
+ * Please be aware of the implication such an operation has.
+ *
+ * @since 0.5
+ *
+ * @license GNU GPL v2+
+ * @author Marius Hoch
+ */
+class PropertyDataTypeChanger {
+
+ /**
+ * @var EntityStore
+ */
+ private $entityStore;
+
+ /**
+ * @var EntityRevisionLookup
+ */
+ private $entityRevisionLookup;
+
+ /**
+ * @param EntityRevisionLookup $entityRevisionLookup
+ * @param EntityStore $entityStore
+ */
+ public function __construct( EntityRevisionLookup
$entityRevisionLookup, EntityStore $entityStore ) {
+ $this->entityRevisionLookup = $entityRevisionLookup;
+ $this->entityStore = $entityStore;
+ }
+
+ /**
+ * @throws StorageException
+ *
+ * @param PropertyId $propertyId
+ * @param string $dataTypeId
+ */
+ public function changeDataType( PropertyId $propertyId, $dataTypeId ) {
+ $entityRevision =
$this->entityRevisionLookup->getEntityRevision(
+ $propertyId,
+ EntityRevisionLookup::LATEST_FROM_MASTER
+ );
+
+ if ( $entityRevision === null ) {
+ throw new StorageException( "Couldn't load property: "
. $propertyId->getSerialization() );
+ }
+
+ /* @var $property Property */
+ $property = $entityRevision->getEntity();
+
+ $oldDataTypeId = $property->getDataTypeId();
+ $property->setDataTypeId( $dataTypeId );
+
+ // XXX: Use some(thing|one) else? Inject?
+ $user = User::newFromId( 0 );
+ $user->setName( '127.0.0.1' );
+
+ $this->entityStore->saveEntity(
+ $property,
+ 'Changing DataType from ' . $oldDataTypeId . ' to ' .
$dataTypeId,
+ $user,
+ EDIT_UPDATE,
+ $entityRevision->getRevisionId()
+ );
+ }
+
+}
diff --git a/repo/maintenance/changePropertyDataType.php
b/repo/maintenance/changePropertyDataType.php
new file mode 100644
index 0000000..ff4e252
--- /dev/null
+++ b/repo/maintenance/changePropertyDataType.php
@@ -0,0 +1,65 @@
+<?php
+
+namespace Wikibase;
+
+use InvalidArgumentException;
+use Maintenance;
+use Wikibase\DataModel\Entity\PropertyId;
+use Wikibase\Lib\Store\StorageException;
+use Wikibase\Repo\PropertyDataTypeChanger;
+use Wikibase\Repo\WikibaseRepo;
+
+$basePath = getenv( 'MW_INSTALL_PATH' ) !== false ? getenv( 'MW_INSTALL_PATH'
) : __DIR__ . '/../../../..';
+require_once $basePath . '/maintenance/Maintenance.php';
+
+/**
+ * @license GNU GPL v2+
+ *
+ * @author Marius Hoch
+ */
+class ChangePropertyDataType extends Maintenance {
+
+ public function __construct() {
+ parent::__construct();
+ $this->mDescription = "Change the data type of a given
Property.\n"
+ . "Please note: This is extremely dangerous,
make sure you know what you're doing.";
+
+ $this->addOption( 'property-id', 'Id of the property to
change.', true, true );
+ $this->addOption( 'data-type-id', 'New data type id.', true,
true );
+ }
+
+ public function execute() {
+ if ( !defined( 'WB_VERSION' ) ) {
+ $this->output( "You need to have Wikibase enabled in
order to use this maintenance script!\n\n" );
+ exit;
+ }
+
+ $repo = WikibaseRepo::getDefaultInstance();
+
+ $propertyIdSerialization = $this->getOption( 'property-id' );
+ $dataTypeId = $this->getOption( 'data-type-id' );
+ try {
+ $propertyId = new PropertyId( $propertyIdSerialization
);
+ } catch ( InvalidArgumentException $e ) {
+ $this->error( "Invalid property id: " .
$propertyIdSerialization, 1 );
+ }
+
+ $propertyDataTypeChanger = new PropertyDataTypeChanger(
+ $repo->getEntityRevisionLookup(),
+ $repo->getEntityStore()
+ );
+
+ try {
+ $propertyDataTypeChanger->changeDataType( $propertyId,
$dataTypeId );
+ } catch ( StorageException $e ) {
+ $this->error( "An error occured: " . $e->getMessage(),
1 );
+ return;
+ }
+
+ $this->output( "Successfully updated the property data type to
$dataTypeId.\n" );
+ }
+
+}
+
+$maintClass = 'Wikibase\ChangePropertyDataType';
+require_once RUN_MAINTENANCE_IF_MAIN;
diff --git a/repo/tests/phpunit/includes/PropertyDataTypeChangerTest.php
b/repo/tests/phpunit/includes/PropertyDataTypeChangerTest.php
new file mode 100644
index 0000000..4ec3bea
--- /dev/null
+++ b/repo/tests/phpunit/includes/PropertyDataTypeChangerTest.php
@@ -0,0 +1,103 @@
+<?php
+
+namespace Wikibase\Repo\Test;
+
+use PHPUnit_Framework_TestCase;
+use Status;
+use Wikibase\DataModel\Entity\Property;
+use Wikibase\DataModel\Entity\PropertyId;
+use Wikibase\EntityRevision;
+use Wikibase\Lib\Store\EntityStore;
+use Wikibase\Lib\Store\StorageException;
+use Wikibase\Repo\PropertyDataTypeChanger;
+
+/**
+ * @covers Wikibase\Repo\PropertyDataTypeChanger;
+ *
+ * @since 0.5
+ *
+ * @group WikibaseRepo
+ * @group Wikibase
+ *
+ * @license GNU GPL v2+
+ * @author Marius Hoch
+ */
+class PropertyDataTypeChangerTest extends PHPUnit_Framework_TestCase {
+
+ public function testChangeDataType_success() {
+ $propertyId = new PropertyId( 'P42' );
+
+ $expectedProperty = new Property( $propertyId, null,
'shinydata' );
+
+ $entityStore = $this->getMock( 'Wikibase\Lib\Store\EntityStore'
);
+ $entityStore->expects( $this->once() )
+ ->method( 'saveEntity' )
+ ->with(
+ $expectedProperty,
+ 'Changing DataType from rustydata to shinydata',
+ $this->isInstanceOf( 'User' ),
+ EDIT_UPDATE, 6789
+ )
+ ->will( $this->returnValue( Status::newGood() ) );
+
+ $propertyDataTypeChanger = $this->getPropertyDataTypeChanger(
$entityStore );
+ $propertyDataTypeChanger->changeDataType( $propertyId,
'shinydata' );
+ }
+
+ public function testChangeDataType_propertyNotFound() {
+ $propertyId = new PropertyId( 'P43' );
+
+ $entityStore = $this->getMock( 'Wikibase\Lib\Store\EntityStore'
);
+
+ $propertyDataTypeChanger = $this->getPropertyDataTypeChanger(
$entityStore );
+
+ $this->setExpectedException(
'Wikibase\Lib\Store\StorageException' );
+ $propertyDataTypeChanger->changeDataType( $propertyId,
'shinydata' );
+ }
+
+ public function testChangeDataType_saveFailed() {
+ $propertyId = new PropertyId( 'P42' );
+
+ $expectedProperty = new Property( $propertyId, null,
'shinydata' );
+ $storageException = new StorageException( 'whatever' );
+
+ $entityStore = $this->getMock( 'Wikibase\Lib\Store\EntityStore'
);
+ $entityStore->expects( $this->once() )
+ ->method( 'saveEntity' )
+ ->with(
+ $expectedProperty,
+ 'Changing DataType from rustydata to shinydata',
+ $this->isInstanceOf( 'User' ),
+ EDIT_UPDATE, 6789
+ )
+ ->will( $this->throwException( $storageException ) );
+
+ $propertyDataTypeChanger = $this->getPropertyDataTypeChanger(
$entityStore );
+
+ $this->setExpectedException(
'Wikibase\Lib\Store\StorageException' );
+ $propertyDataTypeChanger->changeDataType( $propertyId,
'shinydata' );
+ }
+
+ private function getPropertyDataTypeChanger( EntityStore $entityStore )
{
+ $entityRevisionLookup = $this->getMock(
'Wikibase\Lib\Store\EntityRevisionLookup' );
+
+ $entityRevisionLookup->expects( $this->once() )
+ ->method( 'getEntityRevision' )
+ ->will( $this->returnCallback( function( PropertyId
$propertyId ) {
+ if ( $propertyId->getSerialization() === 'P42'
) {
+ $property = new Property(
+ new PropertyId( 'P42' ),
+ null,
+ 'rustydata'
+ );
+
+ return new EntityRevision( $property,
6789, '20151015195144' );
+ } else {
+ return null;
+ }
+ } ) );
+
+ return new PropertyDataTypeChanger( $entityRevisionLookup,
$entityStore );
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/246781
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I92ff59debf75096810d47045f11ff6906b2a7322
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Hoo man <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits