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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
<?php
namespace SMW\PropertyAnnotators;
use SMW\DataTypeRegistry;
use SMW\DataValueFactory;
use SMW\DIProperty;
/**
* @license GNU GPL v2+
* @since 2.2
*
* @author mwjames
*/
class MandatoryTypePropertyAnnotator extends PropertyAnnotatorDecorator {
/**
* Indicates a forced removal
*/
const IMPO_REMOVED_TYPE = 'mandatorytype.propertyannotator.impo.removed.type';
protected function addPropertyValues() {
$subject = $this->getSemanticData()->getSubject();
if ( $subject->getNamespace() !== SMW_NS_PROPERTY ) {
return;
}
$property = DIProperty::newFromUserLabel(
str_replace( '_', ' ', $subject->getDBKey() )
);
if ( !$property->isUserDefined() ) {
return;
}
$this->findMandatoryTypeForImportVocabulary();
}
private function findMandatoryTypeForImportVocabulary() {
$property = new DIProperty( '_IMPO' );
$dataItems = $this->getSemanticData()->getPropertyValues(
$property
);
if ( $dataItems === null || $dataItems === [] ) {
return;
}
$this->addTypeFromImportVocabulary( $property, current( $dataItems ) );
}
private function addTypeFromImportVocabulary( $property, $dataItem ) {
$importValue = DataValueFactory::getInstance()->newDataValueByItem(
$dataItem,
$property
);
if ( strpos( $importValue->getTermType(), ':' ) === false ) {
return;
}
$property = new DIProperty( '_TYPE' );
list( $ns, $type ) = explode( ':', $importValue->getTermType(), 2 );
$typeId = DataTypeRegistry::getInstance()->findTypeId( $type );
if ( $typeId === '' ) {
return;
}
$dataValue = DataValueFactory::getInstance()->newDataValueByProperty(
$property,
$typeId
);
$this->replaceAnyTypeByImportType( $property, $dataValue );
}
private function replaceAnyTypeByImportType( DIProperty $property, $dataValue ) {
foreach ( $this->getSemanticData()->getPropertyValues( $property ) as $dataItem ) {
$this->getSemanticData()->setOption( self::IMPO_REMOVED_TYPE, $dataItem );
$this->getSemanticData()->removePropertyObjectValue(
$property,
$dataItem
);
}
$this->getSemanticData()->addDataValue( $dataValue );
}
}
|