summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/DataValues/ValueParsers/PropertyValueParser.php
blob: 884aac6246809324e928408e3ff840299f8d4c3b (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
<?php

namespace SMW\DataValues\ValueParsers;

use SMW\Localizer;

/**
 * @private
 *
 * @license GNU GPL v2+
 * @since 2.5
 *
 * @author mwjames
 */
class PropertyValueParser implements ValueParser {

	/**
	 * @var array
	 */
	private $errors = [];

	/**
	 * @var array
	 */
	private $invalidCharacterList = [];

	/**
	 * @var boolean
	 */
	private $isCapitalLinks = true;

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

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

	/**
	 * @since 2.5
	 *
	 * {@inheritDoc}
	 */
	public function getErrors() {
		return $this->errors;
	}

	/**
	 * @since 2.5
	 *
	 * @param array $invalidCharacterList
	 */
	public function setInvalidCharacterList( array $invalidCharacterList ) {
		$this->invalidCharacterList = $invalidCharacterList;
	}

	/**
	 * Corresponds to the $wgCapitalLinks setting
	 *
	 * @since 3.0
	 *
	 * @param boolean $isCapitalLinks
	 */
	public function isCapitalLinks( $isCapitalLinks ) {
		$this->isCapitalLinks = (bool)$isCapitalLinks;
	}

	/**
	 * Whether upper case for the first character is required or not in case of
	 * $wgCapitalLinks = false.
	 *
	 * @since 2.5
	 *
	 * @param boolean $reqCapitalizedFirstChar
	 */
	public function reqCapitalizedFirstChar( $reqCapitalizedFirstChar ) {
		$this->reqCapitalizedFirstChar = (bool)$reqCapitalizedFirstChar;
	}

	/**
	 * Whether or not the parsing is executed within a query context which may
	 * allow exceptions on the validation of invalid characters.
	 *
	 * @since 2.5
	 *
	 * @param boolean $isQueryContext
	 */
	public function isQueryContext( $isQueryContext ) {
		$this->isQueryContext = (bool)$isQueryContext;
	}

	/**
	 * @since 2.5
	 *
	 * @param string $userValue
	 *
	 * @return array
	 */
	public function parse( $userValue ) {

		$this->errors = [];

		// #1727 <Foo> or <Foo-<Bar> are not permitted but
		// Foo-<Bar will be converted to Foo-
		$userValue = strip_tags(
			htmlspecialchars_decode( $userValue )
		);

		if ( !$this->hasValidCharacters( $userValue ) ) {
			return [ null, null, null ];
		}

		return $this->getNormalizedValueFrom( $userValue );
	}

	private function hasValidCharacters( $value ) {

		if ( trim( $value ) === '' ) {
			$this->errors[] = [ 'smw_emptystring' ];
			return false;
		}

		$invalidCharacter = '';

		foreach ( $this->invalidCharacterList as $character ) {
			if ( strpos( $value, $character ) !== false ) {
				$invalidCharacter = $character;
				break;
			}
		}

		// #1567, Only allowed in connection with a query context (e.g sort=#)
		if ( $invalidCharacter === '' && strpos( $value, '#' ) !== false && !$this->isQueryContext ) {
			$invalidCharacter = '#';
		}

		if ( $invalidCharacter !== '' ) {

			// Replace selected control chars otherwise the error display becomes
			// unreadable
			$invalidCharacter = str_replace(
				[ "\r", "\n", ],
				[ "CR", "LF" ],
				$invalidCharacter
			);

			$this->errors[] = [ 'smw-datavalue-property-invalid-character', $value, $invalidCharacter ];
			return false;
		}

		// #676, only on a query context allow Foo.Bar
		if ( $invalidCharacter === '' && !$this->isQueryContext && strpos( $value, '.' ) !== false ) {
			$this->errors[] = [ 'smw-datavalue-property-invalid-chain', $value ];
			return false;
		}

		return true;
	}

	private function getNormalizedValueFrom( $value ) {

		$inverse = false;
		$capitalizedName = '';

		// slightly normalise label
		$propertyName = $this->doNormalize(
			ltrim( rtrim( $value, ' ]' ), ' [' ),
			$this->isCapitalLinks
		);

		if ( $this->reqCapitalizedFirstChar ) {
			$capitalizedName = $this->doNormalize( $propertyName, true );
		}

		// property refers to an inverse
		if ( ( $propertyName !== '' ) && ( $propertyName { 0 } == '-' ) ) {
			$propertyName = $this->doNormalize( (string)substr( $value, 1 ), $this->isCapitalLinks );
			/// NOTE The cast is necessary at least in PHP 5.3.3 to get string '' instead of boolean false.
			/// NOTE It is necessary to normalize again here, since normalization may uppercase the first letter.
			$inverse = true;
		}

		return [ $propertyName, $capitalizedName, $inverse ];
	}

	private function doNormalize( $text, $isCapitalLinks ) {

		$text = trim( $text );

		if ( $isCapitalLinks ) {
			$text = Localizer::getInstance()->getContentLanguage()->ucfirst( $text );
		}

		return str_replace( '_', ' ', $text );
	}

}