summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Specials/SearchByProperty/PageRequestOptions.php
blob: 0d16bf254b118d252faedf6100e12feeb5bc98cb (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
<?php

namespace SMW\MediaWiki\Specials\SearchByProperty;

use SMW\DataValueFactory;
use SMW\DataValues\TelephoneUriValue;
use SMW\Encoder;
use SMWNumberValue as NumberValue;

/**
 * @license GNU GPL v2+
 * @since   2.1
 *
 * @author mwjames
 */
class PageRequestOptions {

	/**
	 * @var string
	 */
	private $queryString;

	/**
	 * @var array
	 */
	private $requestOptions;

	/**
	 * @var Encoder
	 */
	private $urlEncoder;

	/**
	 * @var PropertyValue
	 */
	public $property;

	/**
	 * @var string
	 */
	public $propertyString;

	/**
	 * @var string
	 */
	public $valueString;

	/**
	 * @var DataValue
	 */
	public $value;

	/**
	 * @var integer
	 */
	public $limit = 20;

	/**
	 * @var integer
	 */
	public $offset = 0;

	/**
	 * @var boolean
	 */
	public $nearbySearch = false;

	/**
	 * @since 2.1
	 *
	 * @param string $queryString
	 * @param array $requestOptions
	 */
	public function __construct( $queryString, array $requestOptions ) {
		$this->queryString = $queryString;
		$this->requestOptions = $requestOptions;
		$this->urlEncoder = new Encoder();
	}

	/**
	 * @since 2.1
	 */
	public function initialize() {

		$params = explode( '/', $this->queryString );
		reset( $params );
		$escaped = false;

		// Remove empty elements
		$params = array_filter( $params, 'strlen' );

		$property = isset( $this->requestOptions['property'] ) ? $this->requestOptions['property'] : current( $params );
		$value = isset( $this->requestOptions['value'] ) ? $this->requestOptions['value'] : next( $params );

		// Auto-generated link is marked with a leading :
		if ( $property !== '' && $property{0} === ':' ) {
			$escaped = true;
			$property = $this->urlEncoder->unescape( ltrim( $property, ':' ) );
		}

		$this->property = DataValueFactory::getInstance()->newPropertyValueByLabel(
			str_replace( [ '_' ], [ ' ' ], $property )
		);

		if ( !$this->property->isValid() ) {
			$this->propertyString = $property;
			$this->value = null;
			$this->valueString = $value;
		} else {
			$this->propertyString = $this->property->getDataItem()->getLabel();
			$this->valueString = $this->getValue( (string)$value, $escaped );
		}

		$this->setLimit();
		$this->setOffset();
		$this->setNearbySearch();
	}

	private function getValue( $value, $escaped ) {

		$this->value = DataValueFactory::getInstance()->newDataValueByProperty(
			$this->property->getDataItem()
		);

		$value = $this->unescape( $value, $escaped );
		$this->value->setUserValue( $value );

		return $this->value->isValid() ? $this->value->getWikiValue() : $value;
	}

	private function unescape( $value, $escaped ) {

		if ( $this->value instanceof NumberValue ) {
			$value = $escaped ? str_replace( [ '-20', '-2D' ], [ ' ', '-' ], $value ) : $value;
			// Do not try to decode things like 1.2e-13
			// Signals that we don't want any precision limitation
			$this->value->setOption( NumberValue::NO_DISP_PRECISION_LIMIT, true );
		} elseif ( $this->value instanceof TelephoneUriValue ) {
			$value = $escaped ? str_replace( [ '-20', '-2D' ], [ ' ', '-' ], $value ) : $value;
			// No encoding to avoid turning +1-201-555-0123
			// into +1 1U523 or further obfuscate %2B1-2D201-2D555-2D0123 ...
		} else {
			$value = $escaped ? $this->urlEncoder->unescape( $value ) : $value;
		}

		return $value;
	}

	private function setLimit() {
		if ( isset( $this->requestOptions['limit'] ) ) {
			$this->limit = intval( $this->requestOptions['limit'] );
		}
	}

	private function setOffset() {
		if ( isset( $this->requestOptions['offset'] ) ) {
			$this->offset = intval( $this->requestOptions['offset'] );
		}
	}

	private function setNearbySearch() {

		if ( $this->value === null ) {
			return null;
		}

		if ( isset( $this->requestOptions['nearbySearchForType'] ) && is_array( $this->requestOptions['nearbySearchForType'] ) ) {
			$this->nearbySearch = in_array( $this->value->getTypeID(), $this->requestOptions['nearbySearchForType'] );
		}
	}

}