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
@@ -28,6 +28,11 @@ class ImportEntities extends \Maintenance {
2828 */
2929 private $ logger ;
3030
31+ /**
32+ * @var ImportOptions
33+ */
34+ private $ importOptions ;
35+
3136 public function __construct () {
3237 parent ::__construct ();
3338
@@ -44,45 +49,46 @@ private function addOptions() {
4449
4550 public function execute () {
4651 $ this ->logger = LoggerFactory::newLogger ( 'wikibase-import ' , $ this ->mQuiet );
52+ $ this ->importOptions = $ this ->extractOptions ();
4753
48- $ importOptions = $ this ->extractOptions ();
54+ try {
55+ $ importMode = $ this ->getImportMode ();
56+ $ entityIdListBuilder = $ this ->newEntityIdListBuilder ( $ importMode );
4957
50- $ entityIdListBuilderFactory = $ this ->newEntityIdListBuilderFactory ();
58+ $ input = $ this ->getInputForMode ( $ importMode );
59+ $ ids = $ entityIdListBuilder ->getEntityIds ( $ input );
5160
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- }
61+ $ entityImporter = $ this ->newEntityImporter ();
62+ $ entityImporter ->importEntities ( $ ids );
63+ }
64+ catch ( Exception $ ex ) {
65+ $ this ->error ( $ ex ->getMessage () );
6666 }
6767
68- if ( ! isset ( $ entityIdListBuilder ) ) {
69- $ this -> logger -> error ( ' ERROR: No valid import option was provided ' );
68+ $ this -> logger -> info ( ' Done ' );
69+ }
7070
71- return ;
72- } else {
73- try {
74- $ ids = $ entityIdListBuilder ->getEntityIds ( $ input );
71+ /**
72+ * @inheritdoc
73+ */
74+ protected function error ( $ err , $ die = 0 ) {
75+ $ err = "\033[31mERROR: \033[0m $ err " ;
7576
76- $ entityImporter = $ this ->newEntityImporter ();
77- $ entityImporter ->importEntities ( $ ids );
78- } catch ( Exception $ ex ) {
79- $ this ->logger ->error ( $ ex ->getMessage () );
80- }
77+ $ this ->logger ->error ( $ err );
78+ $ this ->maybeHelp ( true );
79+ }
8180
82- $ this ->logger ->info ( 'Done ' );
83- }
81+ /**
82+ * @return array
83+ */
84+ private function getValidOptions () {
85+ return [ 'entity ' , 'file ' , 'all-properties ' , 'query ' , 'range ' ];
8486 }
8587
88+ /**
89+ * @return ImportOptions
90+ * @throws RuntimeException
91+ */
8692 private function extractOptions () {
8793 $ options = [];
8894
@@ -91,42 +97,65 @@ private function extractOptions() {
9197 }
9298
9399 if ( empty ( $ options ) ) {
94- $ this -> maybeHelp ( true );
100+ throw new RuntimeException ( ' No valid import mode option provided ' );
95101 }
96102
97103 return new ImportOptions ( $ options );
98104 }
99105
100- private function getValidOptions () {
101- return [ 'entity ' , 'file ' , 'all-properties ' , 'query ' , 'range ' ];
106+ /**
107+ * @return string
108+ * @throws RuntimeException
109+ */
110+ private function getImportMode () {
111+ foreach ( $ this ->getValidOptions () as $ option ) {
112+ if ( $ this ->importOptions ->hasOption ( $ option ) ) {
113+ return $ option ;
114+ }
115+ }
116+
117+ throw new RuntimeException ( 'No valid import option was provided ' );
102118 }
103119
104- private function newEntityIdListBuilderFactory () {
105- $ queryRunner = new QueryRunner (
106- new QueryBuilder ( $ this ->getConfig ()->get ( 'WBImportQueryPrefixes ' ) ),
107- new QueryExecuter ( $ this ->getConfig ()->get ( 'WBImportQueryUrl ' ) )
108- );
120+ /**
121+ * @param string $mode
122+ * @return mixed
123+ */
124+ private function getInputForMode ( $ mode ) {
125+ if ( $ mode === 'all-properties ' ) {
126+ return 'all-properties ' ;
127+ } else {
128+ return $ this ->importOptions ->getOption ( $ mode );
129+ }
130+ }
109131
110- return new EntityIdListBuilderFactory (
132+ /**
133+ * @param string $importMode
134+ * @return EntityIdListBuilder
135+ */
136+ private function newEntityIdListBuilder ( $ importMode ) {
137+ $ entityIdListBuilderFactory = new EntityIdListBuilderFactory (
111138 WikibaseRepo::getDefaultInstance ()->getEntityIdParser (),
112139 new PropertyIdLister (),
113- $ queryRunner ,
140+ $ this ->getConfig ()->get ( 'WBImportQueryPrefixes ' ),
141+ $ this ->getConfig ()->get ( 'WBImportQueryUrl ' ),
114142 $ this ->getConfig ()->get ( 'WBImportSourceApi ' )
115143 );
144+
145+ return $ entityIdListBuilderFactory ->newEntityIdListBuilder ( $ importMode );
116146 }
117147
118148 private function newEntityImporter () {
119149 $ entityImporterFactory = new EntityImporterFactory (
120150 WikibaseRepo::getDefaultInstance ()->getStore ()->getEntityStore (),
121- wfGetLB (),
151+ MediaWikiServices:: getInstance ()-> getDBLoadBalancer (),
122152 $ this ->logger ,
123153 $ this ->getConfig ()->get ( 'WBImportSourceApi ' )
124154 );
125155
126156 return $ entityImporterFactory ->newEntityImporter ();
127157 }
128-
129158}
130159
131- $ maintClass = " Wikibase\Import\Maintenance\ ImportEntities" ;
160+ $ maintClass = ImportEntities::class ;
132161require_once RUN_MAINTENANCE_IF_MAIN ;
0 commit comments