Skip to content

Commit

Permalink
Extract options to separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
aude committed Apr 8, 2016
1 parent 523ca6a commit ebb47ce
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 34 deletions.
59 changes: 25 additions & 34 deletions maintenance/importEntities.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\Import\Console\ImportOptions;
use Wikibase\Import\EntityImporter;
use Wikibase\Import\EntityImporterFactory;
use Wikibase\Import\PropertyIdLister;
Expand All @@ -34,11 +35,10 @@ class ImportEntities extends \Maintenance {

private $queryRunner;

private $entity;

private $file;

private $allProperties;
/**
* @var ImportOptions
*/
private $importOptions;

public function __construct() {
parent::__construct();
Expand All @@ -55,32 +55,27 @@ private function addOptions() {
}

public function execute() {
if ( $this->extractOptions() === false ) {
$this->maybeHelp( true );

return;
}

$this->importOptions = $this->extractOptions();
$this->initServices();

if ( $this->allProperties ) {
if ( $this->importOptions->hasOption( 'all-properties' ) ) {
$this->importProperties();
}

if ( $this->file ) {
$this->importEntitiesFromFile( $this->file );
if ( $this->importOptions->hasOption( 'file' ) ) {
$this->importEntitiesFromFile( $this->importOptions->getOption( 'file' ) );
}

if ( $this->entity ) {
$this->importEntity( $this->entity );
if ( $this->importOptions->hasOption( 'entity' ) ) {
$this->importEntity( $this->importOptions->getOption( 'entity' ) );
}

if ( $this->range ) {
$this->importRange( $this->range );
if ( $this->importOptions->hasOption( 'range' ) ) {
$this->importRange( $this->importOptions->getOption( 'range' ) );
}

if ( $this->query ) {
$this->importFromQuery( $this->query );
if ( $this->importOptions->hasOption( 'query' ) ) {
$this->importFromQuery( $this->importOptions->getOption( 'query' ) );
}

$this->logger->info( 'Done' );
Expand Down Expand Up @@ -122,22 +117,18 @@ private function newLogger() {
}

private function extractOptions() {
$this->entity = $this->getOption( 'entity' );
$this->file = $this->getOption( 'file' );
$this->allProperties = $this->getOption( 'all-properties' );
$this->query = $this->getOption( 'query' );
$this->range = $this->getOption( 'range' );

if ( $this->file === null
&& $this->allProperties === null
&& $this->entity === null
&& $this->query === null
&& $this->range === null
) {
return false;
$options = array();
$validOptions = [ 'entity', 'file', 'all-properties', 'query', 'range' ];

foreach ( $validOptions as $optionName ) {
$options[$optionName] = $this->getOption( $optionName );
}

if ( empty( $options ) ) {
$this->maybeHelp( true );
}

return true;
return new ImportOptions( $options );
}

private function importProperties() {
Expand Down
47 changes: 47 additions & 0 deletions src/Console/ImportOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Wikibase\Import\Console;

use Maintenance;

class ImportOptions {

/**
* @var array
*/
private $options;

/**
* @param array $options
*/
public function __construct( array $options ) {
$this->options = $options;
}

public function setOption( $key, $value ) {
$this->options[$key] = $value;
}

/**
* @param string $name
*
* @return bool
*/
public function hasOption( $name ) {
return isset( $this->options[$name] );
}

/**
* @param string $name
*
* @return mixed
*/
public function getOption( $name ) {
if ( !$this->hasOption( $name ) ) {
throw new \OutOfBoundsException( "Unknown option: $name" );
}

return $this->options[$name];
}

}

0 comments on commit ebb47ce

Please sign in to comment.