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

namespace SMW\Query\Language;

use Exception;
use SMW\DataValueFactory;
use SMW\DIWikiPage;
use SMW\Localizer;

/**
 * Description of a single class as given by a wiki category, or of a
 * disjunction of such classes. Corresponds to (disjunctions of) atomic classes
 * in OWL and to (unions of) classes in RDF.
 *
 * @license GNU GPL v2+
 * @since 1.6
 *
 * @author Markus Krötzsch
 */
class ClassDescription extends Description {

	/**
	 * @var array of DIWikiPage
	 */
	protected $m_diWikiPages;

	/**
	 * @var integer|null
	 */
	protected $hierarchyDepth;

	/**
	 * Constructor.
	 *
	 * @param mixed $content DIWikiPage or array of DIWikiPage
	 *
	 * @throws Exception
	 */
	public function __construct( $content ) {
		if ( $content instanceof DIWikiPage ) {
			$this->m_diWikiPages = [ $content ];
		} elseif ( is_array( $content ) ) {
			$this->m_diWikiPages = $content;
		} else {
			throw new Exception( "ClassDescription::__construct(): parameter must be an DIWikiPage object or an array of such objects." );
		}
	}

	/**
	 * @since 3.0
	 *
	 * @param integer $hierarchyDepth
	 */
	public function setHierarchyDepth( $hierarchyDepth ) {

		if ( $hierarchyDepth > $GLOBALS['smwgQSubcategoryDepth'] ) {
			$hierarchyDepth = $GLOBALS['smwgQSubcategoryDepth'];
		}

		$this->hierarchyDepth = $hierarchyDepth;
	}

	/**
	 * @since 3.0
	 *
	 * @return integer|null
	 */
	public function getHierarchyDepth() {
		return $this->hierarchyDepth;
	}

	/**
	 * @since 3.0
	 *
	 * @param ClassDescription $description
	 *
	 * @return boolean
	 */
	public function isMergableDescription( ClassDescription $description ) {

		if ( isset( $this->isNegation ) && isset( $description->isNegation ) ) {
			return true;
		}

		if ( !isset( $this->isNegation ) && !isset( $description->isNegation ) ) {
			return true;
		}

		return false;
	}

	/**
	 * @since  3.0
	 *
	 * @param DIWikiPage $dataItem
	 */
	public function addClass( DIWikiPage $dataItem ) {
		$this->m_diWikiPages[] = $dataItem;
	}

	/**
	 * @param ClassDescription $description
	 */
	public function addDescription( ClassDescription $description ) {
		$this->m_diWikiPages = array_merge( $this->m_diWikiPages, $description->getCategories() );
	}

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

		$hash = [];

		foreach ( $this->m_diWikiPages as $subject ) {
			$hash[$subject->getHash()] = true;
		}

		ksort( $hash );
		$extra = ( isset( $this->isNegation ) ? '|' . $this->isNegation : '' );

		return 'Cl:' . md5( implode( '|', array_keys( $hash ) ) . $this->hierarchyDepth . $extra );
	}

	/**
	 * @return array of DIWikiPage
	 */
	public function getCategories() {
		return $this->m_diWikiPages;
	}

	public function getQueryString( $asValue = false ) {

		$first = true;
		$namespaceText = Localizer::getInstance()->getNamespaceTextById( NS_CATEGORY );

		foreach ( $this->m_diWikiPages as $wikiPage ) {
			$wikiValue = DataValueFactory::getInstance()->newDataValueByItem( $wikiPage, null );
			if ( $first ) {
				$result = '[[' . $namespaceText . ':' . ( isset( $this->isNegation ) ? '!' : '' ) . $wikiValue->getText();
				$first = false;
			} else {
				$result .= '||' . ( isset( $this->isNegation ) ? '!' : '' ) . $wikiValue->getText();
			}
		}

		if ( $this->hierarchyDepth !== null ) {
			$result .= '|+depth=' . $this->hierarchyDepth;
		}

		$result .= ']]';

		if ( $asValue ) {
			return ' <q>' . $result . '</q> ';
		}

		return $result;
	}

	public function isSingleton() {
		return false;
	}

	public function getSize() {

		if ( $GLOBALS['smwgQSubcategoryDepth'] > 0 ) {
			return 1; // disj. of cats should not cause much effort if we compute cat-hierarchies anyway!
		}

		return count( $this->m_diWikiPages );
	}

	public function getQueryFeatures() {

		if ( count( $this->m_diWikiPages ) > 1 ) {
			return SMW_CATEGORY_QUERY | SMW_DISJUNCTION_QUERY;
		}

		return SMW_CATEGORY_QUERY;
	}

	public function prune( &$maxsize, &$maxdepth, &$log ) {

		if ( $maxsize >= $this->getSize() ) {
			$maxsize = $maxsize - $this->getSize();
			return $this;
		} elseif ( $maxsize <= 0 ) {
			$log[] = $this->getQueryString();
			$result = new ThingDescription();
		} else {
			$result = new ClassDescription( array_slice( $this->m_diWikiPages, 0, $maxsize ) );
			$rest = new ClassDescription( array_slice( $this->m_diWikiPages, $maxsize ) );

			$result->setHierarchyDepth(
				$this->getHierarchyDepth()
			);

			$log[] = $rest->getQueryString();
			$maxsize = 0;
		}

		$result->setPrintRequests( $this->getPrintRequests() );

		return $result;
	}

}