22
33namespace Wikibase \Import \Maintenance ;
44
5- use Asparagus \QueryBuilder ;
6- use Asparagus \QueryExecuter ;
75use Exception ;
6+ use MediaWiki \MediaWikiServices ;
87use Psr \Log \LoggerInterface ;
8+ use RuntimeException ;
99use Wikibase \Import \Console \ImportOptions ;
10+ use Wikibase \Import \EntityId \EntityIdListBuilder ;
1011use Wikibase \Import \EntityId \EntityIdListBuilderFactory ;
1112use Wikibase \Import \EntityImporterFactory ;
1213use Wikibase \Import \LoggerFactory ;
13- use Wikibase \Import \QueryRunner ;
1414use Wikibase \Import \PropertyIdLister ;
1515use Wikibase \Repo \WikibaseRepo ;
1616
2222require_once "$ IP /maintenance/Maintenance.php " ;
2323
2424class ImportEntities extends \Maintenance {
25-
2625 /**
2726 * @var LoggerInterface
2827 */
2928 private $ logger ;
3029
30+ /**
31+ * @var ImportOptions
32+ */
33+ private $ importOptions ;
34+
3135 public function __construct () {
3236 parent ::__construct ();
3337
@@ -44,45 +48,46 @@ private function addOptions() {
4448
4549 public function execute () {
4650 $ this ->logger = LoggerFactory::newLogger ( 'wikibase-import ' , $ this ->mQuiet );
51+ $ this ->importOptions = $ this ->extractOptions ();
4752
48- $ importOptions = $ this ->extractOptions ();
53+ try {
54+ $ importMode = $ this ->getImportMode ();
55+ $ entityIdListBuilder = $ this ->newEntityIdListBuilder ( $ importMode );
4956
50- $ entityIdListBuilderFactory = $ this ->newEntityIdListBuilderFactory ();
57+ $ input = $ this ->getInputForMode ( $ importMode );
58+ $ ids = $ entityIdListBuilder ->getEntityIds ( $ input );
5159
52- foreach ( $ this ->getValidOptions () as $ option ) {
53- if ( $ importOptions ->hasOption ( $ option ) ) {
54- $ entityIdListBuilder = $ entityIdListBuilderFactory ->newEntityIdListBuilder (
55- $ option
56- );
57-
58- if ( $ option === 'all-properties ' ) {
59- $ input = 'all-properties ' ;
60- } else {
61- $ input = $ importOptions ->getOption ( $ option );
62- }
63-
64- break ;
65- }
60+ $ entityImporter = $ this ->newEntityImporter ();
61+ $ entityImporter ->importEntities ( $ ids );
62+ }
63+ catch ( Exception $ ex ) {
64+ $ this ->error ( $ ex ->getMessage () );
6665 }
6766
68- if ( ! isset ( $ entityIdListBuilder ) ) {
69- $ this -> logger -> error ( ' ERROR: No valid import option was provided ' );
67+ $ this -> logger -> info ( ' Done ' );
68+ }
7069
71- return ;
72- } else {
73- try {
74- $ ids = $ entityIdListBuilder ->getEntityIds ( $ input );
70+ /**
71+ * @inheritdoc
72+ */
73+ protected function error ( $ err , $ die = 0 ) {
74+ $ err = "\033[31mERROR: \033[0m $ err " ;
7575
76- $ entityImporter = $ this ->newEntityImporter ();
77- $ entityImporter ->importEntities ( $ ids );
78- } catch ( Exception $ ex ) {
79- $ this ->logger ->error ( $ ex ->getMessage () );
80- }
76+ $ this ->logger ->error ( $ err );
77+ $ this ->maybeHelp ( true );
78+ }
8179
82- $ this ->logger ->info ( 'Done ' );
83- }
80+ /**
81+ * @return array
82+ */
83+ private function getValidOptions () {
84+ return [ 'entity ' , 'file ' , 'all-properties ' , 'query ' , 'range ' ];
8485 }
8586
87+ /**
88+ * @return ImportOptions
89+ * @throws RuntimeException
90+ */
8691 private function extractOptions () {
8792 $ options = [];
8893
@@ -91,41 +96,61 @@ private function extractOptions() {
9196 }
9297
9398 if ( empty ( $ options ) ) {
94- $ this -> maybeHelp ( true );
99+ throw new RuntimeException ( ' No valid import mode option provided ' );
95100 }
96101
97102 return new ImportOptions ( $ options );
98103 }
99104
100- private function getValidOptions () {
101- return [ 'entity ' , 'file ' , 'all-properties ' , 'query ' , 'range ' ];
105+ /**
106+ * @return string
107+ * @throws RuntimeException
108+ */
109+ private function getImportMode () {
110+ foreach ( $ this ->getValidOptions () as $ option ) {
111+ if ( $ this ->importOptions ->hasOption ( $ option ) ) {
112+ return $ option ;
113+ }
114+ }
115+
116+ throw new RuntimeException ( 'No valid import option was provided ' );
102117 }
103118
104- private function newEntityIdListBuilderFactory () {
105- $ queryRunner = new QueryRunner (
106- new QueryBuilder ( $ this ->getConfig ()->get ( 'WBImportQueryPrefixes ' ) ),
107- new QueryExecuter ( $ this ->getConfig ()->get ( 'WBImportQueryUrl ' ) )
108- );
109-
110- return new EntityIdListBuilderFactory (
111- WikibaseRepo::getDefaultInstance ()->getEntityIdParser (),
112- new PropertyIdLister (),
113- $ queryRunner ,
114- $ this ->getConfig ()->get ( 'WBImportSourceApi ' )
115- );
119+ /**
120+ * @param string $mode
121+ * @return mixed
122+ */
123+ private function getInputForMode ( $ mode ) {
124+ if ( $ mode === 'all-properties ' ) {
125+ return 'all-properties ' ;
126+ } else {
127+ return $ this ->importOptions ->getOption ( $ mode );
128+ }
129+ }
130+
131+ /**
132+ * @param string $importMode
133+ * @return EntityIdListBuilder
134+ */
135+ private function newEntityIdListBuilder ( $ importMode ) {
136+ $ entityIdListBuilderFactory =
137+ new EntityIdListBuilderFactory ( WikibaseRepo::getDefaultInstance ()->getEntityIdParser (),
138+ new PropertyIdLister (), $ this ->getConfig ()->get ( 'WBImportQueryPrefixes ' ),
139+ $ this ->getConfig ()->get ( 'WBImportQueryUrl ' ),
140+ $ this ->getConfig ()->get ( 'WBImportSourceApi ' ) );
141+
142+ return $ entityIdListBuilderFactory ->newEntityIdListBuilder ( $ importMode );
116143 }
117144
118145 private function newEntityImporter () {
119- $ entityImporterFactory = new EntityImporterFactory (
120- WikibaseRepo::getDefaultInstance ()->getStore ()->getEntityStore (),
121- wfGetLB (),
122- $ this ->logger ,
123- $ this ->getConfig ()->get ( 'WBImportSourceApi ' )
124- );
146+ $ entityImporterFactory =
147+ new EntityImporterFactory ( WikibaseRepo::getDefaultInstance ()
148+ ->getStore ()
149+ ->getEntityStore (), MediaWikiServices::getInstance ()->getDBLoadBalancer (),
150+ $ this ->logger , $ this ->getConfig ()->get ( 'WBImportSourceApi ' ) );
125151
126152 return $ entityImporterFactory ->newEntityImporter ();
127153 }
128-
129154}
130155
131156$ maintClass = "Wikibase\Import\Maintenance\ImportEntities " ;
0 commit comments