summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/SQLStore/EntityStore/DIHandlers/DIWikiPageHandler.php
blob: dcad8b7f76d13c67a4b80b58c5aa392242f7d472 (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
<?php

namespace SMW\SQLStore\EntityStore\DIHandlers;

use SMW\DIProperty;
use SMW\DIWikiPage;
use SMW\SQLStore\EntityStore\DataItemHandler;
use SMW\SQLStore\EntityStore\Exception\DataItemHandlerException;
use SMW\Exception\PredefinedPropertyLabelMismatchException;
use SMW\SQLStore\TableBuilder\FieldType;
use SMWDataItem as DataItem;

/**
 * DataItemHandler for dataitems of type DIWikiPage.
 *
 * This handler is slightly different from other handlers since wikipages are
 * stored in a separate table and referred to by numeric IDs. The handler thus
 * returns IDs in most cases, but expects data from the SMW IDs table (with
 * DBkey, namespace, interwiki, subobjectname) to be given for creating new
 * dataitems. The store recognizes this special behavior from the field type
 * 'p' that the handler reports for its only data field.
 *
 * @license GNU GPL v2+
 * @since 1.8
 *
 * @author Nischay Nahata
 * @author Markus Kroetzsch
 */
class DIWikiPageHandler extends DataItemHandler {

	/**
	 * @since 1.8
	 *
	 * {@inheritDoc}
	 */
	public function getTableFields() {
		return [ 'o_id' => FieldType::FIELD_ID ];
	}

	/**
	 * @since 1.8
	 *
	 * {@inheritDoc}
	 */
	public function getFetchFields() {
		return [ 'o_id' => FieldType::FIELD_ID ];
	}

	/**
	 * @since 1.8
	 *
	 * {@inheritDoc}
	 */
	public function getTableIndexes() {
		return [
			'o_id',

			// SMWSQLStore3Readers::getPropertySubjects
			'p_id,s_id',

			// SMWSQLStore3Readers::fetchSemanticData
			// ... FROM `smw_fpt_sobj` INNER JOIN `smw_object_ids` AS o0 ON
			// o_id=o0.smw_id WHERE s_id='104322'
			's_id,o_id',

			// SMWSQLStore3Readers::fetchSemanticData
			// ... FROM `smw_di_wikipage` INNER JOIN `smw_object_ids` AS p ON
			// p_id=p.smw_id INNER JOIN `smw_object_ids` AS o0 ON o_id=o0.smw_id
			// WHERE s_id='104815'
			's_id,p_id,o_id',

			// QueryEngine::getInstanceQueryResult
			// ... INNER JOIN `smw_fpt_inst` AS t3 ON t2.smw_id=t3.s_id WHERE
			// (t2.smw_namespace='0' AND (t3.o_id='56')
			'o_id,s_id',

			// QueryEngine::getInstanceQueryResult
			//'p_id,o_id,s_sort',

			// SMWSQLStore3Readers::getPropertySubjects
			// SELECT DISTINCT s_id FROM `smw_fpt_sobj` ORDER BY s_sort
			//'s_sort,s_id',

			// SELECT DISTINCT s_id FROM `smw_fpt_subp` WHERE o_id='96' ORDER BY s_sort
			//'o_id,s_sort,s_id',

			// In-property lookup
			'o_id,p_id',
			//'o_id,p_id,s_sort',

			// SMWSQLStore3Readers::getPropertySubjects
			// SELECT DISTINCT s_id FROM `smw_di_wikipage` WHERE (p_id='64' AND o_id='104') ORDER BY s_sort ASC
			//'o_id,p_id,s_id,s_sort'
		];
	}

	/**
	 * @since 1.8
	 *
	 * {@inheritDoc}
	 */
	public function getWhereConds( DataItem $dataItem ) {

		$oid = $this->store->getObjectIds()->getSMWPageID(
			$dataItem->getDBkey(),
			$dataItem->getNamespace(),
			$dataItem->getInterwiki(),
			$dataItem->getSubobjectName()
		);

		return [ 'o_id' => $oid ];
	}

	/**
	 * @since 1.8
	 *
	 * {@inheritDoc}
	 */
	public function getInsertValues( DataItem $dataItem ) {

		$oid = $this->store->getObjectIds()->makeSMWPageID(
			$dataItem->getDBkey(),
			$dataItem->getNamespace(),
			$dataItem->getInterwiki(),
			$dataItem->getSubobjectName()
		);

		return [ 'o_id' => $oid ];
	}

	/**
	 * @since 1.8
	 *
	 * {@inheritDoc}
	 */
	public function getIndexField() {
		return 'o_id';
	}

	/**
	 * @since 1.8
	 *
	 * {@inheritDoc}
	 */
	public function getLabelField() {
		return 'o_id';
	}

	/**
	 * @since 1.8
	 *
	 * {@inheritDoc}
	 */
	public function dataItemFromDBKeys( $dbkeys ) {

		if ( !is_array( $dbkeys ) || count( $dbkeys ) != 5 ) {
			throw new DataItemHandlerException( 'Failed to create data item from DB keys.' );
		}

		$namespace = intval( $dbkeys[1] );

		// Correctly interpret internal property keys
		if ( $namespace == SMW_NS_PROPERTY && $dbkeys[0] != '' &&
			$dbkeys[0]{0} == '_' && $dbkeys[2] == '' ) {

			try {
				$property = new DIProperty( $dbkeys[0] );
			} catch( PredefinedPropertyLabelMismatchException $e ) {
				// Most likely an outdated, no longer existing predefined
				// property, mark it as outdate
				$dbkeys[2] = SMW_SQL3_SMWIW_OUTDATED;

				return $this->newDiWikiPage( $dbkeys );
			}

			$wikipage = $property->getCanonicalDiWikiPage( $dbkeys[4] );

			if ( !is_null( $wikipage ) ) {
				return $wikipage;
			}
		}

		return $this->newDiWikiPage( $dbkeys );
	}

	private function newDiWikiPage( $dbkeys ) {

		$diWikiPage = new DIWikiPage(
			$dbkeys[0],
			intval( $dbkeys[1] ),
			$dbkeys[2],
			$dbkeys[4]
		);

		$diWikiPage->setSortKey( $dbkeys[3] );

		return $diWikiPage;
	}

}