wikimedia/mediawiki-extensions-Wikibase

View on GitHub
repo/maintenance/importFederatedPropertiesSampleData.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php
// phpcs:disable MediaWiki.Files.ClassMatchesFilename.WrongCase -- keep file name for B/C

declare( strict_types = 1 );
namespace Wikibase\Repo\Maintenance;

use MediaWiki\Maintenance\Maintenance;
use MediaWiki\User\User;
use Wikibase\DataModel\Entity\Item;
use Wikibase\Lib\Store\EntityStore;
use Wikibase\Repo\WikibaseRepo;

/**
 * Import data from a data file to a Federated Properties Wikibase instance
 * For an example file format to import, see repo/tests/phpunit/data/maintenance/federatedPropertiesTestDataFile.tsv
 *
 * @license GPL-2.0-or-later
 */
class ImportFederatedPropertiesSampleData extends Maintenance {

    public function __construct() {
        parent::__construct();
        $this->addDescription( "Create new Items for federated properties test system." );
        $this->addOption( 'dataFile', 'Data source for the new Items to create', true, true );
        $this->addOption(
            'delimiter',
            'Character separating data across columns in the data file. default is tab.',
            false,
            true
        );
    }

    public function execute() {
        $dataFile = $this->getOption( 'dataFile' );
        $lineDelimiter = $this->getOption( 'delimiter', "\t" );

        $entityStore = WikibaseRepo::getEntityStore();
        $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );

        foreach ( $this->getDataToImport( $dataFile ) as $dataline ) {
            if ( $dataline !== '' ) {
                $dataArray = explode( $lineDelimiter, $dataline );
                $this->storeNewItemWithTermData( $dataArray, $entityStore, $user );
            }
        }
        $this->output( 'Created new Items from data file:' . $dataFile . "\n" );
    }

    public function storeNewItemWithTermData( array $data, EntityStore $entityStore, User $user ) {
        $item = new Item();
        $item->setLabel( 'en', $data[0] );
        $item->setDescription( 'en', $data[1] );

        $entityStore->saveEntity( $item, 'Create new Item', $user, EDIT_NEW );

        return $item;
    }

    public function getDataToImport( $dataFileLocation ) {
        $data = file_get_contents( $dataFileLocation );
        if ( $data === '' ) {
            $this->fatalError( 'Error: ' . $dataFileLocation . ' is an empty file' . "\n" );
        }

        return explode( "\n", $data );
    }
}

return ImportFederatedPropertiesSampleData::class;