Completed
Push — master ( 26f8aa...e860de )
by
unknown
02:28
created

SqlDumpMetaInformationRepo::getSourceItemIds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
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 DatabaseBase;
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 id from database.
26
	 *
27
	 * @param $dumpId
28
	 * @return DumpMetaInformation
29
	 * @throws InvalidArgumentException
30
	 */
31
	public function getWithId( $dumpId ) {
32
		if ( !is_string( $dumpId ) ) {
33
			throw new InvalidArgumentException( '$dumpId must be string.' );
34
		}
35
36
		$dumpMetaInformation = $this->getWithIds( array( $dumpId ) );
37
38
		return reset( $dumpMetaInformation );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression reset($dumpMetaInformation); of type WikibaseQuality\External...mpMetaInformation|false adds false to the return on line 38 which is incompatible with the return type declared by the interface WikibaseQuality\External...mationLookup::getWithId of type WikibaseQuality\External...ion\DumpMetaInformation. It seems like you forgot to handle an error condition.
Loading history...
39
	}
40
41
	/**
42
	 * Gets DumpMetaInformation for specific dump ids from database
43
	 * Returns array in the form 'dumpId' => DumpMetaInformation
44
	 *
45
	 * @param string[] $dumpIds
46
	 * @return DumpMetaInformation[]
47
	 * @throws InvalidArgumentException
48
	 */
49
	public function getWithIds( array $dumpIds ) {
50
		foreach ( $dumpIds as $dumpId ) {
51
			if ( !is_string( $dumpId ) ) {
52
				throw new InvalidArgumentException( '$dumpIds must contain only strings.' );
53
			}
54
		}
55
56
		if( count( $dumpIds ) > 0 ) {
57
			$db = wfGetDB( DB_SLAVE );
58
			$result = $db->select(
59
				array(
60
					self::META_TABLE_NAME,
61
					self::IDENTIFIER_PROPERTIES_TABLE_NAME
62
				),
63
				'*',
64
				array(
65
					'id' => $dumpIds
66
				),
67
				__METHOD__,
68
				array(),
69
				array(
70
					self::IDENTIFIER_PROPERTIES_TABLE_NAME => array(
71
						'LEFT JOIN',
72
						'dump_id = id'
73
					)
74
				)
75
			);
76
77
			return $this->buildDumpMetaInformationFromResult( $result );
78
		}
79
80
		return array();
81
	}
82
83
	/**
84
	 * Gets DumpMetaInformation for specific identifier properties from database
85
	 * Returns array in the form 'dumpId' => DumpMetaInformation
86
	 *
87
	 * @param PropertyId[] $identifierPropertyIds
88
	 *
89
	 * @throws InvalidArgumentException
90
	 * @return DumpMetaInformation[]
91
	 */
92
	public function getWithIdentifierProperties( array $identifierPropertyIds ) {
93
		foreach ( $identifierPropertyIds as $propertyId ) {
94
			if ( !( $propertyId instanceof PropertyId ) ) {
95
				throw new InvalidArgumentException( '$identifierProperties must contain only PropertyIds.' );
96
			}
97
		}
98
99
		if( count( $identifierPropertyIds ) > 0 ) {
100
			$db = wfGetDB( DB_SLAVE );
101
			$identifierPropertyIds = array_map(
102
				function( PropertyId $id ) {
103
					return $id->getSerialization();
104
				},
105
				$identifierPropertyIds
106
			);
107
			$result = $db->select(
108
				self::IDENTIFIER_PROPERTIES_TABLE_NAME,
109
				'dump_id',
110
				array(
111
					'identifier_pid' => $identifierPropertyIds
112
				)
113
			);
114
			$dumpIds = array();
115
			foreach ( $result as $row ) {
116
				if ( !in_array( $row->dump_id, $dumpIds ) ) {
117
					$dumpIds[] = $row->dump_id;
118
				}
119
			}
120
121
			return $this->getWithIds( $dumpIds );
122
		}
123
124
		return array();
125
	}
126
127
	/**
128
	 * Gets id of item that represents the data source for each dump.
129
	 *
130
	 * @return ItemId[]
131
	 */
132
	public function getSourceItemIds() {
133
		$db = wfGetDB( DB_SLAVE );
134
		$result = $db->selectFieldValues(
135
			self::META_TABLE_NAME,
136
			'source_qid',
137
			array(),
138
			__METHOD__,
139
			'DISTINCT'
140
		);
141
142
		$sourceItemIds = $result; // TODO: Parse as ItemId, when ItemIds are used in violation table
143
		/*$sourceItemIds = array();
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
144
		foreach ( $result as $itemId ) {
145
			$sourceItemIds = new ItemId( $itemId );
146
		}*/
147
148
		return $sourceItemIds;
149
	}
150
151
	/**
152
	 * Gets all DumpMetaInformation from database
153
	 * Returns array in the form 'dumpId' => DumpMetaInformation
154
	 *
155
	 * @return DumpMetaInformation[]
156
	 */
