summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Specials/SearchByProperty/QueryResultLookup.php
blob: 2b689877d982a22d8a1a3dc4d7e7e3bf81f7ed67 (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\MediaWiki\Specials\SearchByProperty;

use SMW\DataValueFactory;
use SMW\DIWikiPage;
use SMW\Query\DescriptionFactory;
use SMW\Query\PrintRequest as PrintRequest;
use SMW\SQLStore\QueryDependencyLinksStoreFactory;
use SMW\Store;
use SMWQuery as Query;
use SMWRequestOptions as RequestOptions;

/**
 * @license GNU GPL v2+
 * @since   2.1
 *
 * @author Denny Vrandecic
 * @author Daniel Herzig
 * @author Markus Kroetzsch
 * @author mwjames
 */
class QueryResultLookup {

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

	/**
	 * @since 2.1
	 */
	public function __construct( Store $store ) {
		$this->store = $store;
	}

	/**
	 * @since 2.5
	 *
	 * @param QueryOptions $pageRequestOptions
	 *
	 * @return array
	 */
	public function doQueryLinksReferences( PageRequestOptions $pageRequestOptions ) {

		$requestOptions = new RequestOptions();
		$requestOptions->setLimit( $pageRequestOptions->limit + 1 );
		$requestOptions->setOffset( $pageRequestOptions->offset );
		$requestOptions->sort = true;

		$queryDependencyLinksStoreFactory = new QueryDependencyLinksStoreFactory();

		$queryReferenceLinks = $queryDependencyLinksStoreFactory->newQueryReferenceBacklinks(
			$this->store
		);

		$queryBacklinks = $queryReferenceLinks->findReferenceLinks(
			$pageRequestOptions->value->getDataItem(),
			$requestOptions
		);

		$results = [];

		$dataValueFactory = DataValueFactory::getInstance();

		foreach ( $queryBacklinks as $result ) {
			$results[] = [
				$dataValueFactory->newDataValueByItem( DIWikiPage::doUnserialize( $result ), null ),
				$pageRequestOptions->value
			];
		}

		return $results;
	}

	/**
	 * @since 2.1
	 *
	 * @param  QueryOptions $pageRequestOptions
	 *
	 * @return array of array(SMWWikiPageValue, SMWDataValue) with the
	 * first being the entity, and the second the value
	 */
	public function doQuery( PageRequestOptions $pageRequestOptions ) {

		$requestOptions = new RequestOptions();
		$requestOptions->limit = $pageRequestOptions->limit + 1;
		$requestOptions->offset = $pageRequestOptions->offset;
		$requestOptions->sort = true;

		if ( $pageRequestOptions->value === null || !$pageRequestOptions->value->isValid() ) {
			$res = $this->doQueryForNonValue( $pageRequestOptions, $requestOptions );
		} else {
			$res = $this->doQueryForExactValue( $pageRequestOptions, $requestOptions );
		}

		$results = [];

		$dataValueFactory = DataValueFactory::getInstance();

		foreach ( $res as $result ) {
			$results[] = [
				$dataValueFactory->newDataValueByItem( $result, null ),
				$pageRequestOptions->value
			];
		}

		return $results;
	}

	/**
	 * Returns all results that have a value near to the searched for value
	 * on the property, ordered, and sorted by ending with the smallest
	 * one.
	 *
	 * @param QueryOptions $pageRequestOptions
	 * @param integer $count How many entities have the exact same value on the property?
	 * @param integer $greater Should the values be bigger? Set false for smaller values.
	 *
	 * @return array of array of SMWWikiPageValue, SMWDataValue with the
	 * first being the entity, and the second the value
	 */
	public function doQueryForNearbyResults( PageRequestOptions $pageRequestOptions, $count, $greater = true ) {

		$comparator = $greater ? SMW_CMP_GRTR : SMW_CMP_LESS;
		$sortOrder = $greater ? 'ASC' : 'DESC';

		if ( $pageRequestOptions->value !== null && $pageRequestOptions->value->getTypeID() === '_txt' && strlen( $pageRequestOptions->valueString ) > 72 ) {
			$comparator = SMW_CMP_LIKE;
		}

		$descriptionFactory = new DescriptionFactory();

		if ( $pageRequestOptions->valueString === '' || $pageRequestOptions->valueString === null ) {
			$description = $descriptionFactory->newThingDescription();
		} else {
			$description = $descriptionFactory->newValueDescription(
				$pageRequestOptions->value->getDataItem(),
				$pageRequestOptions->property->getDataItem(),
				$comparator
			);

			$description = $descriptionFactory->newSomeProperty(
				$pageRequestOptions->property->getDataItem(),
				$description
			);
		}

		$query = new Query( $description );

		$query->setLimit( $pageRequestOptions->limit );
		$query->setOffset( $pageRequestOptions->offset );
		$query->sort = true;
		$query->sortkeys = [
			$pageRequestOptions->property->getDataItem()->getKey() => $sortOrder
		];

		// Note: printrequests change the caption of properties they
		// get (they expect properties to be given to them).
		// Since we want to continue using the property for our
		// purposes, we give a clone to the print request.
		$printouts = [
			new PrintRequest( PrintRequest::PRINT_THIS, '' ),
			new PrintRequest( PrintRequest::PRINT_PROP, '', clone $pageRequestOptions->property )
		];

		$query->setExtraPrintouts( $printouts );

		$queryResults = $this->store->getQueryResult( $query );

		$result = [];

		while ( $resultArrays = $queryResults->getNext() ) {
			$r = [];

			foreach ( $resultArrays as $resultArray ) {
				$r[] = $resultArray->getNextDataValue();
			}
			// Note: if results have multiple values for the property
			// then this code just pick the first, which may not be
			// the reason why the result is shown here, i.e., it could
			// be out of order.
			$result[] = $r;
		}

		if ( !$greater ) {
			$result = array_reverse( $result );
		}

		return $result;
	}

	private function doQueryForNonValue( PageRequestOptions $pageRequestOptions, RequestOptions $requestOptions ) {
		return $this->store->getPropertyValues(
			null,
			$pageRequestOptions->property->getDataItem(),
			$requestOptions
		);
	}

	private function doQueryForExactValue( PageRequestOptions $pageRequestOptions, RequestOptions $requestOptions ) {

		$pageRequestOptions->value->setOption( 'is.search', true );

		return $this->store->getPropertySubjects(
			$pageRequestOptions->property->getDataItem(),
			$pageRequestOptions->value->getDataItem(),
			$requestOptions
		);
	}

}