summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/SQLStore/PropertyTableIdReferenceDisposer.php
blob: ab7336c56e060da35f17c30ba4368dfc8e0e9196 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php

namespace SMW\SQLStore;

use SMW\ApplicationFactory;
use SMW\DIWikiPage;
use SMW\EventHandler;
use SMW\Iterators\ResultIterator;

/**
 * @private
 *
 * Class responsible for the clean-up (aka disposal) of any outdated table entries
 * that are contained in either the ID_TABLE or related property tables with
 * reference to a matchable ID.
 *
 * @license GNU GPL v2+
 * @since 2.4
 *
 * @author mwjames
 */
class PropertyTableIdReferenceDisposer {

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

	/**
	 * @var Database
	 */
	private $connection = null;

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

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

	/**
	 * @since 2.4
	 *
	 * @param SQLStore $store
	 */
	public function __construct( SQLStore $store ) {
		$this->store = $store;
		$this->connection = $this->store->getConnection( 'mw.db' );
	}

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

	/**
	 * @since 2.5
	 */
	public function waitOnTransactionIdle() {
		$this->onTransactionIdle = true;
	}

	/**
	 * @since 3.0
	 *
	 * @param integer $id
	 *
	 * @return boolean
	 */
	public function isDisposable( $id ) {
		return $this->store->getPropertyTableIdReferenceFinder()->hasResidualReferenceForId( $id ) === false;
	}

	/**
	 * Use case: After a property changed its type (_wpg -> _txt), object values in the
	 * ID table are not removed at the time of the conversion process.
	 *
	 * Before an attempt to remove the ID from entity tables, it is secured that no
	 * references exists for the ID.
	 *
	 * @note This method does not check for an ID being object or subject value
	 * and has to be done prior calling this routine.
	 *
	 * @since 2.4
	 *
	 * @param integer $id
	 */
	public function removeOutdatedEntityReferencesById( $id ) {

		if ( $this->store->getPropertyTableIdReferenceFinder()->hasResidualReferenceForId( $id ) ) {
			return null;
		}

		$this->cleanUpSecondaryReferencesById( $id, false );
	}

	/**
	 * @since 2.5
	 *
	 * @return ResultIterator
	 */
	public function newOutdatedEntitiesResultIterator() {

		$res = $this->connection->select(
			SQLStore::ID_TABLE,
			[ 'smw_id' ],
			[ 'smw_iw' => SMW_SQL3_SMWDELETEIW ],
			__METHOD__
		);

		return new ResultIterator( $res );
	}

	/**
	 * @since 2.5
	 *
	 * @param stdClass $row
	 */
	public function cleanUpTableEntriesByRow( $row ) {

		if ( !isset( $row->smw_id ) ) {
			return;
		}

		$this->cleanUpTableEntriesById( $row->smw_id );
	}

	/**
	 * @note This method does not make any assumption about the ID state and therefore
	 * has to be validated before this method is called.
	 *
	 * @since 2.4
	 *
	 * @param integer $id
	 */
	public function cleanUpTableEntriesById( $id ) {

		if ( $this->onTransactionIdle ) {
			return $this->connection->onTransactionIdle( function() use ( $id ) {
				$this->cleanUpReferencesById( $id );
			} );
		} else {
			$this->cleanUpReferencesById( $id );
		}
	}

	private function cleanUpReferencesById( $id ) {

		$subject = $this->store->getObjectIds()->getDataItemById( $id );
		$isRedirect = false;

		if ( $subject instanceof DIWikiPage ) {
			$isRedirect = $subject->getInterwiki() === SMW_SQL3_SMWREDIIW;

			// Use the subject without an internal 'smw-delete' iw marker
			$subject = new DIWikiPage(
				$subject->getDBKey(),
				$subject->getNamespace(),
				'',
				$subject->getSubobjectName()
			);
		}

		$this->triggerCleanUpEvents( $subject );

		$this->connection->beginAtomicTransaction( __METHOD__ );

		foreach ( $this->store->getPropertyTables() as $proptable ) {
			if ( $proptable->usesIdSubject() ) {
				$this->connection->delete(
					$proptable->getName(),
					[ 's_id' => $id ],
					__METHOD__
				);
			}

			if ( !$proptable->isFixedPropertyTable() ) {
				$this->connection->delete(
					$proptable->getName(),
					[ 'p_id' => $id ],
					__METHOD__
				);
			}

			$fields = $proptable->getFields( $this->store );

			// Match tables (including ftp_redi) that contain an object reference
			if ( isset( $fields['o_id'] ) ) {
				$this->connection->delete(
					$proptable->getName(),
					[ 'o_id' => $id ],
					__METHOD__
				);
			}
		}

		$this->cleanUpSecondaryReferencesById( $id, $isRedirect );
		$this->connection->endAtomicTransaction( __METHOD__ );

		\Hooks::run(
			'SMW::SQLStore::EntityReferenceCleanUpComplete',
			[ $this->store, $id, $subject, $isRedirect ]
		);
	}

	private function cleanUpSecondaryReferencesById( $id, $isRedirect ) {

		// When marked as redirect, don't remove the reference
		if ( $isRedirect === false || ( $isRedirect && $this->redirectRemoval ) ) {
			$this->connection->delete(
				SQLStore::ID_TABLE,
				[ 'smw_id' => $id ],
				__METHOD__
			);
		}

		$this->connection->delete(
			SQLStore::PROPERTY_STATISTICS_TABLE,
			[ 'p_id' => $id ],
			__METHOD__
		);

		$this->connection->delete(
			SQLStore::QUERY_LINKS_TABLE,
			[ 's_id' => $id ],
			__METHOD__
		);

		$this->connection->delete(
			SQLStore::QUERY_LINKS_TABLE,
			[ 'o_id' => $id ],
			__METHOD__
		);

		// Avoid Query: DELETE FROM `smw_ft_search` WHERE s_id = '92575'
		// Error: 126 Incorrect key file for table '.\mw@002d25@002d01\smw_ft_search.MYI'; ...
		try {
			$this->connection->delete(
				SQLStore::FT_SEARCH_TABLE,
				[ 's_id' => $id ],
				__METHOD__
			);
		} catch ( \DBError $e ) {
			ApplicationFactory::getInstance()->getMediaWikiLogger()->info( __METHOD__ . ' reported: ' . $e->getMessage() );
		}
	}

	private function triggerCleanUpEvents( $subject ) {

		if ( !$subject instanceof DIWikiPage ) {
			return;
		}

		// Skip any reset for subobjects where it is expected that the base
		// subject is cleaning up all related cache entries
		if ( $subject->getSubobjectName() !== '' ) {
			return;
		}

		$eventHandler = EventHandler::getInstance();

		$dispatchContext = $eventHandler->newDispatchContext();
		$dispatchContext->set( 'subject', $subject );
		$dispatchContext->set( 'context', 'PropertyTableIdReferenceDisposal' );

		$eventHandler->getEventDispatcher()->dispatch(
			'cached.prefetcher.reset',
			$dispatchContext
		);

		$eventHandler->getEventDispatcher()->dispatch(
			'factbox.cache.delete',
			$dispatchContext
		);
	}

}