summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Api/Browse/PValueLookup.php
blob: bc7eca51db4b5e3c28ac6f07730029cf28533edf (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
<?php

namespace SMW\MediaWiki\Api\Browse;

use SMW\DataTypeRegistry;
use SMW\DataValueFactory;
use SMW\RequestOptions;
use SMW\DIProperty;
use SMW\SQLStore\SQLStore;
use SMW\Store;
use SMWDataItem as DataItem;
use SMWDITime as DIime;
use SMW\SQLStore\Lookup\ProximityPropertyValueLookup;

/**
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class PValueLookup extends Lookup {

	const VERSION = 1;

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

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

	/**
	 * @since 3.0
	 *
	 * @return string|integer
	 */
	public function getVersion() {
		return __METHOD__ . self::VERSION;
	}

	/**
	 * @since 3.0
	 *
	 * @param array $parameters
	 *
	 * @return array
	 */
	public function lookup( array $parameters ) {

		$limit = 20;
		$offset = 0;

		if ( isset( $parameters['limit'] ) ) {
			$limit = (int)$parameters['limit'];
		}

		if ( isset( $parameters['offset'] ) ) {
			$offset = (int)$parameters['offset'];
		}

		$res = [];
		$continueOffset = 0;
		$property = null;
		$sort = false;
		$count = 0;

		if ( isset( $parameters['property'] ) ) {
			$property = $parameters['property'];

			// Get the last which represents the final output
			// Foo.Bar.Foobar.Baz
			if ( strpos( $property, '.' ) !== false ) {
				$chain = explode( '.', $property );
				$property = array_pop( $chain );
			}
		}

		if ( $property === '' || $property === null ) {
			return [];
		}

		// Generally we don't want to sort results to avoid having the DB to use
		// temporary tables/filesort when the value pool is very large
		if ( isset( $parameters['sort'] ) ) {
			$sort = in_array( strtolower( $parameters['sort'] ), [ 'asc', 'desc' ] ) ? $parameters['sort'] : 'asc';
		}

		if ( isset( $parameters['search'] ) ) {

			$opts = new RequestOptions();
			$opts->limit = $limit;
			$opts->offset = $offset;
			$opts->sort = $sort;

			$property = DIProperty::newFromUserLabel(
				$property
			);

			$proximityPropertyValueLookup = $this->store->service(
				'ProximityPropertyValueLookup'
			);

			$res = $proximityPropertyValueLookup->lookup(
				$property,
				$parameters['search'],
				$opts
			);

			if ( $this->is_iterable( $res ) ) {
				$count = count( $res );
			}

			if ( $count > $limit ) {
				$continueOffset = $offset + $count;
				array_pop( $res );
			}
		}

		// Changing this output format requires to set a new version
		$res = [
			'query' => $res,
			'query-continue-offset' => $continueOffset,
			'version' => self::VERSION,
			'meta' => [
				'type'  => 'pvalue',
				'limit' => $limit,
				'count' => $count
			]
		];

		return $res;
	}

	private function is_iterable( $obj ) {
		return is_array( $obj ) || ( is_object( $obj ) && ( $obj instanceof \Traversable ) );
	}

}