|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\QueryEngine\Importer; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Iterator; |
|
7
|
|
|
use Wikibase\DataModel\Entity\Entity; |
|
8
|
|
|
use Wikibase\DataModel\Entity\EntityDocument; |
|
9
|
|
|
use Wikibase\QueryEngine\QueryStoreWriter; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @since 0.3 |
|
13
|
|
|
* @licence GNU GPL v2+ |
|
14
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
15
|
|
|
*/ |
|
16
|
|
|
class EntitiesImporter { |
|
17
|
|
|
|
|
18
|
|
|
private $storeWriter; |
|
19
|
|
|
private $entityIterator; |
|
20
|
|
|
private $reporter; |
|
21
|
|
|
|
|
22
|
|
|
private $shouldStop = false; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param QueryStoreWriter $storeWriter |
|
26
|
|
|
* @param Iterator $entityIterator Each value should be of type Entity |
|
27
|
|
|
* @param ImportReporter|null $reporter |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct( QueryStoreWriter $storeWriter, Iterator $entityIterator, ImportReporter $reporter = null ) { |
|
30
|
|
|
$this->storeWriter = $storeWriter; |
|
31
|
|
|
$this->entityIterator = $entityIterator; |
|
32
|
|
|
$this->reporter = $reporter === null ? new NullImportReporter() : $reporter; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function run() { |
|
36
|
|
|
$this->reporter->onImportStarted(); |
|
37
|
|
|
|
|
38
|
|
|
foreach ( $this->entityIterator as $entity ) { |
|
39
|
|
|
if ( function_exists( 'pcntl_signal_dispatch' ) ) { |
|
40
|
|
|
pcntl_signal_dispatch(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if ( $this->shouldStop ) { |
|
44
|
|
|
$this->shouldStop = false; |
|
45
|
|
|
$this->reporter->onImportAborted(); |
|
46
|
|
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$this->importEntity( $entity ); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$this->reporter->onImportCompleted(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function importEntity( EntityDocument $entity ) { |
|
56
|
|
|
$this->reporter->onEntityInsertStarted( $entity ); |
|
57
|
|
|
$wasSuccessful = false; |
|
58
|
|
|
|
|
59
|
|
|
try { |
|
60
|
|
|
$this->storeWriter->updateEntity( $entity ); |
|
61
|
|
|
$wasSuccessful = true; |
|
62
|
|
|
} |
|
63
|
|
|
catch ( Exception $ex ) { |
|
64
|
|
|
$this->reporter->onEntityInsertFailed( $entity, $ex ); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ( $wasSuccessful ) { |
|
68
|
|
|
$this->reporter->onEntityInsertSucceeded( $entity ); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function stop() { |
|
73
|
|
|
$this->shouldStop = true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|