summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/RequestOptions.php
blob: a668ac5a501e7f84d1eeeb14d816d3f8e027d051 (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
<?php

namespace SMW;

/**
 * Container object for various options that can be used when retrieving
 * data from the store. These options are mostly relevant for simple,
 * direct requests -- inline queries may require more complex options due
 * to their more complex structure.
 * Options that should not be used or where default values should be used
 * can be left as initialised.
 *
 * @license GNU GPL v2+
 * @since 1.0
 *
 * @author Markus Krötzsch
 */
class RequestOptions {

	const SEARCH_FIELD = 'search_field';

	/**
	 * The maximum number of results that should be returned.
	 */
	public $limit = -1;

	/**
	 * A numerical offset. The first $offset results are skipped.
	 * Note that this does not imply a defined order of results
	 * (see SMWRequestOptions->$sort below).
	 */
	public $offset = 0;

	/**
	 * Should the result be ordered? The employed order is defined
	 * by the type of result that are requested: wiki pages and strings
	 * are ordered alphabetically, whereas other data is ordered
	 * numerically. Usually, the order should be fairly "natural".
	 */
	public $sort = false;

	/**
	 * If SMWRequestOptions->$sort is true, this parameter defines whether
	 * the results are ordered in ascending or descending order.
	 */
	public $ascending = true;

	/**
	 * Specifies a lower or upper bound for the values returned by the query.
	 * Whether it is lower or upper is specified by the parameter "ascending"
	 * (true->lower, false->upper).
	 */
	public $boundary = null;

	/**
	 * Specifies whether or not the requested boundary should be returned
	 * as a result.
	 */
	public $include_boundary = true;

	/**
	 * An array of string conditions that are applied if the result has a
	 * string label that can be subject to those patterns.
	 *
	 * @var StringCondition[]
	 */
	private $stringConditions = [];

	/**
	 * Contains extra conditions which a consumer is being allowed to interpret
	 * freely to modify a search condition.
	 *
	 * @var array
	 */
	private $extraConditions = [];

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

	/**
	 * @since 1.0
	 *
	 * @param string $string to match
	 * @param integer $condition one of STRCOND_PRE, STRCOND_POST, STRCOND_MID
	 * @param boolean $isOr
	 * @param boolean $isNot
	 */
	public function addStringCondition( $string, $condition, $isOr = false, $isNot = false ) {
		$this->stringConditions[] = new StringCondition( $string, $condition, $isOr, $isNot );
	}

	/**
	 * Return the specified array of SMWStringCondition objects.
	 *
	 * @since 1.0
	 *
	 * @return array
	 */
	public function getStringConditions() {
		return $this->stringConditions;
	}

	/**
	 * @since 2.5
	 *
	 * @param mixed $extraCondition
	 */
	public function addExtraCondition( $extraCondition ) {
		$this->extraConditions[] = $extraCondition;
	}

	/**
	 * @since 2.5
	 *
	 * @param array
	 */
	public function getExtraConditions() {
		return $this->extraConditions;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $key
	 * @param string $value
	 */
	public function setOption( $key, $value ) {
		$this->options[$key] = $value;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $key
	 * @param mixed $default
	 *
	 * @return mixed
	 */
	public function getOption( $key, $default = null ) {

		if ( isset( $this->options[$key] ) ) {
			return $this->options[$key];
		}

		return $default;
	}

	/**
	 * @since 2.5
	 *
	 * @param integer $limit
	 */
	public function setLimit( $limit ) {
		$this->limit = (int)$limit;
	}

	/**
	 * @since 2.5
	 *
	 * @return integer
	 */
	public function getLimit() {
		return (int)$this->limit;
	}

	/**
	 * @since 2.5
	 *
	 * @param integer $offset
	 */
	public function setOffset( $offset ) {
		$this->offset = (int)$offset;
	}

	/**
	 * @since 2.5
	 *
	 * @return integer
	 */
	public function getOffset() {
		return (int)$this->offset;
	}

	/**
	 * @since 2.4
	 *
	 * @return string
	 */
	public function getHash() {

		$stringConditions = '';

		foreach ( $this->stringConditions as $stringCondition ) {
			$stringConditions .= $stringCondition->getHash();
		}

		return json_encode( [
			$this->limit,
			$this->offset,
			$this->sort,
			$this->ascending,
			$this->boundary,
			$this->include_boundary,
			$stringConditions,
			$this->extraConditions,
			$this->options,
		] );
	}

}