Skip to content

Commit bbc1b0f

Browse files
author
aude
committed
Adding tests for ImportEntities [WIP]
1 parent 47418de commit bbc1b0f

File tree

5 files changed

+282
-142
lines changed

5 files changed

+282
-142
lines changed

maintenance/importEntities.php

Lines changed: 2 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,14 @@
11
<?php
22

3-
namespace Wikibase\Import\Maintenance;
4-
5-
use Exception;
6-
use MediaWiki\MediaWikiServices;
7-
use Psr\Log\LoggerInterface;
8-
use RuntimeException;
9-
use Wikibase\Import\EntityId\EntityIdListBuilder;
10-
use Wikibase\Import\EntityId\EntityIdListBuilderFactory;
11-
use Wikibase\Import\EntityImporterFactory;
12-
use Wikibase\Import\LoggerFactory;
13-
use Wikibase\Import\Maintenance\ImportOptions;
14-
use Wikibase\Import\PropertyIdLister;
15-
use Wikibase\Repo\WikibaseRepo;
3+
use \Wikibase\Import\Maintenance\ImportEntities;
164

175
$IP = getenv( 'MW_INSTALL_PATH' );
186
if ( $IP === false ) {
197
$IP = __DIR__ . '/../../..';
208
}
219

2210
require_once "$IP/maintenance/Maintenance.php";
23-
24-
class ImportEntities extends \Maintenance {
25-
26-
/**
27-
* @var LoggerInterface
28-
*/
29-
private $logger;
30-
31-
/**
32-
* @var ImportOptions
33-
*/
34-
private $importOptions;
35-
36-
public function __construct() {
37-
parent::__construct();
38-
39-
$this->addOptions();
40-
}
41-
42-
private function addOptions() {
43-
$this->addOption( 'file', 'File with list of entity ids to import', false, true );
44-
$this->addOption( 'entity', 'ID of entity to import', false, true );
45-
$this->addOption( 'query', 'Import items with property and entity id value', false, true );
46-
$this->addOption( 'range', 'Range of ids to import', false, true );
47-
$this->addOption( 'all-properties', 'Import all properties', false, false );
48-
}
49-
50-
public function execute() {
51-
$this->logger = LoggerFactory::newLogger( 'wikibase-import', $this->mQuiet );
52-
$this->importOptions = $this->extractOptions();
53-
54-
try {
55-
foreach ( $this->importOptions as $importMode => $input ) {
56-
$this->output( "Importing $importMode\n" );
57-
58-
$entityIdListBuilder = $this->newEntityIdListBuilder( $importMode );
59-
60-
$ids = $entityIdListBuilder->getEntityIds( $input );
61-
62-
$entityImporter = $this->newEntityImporter();
63-
$entityImporter->importEntities( $ids );
64-
}
65-
}
66-
catch ( Exception $ex ) {
67-
$this->error( $ex->getMessage() );
68-
}
69-
70-
$this->logger->info( 'Done' );
71-
}
72-
73-
/**
74-
* @inheritdoc
75-
*/
76-
protected function error( $err, $die = 0 ) {
77-
$err = "\033[31mERROR:\033[0m $err";
78-
79-
$this->logger->error( $err );
80-
$this->maybeHelp( true );
81-
}
82-
83-
/**
84-
* @return array
85-
*/
86-
private function getValidOptions() {
87-
return [ 'entity', 'file', 'all-properties', 'query', 'range' ];
88-
}
89-
90-
/**
91-
* @return ImportOptions
92-
* @throws RuntimeException
93-
*/
94-
private function extractOptions() {
95-
$options = [];
96-
97-
foreach ( $this->getValidOptions() as $optionName ) {
98-
if ( $this->hasOption( $optionName ) ) {
99-
$options[$optionName] = $this->getOptionValue( $optionName );
100-
}
101-
}
102-
103-
if ( empty( $options ) ) {
104-
throw new RuntimeException( 'No valid import mode option provided' );
105-
}
106-
107-
return new ImportOptions( $options );
108-
}
109-
110-
/**
111-
* @param string $optionName
112-
* @return mixed
113-
*/
114-
private function getOptionValue( $optionName ) {
115-
if ( $optionName === 'all-properties' ) {
116-
return 'all-properties';
117-
} else {
118-
return $this->getOption( $optionName );
119-
}
120-
}
121-
122-
/**
123-
* @param string $importMode
124-
* @return EntityIdListBuilder
125-
*/
126-
private function newEntityIdListBuilder( $importMode ) {
127-
$entityIdListBuilderFactory = new EntityIdListBuilderFactory(
128-
WikibaseRepo::getDefaultInstance()->getEntityIdParser(),
129-
new PropertyIdLister(),
130-
$this->getConfig()->get( 'WBImportQueryPrefixes' ),
131-
$this->getConfig()->get( 'WBImportQueryUrl' ),
132-
$this->getConfig()->get( 'WBImportSourceApi' )
133-
);
134-
135-
return $entityIdListBuilderFactory->newEntityIdListBuilder( $importMode );
136-
}
137-
138-
private function newEntityImporter() {
139-
$entityImporterFactory = new EntityImporterFactory(
140-
WikibaseRepo::getDefaultInstance()->getStore()->getEntityStore(),
141-
MediaWikiServices::getInstance()->getDBLoadBalancer(),
142-
$this->logger,
143-
$this->getConfig()->get( 'WBImportSourceApi' )
144-
);
145-
146-
return $entityImporterFactory->newEntityImporter();
147-
}
148-
}
11+
require_once __DIR__ . '/../src/Maintenance/ImportEntities.php';
14912

15013
$maintClass = ImportEntities::class;
15114
require_once RUN_MAINTENANCE_IF_MAIN;

src/EntityId/EntityIdListBuilderFactory.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ class EntityIdListBuilderFactory {
4444
* @param string $apiUrl
4545
*/
4646
public function __construct(
47-
EntityIdParser $idParser, PropertyIdLister $propertyIdLister, array $queryPrefixes,
48-
$queryUrl, $apiUrl
47+
EntityIdParser $idParser,
48+
PropertyIdLister $propertyIdLister,
49+
array $queryPrefixes,
50+
$queryUrl,
51+
$apiUrl
4952
) {
5053
$this->idParser = $idParser;
5154
$this->propertyIdLister = $propertyIdLister;

src/EntityImporter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Wikibase\DataModel\Snak\PropertyValueSnak;
1212
use Wikibase\DataModel\Statement\StatementList;
1313
use Wikibase\Import\Store\ImportedEntityMappingStore;
14+
use Wikibase\Lib\Store\EntityStore;
1415
use Wikibase\Repo\Store\WikiPageEntityStore;
1516

1617
class EntityImporter {
@@ -39,7 +40,7 @@ public function __construct(
3940
StatementsImporter $statementsImporter,
4041
BadgeItemUpdater $badgeItemUpdater,
4142
ApiEntityLookup $apiEntityLookup,
42-
WikiPageEntityStore $entityStore,
43+
EntityStore $entityStore,
4344
ImportedEntityMappingStore $entityMappingStore,
4445
StatementsCountLookup $statementsCountLookup,
4546
LoggerInterface $logger

src/Maintenance/ImportEntities.php

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<?php
2+
3+
namespace Wikibase\Import\Maintenance;
4+
5+
use Exception;
6+
use MediaWiki\MediaWikiServices;
7+
use Psr\Log\LoggerInterface;
8+
use RuntimeException;
9+
use Wikibase\Import\EntityId\EntityIdListBuilderFactory;
10+
use Wikibase\Import\EntityImporterFactory;
11+
use Wikibase\Import\LoggerFactory;
12+
use Wikibase\Import\PropertyIdLister;
13+
use Wikibase\Repo\WikibaseRepo;
14+
15+
class ImportEntities extends \Maintenance {
16+
17+
/**
18+
* @var EntityIdListBuilderFactory
19+
*/
20+
private $entityIdListBuilderFactory;
21+
22+
/**
23+
* @var EntityImporterFactory
24+
*/
25+
private $entityImporterFactory;
26+
27+
/**
28+
* @var LoggerInterface
29+
*/
30+
private $logger;
31+
32+
public function __construct() {
33+
parent::__construct();
34+
35+
$this->addOptions();
36+
}
37+
38+
private function addOptions() {
39+
$this->addOption( 'file', 'File with list of entity ids to import', false, true );
40+
$this->addOption( 'entity', 'ID of entity to import', false, true );
41+
$this->addOption( 'query', 'Import items with property and entity id value', false, true );
42+
$this->addOption( 'range', 'Range of ids to import', false, true );
43+
$this->addOption( 'all-properties', 'Import all properties', false, false );
44+
}
45+
46+
public function setServices(
47+
EntityIdListBuilderFactory $entityIdListBuilderFactory,
48+
EntityImporterFactory $entityImporterFactory,
49+
LoggerInterface $logger
50+
) {
51+
$this->entityIdListBuilderFactory = $entityIdListBuilderFactory;
52+
$this->entityImporterFactory = $entityImporterFactory;
53+
$this->logger = $logger;
54+
}
55+
56+
public function initServices() {
57+
// needed for EntityImporter
58+
if ( !isset( $this->logger ) ) {
59+
$this->logger = LoggerFactory::newLogger( 'wikibase-import', $this->mQuiet );
60+
}
61+
62+
if ( !isset( $this->entityIdListBuilderFactory ) ) {
63+
$this->entityIdListBuilderFactory = $this->newEntityIdListBuilderFactory();
64+
}
65+
66+
if ( !isset( $this->entityImporterFactory ) ) {
67+
$this->entityImporterFactory = $this->newEntityImporterFactory( $this->logger );
68+
}
69+
}
70+
71+
public function execute() {
72+
$this->initServices();
73+
74+
try {
75+
$this->doImport( $this->extractOptions() );
76+
} catch ( Exception $ex ) {
77+
$this->error( $ex->getMessage() );
78+
}
79+
80+
$this->logger->info( 'Done' );
81+
}
82+
83+
/**
84+
* @param ImportOptions $importOptions
85+
*/
86+
private function doImport( ImportOptions $importOptions ) {
87+
foreach ( $importOptions as $importMode => $input ) {
88+
$this->logger->info( "Importing $importMode\n" );
89+
90+
$entityIdListBuilder = $this->entityIdListBuilderFactory->newEntityIdListBuilder(
91+
$importMode
92+
);
93+
94+
$ids = $entityIdListBuilder->getEntityIds( $input );
95+
$this->entityImporterFactory->newEntityImporter()->importEntities( $ids );
96+
}
97+
}
98+
99+
/**
100+
* @inheritdoc
101+
*/
102+
protected function error( $err, $die = 0 ) {
103+
$err = "\033[31mERROR:\033[0m $err";
104+
105+
$this->logger->error( $err );
106+
$this->maybeHelp( true );
107+
}
108+
109+
/**
110+
* @return array
111+
*/
112+
private function getValidOptions() {
113+
return [ 'entity', 'file', 'all-properties', 'query', 'range' ];
114+
}
115+
116+
/**
117+
* @return ImportOptions
118+
* @throws RuntimeException
119+
*/
120+
private function extractOptions() {
121+
$options = [];
122+
123+
foreach ( $this->getValidOptions() as $optionName ) {
124+
if ( $this->hasOption( $optionName ) ) {
125+
$options[$optionName] = $this->getOptionValue( $optionName );
126+
}
127+
}
128+
129+
if ( empty( $options ) ) {
130+
throw new RuntimeException( 'No valid import mode option provided' );
131+
}
132+
133+
return new ImportOptions( $options );
134+
}
135+
136+
/**
137+
* @param string $optionName
138+
* @return mixed
139+
*/
140+
private function getOptionValue( $optionName ) {
141+
if ( $optionName === 'all-properties' ) {
142+
return 'all-properties';
143+
} else {
144+
return $this->getOption( $optionName );
145+
}
146+
}
147+
148+
/**
149+
* @return EntityIdListBuilderFactory
150+
*/
151+
private function newEntityIdListBuilderFactory() {
152+
$entityIdListBuilderFactory = new EntityIdListBuilderFactory(
153+
WikibaseRepo::getDefaultInstance()->getEntityIdParser(),
154+
new PropertyIdLister(),
155+
$this->getConfig()->get( 'WBImportQueryPrefixes' ),
156+
$this->getConfig()->get( 'WBImportQueryUrl' ),
157+
$this->getConfig()->get( 'WBImportSourceApi' )
158+
);
159+
160+
return $entityIdListBuilderFactory;
161+
}
162+
163+
/**
164+
* @return EntityImporterFactory
165+
*/
166+
private function newEntityImporterFactory( LoggerInterface $logger ) {
167+
$entityImporterFactory = new EntityImporterFactory(
168+
WikibaseRepo::getDefaultInstance()->getStore()->getEntityStore(),
169+
MediaWikiServices::getInstance()->getDBLoadBalancer(),
170+
$logger,
171+
$this->getConfig()->get( 'WBImportSourceApi' )
172+
);
173+
174+
return $entityImporterFactory;
175+
}
176+
}

0 commit comments

Comments
 (0)