Completed
Push — master ( d01339...b9998b )
by Jeroen De
02:51
created

ApiImportCommand::executeCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Queryr\Replicator\Cli\Command;
4
5
use BatchingIterator\BatchingIterator;
6
use Queryr\Replicator\Cli\Import\PagesImporterCli;
7
use Queryr\Replicator\EntityIdListNormalizer;
8
use Queryr\Replicator\EntitySource\Api\GetEntitiesClient;
9
use Queryr\Replicator\EntitySource\Api\Http;
10
use Queryr\Replicator\EntitySource\BatchingEntityPageFetcher;
11
use Queryr\Replicator\EntitySource\ReferencedEntityPageIterator;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Wikibase\DataModel\Entity\BasicEntityIdParser;
17
18
/**
19
 * @licence GNU GPL v2+
20
 * @author Jeroen De Dauw < [email protected] >
21
 */
22
class ApiImportCommand extends ImportCommandBase {
23
24
	protected function configure() {
25
		$this->setName( 'import:api' );
26
		$this->setDescription( 'Imports entities via a Wikibase Repo web API' );
27
28
		$this->addArgument(
29
			'entities',
30
			InputArgument::IS_ARRAY,
31
			'The IDs of the entities to import, separated by spaces. ID ranges can be specified: Q1-Q100'
32
		);
33
34
//		$this->addOption(
35
//			'url',
36
//			null,
37
//			InputOption::VALUE_OPTIONAL,
38
//			'The full url of the Wikibase Repo web API to use',
39
//			'https://www.wikidata.org/w/api.php'
40
//		);
41
42
		$this->addOption(
43
			'batchsize',
44
			'b',
45
			InputOption::VALUE_OPTIONAL,
46
			'The number of API requests to bundle together',
47
			10
48
		);
49
50
		$this->addOption(
51
			'include-references',
52
			'r',
53
			InputOption::VALUE_NONE,
54
			'If referenced entities should also be imported'
55
		);
56
	}
57
58
	/**
59
	 * @var Http|null
60
	 */
61
	private $http;
62
63
	public function setHttp( Http $http ) {
64
		$this->http = $http;
65
	}
66
67
	protected function executeCommand( InputInterface $input, OutputInterface $output ) {
68
		$importer = new PagesImporterCli( $input, $output, $this->factory );
0 ignored issues
show
Bug introduced by
It seems like $this->factory can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
69
70
		$importer->runImport( $this->getEntityPageIterator( $input ) );
71
	}
72
73
	private function getEntityPageIterator( InputInterface $input ): \Iterator {
74
		$http = $this->http === null ? new Http() : $this->http;
75
76
		$idListNormalizer = new EntityIdListNormalizer( new BasicEntityIdParser() );
77
		$ids = [];
78
79
		foreach ( $idListNormalizer->getNormalized( $input->getArgument( 'entities' ) ) as $id ) {
80
			  $ids[] = $id->getSerialization();
81
		}
82
83
		$batchingFetcher = new BatchingEntityPageFetcher(
84
			new GetEntitiesClient( $http ),
85
			$ids
86
		);
87
88
		$iterator = new BatchingIterator( $batchingFetcher );
89
		$iterator->setMaxBatchSize( (int)$input->getOption( 'batchsize' ) );
90
91
		if ( $input->getOption( 'include-references' ) ) {
92
			return new ReferencedEntityPageIterator(
93
				$iterator,
94
				$batchingFetcher,
95
				$this->factory->newLegacyEntityDeserializer()
96
			);
97
		}
98
99
		return $iterator;
100
	}
101
102
}
103