summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/SQLStore/EntityStore/UniquenessLookup.php
blob: 0076701553bc1138669506ab8df857a4fc5ffbfa (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
<?php

namespace SMW\SQLStore\EntityStore;

use Onoi\Cache\Cache;
use SMW\DIWikiPage;
use SMW\IteratorFactory;
use SMW\RequestOptions;
use SMW\SQLStore\SQLStore;
use SMW\Store;
use InvalidArgumentException;
use SMWDataItem as DataItem;

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

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

	/**
	 * @var IteratorFactory
	 */
	private $iteratorFactory;

	/**
	 * @since 3.0
	 *
	 * @param Store $store
	 * @param IteratorFactory $iteratorFactory
	 */
	public function __construct( Store $store, IteratorFactory $iteratorFactory ) {
		$this->store = $store;
		$this->iteratorFactory = $iteratorFactory;
	}

	/**
	 * @since 3.0
	 *
	 * @param DataItem $dataItem
	 *
	 * @return boolean
	 */
	public function isUnique( DataItem $dataItem ) {

		$type = $dataItem->getDIType();

		if ( $type !== DataItem::TYPE_WIKIPAGE && $type !== DataItem::TYPE_PROPERTY ) {
			throw new InvalidArgumentException( 'Expects a DIProperty or DIWikiPage object.' );
		}

		$connection = $this->store->getConnection( 'mw.db' );
		$query = $connection->newQuery();

		$query->type( 'SELECT' );
		$query->options( [ 'LIMIT' => 2 ] );

		$query->table( SQLStore::ID_TABLE );

		// Only find entities
		$query->fields( [ 'smw_id', 'smw_sortkey' ] );

		if ( $type === DataItem::TYPE_WIKIPAGE ) {
			$query->condition( $query->eq( 'smw_title', $dataItem->getDBKey() ) );
			$query->condition( $query->eq( 'smw_namespace', $dataItem->getNamespace() ) );
			$query->condition( $query->eq( 'smw_subobject', $dataItem->getSubobjectName() ) );
		} else {
			$query->condition( $query->eq( 'smw_sortkey', $dataItem->getCanonicalLabel() ) );
			$query->condition( $query->eq( 'smw_namespace', SMW_NS_PROPERTY ) );
			$query->condition( $query->eq( 'smw_subobject', '' ) );
		}

		$query->condition( $query->neq( 'smw_iw', SMW_SQL3_SMWIW_OUTDATED ) );
		$query->condition( $query->neq( 'smw_iw', SMW_SQL3_SMWDELETEIW ) );
		$query->condition( $query->neq( 'smw_iw', SMW_SQL3_SMWREDIIW ) );

		$res = $connection->query(
			$query,
			__METHOD__
		);

		return $res->numRows() < 2;
	}

	/**
	 * @since 3.0
	 *
	 * @return Iterator|[]
	 */
	public function findDuplicates() {

		$connection = $this->store->getConnection( 'mw.db' );
		$query = $connection->newQuery();

		$query->type( 'SELECT' );
		$query->table( SQLStore::ID_TABLE );

		$query->fields(
			[
				'COUNT(*) as count',
				'smw_title',
				'smw_namespace',
				'smw_iw',
				'smw_subobject'
			]
		);

		$query->condition( $query->neq( 'smw_iw', SMW_SQL3_SMWIW_OUTDATED ) );
		$query->condition( $query->neq( 'smw_iw', SMW_SQL3_SMWDELETEIW ) );

		$query->options(
			[
				'GROUP BY' => 'smw_title, smw_namespace, smw_iw, smw_subobject',

				// @see https://stackoverflow.com/questions/8119489/postgresql-where-count-condition
				// "HAVING count > 1"; doesn't work with postgres
				'HAVING' => 'count(*) > 1'
			]
		);

		$rows = $connection->query(
			$query,
			__METHOD__
		);

		if ( $rows === false ) {
			return [];
		}

		$resultIterator = $this->iteratorFactory->newResultIterator(
			$rows
		);

		$mappingIterator = $this->iteratorFactory->newMappingIterator( $resultIterator, function( $row ) {
			return [
				'count'=> $row->count,
				'smw_title'=> $row->smw_title,
				'smw_namespace'=> $row->smw_namespace,
				'smw_iw'=> $row->smw_iw,
				'smw_subobject'=> $row->smw_subobject
			];
		} );

		return $mappingIterator;
	}

}