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

namespace SMW\MediaWiki\Jobs;

use SMW\MediaWiki\Job;
use Hooks;
use SMW\ApplicationFactory;
use SMW\HashBuilder;
use SMW\RequestOptions;
use SMW\SQLStore\QueryDependencyLinksStoreFactory;
use SMW\Utils\Timer;
use SMW\DIWikiPage;
use SMWQuery as Query;
use Title;

/**
 * @license GNU GPL v2+
 * @since 2.3
 *
 * @author mwjames
 */
class ParserCachePurgeJob extends Job {

	/**
	 * A balanced size that should be carefully monitored in order to not have a
	 * negative impact when running the initial update in online mode.
	 */
	const CHUNK_SIZE = 300;

	/**
	 * Using DB update execution mode to immediately execute the purge which may
	 * cause a surge in DB inserts.
	 */
	const EXEC_DB = 'exec.db';

	/**
	 * Using journal update execution mode to pause the execution and temporary
	 * store until an actual page is viewed.
	 */
	const EXEC_JOURNAL = 'exec.journal';

	/**
	 * @var ApplicationFactory
	 */
	protected $applicationFactory;

	/**
	 * @var integer
	 */
	private $limit = self::CHUNK_SIZE;

	/**
	 * @var integer
	 */
	private $offset = 0;

	/**
	 * @var PageUpdater
	 */
	protected $pageUpdater;

	/**
	 * @since 2.3
	 *
	 * @param Title $title
	 * @param array $params job parameters
	 */
	public function __construct( Title $title, $params = [] ) {
		parent::__construct( 'smw.parserCachePurge', $title, $params );
		$this->removeDuplicates = true;
	}

	/**
	 * @see Job::run
	 */
	public function insert() {

		if (
			$this->hasParameter( 'is.enabled' ) &&
			$this->getParameter( 'is.enabled' ) === false ) {
			return;
		}

		parent::insert();
	}

	/**
	 * @see Job::run
	 *
	 * @since  2.3
	 */
	public function run() {

		Timer::start( __METHOD__ );
		$this->applicationFactory = ApplicationFactory::getInstance();
		$this->pageUpdater = $this->applicationFactory->newPageUpdater();

		$count = 0;
		$linksCount = 0;

		if ( $this->hasParameter( 'limit' ) ) {
			$this->limit = $this->getParameter( 'limit' );
		}

		if ( $this->hasParameter( 'offset' ) ) {
			$this->offset = $this->getParameter( 'offset' );
		}

		if ( $this->hasParameter( 'idlist' ) ) {
			$this->purgeTargetLinksFromList( $this->getParameter( 'idlist' ), $count, $linksCount );
		}

		if ( $this->getParameter( 'exec.mode' ) !== self::EXEC_JOURNAL ) {
			$this->pageUpdater->addPage( $this->getTitle() );
			$this->pageUpdater->setOrigin( __METHOD__ );
			$this->pageUpdater->doPurgeParserCacheAsPool();
		}

		Hooks::run( 'SMW::Job::AfterParserCachePurgeComplete', [ $this ] );

		$this->applicationFactory->getMediaWikiLogger()->info(
			[
				'Job',
				"ParserCachePurgeJob",
				"List count:{count}",
				"Links count:{linksCount}",
				"Limit:{limit}",
				"Offset:{offset}",
				"procTime in sec: {procTime}"
			],
			[
				'method'  => __METHOD__,
				'role' => 'user',
				'procTime' => Timer::getElapsedTime( __METHOD__, 7 ),
				'limit'  => $this->limit,
				'offset' => $this->offset,
				'count'  => $count,
				'linksCount'  => $linksCount
			]
		);

		return true;
	}

	/**
	 * Based on the CHUNK_SIZE, target links are purged in an instant if those
	 * selected entities are < CHUNK_SIZE which should be enough for most
	 * common queries that only share a limited amount of dependencies, yet for
	 * queries that expect a large subject/dependency pool, doing an online update
	 * for all at once is not feasible hence the iterative process of creating
	 * batches that run through the job scheduler.
	 *
	 * @param array|string $idList
	 */
	private function purgeTargetLinksFromList( $idList, &$listCount, &$linksCount ) {

		if ( is_string( $idList ) && strpos( $idList, '|' ) !== false ) {
			$idList = explode( '|', $idList );
		}

		if ( $idList === [] ) {
			return true;
		}

		$queryDependencyLinksStoreFactory = $this->applicationFactory->singleton(
			'QueryDependencyLinksStoreFactory'
		);

		$queryDependencyLinksStore = $queryDependencyLinksStoreFactory->newQueryDependencyLinksStore(
			$this->applicationFactory->getStore()
		);

		$dependencyLinksUpdateJournal = $queryDependencyLinksStoreFactory->newDependencyLinksUpdateJournal();

		$requestOptions = new RequestOptions();

		// +1 to look ahead
		$requestOptions->setLimit( $this->limit + 1 );
		$requestOptions->setOffset( $this->offset );
		$requestOptions->setOption( 'links.count', 0 );

		$hashList = $queryDependencyLinksStore->findDependencyTargetLinks(
			$idList,
			$requestOptions
		);

		$linksCount = $requestOptions->getOption( 'links.count' );

		// If more results are available then use an iterative increase to fetch
		// the remaining updates by creating successive jobs
		if ( $linksCount > $this->limit ) {
			$job = new self(
				$this->getTitle(),
				[
					'idlist'  => $idList,
					'limit'   => $this->limit,
					'offset'  => $this->offset + self::CHUNK_SIZE,
					'exec.mode' => $this->getParameter( 'exec.mode' )
				]
			);

			$job->run();
		}

		if ( $hashList === [] ) {
			return true;
		}

		list( $hashList, $queryList ) = $this->splitList( $hashList	);
		$listCount = count( $hashList );

		$cachedQueryResultPrefetcher = $this->applicationFactory->singleton(
			'CachedQueryResultPrefetcher'
		);

		$cachedQueryResultPrefetcher->resetCacheBy(
			$queryList,
			'ParserCachePurgeJob'
		);

		if ( $this->getParameter( 'exec.mode' ) === self::EXEC_JOURNAL ) {
			$dependencyLinksUpdateJournal->updateFromList( $hashList, $this->getTitle()->getLatestRevID() );
		} else{
			$this->addPagesToUpdater( $hashList );
		}
	}

	public function splitList( $hashList ) {

		$targetLinksList = [];
		$queryList = [];

		foreach ( $hashList as $hash ) {

			if ( $hash instanceof DIWikiPage ) {
				$hash = $hash->getHash();
			}

			list( $title, $namespace, $iw, $subobjectname ) = explode( '#', $hash, 4 );

			// QueryResultCache stores queries with they queryID = $subobjectname
			if ( strpos( $subobjectname, Query::ID_PREFIX ) !== false ) {
				$queryList[$subobjectname] = true;
			}

			// We make an assumption (as we avoid to query the DB) about that a
			// query is bind to its subject by simply removing the subobject
			// identifier (_QUERY*) and creating the base (or root) subject for
			// the selected target (embedded query)
			$targetLinksList[HashBuilder::createHashIdFromSegments( $title, $namespace, $iw )] = true;
		}

		return [ array_keys( $targetLinksList ), array_keys( $queryList ) ];
	}

	private function addPagesToUpdater( array $hashList ) {
		foreach ( $hashList as $hash ) {
			$this->pageUpdater->addPage(
				HashBuilder::newTitleFromHash( $hash )
			);
		}
	}

}