summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/DataValues/ValueValidators/UniquenessConstraintValueValidator.php
blob: 65ace8e3e9b18df787e70b7bfd2d0502a744a0a6 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php

namespace SMW\DataValues\ValueValidators;

use SMW\PropertySpecificationLookup;
use SMW\DIWikiPage;
use SMWDataValue as DataValue;
use SMW\RequestOptions;
use SMW\Store;

/**
 * @private
 *
 * Only allow values that are unique where uniqueness is establised for the first (
 * in terms of time which also entails that after a full rebuild the first value
 * found is being categorised as established value) value assigned to a property
 * (that requires this trait) and any value that compares to an establised
 * value with the same literal representation is being identified as violating the
 * uniqueness constraint.
 *
 * @license GNU GPL v2+
 * @since 2.4
 *
 * @author mwjames
 */
class UniquenessConstraintValueValidator implements ConstraintValueValidator {

	/**
	 * @var Store
	 */
	private $store;

	/**
	 * @var PropertySpecificationLookup
	 */
	private $propertySpecificationLookup;

	/**
	 * @var boolean
	 */
	private $hasConstraintViolation = false;

	/**
	 * Tracks annotations for the current context to verify that a subject only
	 * contains unique assignments.
	 *
	 * @var array
	 */
	private static $annotations = [];

	/**
	 * @since 2.4
	 *
	 * @param Store $store
	 * @param PropertySpecificationLookup $propertySpecificationLookup
	 */
	public function __construct( Store $store, PropertySpecificationLookup $propertySpecificationLookup ) {
		$this->store = $store;
		$this->propertySpecificationLookup = $propertySpecificationLookup;
	}

	/**
	 * @since 2.4
	 *
	 * {@inheritDoc}
	 */
	public function hasConstraintViolation() {
		return $this->hasConstraintViolation;
	}

	/**
	 * @since 3.0
	 *
	 * {@inheritDoc}
	 */
	public function clear() {
		self::$annotations = [];
	}

	/**
	 * @since 2.4
	 *
	 * {@inheritDoc}
	 */
	public function validate( $dataValue ) {

		$this->hasConstraintViolation = false;

		if ( !$this->canValidate( $dataValue ) ) {
			return $this->hasConstraintViolation;
		}

		$property = $dataValue->getProperty();

		if ( !$this->propertySpecificationLookup->hasUniquenessConstraint( $property ) ) {
			return $this->hasConstraintViolation;
		}

		$contextPage = $dataValue->getContextPage();

		// Exclude the current page from the result match to check whether another
		// page matches the condition and if so then the value can no longer be
		// assigned and is not unique
		$requestOptions = new RequestOptions();

		$requestOptions->addExtraCondition( function( $store, $query, $alias ) use( $contextPage ) {
				return $query->neq( "$alias.s_id", $store->getObjectIds()->getId( $contextPage ) );
			}
		);

		$requestOptions->setLimit( 2 );
		$count = 0;

		if ( !$this->hasAnnotation( $dataValue ) ) {
			$entityValueUniquenessConstraintChecker = $this->store->service( 'EntityValueUniquenessConstraintChecker' );

			$res = $entityValueUniquenessConstraintChecker->checkConstraint(
				$property,
				$dataValue->getDataItem(),
				$requestOptions
			);

			$count = count( $res );
		}

		// Check whether the current page has any other annotation for the
		// same property
		if ( $count < 1 && $this->isRegistered( $dataValue ) ) {
			$dataValue->addErrorMsg(
				[
					'smw-datavalue-uniqueness-constraint-isknown',
					$property->getLabel(),
					$contextPage->getTitle()->getPrefixedText(),
					$dataValue->getWikiValue()
				]
			);

			$this->hasConstraintViolation = true;
		}

		// Has the page different values for the same property?
		if ( $count < 1 ) {
			return $this->hasConstraintViolation;
		}

		$this->hasConstraintViolation = true;

		foreach ( $res as $dataItem ) {
			$val = $dataValue->isValid() ? $dataValue->getWikiValue() : '...';
			$text = '';

			if ( $dataItem !== null && ( $title = $dataItem->getTitle() ) !== null ) {
				$text = $title->getPrefixedText();
			}

			$dataValue->addErrorMsg(
				[
					'smw-datavalue-uniqueness-constraint-error',
					$property->getLabel(),
					$val,
					$text
				]
			);
		}

		return $this->hasConstraintViolation;
	}

	private function canValidate( $dataValue ) {

		if ( !$dataValue->isEnabledFeature( SMW_DV_PVUC ) || !$dataValue instanceof DataValue ) {
			return false;
		}

		return $dataValue->getContextPage() !== null && $dataValue->getProperty() !== null;
	}

	private function isRegistered( $dataValue ) {

		$contextPage = $dataValue->getContextPage();
		$dataItem = $dataValue->getDataItem();
		$property = $dataValue->getProperty();

		$valueHash = md5( $property->getKey() . $dataItem->getHash() );
		$key = $property->getKey();
		$hash = $contextPage->getHash();

		if ( isset( self::$annotations[$hash][$key] ) && self::$annotations[$hash][$key] !== $valueHash ) {
			return true;
		} else {
			self::$annotations[$hash][$key] = $valueHash;
		}

		return false;
	}

	private function hasAnnotation( $dataValue ) {

		$key = $dataValue->getProperty()->getKey();
		$hash = $dataValue->getContextPage()->getHash();

		return isset( self::$annotations[$hash][$key] );
	}

}