157
	public function getAll() {
158
		$db = wfGetDB( DB_SLAVE );
159
		$result = $db->select(
160
			array(
161
				self::META_TABLE_NAME,
162
				self::IDENTIFIER_PROPERTIES_TABLE_NAME
163
			),
164
			'*',
165
			array(),
166
			__METHOD__,
167
			array(),
168
			array(
169
				self::IDENTIFIER_PROPERTIES_TABLE_NAME => array(
170
					'LEFT JOIN',
171
					'dump_id = id'
172
				)
173
			)
174
		);
175
176
		return $this->buildDumpMetaInformationFromResult( $result );
177
	}
178
179
	/**
180
	 * @param ResultWrapper $result
181
	 * @return null|array
182
	 * @throws UnexpectedValueException
183
	 */
184
	private function buildDumpMetaInformationFromResult( ResultWrapper $result ) {
185
		$aggregatedRows = array();
186
		foreach ( $result as $row ) {
187
			if ( array_key_exists( $row->id, $aggregatedRows ) ) {
188
				$propertyId = new PropertyId( $row->identifier_pid );
189
				$aggregatedRows[$row->id]->identifier_pid[] = $propertyId;
190
			} else {
191
				if ( $row->identifier_pid !== null ) {
192
					$propertyId = new PropertyId( $row->identifier_pid );
193
					$row->identifier_pid = array( $propertyId );
194
				}
195
				$aggregatedRows[$row->id] = $row;
196
			}
197
		}
198
199
		$dumpMetaInformation = array();
200
		foreach ( $aggregatedRows as $row ) {
201
			$dumpId = $row->id;
202
			$sourceItemId = new ItemId( $row->source_qid );
203
			$importDate = wfTimestamp( TS_MW, $row->import_date );
204
			$language = $row->language;
205
			$sourceUrl = $row->source_url;
206
			$size = (int)$row->size;
207
			$licenseItemId = new ItemId( $row->license_qid );
208
			$identifierPropertyIds = $row->identifier_pid;
209
210
			$dumpMetaInformation[$row->dump_id] =
211
				new DumpMetaInformation( $dumpId,
212
					$sourceItemId,
213
					$identifierPropertyIds,
214
					$importDate,
215
					$language,
216
					$sourceUrl,
217
					$size,
218
					$licenseItemId
219
				);
220
		}
221
222
		return $dumpMetaInformation;
223
	}
224
225
	/**
226
	 * Inserts or updates given dump meta information to database
227
	 *
228
	 * @param DumpMetaInformation $dumpMetaInformation
229
	 */
230
	public function save( DumpMetaInformation $dumpMetaInformation ) {
231
		$db = wfGetDB( DB_SLAVE );
232
		$dumpId = $dumpMetaInformation->getDumpId();
233
		$accumulator = $this->getDumpInformationFields( $db, $dumpMetaInformation );
234
235
		$existing = $db->selectRow(
236
			self::META_TABLE_NAME,
237
			array( 'id' ),
238
			array( 'id' => $dumpId )
239
		);
240
241
		if ( $existing ) {
242
			$db->update(
243
				self::META_TABLE_NAME,
244
				$accumulator,
245
				array( 'id' => $dumpId )
246
			);
247
		} else {
248
			$db->insert(
249
				self::META_TABLE_NAME,
250
				$accumulator
251
			);
252
		}
253
254
		$db->delete(
255
			self::IDENTIFIER_PROPERTIES_TABLE_NAME,
256
			array( 'dump_id' => $dumpId )
257
		);
258
		$db->insert(
259
			self::IDENTIFIER_PROPERTIES_TABLE_NAME,
260
			array_map(
261
				function ( PropertyId $identifierPropertyId ) use ( $dumpId ) {
262
					return array(
263
						'dump_id' => $dumpId,
264
						'identifier_pid' => $identifierPropertyId->getSerialization()
265
					);
266
				},
267
				$dumpMetaInformation->getIdentifierPropertyIds()
268
			)
269
		);
270
	}
271
272
	/**
273
	 * @param DumpMetaInformation $dumpMetaInformation *
274
	 * @return array
275
	 */
276
	private function getDumpInformationFields( DatabaseBase $db,  DumpMetaInformation $dumpMetaInformation ) {
277
		return array(
278
			'id' => $dumpMetaInformation->getDumpId(),
279
			'source_qid' => $dumpMetaInformation->getSourceItemId()->getSerialization(),
280
			'import_date' => $db->timestamp( $dumpMetaInformation->getImportDate() ),
281
			'language' => $dumpMetaInformation->getLanguageCode(),
282
			'source_url' => $dumpMetaInformation->getSourceUrl(),
283
			'size' => $dumpMetaInformation->getSize(),
284
			'license_qid' => $dumpMetaInformation->getLicenseItemId()->getSerialization()
285
		);
286
	}
287
288
}
289