summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/SQLStore/QueryEngine/Fulltext/TextChangeUpdater.php
blob: 25e94d07fcb1f9f4b0753991371b8bf49e94b564 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php

namespace SMW\SQLStore\QueryEngine\Fulltext;

use Onoi\Cache\Cache;
use Psr\Log\LoggerAwareTrait;
use SMW\MediaWiki\Database;
use SMW\ApplicationFactory;
use SMW\DIWikiPage;
use SMW\SQLStore\ChangeOp\ChangeDiff;
use SMW\SQLStore\ChangeOp\ChangeOp;
use SMW\SQLStore\ChangeOp\TableChangeOp;
use SMW\Utils\Timer;

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

	use LoggerAwareTrait;

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

	/**
	 * @var Cache
	 */
	private $cache;

	/**
	 * @var SearchTableUpdater
	 */
	private $searchTableUpdater;

	/**
	 * @var boolean
	 */
	private $asDeferredUpdate = true;

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

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

	/**
	 * @since 2.5
	 *
	 * @param Database $connection
	 * @param Cache $cache
	 * @param SearchTableUpdater $searchTableUpdater
	 * @param TextSanitizer $textSanitizer
	 */
	public function __construct( Database $connection, Cache $cache, SearchTableUpdater $searchTableUpdater ) {
		$this->connection = $connection;
		$this->cache = $cache;
		$this->searchTableUpdater = $searchTableUpdater;
	}

	/**
	 * @note See comments in the DefaultSettings.php on the smwgFulltextDeferredUpdate setting
	 *
	 * @since 2.5
	 *
	 * @param boolean $asDeferredUpdate
	 */
	public function asDeferredUpdate( $asDeferredUpdate ) {
		$this->asDeferredUpdate = (bool)$asDeferredUpdate;
	}

	/**
	 * When running from commandLine, push updates directly to avoid overhead when
	 * it is known that within that mode transactions are FIFO (i.e. the likelihood
	 * for race conditions of unfinished updates are diminishable).
	 *
	 * @since 2.5
	 *
	 * @param boolean $isCommandLineMode
	 */
	public function isCommandLineMode( $isCommandLineMode ) {
		$this->isCommandLineMode = (bool)$isCommandLineMode;
	}

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

	/**
	 * @see SMW::SQLStore::AfterDataUpdateComplete hook
	 *
	 * @since 2.5
	 *
	 * @param ChangeOp $changeOp
	 */
	public function pushUpdates( ChangeOp $changeOp ) {

		if ( !$this->searchTableUpdater->isEnabled() ) {
			return;
		}

		Timer::start( __METHOD__ );

		// Update within the same transaction as started by SMW::SQLStore::AfterDataUpdateComplete
		if ( !$this->asDeferredUpdate || $this->isCommandLineMode || $this->isPrimary ) {
			return $this->doUpdateFromChangeDiff( $changeOp->newChangeDiff() );
		}

		if ( !$this->canPostUpdate( $changeOp ) ) {
			return;
		}

		$fulltextSearchTableUpdateJob = ApplicationFactory::getInstance()->newJobFactory()->newFulltextSearchTableUpdateJob(
			$changeOp->getSubject()->getTitle(),
			[
				'slot:id' => $changeOp->getSubject()->getHash()
			]
		);

		$fulltextSearchTableUpdateJob->lazyPush();

		$this->logger->info(
			[
				'Fulltext',
				'TextChangeUpdater',
				'Table update (as job) scheduled',
				'procTime in sec: {procTime}'
			],
			[
				'method' => __METHOD__,
				'role' => 'developer',
				'procTime' => Timer::getElapsedTime( __METHOD__, 5 )
			]
		);
	}

	/**
	 * @see SearchTableUpdateJob::run
	 *
	 * @since 2.5
	 *
	 * @param array|boolan $parameters
	 */
	public function pushUpdatesFromJobParameters( $parameters ) {

		if ( !$this->searchTableUpdater->isEnabled() || !isset( $parameters['slot:id'] ) || $parameters['slot:id'] === false ) {
			return;
		}

		$subject = DIWikiPage::doUnserialize( $parameters['slot:id'] );
		$changeDiff = ChangeDiff::fetch( $this->cache, $subject );

		if ( $changeDiff !== false ) {
			return $this->doUpdateFromChangeDiff( $changeDiff );
		}

		$this->logger->info(
			[
				'Fulltext',
				'TextChangeUpdater',
				'Failed update (ChangeDiff) on {id}'
			],
			[
				'method' => __METHOD__,
				'role' => 'developer',
				'id' => $parameters['slot:id']
			]
		);
	}

	/**
	 * @since 2.5
	 *
	 * @param ChangeOp $changeOp
	 */
	public function doUpdateFromChangeDiff( ChangeDiff $changeDiff ) {

		if ( !$this->searchTableUpdater->isEnabled() ) {
			return;
		}

		Timer::start( __METHOD__ );

		$textItems = $changeDiff->getTextItems();
		$diffChangeOps = $changeDiff->getTableChangeOps();

		$changeList = $changeDiff->getChangeListByType( 'insert' );
		$updates = [];

		// Ensure that any delete operation is being accounted for to avoid that
		// removed value annotation remain
		if ( $diffChangeOps !== [] ) {
			$this->doDeleteFromTableChangeOps( $diffChangeOps );
		}

		// Build a composite of replacements where a change occurred, this my
		// contain some false positives
		foreach ( $textItems as $sid => $textItem ) {

			if ( !isset( $changeList[$sid] ) ) {
				continue;
			}

			$this->collectUpdates( $sid, $textItem, $changeList, $updates );
		}

		foreach ( $updates as $key => $value ) {
			list( $sid, $pid ) = explode( ':', $key, 2 );

			if ( $this->searchTableUpdater->exists( $sid, $pid ) === false ) {
				$this->searchTableUpdater->insert( $sid, $pid );
			}

			$this->searchTableUpdater->update(
				$sid,
				$pid,
				$value
			);
		}

		$this->logger->info(
			[
				'Fulltext',
				'TextChangeUpdater',
				'Table update completed',
				'procTime in sec: {procTime}'
			],
			[
				'method' => __METHOD__,
				'role' => 'developer',
				'procTime' => Timer::getElapsedTime( __METHOD__, 5 )
			]
		);
	}

	private function collectUpdates( $sid, array $textItem, $changeList, &$updates ) {

		$searchTable = $this->searchTableUpdater->getSearchTable();

		foreach ( $textItem as $pid => $text ) {

			// Exempted property -> out
			if ( $searchTable->isExemptedPropertyById( $pid ) ) {
				continue;
			}

			$text = implode( ' ', $text );
			$key = $sid . ':' . $pid;

			$updates[$key] = !isset( $updates[$key] ) ? $text : $updates[$key] . ' ' . $text;
		}
	}

	private function doDeleteFromTableChangeOps( array $tableChangeOps ) {
		foreach ( $tableChangeOps as $tableChangeOp ) {
			$this->doDeleteFromTableChangeOp( $tableChangeOp );
		}
	}

	private function doDeleteFromTableChangeOp( TableChangeOp $tableChangeOp ) {

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

			// Replace s_id for subobjects etc. with the o_id
			if ( $tableChangeOp->isFixedPropertyOp() ) {
				$fieldChangeOp->set( 's_id', $fieldChangeOp->has( 'o_id' ) ? $fieldChangeOp->get( 'o_id' ) : $fieldChangeOp->get( 's_id' ) );
				$fieldChangeOp->set( 'p_id', $tableChangeOp->getFixedPropertyValueBy( 'p_id' ) );
			}

			if ( !$fieldChangeOp->has( 'p_id' ) ) {
				continue;
			}

			$this->searchTableUpdater->delete(
				$fieldChangeOp->get( 's_id' ),
				$fieldChangeOp->get( 'p_id' )
			);
		}
	}

	private function canPostUpdate( $changeOp ) {

		$searchTable = $this->searchTableUpdater->getSearchTable();
		$canPostUpdate = false;

		// Find out whether we should actual initiate an update
		foreach ( $changeOp->getChangedEntityIdSummaryList() as $id ) {
			if ( ( $dataItem = $searchTable->getDataItemById( $id ) ) instanceof DIWikiPage && $dataItem->getNamespace() === SMW_NS_PROPERTY ) {
				if ( !$searchTable->isExemptedPropertyById( $id ) ) {
					$canPostUpdate = true;
					break;
				}
			}
		}

		return $canPostUpdate;
	}

}