summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Query/Language/ValueDescription.php
blob: be0161d2a310200fa3fc43547603187e3b1a1d64 (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
<?php

namespace SMW\Query\Language;

use SMW\DataValueFactory;
use SMw\DIProperty;
use SMW\Query\QueryComparator;
use SMWDataItem as DataItem;
use SMWNumberValue as NumberValue;
use SMWURIValue as UriValue;

/**
 * Description of one data value, or of a range of data values.
 *
 * Technically this usually corresponds to nominal predicates or to unary
 * concrete domain predicates in OWL which are parametrised by one constant
 * from the concrete domain.
 * In RDF, concrete domain predicates that define ranges (like "greater or
 * equal to") are not directly available.
 *
 * @license GNU GPL v2+
 * @since 1.6
 *
 * @author Markus Krötzsch
 */
class ValueDescription extends Description {

	/**
	 * @var DataItem
	 */
	private $dataItem;

	/**
	 * @var integer element in the SMW_CMP_ enum
	 */
	private $comparator;

	/**
	 * @var null|DIProperty
	 */
	private $property = null;

	/**
	 * @param DataItem $dataItem
	 * @param null|DIProperty $property
	 * @param integer $comparator
	 */
	public function __construct( DataItem $dataItem, DIProperty $property = null, $comparator = SMW_CMP_EQ ) {
		$this->dataItem = $dataItem;
		$this->comparator = $comparator;
		$this->property = $property;
	}

	/**
	 * @see Description::getFingerprint
	 * @since 2.5
	 *
	 * @return string
	 */
	public function getFingerprint() {

		$property = null;

		if ( $this->property !== null ) {
			$property = $this->property->getSerialization();
		}

		// A change to the order does also change the signature and renders a
		// different query ID
		return 'V:' . md5( $this->comparator . '|' . $this->dataItem->getHash() . '|' . $property );
	}

	/**
	 * @deprecated Use getDataItem() and \SMW\DataValueFactory::getInstance()->newDataValueByItem() if needed. Vanishes before SMW 1.7
	 * @return DataItem
	 */
	public function getDataValue() {
		// FIXME: remove
		return $this->dataItem;
	}

	/**
	 * @return DataItem
	 */
	public function getDataItem() {
		return $this->dataItem;
	}

	/**
	 * @since  2.1
	 *
	 * @return DIProperty|null
	 */
	public function getProperty() {
		return $this->property;
	}

	/**
	 * @return integer
	 */
	public function getComparator() {
		return $this->comparator;
	}

	/**
	 * @param bool $asValue
	 *
	 * @return string
	 */
	public function getQueryString( $asValue = false ) {

		$comparator = QueryComparator::getInstance()->getStringForComparator(
			$this->comparator
		);

		$dataValue = DataValueFactory::getInstance()->newDataValueByItem(
			$this->dataItem,
			$this->property
		);

		// Set option to ensure that the output doesn't alter the display
		// characteristics of a value
		$dataValue->setOption( UriValue::VALUE_RAW, true );
		$dataValue->setOption( NumberValue::NO_DISP_PRECISION_LIMIT, true );

		if ( $asValue ) {
			return $comparator . $dataValue->getWikiValue();
		}

		// this only is possible for values of Type:Page
		if ( $comparator === '' ) { // some extra care for Category: pages
			return '[[:' . $dataValue->getWikiValue() . ']]';
		}

		return '[[' . $comparator . $dataValue->getWikiValue() . ']]';
	}

	public function isSingleton() {
		return $this->comparator == SMW_CMP_EQ;
	}

	public function getSize() {
		return 1;
	}

}