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

namespace SMW\SQLStore\QueryDependency;

use Psr\Log\LoggerAwareTrait;
use SMW\SQLStore\ChangeOp\ChangeOp;
use SMW\Store;
use SMW\Utils\Timer;

/**
 * This class filters entities recorded in the ChangeOp
 * and applies a relevance rule set by:
 *
 * - Remove exempted properties (not relevant)
 * - Add properties that are affiliated on a relational change
 *
 * By affiliation implies that a property listed is not directly related to a query
 * dependency, yet it is monitored and can, if altered trigger a dependency update
 * that normally is only reserved to dependent properties.
 *
 * @license GNU GPL v2+
 * @since 2.4
 *
 * @author mwjames
 */
class EntityIdListRelevanceDetectionFilter {

	use LoggerAwareTrait;

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

	/**
	 * @var ChangeOp
	 */
	private $changeOp = null;

	/**
	 * @var array
	 */
	private $propertyExemptionList = [];

	/**
	 * @var array
	 */
	private $affiliatePropertyDetectionList = [];

	/**
	 * @since 2.4
	 *
	 * @param Store $store
	 * @param ChangeOp $changeOp
	 */
	public function __construct( Store $store, ChangeOp $changeOp ) {
		$this->store = $store;
		$this->changeOp = $changeOp;
	}

	/**
	 * @since 3.0
	 *
	 * @return DIWikiPage
	 */
	public function getSubject() {
		return $this->changeOp->getSubject();
	}

	/**
	 * @since 2.4
	 *
	 * @param array $propertyExemptionList
	 */
	public function setPropertyExemptionList( array $propertyExemptionList ) {
		$this->propertyExemptionList = array_flip(
			str_replace( ' ', '_', $propertyExemptionList )
		);
	}

	/**
	 * @since 2.4
	 *
	 * @param array $affiliatePropertyDetectionList
	 */
	public function setAffiliatePropertyDetectionList( array $affiliatePropertyDetectionList ) {
		$this->affiliatePropertyDetectionList = array_flip(
			str_replace( ' ', '_', $affiliatePropertyDetectionList )
		);
	}

	/**
	 * @since 2.4
	 *
	 * @return array
	 */
	public function getFilteredIdList() {

		Timer::start( __CLASS__ );

		$changedEntityIdSummaryList = array_flip(
			$this->changeOp->getChangedEntityIdSummaryList()
		);

		$affiliateEntityList = [];
		$tableChangeOps = $this->changeOp->getTableChangeOps();

		foreach ( $tableChangeOps as $tableChangeOp ) {
			$this->applyFilterToTableChangeOp(
				$tableChangeOp,
				$affiliateEntityList,
				$changedEntityIdSummaryList
			);
		}

		$filteredIdList = array_merge(
			array_keys( $changedEntityIdSummaryList ),
			array_keys( $affiliateEntityList )
		);

		$this->logger->info(
			[
				'QueryDependency',
				'EntityIdListRelevanceDetectionFilter',
				'Filter changeOp list',
				'procTime in sec: {procTime}'
			],
			[
				'method' => __METHOD__,
				'role' => 'developer',
				'procTime' => Timer::getElapsedTime( __CLASS__, 6 )
			]
		);

		return $filteredIdList;
	}

	private function applyFilterToTableChangeOp( $tableChangeOp, &$affiliateEntityList, &$changedEntityIdSummaryList ) {

		foreach ( $tableChangeOp->getFieldChangeOps( 'insert' ) as $insertFieldChangeOp ) {

			// Copy fields temporarily
			if ( $tableChangeOp->isFixedPropertyOp() ) {
				$insertFieldChangeOp->set( 'p_id', $tableChangeOp->getFixedPropertyValueBy( 'p_id' ) );
				$insertFieldChangeOp->set( 'key', $tableChangeOp->getFixedPropertyValueBy( 'key' ) );
			}

			$this->modifyEntityList( $insertFieldChangeOp, $affiliateEntityList, $changedEntityIdSummaryList );
		}

		foreach ( $tableChangeOp->getFieldChangeOps( 'delete' ) as $deleteFieldChangeOp ) {

			if ( $tableChangeOp->isFixedPropertyOp() ) {
				$deleteFieldChangeOp->set( 'p_id', $tableChangeOp->getFixedPropertyValueBy( 'p_id' ) );
				$deleteFieldChangeOp->set( 'key', $tableChangeOp->getFixedPropertyValueBy( 'key' ) );
			}

			$this->modifyEntityList( $deleteFieldChangeOp, $affiliateEntityList, $changedEntityIdSummaryList );
		}
	}

	private function modifyEntityList( $fieldChangeOp, &$affiliateEntityList, &$changedEntityIdSummaryList ) {
		$key = '';

		if ( $fieldChangeOp->has( 'key' ) ) {
			$key = $fieldChangeOp->get( 'key' );
		} elseif ( $fieldChangeOp->has( 'p_id' ) ) {
			$dataItem = $this->store->getObjectIds()->getDataItemById( $fieldChangeOp->get( 'p_id' ) );
			$key = $dataItem !== null ? $dataItem->getDBKey() : null;
		}

		// Exclusion before inclusion
		if ( isset( $this->propertyExemptionList[$key]) ) {
			$this->unsetEntityList( $fieldChangeOp, $changedEntityIdSummaryList );
			return;
		}

		if ( isset( $this->affiliatePropertyDetectionList[$key] ) && $fieldChangeOp->has( 's_id' ) ) {
			$affiliateEntityList[$fieldChangeOp->get( 's_id' )] = true;
		}
	}

	private function unsetEntityList( $fieldChangeOp, &$changedEntityIdSummaryList ) {
		// Remove matched blacklisted property reference
		if ( $fieldChangeOp->has( 'p_id' ) ) {
			unset( $changedEntityIdSummaryList[$fieldChangeOp->get( 'p_id' )] );
		}

		// Remove associated subject ID's
		if ( $fieldChangeOp->has( 's_id' ) ) {
			unset( $changedEntityIdSummaryList[$fieldChangeOp->get( 's_id' )] );
		}
	}

}