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

namespace SMW\SQLStore\EntityStore;

use SMW\DIProperty;
use SMW\DIWikiPage;
use SMW\IteratorFactory;
use SMW\SQLStore\SQLStore;

/**
 * @private
 *
 * @license GNU GPL v2+
 * @since 2.5
 *
 * @author mwjames
 */
class SubobjectListFinder {

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

	/**
	 * @var IteratorFactory
	 */
	private $iteratorFactory;

	/**
	 * @var DIWikiPage
	 */
	private $subject;

	/**
	 * @var []
	 */
	private $mappingIterator = [];

	/**
	 * @var []
	 */
	private $skipConditions = [];

	/**
	 * @since 2.5
	 *
	 * @param SQLStore $store
	 * @param IteratorFactory $iteratorFactory
	 */
	public function __construct( SQLStore $store, IteratorFactory $iteratorFactory ) {
		$this->store = $store;
		$this->iteratorFactory = $iteratorFactory;
	}

	/**
	 * @since 3.0
	 *
	 * @param DIWikiPage $subject
	 *
	 * @return MappingIterator
	 */
	public function find( DIWikiPage $subject ) {

		$key = $subject->getHash() . ':' . $subject->getId();

		if ( !isset( $this->mappingIterator[$key] ) ) {
			$this->mappingIterator[$key] = $this->newMappingIterator( $subject );
		}

		return $this->mappingIterator[$key];
	}

	/**
	 * Fetch all subobjects for a given subject using a lazy-mapping iterator
	 * in order to only resolve one subobject per iteration step.
	 *
	 * @since 2.5
	 *
	 * @param DIWikiPage $subject
	 *
	 * @return MappingIterator
	 */
	private function newMappingIterator( DIWikiPage $subject ) {

		$callback = function( $row ) use ( $subject ) {

			// #1955
			if ( $subject->getNamespace() === SMW_NS_PROPERTY ) {
				$property = new DIProperty( $subject->getDBkey() );
				$subobject = $property->getCanonicalDiWikiPage( $row->smw_subobject );
			} else {
				$subobject = new DIWikiPage(
					$subject->getDBkey(),
					$subject->getNamespace(),
					$subject->getInterwiki(),
					$row->smw_subobject
				);
			}

			$subobject->setSortKey( $row->smw_sortkey );
			$subobject->setId( $row->smw_id );

			return $subobject;
		};

		return $this->iteratorFactory->newMappingIterator(
			$this->newResultIterator( $subject ),
			$callback
		);
	}

	private function newResultIterator( DIWikiPage $subject ) {

		$connection = $this->store->getConnection( 'mw.db' );
		$key = $subject->getDBkey();

		// #1955 Ensure to match a possible predefined property
		// (Modification date -> _MDAT)
		if ( $subject->getNamespace() === SMW_NS_PROPERTY ) {
			$key = DIProperty::newFromUserLabel( $key )->getKey();
		}

		$conditions = [
			'smw_title='      . $connection->addQuotes( $key ),
			'smw_namespace='  . $connection->addQuotes( $subject->getNamespace() ),
			'smw_iw='         . $connection->addQuotes( $subject->getInterwiki() ),
			'smw_subobject!=' . $connection->addQuotes( '' )
		];

		foreach ( $this->skipConditions as $skipOn ) {
			$conditions[] = 'smw_subobject!=' . $connection->addQuotes( $skipOn );
		}

		$res = $connection->select(
			$connection->tablename( SQLStore::ID_TABLE ),
			[
				'smw_id',
				'smw_subobject',
				'smw_sortkey'
			],
			implode( ' AND ' , $conditions ),
			__METHOD__
		);

		return $this->iteratorFactory->newResultIterator( $res );
	}

}