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