Skip to content

Commit 025417a

Browse files
author
aude
committed
Split code in importEntities execute method
also improve error handling/formatting, and move QueryRunner obj construction to factory.
1 parent 0cdf20d commit 025417a

File tree

3 files changed

+106
-72
lines changed

3 files changed

+106
-72
lines changed

maintenance/importEntities.php

Lines changed: 80 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
namespace Wikibase\Import\Maintenance;
44

5-
use Asparagus\QueryBuilder;
6-
use Asparagus\QueryExecuter;
75
use Exception;
6+
use MediaWiki\MediaWikiServices;
87
use Psr\Log\LoggerInterface;
8+
use RuntimeException;
99
use Wikibase\Import\Console\ImportOptions;
10+
use Wikibase\Import\EntityId\EntityIdListBuilder;
1011
use Wikibase\Import\EntityId\EntityIdListBuilderFactory;
1112
use Wikibase\Import\EntityImporterFactory;
1213
use Wikibase\Import\LoggerFactory;
13-
use Wikibase\Import\QueryRunner;
1414
use Wikibase\Import\PropertyIdLister;
1515
use Wikibase\Repo\WikibaseRepo;
1616

@@ -22,12 +22,16 @@
2222
require_once "$IP/maintenance/Maintenance.php";
2323

