-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSemanticDataUpdate.php
More file actions
80 lines (62 loc) · 2.81 KB
/
SemanticDataUpdate.php
File metadata and controls
80 lines (62 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
declare( strict_types = 1 );
namespace MediaWiki\Extension\SemanticWikibase;
use MediaWiki\Extension\SemanticWikibase\SMW\SemanticEntity;
use MediaWiki\Extension\SemanticWikibase\Translation\ItemTranslator;
use MediaWiki\Extension\SemanticWikibase\Translation\PropertyTranslator;
use MediaWiki\Extension\SemanticWikibase\Translation\TranslatorFactory;
use SMW\DIWikiPage;
use SMW\SemanticData;
use Title;
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\DataModel\Entity\NumericPropertyId;
use Wikibase\DataModel\Services\Lookup\ItemLookup;
use Wikibase\DataModel\Services\Lookup\PropertyLookup;
class SemanticDataUpdate {
private TranslatorFactory $translatorFactory;
private ItemLookup $itemLookup;
private PropertyLookup $propertyLookup;
public function __construct( TranslatorFactory $translatorFactory, ItemLookup $itemLookup, PropertyLookup $propertyLookup ) {
$this->translatorFactory = $translatorFactory;
$this->itemLookup = $itemLookup;
$this->propertyLookup = $propertyLookup;
}
public function run( SemanticData $semanticData ): void {
$title = $semanticData->getSubject()->getTitle();
if ( $title === null ) {
return;
}
$semanticData->importDataFrom(
$this->getSemanticEntityFromTitle( $title )->toSemanticData( $semanticData->getSubject() )
);
}
private function getSemanticEntityFromTitle( Title $title ): SemanticEntity {
if ( $title->getNamespace() === WB_NS_ITEM ) {
return $this->getSemanticEntityForItemTitle( $title );
}
if ( $title->getNamespace() === WB_NS_PROPERTY ) {
return $this->getSemanticEntityForPropertyTitle( $title );
}
return new SemanticEntity();
}
private function getSemanticEntityForItemTitle( Title $title ): SemanticEntity {
return $this->newItemTranslator( $title )->translateItem(
$this->itemLookup->getItemForId( new ItemId( $title->getText() ) )
);
}
private function newItemTranslator( Title $title ): ItemTranslator {
return $this->translatorFactory->newItemTranslator( DIWikiPage::newFromTitle( $title ) );
}
private function getSemanticEntityForPropertyTitle( Title $title ): SemanticEntity {
wfDebug(__METHOD__. "swb: getSemanticEntity:".json_encode($title));
wfDebug(__METHOD__. "swb: getSemanticEntity:".json_encode($title->getText()));
wfDebug(__METHOD__. "swb: getSemanticEntity:".json_encode(new NumericPropertyId( $title->getText() )));
wfDebug(__METHOD__. "swb: getSemanticEntity:".json_encode($this->propertyLookup->getPropertyForId( new NumericPropertyId( $title->getText() ) )));
return $this->newPropertyTranslator( $title )->translateProperty(
$this->propertyLookup->getPropertyForId( new NumericPropertyId( $title->getText() ) )
);
}
private function newPropertyTranslator( Title $title ): PropertyTranslator {
return $this->translatorFactory->newPropertyTranslator( DIWikiPage::newFromTitle( $title ) );
}
}