Completed
Push — master ( a617a1...68b2f8 )
by
unknown
02:36
created

SqlDumpMetaInformationRepo::getSourceItemIds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace WikibaseQuality\ExternalValidation\DumpMetaInformation;
4
5
use Database;
6
use InvalidArgumentException;
7
use UnexpectedValueException;
8
use ResultWrapper;
9
use Wikibase\DataModel\Entity\ItemId;
10
use Wikibase\DataModel\Entity\PropertyId;
11
12
/**
13
 * Implements access to the actual database table that stores the dump information.
14
 *
15
 * @package WikibaseQuality\ExternalValidation\DumpMetaInformation
16
 * @author BP2014N1
17
 * @license GNU GPL v2+
18
 */
19
class SqlDumpMetaInformationRepo implements DumpMetaInformationLookup, DumpMetaInformationStore {
20
21
	const META_TABLE_NAME = 'wbqev_dump_information';
22
	const IDENTIFIER_PROPERTIES_TABLE_NAME = 'wbqev_identifier_properties';
23
24
	/**
25
	 * Gets DumpMetaInformation for specific dump ids from database
26
	 * Returns array in the form 'dumpId' => DumpMetaInformation
27
	 *
28
	 * @param string[] $dumpIds
29
	 * @return DumpMetaInformation[]
30
	 * @throws InvalidArgumentException
31
	 */
32
	public function getWithIds( array $dumpIds ) {
33
		foreach ( $dumpIds as $dumpId ) {
34
			if ( !is_string( $dumpId ) ) {
35
				throw new InvalidArgumentException( '$dumpIds must contain only strings.' );
36
			}
37
		}
38
39
		if( count( $dumpIds ) > 0 ) {
40
			$db = wfGetDB( DB_SLAVE );
41
			$result = $db->select(
42
				array(
43
					self::META_TABLE_NAME,
44
					self::IDENTIFIER_PROPERTIES_TABLE_NAME
45
				),
46
				'*',
47
				array(
48
					'id' => $dumpIds
49
				),
50
				__METHOD__,
51
				array(),
52
				array(
53
					self::IDENTIFIER_PROPERTIES_TABLE_NAME => array(
54
						'LEFT JOIN',
55
						'dump_id = id'
56
					)
57
				)
58
			);
59
60
			return $this->buildDumpMetaInformationFromResult( $result );
61
		}
62
63
		return array();
64
	}
65
66
	/**
67
	 * Gets DumpMetaInformation for specific identifier properties from database
68
	 * Returns array in the form 'dumpId' => DumpMetaInformation
69
	 *
70
	 * @param PropertyId[] $identifierPropertyIds
71
	 *
72
	 * @throws InvalidArgumentException
73
	 * @return DumpMetaInformation[]
74
	 */
75
	public function getWithIdentifierProperties( array $identifierPropertyIds ) {
76
		foreach ( $identifierPropertyIds as $propertyId ) {
77
			if ( !( $propertyId instanceof PropertyId ) ) {
78
				throw new InvalidArgumentException( '$identifierProperties must contain only PropertyIds.' );
79
			}
80
		}
81
82
		if( count( $identifierPropertyIds ) > 0 ) {
83
			$db = wfGetDB( DB_SLAVE );
84
			$identifierPropertyIds = array_map(
85
				function( PropertyId $id ) {
86
					return $id->getSerialization();
87
				},
88
				$identifierPropertyIds
89
			);
90
			$result = $db->select(
91
				self::IDENTIFIER_PROPERTIES_TABLE_NAME,
92
				'dump_id',
93
				array(
94
					'identifier_pid' => $identifierPropertyIds
95
				)
96
			);
97
			$dumpIds = array();
98
			foreach ( $result as $row ) {
99
				if ( !in_array( $row->dump_id, $dumpIds ) ) {
100
					$dumpIds[] = $row->dump_id;
101
				}
102
			}
103
104
			return $this->getWithIds( $dumpIds );
105
		}
106
107
		return array();
108
	}
109
110
	/**
111
	 * Gets all DumpMetaInformation from database
112
	 * Returns array in the form 'dumpId' => DumpMetaInformation
113
	 *
114
	 * @return DumpMetaInformation[]
115
	 */
116
	public function getAll() {
117
		$db = wfGetDB( DB_SLAVE );
118
		$result = $db->select(
119
			array(
120
				self::META_TABLE_NAME,
121
				self::IDENTIFIER_PROPERTIES_TABLE_NAME
122
			),
123
			'*',
124
			array(),
125
			__METHOD__,
126
			array(),
127
			array(
128
				self::IDENTIFIER_PROPERTIES_TABLE_NAME => array(
129
					'LEFT JOIN',
130
					'dump_id = id'
131
				)
132
			)
133
		);
134
135
		return $this->buildDumpMetaInformationFromResult( $result );
136
	}
137
138
	/**
139
	 * @param ResultWrapper $result
140
	 * @return null|array
141
	 * @throws UnexpectedValueException
142
	 */
143
	private function buildDumpMetaInformationFromResult( ResultWrapper $result ) {
144
		$aggregatedRows = array();
145
		foreach ( $result as $row ) {
146
			if ( array_key_exists( $row->id, $aggregatedRows ) ) {
147
				$propertyId = new PropertyId( $row->identifier_pid );
148
				$aggregatedRows[$row->id]->identifier_pid[] = $propertyId;
149
			} else {
150
				if ( $row->identifier_pid !== null ) {
151
					$propertyId = new PropertyId( $row->identifier_pid );
152
					$row->identifier_pid = array( $propertyId );
153
				}
154
				$aggregatedRows[$row->id] = $row;
155
			}
156
		}
157
158
		$dumpMetaInformation = array();
159
		foreach ( $aggregatedRows as $row ) {
160
			$dumpId = $row->id;
161
			$sourceItemId = new ItemId( $row->source_qid );
162
			$importDate = wfTimestamp( TS_MW, $row->import_date );
163
			$language = $row->language;
164
			$sourceUrl = $row->source_url;
165
			$size = (int)$row->size;
166
			$licenseItemId = new ItemId( $row->license_qid );
167
			$identifierPropertyIds = $row->identifier_pid;
168
169
			$dumpMetaInformation[$row->dump_id] =
170
				new DumpMetaInformation( $dumpId,
171
					$sourceItemId,
172
					$identifierPropertyIds,
173
					$importDate,
174
					$language,
175
					$sourceUrl,
176
					$size,
177
					$licenseItemId
178
				);
179
		}
180
181
		return $dumpMetaInformation;
182
	}
183
184
	/**
185
	 * Inserts or updates given dump meta information to database
186
	 *
187
	 * @param DumpMetaInformation $dumpMetaInformation
188
	 */
189
	public function save( DumpMetaInformation $dumpMetaInformation ) {
190
		$db = wfGetDB( DB_SLAVE );
191
		$dumpId = $dumpMetaInformation->getDumpId();
192
		$accumulator = $this->getDumpInformationFields( $db, $dumpMetaInformation );
193
194
		$existing = $db->selectRow(
195
			self::META_TABLE_NAME,
196
			array( 'id' ),
197
			array( 'id' => $dumpId )
198
		);
199
200
		if ( $existing ) {
201
			$db->update(
202
				self::META_TABLE_NAME,
203
				$accumulator,
204
				array( 'id' => $dumpId )
205
			);
206
		} else {
207
			$db->insert(
208
				self::META_TABLE_NAME,
209
				$accumulator
210
			);
211
		}
212
213
		$db->delete(
214
			self::IDENTIFIER_PROPERTIES_TABLE_NAME,
215
			array( 'dump_id' => $dumpId )
216
		);
217
		$db->insert(
218
			self::IDENTIFIER_PROPERTIES_TABLE_NAME,
219
			array_map(
220
				function ( PropertyId $identifierPropertyId ) use ( $dumpId ) {
221
					return array(
222
						'dump_id' => $dumpId,
223
						'identifier_pid' => $identifierPropertyId->getSerialization()
224
					);
225
				},
226
				$dumpMetaInformation->getIdentifierPropertyIds()
227
			)
228
		);
229
	}
230
231
	/**
232
	 * @param Database $db
233
	 * @param DumpMetaInformation $dumpMetaInformation
234
	 * @return array
235
	 */
236
	private function getDumpInformationFields( Database $db,  DumpMetaInformation $dumpMetaInformation ) {
237
		return array(
238
			'id' => $dumpMetaInformation->getDumpId(),
239
			'source_qid' => $dumpMetaInformation->getSourceItemId()->getSerialization(),
240
			'import_date' => $db->timestamp( $dumpMetaInformation->getImportDate() ),
241
			'language' => $dumpMetaInformation->getLanguageCode(),
242
			'source_url' => $dumpMetaInformation->getSourceUrl(),
243
			'size' => $dumpMetaInformation->getSize(),
244
			'license_qid' => $dumpMetaInformation->getLicenseItemId()->getSerialization()
245
		);
246
	}
247
248
}
249