summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Maps/src/SemanticMW/ValueDescriptions/CoordinateDescription.php
blob: a63ea9a0c51d8841d60424b98002001c4365f8ea (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
<?php

namespace Maps\SemanticMW\ValueDescriptions;

use SMW\DataValueFactory;
use SMW\Query\Language\ValueDescription;
use SMWDIGeoCoord;
use Wikimedia\Rdbms\IDatabase;

/**
 * Description of one data value of type Geographical Coordinates.
 *
 * @author Jeroen De Dauw
 */
class CoordinateDescription extends ValueDescription {

	public function getQueryString( $asValue = false ) {
		$queryString = DataValueFactory::getInstance()->newDataValueByItem(
			$this->getDataItem(),
			$this->getProperty()
		)->getWikiValue();

		return $asValue ? $queryString : "[[$queryString]]";
	}

	/**
	 * @see SomePropertyInterpreter::mapValueDescription
	 *
	 * FIXME: store specific code should be in the store component
	 *
	 * @param string $tableName
	 * @param string[] $fieldNames
	 * @param IDatabase $db
	 *
	 * @return string|false
	 */
	public function getSQLCondition( $tableName, array $fieldNames, IDatabase $db ) {
		$dataItem = $this->getDataItem();

		// Only execute the query when the description's type is geographical coordinates,
		// the description is valid, and the near comparator is used.
		if ( $dataItem instanceof SMWDIGeoCoord ) {
			switch ( $this->getComparator() ) {
				case SMW_CMP_EQ:
					$comparator = '=';
					break;
				case SMW_CMP_LEQ:
					$comparator = '<=';
					break;
				case SMW_CMP_GEQ:
					$comparator = '>=';
					break;
				case SMW_CMP_NEQ:
					$comparator = '!=';
					break;
				default:
					return false;
			}

			$lat = $db->addQuotes( $dataItem->getLatitude() );
			$lon = $db->addQuotes( $dataItem->getLongitude() );

			$conditions = [];

			$conditions[] = "{$tableName}.$fieldNames[1] $comparator $lat";
			$conditions[] = "{$tableName}.$fieldNames[2] $comparator $lon";

			return implode( ' AND ', $conditions );
		}

		return false;
	}

}