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