|
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
|
1 |
|
protected function configure() { |
|
25
|
1 |
|
$this->setName( 'import:api' ); |
|
26
|
1 |
|
$this->setDescription( 'Imports entities via a Wikibase Repo web API' ); |
|
27
|
|
|
|
|
28
|
1 |
|
$this->addArgument( |
|
29
|
1 |
|
'entities', |
|
30
|
1 |
|
InputArgument::IS_ARRAY, |
|
31
|
1 |
|
'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
|
1 |
|
$this->addOption( |
|
43
|
1 |
|
'batchsize', |
|
44
|
1 |
|
'b', |
|
45
|
1 |
|
InputOption::VALUE_OPTIONAL, |
|
46
|
1 |
|
'The number of API requests to bundle together', |
|
47
|
1 |
|
10 |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
1 |
|
$this->addOption( |
|
51
|
1 |
|
'include-references', |
|
52
|
1 |
|
'r', |
|
53
|
1 |
|
InputOption::VALUE_NONE, |
|
54
|
1 |
|
'If referenced entities should also be imported' |
|
55
|
|
|
); |
|
56
|
1 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var Http|null |
|
60
|
|
|
*/ |
|
61
|
|
|
private $http; |
|
62
|
|
|
|
|
63
|
1 |
|
public function setHttp( Http $http ) { |
|
64
|
1 |
|
$this->http = $http; |
|
65
|
1 |
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
protected function executeCommand( InputInterface $input, OutputInterface $output ) { |
|
68
|
1 |
|
$importer = new PagesImporterCli( $input, $output, $this->factory ); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
1 |
|
$importer->runImport( $this->getEntityPageIterator( $input ) ); |
|
71
|
1 |
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
private function getEntityPageIterator( InputInterface $input ): \Iterator { |
|
74
|
1 |
|
$http = $this->http === null ? new Http() : $this->http; |
|
75
|
|
|
|
|
76
|
1 |
|
$idListNormalizer = new EntityIdListNormalizer( new BasicEntityIdParser() ); |
|
77
|
1 |
|
$ids = []; |
|
78
|
|
|
|
|
79
|
1 |
|
foreach ( $idListNormalizer->getNormalized( $input->getArgument( 'entities' ) ) as $id ) { |
|
80
|
1 |
|
$ids[] = $id->getSerialization(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
$batchingFetcher = new BatchingEntityPageFetcher( |
|
84
|
1 |
|
new GetEntitiesClient( $http ), |
|
85
|
1 |
|
$ids |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
1 |
|
$iterator = new BatchingIterator( $batchingFetcher ); |
|
89
|
1 |
|
$iterator->setMaxBatchSize( (int)$input->getOption( 'batchsize' ) ); |
|
90
|
|
|
|
|
91
|
1 |
|
if ( $input->getOption( 'include-references' ) ) { |
|
92
|
|
|
return new ReferencedEntityPageIterator( |
|
93
|
|
|
$iterator, |
|
94
|
|
|
$batchingFetcher, |
|
95
|
|
|
$this->factory->newLegacyEntityDeserializer() |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
return $iterator; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
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: