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

namespace SMW\MediaWiki\Jobs;

use SMW\MediaWiki\Job;
use Hooks;
use SMW\ApplicationFactory;
use SMW\SQLStore\PropertyTableIdReferenceDisposer;
use SMW\SQLStore\SQLStore;
use Title;

/**
 * @license GNU GPL v2+
 * @since 2.5
 *
 * @author mwjames
 */
class EntityIdDisposerJob extends Job {

	/**
	 * Commit chunk size
	 */
	const CHUNK_SIZE = 200;

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

	/**
	 * @since 2.5
	 *
	 * @return ResultIterator
	 */
	public function newOutdatedEntitiesResultIterator() {
		return $this->newPropertyTableIdReferenceDisposer()->newOutdatedEntitiesResultIterator();
	}

	/**
	 * @since 2.5
	 *
	 * @param integer|stdClass $id
	 */
	public function dispose( $id ) {

		$propertyTableIdReferenceDisposer = $this->newPropertyTableIdReferenceDisposer();

		if ( is_int( $id ) ) {
			return $propertyTableIdReferenceDisposer->cleanUpTableEntriesById( $id );
		}

		$propertyTableIdReferenceDisposer->cleanUpTableEntriesByRow( $id );
	}

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

		$propertyTableIdReferenceDisposer = $this->newPropertyTableIdReferenceDisposer();

		// MW 1.29+ Avoid transaction collisions during Job execution
		$propertyTableIdReferenceDisposer->waitOnTransactionIdle();

		if ( $this->hasParameter( 'id' ) ) {
			$this->dispose( $this->getParameter( 'id' ) );
		} else {
			$this->dispose_all( $this->newOutdatedEntitiesResultIterator() );
		}

		return true;
	}

	private function dispose_all( $outdatedEntitiesResultIterator ) {

		// Make sure the script is only executed from the command line to avoid
		// Special:RunJobs to execute a queued job
		if ( $this->waitOnCommandLineMode() ) {
			return true;
		}

		$applicationFactory = ApplicationFactory::getInstance();
		$connection = $applicationFactory->getStore()->getConnection( 'mw.db' );

		$chunkedIterator = $applicationFactory->getIteratorFactory()->newChunkedIterator(
			$outdatedEntitiesResultIterator,
			self::CHUNK_SIZE
		);

		foreach ( $chunkedIterator as $chunk ) {

			$transactionTicket = $connection->getEmptyTransactionTicket( __METHOD__ );

			foreach ( $chunk as $row ) {
				$this->dispose( $row );
			}

			$connection->commitAndWaitForReplication( __METHOD__, $transactionTicket );
		}
	}

	private function newPropertyTableIdReferenceDisposer() {

		$applicationFactory = ApplicationFactory::getInstance();
		$store = $applicationFactory->getStore();

		// Expect access to the SQL table structure therefore enforce the
		// SQLStore that provides those methods
		if ( !is_a( $store, SQLStore::class ) ) {
			$store = $applicationFactory->getStore( '\SMW\SQLStore\SQLStore' );
		}

		return new PropertyTableIdReferenceDisposer( $store );
	}

}