Skip to content

Commit b1563d6

Browse files
author
aude
committed
Allow using multiple command options
And clean up the command options code a bit
1 parent bd691e6 commit b1563d6

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

maintenance/importEntities.php

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,16 @@ public function execute() {
5252
$this->importOptions = $this->extractOptions();
5353

5454
try {
55-
$importMode = $this->getImportMode();
56-
$entityIdListBuilder = $this->newEntityIdListBuilder( $importMode );
55+
foreach ( $this->importOptions as $importMode => $input ) {
56+
$this->output( "Importing $importMode\n" );
5757

58-
$input = $this->getInputForMode( $importMode );
59-
$ids = $entityIdListBuilder->getEntityIds( $input );
58+
$entityIdListBuilder = $this->newEntityIdListBuilder( $importMode );
6059

61-
$entityImporter = $this->newEntityImporter();
62-
$entityImporter->importEntities( $ids );
60+
$ids = $entityIdListBuilder->getEntityIds( $input );
61+
62+
$entityImporter = $this->newEntityImporter();
63+
$entityImporter->importEntities( $ids );
64+
}
6365
}
6466
catch ( Exception $ex ) {
6567
$this->error( $ex->getMessage() );
@@ -93,7 +95,9 @@ private function extractOptions() {
9395
$options = [];
9496

9597
foreach ( $this->getValidOptions() as $optionName ) {
96-
$options[$optionName] = $this->getOption( $optionName );
98+
if ( $this->hasOption( $optionName ) ) {
99+
$options[$optionName] = $this->getOptionValue( $optionName );
100+
}
97101
}
98102

99103
if ( empty( $options ) ) {
@@ -104,28 +108,14 @@ private function extractOptions() {
104108
}
105109

106110
/**
107-
* @return string
108-
* @throws RuntimeException
109-
*/
110-
private function getImportMode() {
111-
foreach ( $this->getValidOptions() as $option ) {
112-
if ( $this->importOptions->hasOption( $option ) ) {
113-
return $option;
114-
}
115-
}
116-
117-
throw new RuntimeException( 'No valid import option was provided' );
118-
}
119-
120-
/**
121-
* @param string $mode
111+
* @param string $optionName
122112
* @return mixed
123113
*/
124-
private function getInputForMode( $mode ) {
125-
if ( $mode === 'all-properties' ) {
114+
private function getOptionValue( $optionName ) {
115+
if ( $optionName === 'all-properties' ) {
126116
return 'all-properties';
127117
} else {
128-
return $this->importOptions->getOption( $mode );
118+
return $this->getOption( $optionName );
129119
}
130120
}
131121

src/Console/ImportOptions.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Wikibase\Import\Console;
44

5-
use Maintenance;
5+
use ArrayIterator;
6+
use IteratorAggregate;
67

7-
class ImportOptions {
8+
class ImportOptions implements IteratorAggregate {
89

910
/**
1011
* @var array
@@ -44,4 +45,11 @@ public function getOption( $name ) {
4445
return $this->options[$name];
4546
}
4647

48+
/**
49+
* @return ArrayIterator
50+
*/
51+
public function getIterator() {
52+
return new ArrayIterator( $this->options );
53+
}
54+
4755
}

0 commit comments

Comments
 (0)