summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Maintenance/DuplicateEntitiesDisposer.php
blob: 33afcb1efb2d5178e84992b42f97f12c314a003d (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
<?php

namespace SMW\Maintenance;

use Onoi\Cache\Cache;
use Onoi\MessageReporter\MessageReporterAwareTrait;
use SMW\SQLStore\PropertyTableIdReferenceDisposer;
use SMW\SQLStore\SQLStore;
use SMW\Store;

/**
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class DuplicateEntitiesDisposer {

	use MessageReporterAwareTrait;

	/**
	 * @var Store
	 */
	private $store;

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

	/**
	 * @since 3.0
	 *
	 * @param Store $store
	 * @param Cache|null $cache
	 */
	public function __construct( Store $store, Cache $cache = null ) {
		$this->store = $store;
		$this->cache = $cache;
	}

	/**
	 * @since 3.0
	 */
	public function findDuplicates() {
		return $this->store->getObjectIds()->findDuplicates();
	}

	/**
	 * @since 3.0
	 *
	 * @param Iterator|array $duplicates
	 */
	public function verifyAndDispose( $duplicates ) {

		if ( !$this->is_iterable( $duplicates ) ) {
			return;
		}

		$count = count( $duplicates );
		$this->messageReporter->reportMessage( "Found: $count duplicates\n" );

		if ( $count > 0 ) {
			$this->doDispose( $duplicates );
		}

		if ( $this->cache !== null ) {
			$this->cache->delete( \SMW\MediaWiki\Api\Task::makeCacheKey( 'duplookup' ) );
		}
	}

	private function doDispose( $duplicates ) {

		$propertyTableIdReferenceDisposer = new PropertyTableIdReferenceDisposer(
			$this->store
		);

		$propertyTableIdReferenceDisposer->setRedirectRemoval( true );
		$connection = $this->store->getConnection( 'mw.db' );

		$log = [
			'disposed' => [],
			'untouched' => []
		];

		$i = 0;
		foreach ( $duplicates as $duplicate ) {
			unset( $duplicate['count'] );

			if ( ( $i ) % 60 === 0 ) {
				$this->messageReporter->reportMessage( "\n" );
			}

			$this->messageReporter->reportMessage( '.' );

			$res = $connection->select(
				SQLStore::ID_TABLE,
				[
					'smw_id',
				],
				[
					'smw_title'=> $duplicate['smw_title'],
					'smw_namespace'=> $duplicate['smw_namespace'],
					'smw_iw'=> $duplicate['smw_iw'],
					'smw_subobject'=> $duplicate['smw_subobject']
				],
				__METHOD__
			);

			foreach ( $res as $row ) {
				if ( $propertyTableIdReferenceDisposer->isDisposable( $row->smw_id ) ) {
					$propertyTableIdReferenceDisposer->cleanUpTableEntriesById( $row->smw_id );
					$log['disposed'][$row->smw_id] = $duplicate;
				} else {
					$log['untouched'][$row->smw_id] = $duplicate;
				}
			}

			$i++;
		}

		$this->messageReporter->reportMessage(
			"\n\nLog\n\n" . json_encode( $log, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) . "\n"
		);
	}

	/**
	 * Polyfill for PHP 7.0-
	 *
	 * @see http://php.net/manual/en/function.is-iterable.php
	 *
	 * @since 3.0
	 */
	private function is_iterable( $obj ) {
		return is_array( $obj ) || ( is_object( $obj ) && ( $obj instanceof \Traversable ) );
	}

}