summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/PageUpdater.php
blob: c65727ffedf347cc7b1c82f8f994784326cb8b52 (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
<?php

namespace SMW\MediaWiki;

use DeferrableUpdate;
use DeferredpendingUpdates;
use Psr\Log\LoggerAwareTrait;
use SMW\MediaWiki\Deferred\TransactionalCallableUpdate;
use SMW\Utils\Timer;
use Title;

/**
 * @license GNU GPL v2+
 * @since 2.1
 *
 * @author mwjames
 */
class PageUpdater implements DeferrableUpdate {

	use LoggerAwareTrait;

	/**
	 * @var TransactionalCallableUpdate
	 */
	private $transactionalCallableUpdate;

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

	/**
	 * @var Title[]
	 */
	private $titles = [];

	/**
	 * @var string
	 */
	private $origin = '';

	/**
	 * @var string|null
	 */
	private $fingerprint = null;

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

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

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

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

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

	/**
	 * @since 2.5
	 *
	 * @param Database|null $connection
	 * @param TransactionalCallableUpdate|null $transactionalCallableUpdate
	 */
	public function __construct( Database $connection = null, TransactionalCallableUpdate $transactionalCallableUpdate = null ) {
		$this->connection = $connection;
		$this->transactionalCallableUpdate = $transactionalCallableUpdate;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $origin
	 */
	public function setOrigin( $origin ) {
		$this->origin = $origin;
	}

	/**
	 * @since 3.0
	 *
	 * @param string|null $fingerprint
	 */
	public function setFingerprint( $fingerprint = null ) {
		$this->fingerprint = $fingerprint;
	}

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

	/**
	 * @since 3.0
	 *
	 * @param booloan $isPending
	 */
	public function markAsPending() {
		$this->isPending = true;
	}

	/**
	 * @since 2.1
	 *
	 * @param Title|null $title
	 */
	public function addPage( Title $title = null ) {

		if ( $title === null ) {
			return;
		}

		$this->titles[$title->getDBKey()] = $title;
	}

	/**
	 * @note MW 1.29+ runs Title::invalidateCache in AutoCommitUpdate which has
	 * been shown to cause transaction issues when executed while a transaction
	 * hasn't finished therefore use 'onTransactionIdle' to isolate the
	 * execution.
	 *
	 * @since 2.5
	 */
	public function waitOnTransactionIdle() {
		$this->onTransactionIdle = true;
	}

	/**
	 * Controls the purge to use a direct DB access to make changes to avoid
	 * racing conditions for a large number of title entities.
	 *
	 * @since 3.0
	 */
	public function doPurgeParserCacheAsPool() {
		if ( $this->connection !== null ) {
			$this->connection->onTransactionIdle( function() {
				 $this->doPoolPurge();
			} );
		} else {
			$this->doPoolPurge();
		}
	}

	/**
	 * @since 2.1
	 */
	public function clear() {
		$this->titles = [];
	}

	/**
	 * @since 2.1
	 *
	 * @return boolean
	 */
	public function canUpdate() {
		return !wfReadOnly();
	}

	/**
	 * Push pendingUpdates to be either deferred or direct executable, pending
	 * the setting invoked by PageUPdater::markAsPending.
	 *
	 * @since 3.0
	 */
	public function pushUpdate() {

		if ( $this->transactionalCallableUpdate === null ) {
			return $this->log( __METHOD__ . ' it is not possible to push updates as DeferredTransactionalUpdate)' );
		}

		$this->transactionalCallableUpdate->setCallback( function(){
			$this->doUpdate();
		} );

		if ( $this->onTransactionIdle ) {
			$this->transactionalCallableUpdate->waitOnTransactionIdle();
		}
		if ( $this->isPending ) {
			$this->transactionalCallableUpdate->markAsPending();
		}

		$this->transactionalCallableUpdate->setFingerprint(
			$this->fingerprint
		);

		$this->transactionalCallableUpdate->setOrigin( [
			__METHOD__,
			$this->origin
		] );

		$this->transactionalCallableUpdate->pushUpdate();
	}

	/**
	 * @since 3.0
	 */
	public function doUpdate() {
		$this->isPending = false;
		$this->onTransactionIdle = false;

		foreach ( array_keys( $this->pendingUpdates ) as $update ) {
			call_user_func( [ $this, $update ] );
		}

		$this->pendingUpdates = [];
	}

	/**
	 * @since 2.1
	 */
	public function doPurgeParserCache() {

		$method = __METHOD__;

		if ( $this->isPending || $this->onTransactionIdle ) {
			return $this->pendingUpdates['doPurgeParserCache'] = true;
		}

		foreach ( $this->titles as $title ) {
			$title->invalidateCache();
		}
	}

	/**
	 * @since 2.1
	 */
	public function doPurgeHtmlCache() {

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

		if ( $this->isPending || $this->onTransactionIdle ) {
			return $this->pendingUpdates['doPurgeHtmlCache'] = true;
		}

		$method = __METHOD__;

		// Calls HTMLCacheUpdate, HTMLCacheUpdateJob including HTMLFileCache,
		// CdnCacheUpdate
		foreach ( $this->titles as $title ) {
			$title->touchLinks();
		}
	}

	/**
	 * @since 2.1
	 */
	public function doPurgeWebCache() {

		$method = __METHOD__;

		if ( $this->isPending || $this->onTransactionIdle ) {
			return $this->pendingUpdates['doPurgeWebCache'] = true;
		}

		foreach ( $this->titles as $title ) {
			$title->purgeSquid();
		}
	}

	/**
	 * Copied from PurgeJobUtils to avoid the AutoCommitUpdate from
	 * Title::invalidateCache introduced with MW 1.28/1.29 on a large update pool
	 */
	private function doPoolPurge() {

		Timer::start( __METHOD__ );

		// #3413
		$byNamespace = [];

		foreach ( $this->titles as $title ) {
			$namespace = $title->getNamespace();
			$pagename = $title->getDBkey();
			$byNamespace[$namespace][] = $pagename;
		}

		$conds = [];

		foreach ( $byNamespace as $namespaces => $pagenames ) {

			$cond = [
				'page_namespace' => $namespaces,
				'page_title' => $pagenames,
			];

			$conds[] = $this->connection->makeList( $cond, LIST_AND );
		}

		$titleConds = $this->connection->makeList( $conds, LIST_OR );

		// Required due to postgres and "Error: 22007 ERROR:  invalid input
		// syntax for type timestamp with time zone: "20170408113703""
		$now = $this->connection->timestamp();
		$res = $this->connection->select(
			'page',
			'page_id',
			[
				$titleConds,
				'page_touched < ' . $this->connection->addQuotes( $now )
			],
			__METHOD__
		);

		if ( $res === false ) {
			return;
		}

		$ids = [];

		foreach ( $res as $row ) {
			$ids[] = $row->page_id;
		}

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

		$this->connection->update(
			'page',
			[ 'page_touched' => $now ],
			[
				'page_id' => $ids,
				'page_touched < ' . $this->connection->addQuotes( $now )
			],
			__METHOD__
		);

		$context = [
			'method' => __METHOD__,
			'procTime' => Timer::getElapsedTime( __METHOD__, 7 ),
			'role' => 'developer'
		];

		$this->logger->info( 'Page update, pool update (procTime in sec: {procTime})', $context );
	}

}