2424
class ImportEntities extends \Maintenance {
25-
2625
/**
2726
* @var LoggerInterface
2827
*/
2928
private $logger;
3029

30+
/**
31+
* @var ImportOptions
32+
*/
33+
private $importOptions;
34+
3135
public function __construct() {
3236
parent::__construct();
3337

@@ -44,45 +48,46 @@ private function addOptions() {
4448

4549
public function execute() {
4650
$this->logger = LoggerFactory::newLogger( 'wikibase-import', $this->mQuiet );
51+
$this->importOptions = $this->extractOptions();
4752

48-
$importOptions = $this->extractOptions();
53+
try {
54+
$importMode = $this->getImportMode();
55+
$entityIdListBuilder = $this->newEntityIdListBuilder( $importMode );
4956

50-
$entityIdListBuilderFactory = $this->newEntityIdListBuilderFactory();
57+
$input = $this->getInputForMode( $importMode );
58+
$ids = $entityIdListBuilder->getEntityIds( $input );
5159

52-
foreach ( $this->getValidOptions() as $option ) {
53-
if ( $importOptions->hasOption( $option ) ) {
54-
$entityIdListBuilder = $entityIdListBuilderFactory->newEntityIdListBuilder(
55-
$option
56-
);
57-
58-
if ( $option === 'all-properties' ) {
59-
$input = 'all-properties';
60-
} else {
61-
$input = $importOptions->getOption( $option );
62-
}
63-
64-
break;
65-
}
60+
$entityImporter = $this->newEntityImporter();
61+
$entityImporter->importEntities( $ids );
62+
}
63+
catch ( Exception $ex ) {
64+
$this->error( $ex->getMessage() );
6665
}
6766

68-
if ( !isset( $entityIdListBuilder ) ) {
69-
$this->logger->error( 'ERROR: No valid import option was provided' );
67+
$this->logger->info( 'Done' );
68+
}
7069

71-
return;
72-
} else {
73-
try {
74-
$ids = $entityIdListBuilder->getEntityIds( $input );
70+
/**
71+
* @inheritdoc
72+
*/
73+
protected function error( $err, $die = 0 ) {
74+
$err = "\033[31mERROR:\033[0m $err";
7575

76-
$entityImporter = $this->newEntityImporter();
77-
$entityImporter->importEntities( $ids );
78-
} catch ( Exception $ex ) {
79-
$this->logger->error( $ex->getMessage() );
80-
}
76+
$this->logger->error( $err );
77+
$this->maybeHelp( true );
78+
}
8179

82-
$this->logger->info( 'Done' );
83-
}
80+
/**
81+
* @return array
82+
*/
83+
private function getValidOptions() {
84+
return [ 'entity', 'file', 'all-properties', 'query', 'range' ];
8485
}
8586

87+
/**
88+
* @return ImportOptions
89+
* @throws RuntimeException
90+
*/
8691
private function extractOptions() {
8792
$options = [];
8893

@@ -91,41 +96,61 @@ private function extractOptions() {
9196
}
9297

9398
if ( empty( $options ) ) {
94-
$this->maybeHelp( true );
99+
throw new RuntimeException( 'No valid import mode option provided' );
95100
}
96101

97102
return new ImportOptions( $options );
98103
}
99104

100-
private function getValidOptions() {
101-
return [ 'entity', 'file', 'all-properties', 'query', 'range' ];
105+
/**
106+
* @return string
107+
* @throws RuntimeException
108+
*/
109+
private function getImportMode() {
110+
foreach ( $this->getValidOptions() as $option ) {
111+
if ( $this->importOptions->hasOption( $option ) ) {
112+
return $option;
113+
}
114+
}
115+
116+
throw new RuntimeException( 'No valid import option was provided' );
102117
}
103118

104-
private function newEntityIdListBuilderFactory() {
105-
$queryRunner = new QueryRunner(
106-
new QueryBuilder( $this->getConfig()->get( 'WBImportQueryPrefixes' ) ),
107-
new QueryExecuter( $this->getConfig()->get( 'WBImportQueryUrl' ) )
108-
);
109-
110-
return new EntityIdListBuilderFactory(
111-
WikibaseRepo::getDefaultInstance()->getEntityIdParser(),
112-
new PropertyIdLister(),
113-
$queryRunner,
114-
$this->getConfig()->get( 'WBImportSourceApi' )
115-
);
119+
/**
120+
* @param string $mode
121+
* @return mixed
122+
*/
123+
private function getInputForMode( $mode ) {
124+
if ( $mode === 'all-properties' ) {
125+
return 'all-properties';
126+
} else {
127+
return $this->importOptions->getOption( $mode );
128+
}
129+
}
130+
131+
/**
132+
* @param string $importMode
133+
* @return EntityIdListBuilder
134+
*/
135+
private function newEntityIdListBuilder( $importMode ) {
136+
$entityIdListBuilderFactory =
137+
new EntityIdListBuilderFactory( WikibaseRepo::getDefaultInstance()->getEntityIdParser(),
138+
new PropertyIdLister(), $this->getConfig()->get( 'WBImportQueryPrefixes' ),
139+
$this->getConfig()->get( 'WBImportQueryUrl' ),
140+
$this->getConfig()->get( 'WBImportSourceApi' ) );
141+
142+
return $entityIdListBuilderFactory->newEntityIdListBuilder( $importMode );
116143
}
117144

118145
private function newEntityImporter() {
119-
$entityImporterFactory = new EntityImporterFactory(
120-
WikibaseRepo::getDefaultInstance()->getStore()->getEntityStore(),
121-
wfGetLB(),
122-
$this->logger,
123-
$this->getConfig()->get( 'WBImportSourceApi' )
124-
);
146+
$entityImporterFactory =
147+
new EntityImporterFactory( WikibaseRepo::getDefaultInstance()
148+
->getStore()
149+
->getEntityStore(), MediaWikiServices::getInstance()->getDBLoadBalancer(),
150+
$this->logger, $this->getConfig()->get( 'WBImportSourceApi' ) );
125151

126152
return $entityImporterFactory->newEntityImporter();
127153
}
128-
129154
}
130155

131156
$maintClass = "Wikibase\Import\Maintenance\ImportEntities";

src/EntityId/EntityIdListBuilderFactory.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Wikibase\Import\EntityId;
44

5+
use Asparagus\QueryBuilder;
6+
use Asparagus\QueryExecuter;
57
use InvalidArgumentException;
68
use Wikibase\DataModel\Entity\EntityIdParser;
79
use Wikibase\Import\PropertyIdLister;
@@ -20,9 +22,14 @@ class EntityIdListBuilderFactory {
2022
private $propertyIdLister;
2123

2224
/**
23-
* @var QueryRunner
25+
* @var array
2426
*/
25-
private $queryRunner;
27+
private $queryPrefixes;
28+
29+
/**
30+
* @var string
31+
*/
32+
private $queryUrl;
2633

2734
/**
2835
* @var string
@@ -32,18 +39,18 @@ class EntityIdListBuilderFactory {
3239
/**
3340
* @param EntityIdParser $idParser
3441
* @param PropertyIdLister $propertyIdLister
35-
* @param QueryRunner $queryRunner
42+
* @param array $queryPrefixes
43+
* @param string $queryUrl
3644
* @param string $apiUrl
3745
*/
3846
public function __construct(
39-
EntityIdParser $idParser,
40-
PropertyIdLister $propertyIdLister,
41-
QueryRunner $queryRunner,
42-
$apiUrl
47+
EntityIdParser $idParser, PropertyIdLister $propertyIdLister, array $queryPrefixes,
48+
$queryUrl, $apiUrl
4349
) {
4450
$this->idParser = $idParser;
4551
$this->propertyIdLister = $propertyIdLister;
46-
$this->queryRunner = $queryRunner;
52+
$this->queryPrefixes = $queryPrefixes;
53+
$this->queryUrl = $queryUrl;
4754
$this->apiUrl = $apiUrl;
4855
}
4956

@@ -71,10 +78,7 @@ public function newEntityIdListBuilder( $mode ) {
7178
}
7279

7380
private function newAllPropertiesEntityIdListBuilder() {
74-
return new AllPropertiesEntityIdListBuilder(
75-
$this->propertyIdLister,
76-
$this->apiUrl
77-
);
81+
return new AllPropertiesEntityIdListBuilder( $this->propertyIdLister, $this->apiUrl );
7882
}
7983

8084
private function newFileEntityIdListBuilder() {
@@ -86,10 +90,12 @@ private function newIndividualEntityIdListBuilder() {
8690
}
8791

8892
private function newQueryEntityIdListBuilder() {
89-
return new QueryEntityIdListBuilder(
90-
$this->idParser,
91-
$this->queryRunner
92-
);
93+
return new QueryEntityIdListBuilder( $this->idParser, $this->newQueryRunner() );
94+
}
95+
96+
private function newQueryRunner() {
97+
return new QueryRunner( new QueryBuilder( $this->queryPrefixes ),
98+
new QueryExecuter( $this->queryUrl ) );
9399
}
94100

95101
private function newRangeEntityIdListBuilder() {

src/LoggerFactory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ public static function newLogger( $loggerName, $quietMode ) {
2525
throw new InvalidArgumentException( '$quietMode must be boolean' );
2626
}
2727

28-
$formatter = new LineFormatter( "[%datetime%]: %message%\n" );
28+
// unused
29+
$dateTimeFormatter = new LineFormatter( "[%datetime%]: %message%\n" );
2930

3031
if ( $quietMode === true ) {
3132
$handler = new NullHandler();
3233
} else {
34+
$formatter = new LineFormatter( "%message%\n");
35+
3336
$handler = new StreamHandler( 'php://stdout' );
3437
$handler->setFormatter( $formatter );
3538
}

0 commit comments

Comments
 (0)