-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathimportEntities.php
133 lines (103 loc) · 3.48 KB
/
importEntities.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
namespace Wikibase\Import\Maintenance;
use Asparagus\QueryBuilder;
use Asparagus\QueryExecuter;
use Exception;
use Psr\Log\LoggerInterface;
use Wikibase\Import\Console\ImportOptions;
use Wikibase\Import\EntityId\EntityIdListBuilderFactory;
use Wikibase\Import\EntityImporterFactory;
use Wikibase\Import\LoggerFactory;
use Wikibase\Import\QueryRunner;
use Wikibase\Import\PropertyIdLister;
use Wikibase\Repo\WikibaseRepo;
$IP = getenv( 'MW_INSTALL_PATH' );
if ( $IP === false ) {
$IP = __DIR__ . '/../../..';
}
require_once "$IP/maintenance/Maintenance.php";
class ImportEntities extends \Maintenance {
/**
* @var LoggerInterface
*/
private $logger;
public function __construct() {
parent::__construct();
$this->addOptions();
}
private function addOptions() {
$this->addOption( 'file', 'File with list of entity ids to import', false, true );
$this->addOption( 'entity', 'ID of entity to import', false, true );
$this->addOption( 'query', 'Import items with property and entity id value', false, true );
$this->addOption( 'range', 'Range of ids to import', false, true );
$this->addOption( 'stdin', 'Read entity IDs to import from standard input', false, false );
$this->addOption( 'all-properties', 'Import all properties', false, false );
}
public function execute() {
$this->logger = LoggerFactory::newLogger( 'wikibase-import', $this->mQuiet );
$importOptions = $this->extractOptions();
$entityIdListBuilderFactory = $this->newEntityIdListBuilderFactory();
foreach ( $this->getValidOptions() as $option ) {
if ( $importOptions->hasOption( $option ) ) {
$entityIdListBuilder = $entityIdListBuilderFactory->newEntityIdListBuilder(
$option
);
if ( $option === 'all-properties' ) {
$input = 'all-properties';
} else {
$input = $importOptions->getOption( $option );
}
break;
}
}
if ( !isset( $entityIdListBuilder ) ) {
$this->logger->error( 'ERROR: No valid import option was provided' );
return;
} else {
try {
$ids = $entityIdListBuilder->getEntityIds( $input );
$entityImporter = $this->newEntityImporter();
$entityImporter->importEntities( $ids );
} catch ( Exception $ex ) {
$this->logger->error( $ex->getMessage() );
}
$this->logger->info( 'Done' );
}
}
private function extractOptions() {
$options = [];
foreach ( $this->getValidOptions() as $optionName ) {
$options[$optionName] = $this->getOption( $optionName );
}
if ( empty( $options ) ) {
$this->maybeHelp( true );
}
return new ImportOptions( $options );
}
private function getValidOptions() {
return [ 'entity', 'file', 'all-properties', 'query', 'range', 'stdin' ];
}
private function newEntityIdListBuilderFactory() {
$queryRunner = new QueryRunner(
new QueryBuilder( $this->getConfig()->get( 'WBImportQueryPrefixes' ) ),
new QueryExecuter( $this->getConfig()->get( 'WBImportQueryUrl' ) )
);
return new EntityIdListBuilderFactory(
WikibaseRepo::getDefaultInstance()->getEntityIdParser(),
new PropertyIdLister(),
$queryRunner,
$this->getConfig()->get( 'WBImportSourceApi' )
);
}
private function newEntityImporter() {
$entityImporterFactory = new EntityImporterFactory(
WikibaseRepo::getDefaultInstance()->getStore()->getEntityStore(),
wfGetLB(),
$this->logger,
$this->getConfig()->get( 'WBImportSourceApi' )
);
return $entityImporterFactory->newEntityImporter();
}
}
$maintClass = "Wikibase\Import\Maintenance\ImportEntities";
require_once RUN_MAINTENANCE_IF_MAIN;