1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Queryr\Replicator\Cli\Command; |
4
|
|
|
|
5
|
|
|
use Queryr\Replicator\Cli\Import\PagesImporterCli; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Wikibase\JsonDumpReader\JsonDumpFactory; |
11
|
|
|
use Wikibase\JsonDumpReader\SeekableDumpReader; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @licence GNU GPL v2+ |
15
|
|
|
* @author Jeroen De Dauw < [email protected] > |
16
|
|
|
*/ |
17
|
|
|
class JsonDumpImportCommand extends ImportCommandBase { |
18
|
|
|
|
19
|
3 |
|
protected function configure() { |
20
|
3 |
|
$this->setName( 'import:json' ); |
21
|
3 |
|
$this->setDescription( 'Imports entities from an extracted JSON dump' ); |
22
|
|
|
|
23
|
3 |
|
$this->addArgument( |
24
|
3 |
|
'file', |
25
|
3 |
|
InputArgument::REQUIRED, |
26
|
3 |
|
'Full path of the JSON dump file' |
27
|
|
|
); |
28
|
|
|
|
29
|
3 |
|
$this->addOption( |
30
|
3 |
|
'continue', |
31
|
3 |
|
'c', |
32
|
3 |
|
InputOption::VALUE_OPTIONAL, |
33
|
3 |
|
'The position to resume import from' |
34
|
|
|
); |
35
|
|
|
|
36
|
3 |
|
$this->addOption( |
37
|
3 |
|
'max', |
38
|
3 |
|
'm', |
39
|
3 |
|
InputOption::VALUE_OPTIONAL, |
40
|
3 |
|
'The maximum number of entities to import' |
41
|
|
|
); |
42
|
3 |
|
} |
43
|
|
|
|
44
|
3 |
|
protected function executeCommand( InputInterface $input, OutputInterface $output ) { |
45
|
3 |
|
$reader = ( new JsonDumpFactory() )->newExtractedDumpReader( |
46
|
3 |
|
$input->getArgument( 'file' ), |
|
|
|
|
47
|
3 |
|
$input->getOption( 'continue' ) === null ? 0 : (int)$input->getOption( 'continue' ) |
48
|
|
|
); |
49
|
|
|
|
50
|
3 |
|
$onAborted = function() use ( $output, $reader ) { |
51
|
|
|
$output->writeln( "\n" ); |
52
|
|
|
$output->writeln( "<info>Import process aborted</info>" ); |
53
|
|
|
$output->writeln( "<comment>To resume, run with</comment> --continue=" . $reader->getPosition() ); |
54
|
3 |
|
}; |
55
|
|
|
|
56
|
3 |
|
$importer = new PagesImporterCli( $input, $output, $this->factory, $onAborted ); |
|
|
|
|
57
|
|
|
|
58
|
3 |
|
$iterator = $this->factory->newJsonEntityPageIterator( $reader ); |
59
|
|
|
|
60
|
3 |
|
if ( is_numeric( $input->getOption( 'max' ) ) ) { |
61
|
1 |
|
$iterator = new \LimitIterator( $iterator, 0, (int)$input->getOption( 'max' ) ); |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
$importer->runImport( $iterator ); |
65
|
|
|
|
66
|
3 |
|
$this->outputMaxContinuation( $input, $output, $reader ); |
67
|
3 |
|
} |
68
|
|
|
|
69
|
3 |
|
private function outputMaxContinuation( InputInterface $input, OutputInterface $output, SeekableDumpReader $reader ) { |
70
|
3 |
|
if ( is_numeric( $input->getOption( 'max' ) ) ) { |
71
|
1 |
|
$output->writeln( |
72
|
1 |
|
"\n<comment>To continue from current position, run with</comment> --continue=" . $reader->getPosition() |
73
|
|
|
); |
74
|
|
|
} |
75
|
3 |
|
} |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.