1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Queryr\Replicator\Cli\Command; |
4
|
|
|
|
5
|
|
|
use Queryr\DumpReader\DumpReader; |
6
|
|
|
use Queryr\DumpReader\ReaderFactory; |
7
|
|
|
use Queryr\Replicator\Cli\Import\PagesImporterCli; |
8
|
|
|
use Queryr\Replicator\EntitySource\Dump\DumpEntityPageIterator; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @licence GNU GPL v2+ |
16
|
|
|
* @author Jeroen De Dauw < [email protected] > |
17
|
|
|
*/ |
18
|
|
|
class XmlDumpImportCommand extends ImportCommandBase { |
19
|
|
|
|
20
|
2 |
|
protected function configure() { |
21
|
2 |
|
$this->setName( 'import:xml' ); |
22
|
2 |
|
$this->setDescription( 'Imports entities from an extracted XML dump' ); |
23
|
|
|
|
24
|
2 |
|
$this->addArgument( |
25
|
2 |
|
'file', |
26
|
2 |
|
InputArgument::REQUIRED, |
27
|
2 |
|
'Full path of the XML dump' |
28
|
|
|
); |
29
|
|
|
|
30
|
2 |
|
$this->addOption( |
31
|
2 |
|
'continue', |
32
|
2 |
|
'c', |
33
|
2 |
|
InputOption::VALUE_OPTIONAL, |
34
|
2 |
|
'The title to resume from (title not included)' |
35
|
|
|
); |
36
|
2 |
|
} |
37
|
|
|
|
38
|
|
|
protected function executeCommand( InputInterface $input, OutputInterface $output ) { |
39
|
2 |
|
$onAborted = function( $pageTitle ) use ( $output ) { |
40
|
|
|
$output->writeln( "\n" ); |
41
|
|
|
$output->writeln( "<info>Import process aborted</info>" ); |
42
|
|
|
$output->writeln( "<comment>To resume, run with</comment> --continue=$pageTitle" ); |
43
|
2 |
|
}; |
44
|
|
|
|
45
|
2 |
|
$importer = new PagesImporterCli( $input, $output, $this->factory, $onAborted ); |
|
|
|
|
46
|
|
|
|
47
|
2 |
|
$entityPageIterator = new DumpEntityPageIterator( $this->getDumpIterator( $input, $output ) ); |
48
|
2 |
|
$importer->runImport( $entityPageIterator ); |
49
|
2 |
|
} |
50
|
|
|
|
51
|
2 |
|
private function getDumpIterator( InputInterface $input, OutputInterface $output ) { |
52
|
2 |
|
$reader = $this->newDumpReader( $input->getArgument( 'file' ) ); |
53
|
|
|
|
54
|
2 |
|
$this->handleContinueArgument( $input, $output, $reader ); |
55
|
|
|
|
56
|
2 |
|
return $reader->getIterator(); |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
private function handleContinueArgument( InputInterface $input, OutputInterface $output, DumpReader $reader ) { |
60
|
2 |
|
$continueTitle = $input->getOption( 'continue' ); |
61
|
|
|
|
62
|
2 |
|
if ( $continueTitle !== null ) { |
63
|
1 |
|
$output->write( "<info>Seeking to title </info><comment>$continueTitle</comment><info>... </info>" ); |
64
|
1 |
|
$reader->seekToTitle( $continueTitle ); |
65
|
1 |
|
$output->writeln( "<info>done</info>" ); |
66
|
|
|
} |
67
|
2 |
|
} |
68
|
|
|
|
69
|
2 |
|
private function newDumpReader( $file ) { |
70
|
2 |
|
$dumpReaderFactory = new ReaderFactory(); |
71
|
2 |
|
return $dumpReaderFactory->newDumpReaderForFile( $file ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
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: