Skip to content

Commit c8a5fb1

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

5 files changed

Lines changed: 283 additions & 142 deletions

File tree

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

0 commit comments

Comments
 (0)