Skip to content

Instantly share code, notes, and snippets.

@JeroenDeDauw
Created December 27, 2020 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeroenDeDauw/31c34f5e3300ca98e493df909245a12f to your computer and use it in GitHub Desktop.
Save JeroenDeDauw/31c34f5e3300ca98e493df909245a12f to your computer and use it in GitHub Desktop.
Maintenance script to create a Wikibase Item
<?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