summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/PostProcHandler.php
blob: beb4a0e8dca68856adb89588b803ce0014c9a843 (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php

namespace SMW;

use Html;
use Onoi\Cache\Cache;
use ParserOutput;
use SMW\SQLStore\ChangeOp\ChangeDiff;
use SMW\SQLStore\QueryDependency\DependencyLinksUpdateJournal;
use SMWQuery as Query;
use Title;
use WebRequest;

/**
 * Some updates require to be handled in a "post" process meaning after an update
 * has already taken place to iterate over those results as input for a value
 * dependency.
 *
 * The post process can only happen after the Store and hereby related processes
 * have been updated. A simple null edit is in most cases inappropriate and
 * therefore it is necessary to a complete a re-parse (triggered by the UpdateJob)
 * to ensure consistency among the stored and displayed data.
 *
 * The PostProc relies on an API request to initiate related updates and once
 * finished will handle the reload of the page.
 *
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class PostProcHandler {

	/**
	 * Identifier on whether an update to subject is to be carried out or not
	 * given the query reference used as part of an @annotation request.
	 */
	const POST_EDIT_UPDATE = 'smw-postedit-update';

	/**
	 * Check registered queries and its results on wether the result_hash before
	 * and after is different or not.
	 */
	const POST_EDIT_CHECK = 'smw-postedit-check';

	/**
	 * Specifies the TTL for the temporary tracking of a post edit
	 * update.
	 */
	const POST_UPDATE_TTL = 86400;

	/**
	 * @var ParserOutput
	 */
	private $parserOutput;

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

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

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

	/**
	 * @since 3.0
	 *
	 * @param ParserOutput $parserOutput
	 * @param Cache $cache
	 */
	public function __construct( ParserOutput $parserOutput, Cache $cache ) {
		$this->parserOutput = $parserOutput;
		$this->cache = $cache;
	}

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

	/**
	 * @since 3.0
	 *
	 * @param array $options
	 */
	public function setOptions( array $options ) {
		$this->options = $options;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $key
	 * @param mixed $default
	 *
	 * @return mixed
	 */
	public function getOption( $key, $default = false ) {

		if ( isset( $this->options[$key] ) ) {
			return $this->options[$key];
		}

		return $default;
	}

	/**
	 * @since 3.0
	 *
	 * @return array|string
	 */
	public function getModules() {
		return 'ext.smw.postproc';
	}

	/**
	 * @since 3.0
	 *
	 * @param Title $title
	 * @param WebRequest $webRequest
	 *
	 * @return string
	 */
	public function getHtml( Title $title, WebRequest $webRequest ) {

		if ( $this->isEnabled === false ) {
			return '';
		}

		$subject = DIWikiPage::newFromTitle(
			$title
		);

		$attributes = [
			'class' => 'smw-postproc',
			'data-subject' => $subject->getHash()
		];

		// Ensure to detect the post edit process to distinguish between an edit
		// event and any other post, get request in order to only sent a html
		// fragment once on the edit request and avoid an infinite loop when the
		// page is reloaded using an API request
		// @see Article::view
		$postEdit = $webRequest->getCookie(
			\EditPage::POST_EDIT_COOKIE_KEY_PREFIX . $title->getLatestRevID()
		);

		$jobs = [];

		if ( $postEdit !== null && isset( $this->options['run-jobs'] ) ) {
			$jobs = $this->find_jobs( $this->options['run-jobs'] );
		}

		if ( $jobs !== [] ) {
			$attributes['data-jobs'] = json_encode( $jobs );
		}

		// Was the edit SMW specific or contains it an unrelated (e.g altered
		// some text unrelated to any property/value annotation) change?
		if ( $postEdit !== null && ( $changeDiff = ChangeDiff::fetch( $this->cache, $subject ) ) !== false ) {
			$postEdit = $this->checkDiff( $changeDiff );
		}

		// Is `@annotation` available as part of a #ask query?
		$refs = $this->parserOutput->getExtensionData( self::POST_EDIT_UPDATE );

		if ( $refs !== null && $refs !== [] ) {
			$postEdit = $this->checkRef( $title, $postEdit );
		}

		if ( $postEdit !== null && $refs !== null && $refs !== [] ) {
			$attributes['data-ref'] = json_encode( array_keys( $refs ) );
		}

		if (
			$postEdit !== null &&
			isset( $this->options['check-query'] ) &&
			( $queries = $this->parserOutput->getExtensionData( self::POST_EDIT_CHECK ) ) !== null ) {
			$attributes['data-query'] = json_encode( $queries );
		}

		// The element is only added temporarily in the event of a postEdit, a
		// reload of the page will not have the cookie being set and is therefore
		// neglected
		if ( $postEdit !== null || $jobs !== [] ) {
			return Html::rawElement( 'div', $attributes );
		}

		return '';
	}

	/**
	 * @since 3.0
	 *
	 * @param Query $query
	 */
	public function addUpdate( Query $query ) {

		// Query:getHash returns a hash based on a fingerprint
		// (when $smwgQueryResultCacheType is set) that eliminates duplicate
		// queries, yet for the post processing it is necessary to know each
		// single query (same-condition, different printout) to allow running
		// alternating updates as in case of cascading value dependencies
		$queryRef = HashBuilder::createFromArray( $query->toArray() );

		$data = $this->parserOutput->getExtensionData( self::POST_EDIT_UPDATE );

		if ( $data === null ) {
			$data = [];
		}

		$data[$queryRef] = true;

		$this->parserOutput->setExtensionData(
			self::POST_EDIT_UPDATE,
			$data
		);
	}

	/**
	 * @since 3.0
	 *
	 * @param Query $query
	 */
	public function addCheck( Query $query ) {

		if ( !isset( $this->options['check-query'] ) || $this->options['check-query'] === false ) {
			return;
		}

		$q_array = $query->toArray();

		// Build a concatenated hash from the query and the result_hash
		$hash = md5( json_encode( $q_array ) ) . '#';
		$data = $this->parserOutput->getExtensionData( self::POST_EDIT_CHECK );

		if ( $data === null ) {
			$data = [];
		}

		// Use the result hash to determine whether results differ during the
		// post-edit examination when running the same query
		if ( $query->getOption( 'result_hash' ) ) {
			$hash .= $query->getOption( 'result_hash' );
		}

		$data[$hash] = $q_array;

		$this->parserOutput->setExtensionData(
			self::POST_EDIT_CHECK,
			$data
		);
	}

	private function checkRef( $title, $postEdit ) {

		$key = DependencyLinksUpdateJournal::makeKey( $title );

		// Is a postEdit, mark the update to avoid running in circles
		// when the pageCache is purged, use the latestRevID to distinguish
		// content changes
		if ( $postEdit !== null ) {

			$record = [
				$title->getLatestRevID() => true
			];

			$this->cache->save( $key . ':post', $record, self::POST_UPDATE_TTL );

			return $postEdit;
		}

		// Run outside of a postEdit, check if the dependency journal contains an
		// active reference to the article and run once (== hash that set by the
		// dependency journal which is == revID that initiated the change)
		$hash = $this->cache->fetch( $key );
		$record = $this->cache->fetch( $key . ':post' );

		if ( $hash !== false && ( $record === false || !isset( $record[$hash] ) ) ) {
			$postEdit = true;

			if ( !is_array( $record ) ) {
				$record = [];
			}

			$record[$hash] = true;

			// Add an update marker (1h) to avoid running twice in case the
			// journal reference hasn't been deleted yet as result of an existing
			// PostProcHandler update request.
			$this->cache->save( $key . ':post', $record, self::POST_UPDATE_TTL );
		}

		return $postEdit;
	}

	private function checkDiff( $changeDiff ) {

		$propertyList = $changeDiff->getPropertyList(
			'flip'
		);

		// Investigate whether the changeDiff contains a user invoked modification
		// and if so, allow the postEdit process to continue in order to act
		// on SMW data and not on text that doesn't involve changes to a property
		// value pair.
		foreach ( $changeDiff->getTableChangeOps() as $tableChangeOp ) {
			foreach ( $tableChangeOp->getFieldChangeOps() as $fieldChangeOp ) {
				$pid = $fieldChangeOp->get( 'p_id' );

				if ( !isset( $propertyList[$pid] ) ) {
					continue;
				}

				// Does the change involve an operation with a user defined
				// property?
				//
				// Some data were altered but since we cannot (within the request
				// framework and without further computation) anticipate whether
				// this influences a query or not, it is a good enough heuristic
				// to allow to continue the postProc.
				if ( $propertyList[$pid]{0} !== '_' ) {
					return true;
				}

				if ( $propertyList[$pid] === '_INST' || $propertyList[$pid] === '_ASK' ) {
					return true;
				}
			}
		}

		// Avoid any update since the condition of the diff containing any altered
		// SMW data was not meet.
		return null;
	}

	private function find_jobs( $jobs ) {

		// Not enabled, no need to invoke a job!
		if ( isset( $this->options['smwgEnabledQueryDependencyLinksStore'] ) && $this->options['smwgEnabledQueryDependencyLinksStore'] === false ) {
			unset( $jobs['smw.parserCachePurge'] );
		}

		if ( isset( $this->options['smwgEnabledFulltextSearch'] ) && $this->options['smwgEnabledFulltextSearch'] === false ) {
			unset( $jobs['smw.fulltextSearchTableUpdate'] );
		}

		return $jobs;
	}

}