Created
December 27, 2020 15:36
-
-
Save JeroenDeDauw/31c34f5e3300ca98e493df909245a12f to your computer and use it in GitHub Desktop.
Maintenance script to create a Wikibase Item
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Wikibase\Repo\Maintenance; | |
use Maintenance; | |
use User; | |
use Wikibase\DataModel\Entity\EntityDocument; | |
use Wikibase\DataModel\Entity\Item; | |
use Wikibase\DataModel\Entity\ItemId; | |
use Wikibase\Lib\WikibaseSettings; | |
use Wikibase\Repo\EditEntity\EditEntity; | |
use Wikibase\Repo\WikibaseRepo; | |
$basePath = getenv( 'MW_INSTALL_PATH' ) !== false | |
? getenv( 'MW_INSTALL_PATH' ) | |
: __DIR__ . '/../../../..'; | |
require_once $basePath . '/maintenance/Maintenance.php'; | |
class CreateItem extends Maintenance { | |
public function __construct() { | |
parent::__construct(); | |
$this->addDescription( 'Populates Wikibase db with randomly generated entities and terms' ); | |
} | |
public function execute() { | |
if ( !WikibaseSettings::isRepoEnabled() ) { | |
$this->output( "You need to have Wikibase enabled in order to use this maintenance script!\n" ); | |
exit; | |
} | |
$item = new Item(); | |
$item->setId( new ItemId( 'Q42' ) ); | |
$item->setLabel( 'en', 'testing' ); | |
$this->saveItem( $item ); | |
$this->output( 'done' ); | |
} | |
private function saveItem( Item $item ) { | |
$status = $this->createEntity( $item ); | |
if ( $status->isOK() ) { | |
$this->output( "\n" . $status->getValue()['revision']->getEntity()->getId() . "\n" ); | |
} else { | |
$this->output( "\n" . $status->getValue() . "\n" ); | |
} | |
} | |
private function createEntity( EntityDocument $entity ) { | |
return $this->newEntitySaver()->attemptSave( $entity, 'test summary', EDIT_NEW, false ); | |
} | |
private function newEntitySaver(): EditEntity { | |
return WikibaseRepo::getDefaultInstance()->newEditEntityFactory()->newEditEntity( | |
User::newSystemUser( 'Import Script', [ 'steal' => true ] ) | |
); | |
} | |
} | |
$maintClass = CreateItem::class; | |
require_once RUN_MAINTENANCE_IF_MAIN; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment