summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/DataValues/ValueValidators/AllowsListConstraintValueValidator.php
blob: 80bde017433928932dd978d6a518a5fded8473fa (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php

namespace SMW\DataValues\ValueValidators;

use SMW\ApplicationFactory;
use SMW\DataValues\ValueParsers\AllowsListValueParser;
use SMW\PropertySpecificationLookup;
use SMW\Message;
use SMWDataValue as DataValue;
use SMWNumberValue as NumberValue;
use SMWDIBlob as DIBlob;

/**
 * @private
 *
 * @license GNU GPL v2+
 * @since 2.4
 *
 * @author mwjames
 */
class AllowsListConstraintValueValidator implements ConstraintValueValidator {

	/**
	 * @var AllowsListValueParser
	 */
	private $allowsListValueParser;

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

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

	/**
	 * @var string
	 */
	private $errorMsg = '';

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

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

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

		$this->hasConstraintViolation = false;
		$this->errorMsg = 'smw-datavalue-constraint-error-allows-value-list';

		if ( !$dataValue instanceof DataValue || $dataValue->getProperty() === null ) {
			return $this->hasConstraintViolation;
		}

		$property = $dataValue->getProperty();

		$allowedValues = $this->propertySpecificationLookup->getAllowedValues(
			$property
		);

		$allowedListValues = $this->propertySpecificationLookup->getAllowedListValues(
			$property
		);

		if ( $allowedValues === [] && $allowedListValues === [] ) {
			return $this->hasConstraintViolation;
		}

		$allowedValueList = [];

		$isAllowed = $this->checkConstraintViolation(
			$dataValue,
			$allowedValues,
			$allowedValueList
		);


		if ( !$isAllowed ) {
			foreach ( $allowedListValues as $dataItem ) {
				$list = $this->allowsListValueParser->parse( $dataItem->getString() );

				// Combine different lists into one
				if ( is_array( $list ) ) {
					$allowedValues = array_merge( $allowedValues, $list );
				}
			}

			// On assignments like [Foo => Foo] (* Foo) or [Foo => Bar] (* Foo|Bar)
			// use the key as comparison entity
			$allowedValues = array_keys( $allowedValues );
		} else {
			return;
		}

		$isAllowed = $this->checkConstraintViolation(
			$dataValue,
			$allowedValues,
			$allowedValueList
		);

		if ( $isAllowed === true ) {
			return;
		}

		$count = count( $allowedValueList );

		// Only the first 10 values otherwise the list may become too long
		$allowedValueList = implode( ', ', array_slice(
			array_keys( $allowedValueList ), 0 , 10 )
		);

		$allowedValueList = str_replace( [ '>', '<' ], [ '%3C', '%3E' ], $allowedValueList );

		$dataValue->addErrorMsg(
			[
				$this->errorMsg,
				$dataValue->getWikiValue(),
				$allowedValueList . ( $count > 10 ? ', ...' : '' ),
				$property->getLabel()
			],
			Message::PARSE
		);

		$this->hasConstraintViolation = true;
	}

	private function checkConstraintViolation( $dataValue, $allowedValues, &$allowedValueList ) {

		if ( !is_array( $allowedValues ) ) {
			return true;
		}

		$hash = $dataValue->getDataItem()->getHash();
		$value = $dataValue->getWikiValue();

		$testDataValue = ApplicationFactory::getInstance()->getDataValueFactory()->newTypeIDValue(
			$dataValue->getTypeID()
		);

		$isAllowed = false;

		// Track a range related constraint which can be used as single
		// `[[Allows value::>0]]` assignment or as conjunctive compound as in
		// `[[Allows value::>0]] [[Allows value::<100]]` and can appear in any
		// order
		$range = null;

		foreach ( $allowedValues as $allowedValue ) {

			if ( is_string( $allowedValue ) ) {
				$allowedValue = new DIBlob( $allowedValue );
			}

			if ( !$allowedValue instanceof DIBlob ) {
				continue;
			}

			if ( $testDataValue instanceof NumberValue ) {

				// Check [[Allows value::>0]]
				if ( $this->check_range( '>', $value, $allowedValue, $range, $isAllowed, $allowedValueList ) ) {
					continue;
				}

				// Check [[Allows value::<100]]
				if ( $this->check_range( '<', $value, $allowedValue, $range, $isAllowed, $allowedValueList ) ) {
					continue;
				}

				// Check [[Allows value::1...100]]
				if ( $this->check_bounds( $value, $allowedValue, $isAllowed, $allowedValueList ) ) {
					break;
				}
			}

			// For a time based range one could use the JD date and simply apply
			// a >, < comparison on something like `[[Allows value::>1970]]
			// [[Allows value::<31.12.2100]]`

			// String range based constraints seems to make not much sense for
			// something like `[[Allows value::>abc]] [[Allows value::<def]]`

			$testDataValue->setUserValue( $allowedValue->getString() );

			if ( $hash === $testDataValue->getDataItem()->getHash() ) {
				$isAllowed = true;
				break;
			} else {
				// Filter dups
				$allowedValueList[$allowedValue->getString()] = true;
			}
		}

		return $isAllowed;
	}

	private function check_range( $exp, $value, $allowedValue, &$range, &$isAllowed, &$allowedValueList ) {

		$v = $allowedValue->getString();

		// If a previous range comparison failed then bail-out!
		if ( $v{0} === $exp && ( $range === null || $range ) ) {
			$v = intval( trim( substr( $v, 1 ) ) );

			if ( $exp === '>' && $value > $v ) {
				$isAllowed = true;
			} elseif ( $exp === '<' && $value < $v ) {
				$isAllowed = true;
			} else {
				$isAllowed = false;
				$range = false;
			}

			if ( $range === false ) {
				$allowedValueList[$allowedValue->getString()] = true;
			}

			return true;
		}

		$this->errorMsg = 'smw-datavalue-constraint-error-allows-value-range';

		return false;
	}

	private function check_bounds( $value, $allowedValue, &$isAllowed, &$allowedValueList ) {

		$v = $allowedValue->getString();

		if ( strpos( $v, '...' ) === false ) {
			return false;
		}

		list( $lower, $upper ) = explode( '...', $v );

		if ( $value >= intval( $lower ) && $value <= intval( $upper ) ) {
			return $isAllowed = true;
		} else {
			$allowedValueList[$allowedValue->getString()] = true;
		}

		$this->errorMsg = 'smw-datavalue-constraint-error-allows-value-range';

		return false;
	}

}