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

namespace SMW\SQLStore;

use RuntimeException;
use SMW\DIProperty;
use SMW\DIWikiPage;
use SMW\IteratorFactory;
use SMW\Store;

/**
 * Find all entities related to a change propagation (only expected
 * to be used by `ChangePropagationDispatchJob`).
 *
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class ChangePropagationEntityFinder {

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

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

	/**
	 * @var boolean
	 */
	private $isTypePropagation = false;

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

	/**
	 * @since 3.0
	 *
	 * @param boolean $isTypePropagation
	 */
	public function isTypePropagation( $isTypePropagation ) {
		$this->isTypePropagation = (bool)$isTypePropagation;
	}

	/**
	 * @since 3.0
	 *
	 * @param DIProperty|DIWikiPage $entity
	 *
	 * @return Iterator
	 * @throws RuntimeException
	 */
	public function findAll( $entity ) {

		if ( $entity instanceof DIProperty ) {
			return $this->findByProperty( $entity );
		} elseif ( $entity instanceof DIWikiPage ) {
			return $this->findByCategory( $entity );
		}

		throw new RuntimeException( 'Cannot match the entity type.' );
	}

	/**
	 * @since 3.0
	 *
	 * @param DIProperty $property
	 *
	 * @return Iterator
	 */
	public function findByProperty( DIProperty $property ) {

		$dataItems = [];
		$appendIterator = $this->iteratorFactory->newAppendIterator();

		$res = $this->store->getAllPropertySubjects(
			$property
		);

		$appendIterator->add(
			$res
		);

		// Select any remaining references that are hidden or have been left out
		// during an update
		$appendIterator->add(
			$this->fetchOtherReferencesOnTypePropagation( $property )
		);

		$dataItems = $this->store->getPropertySubjects(
			new DIProperty( DIProperty::TYPE_ERROR ),
			$property->getCanonicalDiWikiPage()
		);

		$appendIterator->add(
			$dataItems
		);

		return $appendIterator;
	}

	/**
	 * @since 3.0
	 *
	 * @param DIWikiPage $category
	 *
	 * @return Iterator
	 */
	public function findByCategory( DIWikiPage $category ) {

		$appendIterator = $this->iteratorFactory->newAppendIterator();

		$property = new DIProperty( '_INST' );

		$appendIterator->add(
			$this->store->getPropertySubjects( $property, $category )
		);

		// Only direct antecedents
		$dataItems = $this->store->getPropertyValues(
			$category,
			new DIProperty( '_SUBC' )
		);

		foreach ( $dataItems as $dataItem ) {
			$appendIterator->add(
				$this->store->getPropertySubjects( $property, $dataItem )
			);
		}

		return $appendIterator;
	}

	private function fetchOtherReferencesOnTypePropagation( $property ) {

		// Find other references only on a type propagation (which causes a
		// change of table/id assignments) for entity references
		if ( $this->isTypePropagation === false ) {
			return [];
		}

		$connection = $this->store->getConnection( 'mw.db' );
		$pid = $this->store->getObjectIds()->getSMWPropertyID( $property );

		$dataItemTables =  $this->store->getPropertyTableInfoFetcher()->getDefaultDataItemTables();
		$idList = [];

		// Matches may temporary create duplicates in regrads to
		// Store::getAllPropertySubjects but it will be dealt with by the
		// deduplication in the ChangePropagationUpdateJob
		foreach ( $dataItemTables as $tableName ) {

			// Select any references that are hidden or remained active
			$rows = $connection->select(
				$connection->tableName( $tableName ),
				[
					's_id'
				],
				[
					'p_id' => $pid
				],
				__METHOD__
			);

			foreach ( $rows as $row ) {
				$idList[] = $row->s_id;
			}
		}

		if ( $idList === [] ) {
			return $idList;
		}

		return $this->store->getObjectIds()->getDataItemPoolHashListFor( $idList );
	}

}