Completed
Push — master ( 1dc4ce...b37983 )
by
unknown
21s
created
src/ConstraintCheck/Helper/SparqlHelper.php 1 patch
Spacing   +184 added lines, -187 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3
-declare( strict_types = 1 );
3
+declare(strict_types=1);
4 4
 
5 5
 namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Helper;
6 6
 
@@ -146,73 +146,73 @@  discard block
 block discarded – undo
146 146
 		$this->defaultUserAgent = $defaultUserAgent;
147 147
 		$this->requestFactory = $requestFactory;
148 148
 		$this->entityPrefixes = [];
149
-		foreach ( $rdfVocabulary->entityNamespaceNames as $namespaceName ) {
150
-			$this->entityPrefixes[] = $rdfVocabulary->getNamespaceURI( $namespaceName );
149
+		foreach ($rdfVocabulary->entityNamespaceNames as $namespaceName) {
150
+			$this->entityPrefixes[] = $rdfVocabulary->getNamespaceURI($namespaceName);
151 151
 		}
152 152
 
153
-		$this->primaryEndpoint = $config->get( 'WBQualityConstraintsSparqlEndpoint' );
154
-		$this->additionalEndpoints = $config->get( 'WBQualityConstraintsAdditionalSparqlEndpoints' ) ?: [];
155
-		$this->maxQueryTimeMillis = $config->get( 'WBQualityConstraintsSparqlMaxMillis' );
156
-		$this->subclassOfId = $config->get( 'WBQualityConstraintsSubclassOfId' );
157
-		$this->cacheMapSize = $config->get( 'WBQualityConstraintsFormatCacheMapSize' );
153
+		$this->primaryEndpoint = $config->get('WBQualityConstraintsSparqlEndpoint');
154
+		$this->additionalEndpoints = $config->get('WBQualityConstraintsAdditionalSparqlEndpoints') ?: [];
155
+		$this->maxQueryTimeMillis = $config->get('WBQualityConstraintsSparqlMaxMillis');
156
+		$this->subclassOfId = $config->get('WBQualityConstraintsSubclassOfId');
157
+		$this->cacheMapSize = $config->get('WBQualityConstraintsFormatCacheMapSize');
158 158
 		$this->timeoutExceptionClasses = $config->get(
159 159
 			'WBQualityConstraintsSparqlTimeoutExceptionClasses'
160 160
 		);
161 161
 		$this->sparqlHasWikibaseSupport = $config->get(
162 162
 			'WBQualityConstraintsSparqlHasWikibaseSupport'
163 163
 		);
164
-		$this->sparqlThrottlingFallbackDuration = (int)$config->get(
164
+		$this->sparqlThrottlingFallbackDuration = (int) $config->get(
165 165
 			'WBQualityConstraintsSparqlThrottlingFallbackDuration'
166 166
 		);
167 167
 
168
-		$this->prefixes = $this->getQueryPrefixes( $rdfVocabulary );
168
+		$this->prefixes = $this->getQueryPrefixes($rdfVocabulary);
169 169
 	}
170 170
 
171
-	private function getQueryPrefixes( RdfVocabulary $rdfVocabulary ): string {
171
+	private function getQueryPrefixes(RdfVocabulary $rdfVocabulary): string {
172 172
 		// TODO: it would probably be smarter that RdfVocabulary exposed these prefixes somehow
173 173
 		$prefixes = '';
174
-		foreach ( $rdfVocabulary->entityNamespaceNames as $sourceName => $namespaceName ) {
174
+		foreach ($rdfVocabulary->entityNamespaceNames as $sourceName => $namespaceName) {
175 175
 			$prefixes .= <<<END
176
-PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI( $namespaceName )}>\n
176
+PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI($namespaceName)}>\n
177 177
 END;
178 178
 		}
179 179
 		$prefixes .= <<<END
180
-PREFIX wds: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_STATEMENT )}>
181
-PREFIX wdv: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_VALUE )}>\n
180
+PREFIX wds: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_STATEMENT)}>
181
+PREFIX wdv: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_VALUE)}>\n
182 182
 END;
183 183
 
184
-		foreach ( $rdfVocabulary->propertyNamespaceNames as $sourceName => $sourceNamespaces ) {
184
+		foreach ($rdfVocabulary->propertyNamespaceNames as $sourceName => $sourceNamespaces) {
185 185
 			$namespaceName = $sourceNamespaces[RdfVocabulary::NSP_DIRECT_CLAIM];
186 186
 			$prefixes .= <<<END
187
-PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI( $namespaceName )}>\n
187
+PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI($namespaceName)}>\n
188 188
 END;
189 189
 			$namespaceName = $sourceNamespaces[RdfVocabulary::NSP_CLAIM];
190 190
 			$prefixes .= <<<END
191
-PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI( $namespaceName )}>\n
191
+PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI($namespaceName)}>\n
192 192
 END;
193 193
 			$namespaceName = $sourceNamespaces[RdfVocabulary::NSP_CLAIM_STATEMENT];
194 194
 			$prefixes .= <<<END
195
-PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI( $namespaceName )}>\n
195
+PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI($namespaceName)}>\n
196 196
 END;
197 197
 			$namespaceName = $sourceNamespaces[RdfVocabulary::NSP_QUALIFIER];
198 198
 			$prefixes .= <<<END
199
-PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI( $namespaceName )}>\n
199
+PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI($namespaceName)}>\n
200 200
 END;
201 201
 			$namespaceName = $sourceNamespaces[RdfVocabulary::NSP_QUALIFIER_VALUE];
202 202
 			$prefixes .= <<<END
203
-PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI( $namespaceName )}>\n
203
+PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI($namespaceName)}>\n
204 204
 END;
205 205
 			$namespaceName = $sourceNamespaces[RdfVocabulary::NSP_REFERENCE];
206 206
 			$prefixes .= <<<END
207
-PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI( $namespaceName )}>\n
207
+PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI($namespaceName)}>\n
208 208
 END;
209 209
 			$namespaceName = $sourceNamespaces[RdfVocabulary::NSP_REFERENCE_VALUE];
210 210
 			$prefixes .= <<<END
211
-PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI( $namespaceName )}>\n
211
+PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI($namespaceName)}>\n
212 212
 END;
213 213
 		}
214 214
 		$prefixes .= <<<END
215
-PREFIX wikibase: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_ONTOLOGY )}>\n
215
+PREFIX wikibase: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_ONTOLOGY)}>\n
216 216
 END;
217 217
 		return $prefixes;
218 218
 	}
@@ -224,21 +224,20 @@  discard block
 block discarded – undo
224 224
 	 * @return CachedBool
225 225
 	 * @throws SparqlHelperException if the query times out or some other error occurs
226 226
 	 */
227
-	public function hasType( string $id, array $classes ): CachedBool {
227
+	public function hasType(string $id, array $classes): CachedBool {
228 228
 		// TODO hint:gearing is a workaround for T168973 and can hopefully be removed eventually
229 229
 		$gearingHint = $this->sparqlHasWikibaseSupport ?
230
-			' hint:Prior hint:gearing "forward".' :
231
-			'';
230
+			' hint:Prior hint:gearing "forward".' : '';
232 231
 
233 232
 		$metadatas = [];
234 233
 
235
-		foreach ( array_chunk( $classes, 20 ) as $classesChunk ) {
236
-			$classesValues = implode( ' ', array_map(
237
-				static function ( $class ) {
238
-					return 'wd:' . $class;
234
+		foreach (array_chunk($classes, 20) as $classesChunk) {
235
+			$classesValues = implode(' ', array_map(
236
+				static function($class) {
237
+					return 'wd:'.$class;
239 238
 				},
240 239
 				$classesChunk
241
-			) );
240
+			));
242 241
 
243 242
 			$query = <<<EOF
244 243
 ASK {
@@ -248,19 +247,19 @@  discard block
 block discarded – undo
248 247
 }
249 248
 EOF;
250 249
 
251
-			$result = $this->runQuery( $query, $this->primaryEndpoint );
250
+			$result = $this->runQuery($query, $this->primaryEndpoint);
252 251
 			$metadatas[] = $result->getMetadata();
253
-			if ( $result->getArray()['boolean'] ) {
252
+			if ($result->getArray()['boolean']) {
254 253
 				return new CachedBool(
255 254
 					true,
256
-					Metadata::merge( $metadatas )
255
+					Metadata::merge($metadatas)
257 256
 				);
258 257
 			}
259 258
 		}
260 259
 
261 260
 		return new CachedBool(
262 261
 			false,
263
-			Metadata::merge( $metadatas )
262
+			Metadata::merge($metadatas)
264 263
 		);
265 264
 	}
266 265
 
@@ -268,7 +267,7 @@  discard block
 block discarded – undo
268 267
 	 * Helper function used by findEntitiesWithSameStatement to filter
269 268
 	 * out entities with different qualifiers or no qualifier value.
270 269
 	 */
271
-	private function nestedSeparatorFilter( PropertyId $separator ): string {
270
+	private function nestedSeparatorFilter(PropertyId $separator): string {
272 271
 		$filter = <<<EOF
273 272
   MINUS {
274 273
     ?statement pq:$separator ?qualifier.
@@ -305,14 +304,14 @@  discard block
 block discarded – undo
305 304
 	 * @return CachedEntityIds
306 305
 	 * @throws SparqlHelperException if the query times out or some other error occurs
307 306
 	 */
308
-	public function findEntitiesWithSameStatement( Statement $statement, array $separators ): CachedEntityIds {
307
+	public function findEntitiesWithSameStatement(Statement $statement, array $separators): CachedEntityIds {
309 308
 		$pid = $statement->getPropertyId()->getSerialization();
310 309
 		$guid = $statement->getGuid();
311 310
 		'@phan-var string $guid'; // statement must have a non-null GUID
312
-		$guidForRdf = str_replace( '$', '-', $guid );
311
+		$guidForRdf = str_replace('$', '-', $guid);
313 312
 
314
-		$separatorFilters = array_map( [ $this, 'nestedSeparatorFilter' ], $separators );
315
-		$finalSeparatorFilter = implode( "\n", $separatorFilters );
313
+		$separatorFilters = array_map([$this, 'nestedSeparatorFilter'], $separators);
314
+		$finalSeparatorFilter = implode("\n", $separatorFilters);
316 315
 
317 316
 		$query = <<<EOF
318 317
 SELECT DISTINCT ?otherEntity WHERE {
@@ -330,12 +329,12 @@  discard block
 block discarded – undo
330 329
 LIMIT 10
331 330
 EOF;
332 331
 
333
-		$results = [ $this->runQuery( $query, $this->primaryEndpoint ) ];
334
-		foreach ( $this->additionalEndpoints as $endpoint ) {
335
-			$results[] = $this->runQuery( $query, $endpoint );
332
+		$results = [$this->runQuery($query, $this->primaryEndpoint)];
333
+		foreach ($this->additionalEndpoints as $endpoint) {
334
+			$results[] = $this->runQuery($query, $endpoint);
336 335
 		}
337 336
 
338
-		return $this->getOtherEntities( $results );
337
+		return $this->getOtherEntities($results);
339 338
 	}
340 339
 
341 340
 	/**
@@ -360,16 +359,15 @@  discard block
 block discarded – undo
360 359
 		$dataType = $this->propertyDataTypeLookup->getDataTypeIdForProperty(
361 360
 			$snak->getPropertyId()
362 361
 		);
363
-		[ $value, $isFullValue ] = $this->getRdfLiteral( $dataType, $dataValue );
364
-		if ( $isFullValue ) {
362
+		[$value, $isFullValue] = $this->getRdfLiteral($dataType, $dataValue);
363
+		if ($isFullValue) {
365 364
 			$prefix .= 'v';
366 365
 		}
367 366
 		$path = $type === Context::TYPE_QUALIFIER ?
368
-			"$prefix:$pid" :
369
-			"prov:wasDerivedFrom/$prefix:$pid";
367
+			"$prefix:$pid" : "prov:wasDerivedFrom/$prefix:$pid";
370 368
 
371 369
 		$deprecatedFilter = '';
372
-		if ( $ignoreDeprecatedStatements ) {
370
+		if ($ignoreDeprecatedStatements) {
373 371
 			$deprecatedFilter = <<< EOF
374 372
   MINUS { ?otherStatement wikibase:rank wikibase:DeprecatedRank. }
375 373
 EOF;
@@ -389,19 +387,19 @@  discard block
 block discarded – undo
389 387
 LIMIT 10
390 388
 EOF;
391 389
 
392
-		$results = [ $this->runQuery( $query, $this->primaryEndpoint ) ];
393
-		foreach ( $this->additionalEndpoints as $endpoint ) {
394
-			$results[] = $this->runQuery( $query, $endpoint );
390
+		$results = [$this->runQuery($query, $this->primaryEndpoint)];
391
+		foreach ($this->additionalEndpoints as $endpoint) {
392
+			$results[] = $this->runQuery($query, $endpoint);
395 393
 		}
396 394
 
397
-		return $this->getOtherEntities( $results );
395
+		return $this->getOtherEntities($results);
398 396
 	}
399 397
 
400 398
 	/**
401 399
 	 * Return SPARQL code for a string literal with $text as content.
402 400
 	 */
403
-	private function stringLiteral( string $text ): string {
404
-		return '"' . strtr( $text, [ '"' => '\\"', '\\' => '\\\\' ] ) . '"';
401
+	private function stringLiteral(string $text): string {
402
+		return '"'.strtr($text, ['"' => '\\"', '\\' => '\\\\']).'"';
405 403
 	}
406 404
 
407 405
 	/**
@@ -411,26 +409,26 @@  discard block
 block discarded – undo
411 409
 	 *
412 410
 	 * @return CachedEntityIds
413 411
 	 */
414
-	private function getOtherEntities( array $results ): CachedEntityIds {
412
+	private function getOtherEntities(array $results): CachedEntityIds {
415 413
 		$allResultBindings = [];
416 414
 		$metadatas = [];
417 415
 
418
-		foreach ( $results as $result ) {
416
+		foreach ($results as $result) {
419 417
 			$metadatas[] = $result->getMetadata();
420
-			$allResultBindings = array_merge( $allResultBindings, $result->getArray()['results']['bindings'] );
418
+			$allResultBindings = array_merge($allResultBindings, $result->getArray()['results']['bindings']);
421 419
 		}
422 420
 
423 421
 		$entityIds = array_map(
424
-			function ( $resultBindings ) {
422
+			function($resultBindings) {
425 423
 				$entityIRI = $resultBindings['otherEntity']['value'];
426
-				foreach ( $this->entityPrefixes as $entityPrefix ) {
427
-					$entityPrefixLength = strlen( $entityPrefix );
428
-					if ( substr( $entityIRI, 0, $entityPrefixLength ) === $entityPrefix ) {
424
+				foreach ($this->entityPrefixes as $entityPrefix) {
425
+					$entityPrefixLength = strlen($entityPrefix);
426
+					if (substr($entityIRI, 0, $entityPrefixLength) === $entityPrefix) {
429 427
 						try {
430 428
 							return $this->entityIdParser->parse(
431
-								substr( $entityIRI, $entityPrefixLength )
429
+								substr($entityIRI, $entityPrefixLength)
432 430
 							);
433
-						} catch ( EntityIdParsingException $e ) {
431
+						} catch (EntityIdParsingException $e) {
434 432
 							// fall through
435 433
 						}
436 434
 					}
@@ -444,8 +442,8 @@  discard block
 block discarded – undo
444 442
 		);
445 443
 
446 444
 		return new CachedEntityIds(
447
-			array_values( array_filter( array_unique( $entityIds ) ) ),
448
-			Metadata::merge( $metadatas )
445
+			array_values(array_filter(array_unique($entityIds))),
446
+			Metadata::merge($metadatas)
449 447
 		);
450 448
 	}
451 449
 
@@ -456,50 +454,50 @@  discard block
 block discarded – undo
456 454
 	 * @return array the literal or IRI as a string in SPARQL syntax,
457 455
 	 * and a boolean indicating whether it refers to a full value node or not
458 456
 	 */
459
-	private function getRdfLiteral( string $dataType, DataValue $dataValue ): array {
460
-		switch ( $dataType ) {
457
+	private function getRdfLiteral(string $dataType, DataValue $dataValue): array {
458
+		switch ($dataType) {
461 459
 			case 'string':
462 460
 			case 'external-id':
463
-				return [ $this->stringLiteral( $dataValue->getValue() ), false ];
461
+				return [$this->stringLiteral($dataValue->getValue()), false];
464 462
 			case 'commonsMedia':
465
-				$url = $this->rdfVocabulary->getMediaFileURI( $dataValue->getValue() );
466
-				return [ '<' . $url . '>', false ];
463
+				$url = $this->rdfVocabulary->getMediaFileURI($dataValue->getValue());
464
+				return ['<'.$url.'>', false];
467 465
 			case 'geo-shape':
468
-				$url = $this->rdfVocabulary->getGeoShapeURI( $dataValue->getValue() );
469
-				return [ '<' . $url . '>', false ];
466
+				$url = $this->rdfVocabulary->getGeoShapeURI($dataValue->getValue());
467
+				return ['<'.$url.'>', false];
470 468
 			case 'tabular-data':
471
-				$url = $this->rdfVocabulary->getTabularDataURI( $dataValue->getValue() );
472
-				return [ '<' . $url . '>', false ];
469
+				$url = $this->rdfVocabulary->getTabularDataURI($dataValue->getValue());
470
+				return ['<'.$url.'>', false];
473 471
 			case 'url':
474 472
 				$url = $dataValue->getValue();
475
-				if ( !preg_match( '/^[^<>"{}\\\\|^`\\x00-\\x20]*$/D', $url ) ) {
473
+				if (!preg_match('/^[^<>"{}\\\\|^`\\x00-\\x20]*$/D', $url)) {
476 474
 					// not a valid URL for SPARQL (see SPARQL spec, production 139 IRIREF)
477 475
 					// such an URL should never reach us, so just throw
478
-					throw new InvalidArgumentException( 'invalid URL: ' . $url );
476
+					throw new InvalidArgumentException('invalid URL: '.$url);
479 477
 				}
480
-				return [ '<' . $url . '>', false ];
478
+				return ['<'.$url.'>', false];
481 479
 			case 'wikibase-item':
482 480
 			case 'wikibase-property':
483 481
 				/** @var EntityIdValue $dataValue */
484 482
 				'@phan-var EntityIdValue $dataValue';
485
-				return [ 'wd:' . $dataValue->getEntityId()->getSerialization(), false ];
483
+				return ['wd:'.$dataValue->getEntityId()->getSerialization(), false];
486 484
 			case 'monolingualtext':
487 485
 				/** @var MonolingualTextValue $dataValue */
488 486
 				'@phan-var MonolingualTextValue $dataValue';
489 487
 				$lang = $dataValue->getLanguageCode();
490
-				if ( !preg_match( '/^[a-zA-Z]+(-[a-zA-Z0-9]+)*$/D', $lang ) ) {
488
+				if (!preg_match('/^[a-zA-Z]+(-[a-zA-Z0-9]+)*$/D', $lang)) {
491 489
 					// not a valid language tag for SPARQL (see SPARQL spec, production 145 LANGTAG)
492 490
 					// such a language tag should never reach us, so just throw
493
-					throw new InvalidArgumentException( 'invalid language tag: ' . $lang );
491
+					throw new InvalidArgumentException('invalid language tag: '.$lang);
494 492
 				}
495
-				return [ $this->stringLiteral( $dataValue->getText() ) . '@' . $lang, false ];
493
+				return [$this->stringLiteral($dataValue->getText()).'@'.$lang, false];
496 494
 			case 'globe-coordinate':
497 495
 			case 'quantity':
498 496
 			case 'time':
499 497
 				// @phan-suppress-next-line PhanUndeclaredMethod
500
-				return [ 'wdv:' . $dataValue->getHash(), true ];
498
+				return ['wdv:'.$dataValue->getHash(), true];
501 499
 			default:
502
-				throw new InvalidArgumentException( 'unknown data type: ' . $dataType );
500
+				throw new InvalidArgumentException('unknown data type: '.$dataType);
503 501
 		}
504 502
 	}
505 503
 	// phpcs:enable
@@ -508,43 +506,43 @@  discard block
 block discarded – undo
508 506
 	 * @throws SparqlHelperException if the query times out or some other error occurs
509 507
 	 * @throws ConstraintParameterException if the $regex is invalid
510 508
 	 */
511
-	public function matchesRegularExpression( string $text, string $regex ): bool {
509
+	public function matchesRegularExpression(string $text, string $regex): bool {
512 510
 		// caching wrapper around matchesRegularExpressionWithSparql
513 511
 
514
-		$textHash = hash( 'sha256', $text );
512
+		$textHash = hash('sha256', $text);
515 513
 		$cacheKey = $this->cache->makeKey(
516 514
 			'WikibaseQualityConstraints', // extension
517 515
 			'regex', // action
518 516
 			'WDQS-Java', // regex flavor
519
-			hash( 'sha256', $regex )
517
+			hash('sha256', $regex)
520 518
 		);
521 519
 
522 520
 		$cacheMapArray = $this->cache->getWithSetCallback(
523 521
 			$cacheKey,
524 522
 			WANObjectCache::TTL_DAY,
525
-			function ( $cacheMapArray ) use ( $text, $regex, $textHash ) {
523
+			function($cacheMapArray) use ($text, $regex, $textHash) {
526 524
 				// Initialize the cache map if not set
527
-				if ( $cacheMapArray === false ) {
525
+				if ($cacheMapArray === false) {
528 526
 					$key = 'wikibase.quality.constraints.regex.cache.refresh.init';
529
-					$this->dataFactory->increment( $key );
527
+					$this->dataFactory->increment($key);
530 528
 					return [];
531 529
 				}
532 530
 
533 531
 				$key = 'wikibase.quality.constraints.regex.cache.refresh';
534
-				$this->dataFactory->increment( $key );
535
-				$cacheMap = MapCacheLRU::newFromArray( $cacheMapArray, $this->cacheMapSize );
536
-				if ( $cacheMap->has( $textHash ) ) {
532
+				$this->dataFactory->increment($key);
533
+				$cacheMap = MapCacheLRU::newFromArray($cacheMapArray, $this->cacheMapSize);
534
+				if ($cacheMap->has($textHash)) {
537 535
 					$key = 'wikibase.quality.constraints.regex.cache.refresh.hit';
538
-					$this->dataFactory->increment( $key );
539
-					$cacheMap->get( $textHash ); // ping cache
536
+					$this->dataFactory->increment($key);
537
+					$cacheMap->get($textHash); // ping cache
540 538
 				} else {
541 539
 					$key = 'wikibase.quality.constraints.regex.cache.refresh.miss';
542
-					$this->dataFactory->increment( $key );
540
+					$this->dataFactory->increment($key);
543 541
 					try {
544
-						$matches = $this->matchesRegularExpressionWithSparql( $text, $regex );
545
-					} catch ( ConstraintParameterException $e ) {
546
-						$matches = $this->serializeConstraintParameterException( $e );
547
-					} catch ( SparqlHelperException $e ) {
542
+						$matches = $this->matchesRegularExpressionWithSparql($text, $regex);
543
+					} catch (ConstraintParameterException $e) {
544
+						$matches = $this->serializeConstraintParameterException($e);
545
+					} catch (SparqlHelperException $e) {
548 546
 						// don’t cache this
549 547
 						return $cacheMap->toArray();
550 548
 					}
@@ -568,42 +566,42 @@  discard block
 block discarded – undo
568 566
 			]
569 567
 		);
570 568
 
571
-		if ( isset( $cacheMapArray[$textHash] ) ) {
569
+		if (isset($cacheMapArray[$textHash])) {
572 570
 			$key = 'wikibase.quality.constraints.regex.cache.hit';
573
-			$this->dataFactory->increment( $key );
571
+			$this->dataFactory->increment($key);
574 572
 			$matches = $cacheMapArray[$textHash];
575
-			if ( is_bool( $matches ) ) {
573
+			if (is_bool($matches)) {
576 574
 				return $matches;
577
-			} elseif ( is_array( $matches ) &&
578
-				$matches['type'] == ConstraintParameterException::class ) {
579
-				throw $this->deserializeConstraintParameterException( $matches );
575
+			} elseif (is_array($matches) &&
576
+				$matches['type'] == ConstraintParameterException::class) {
577
+				throw $this->deserializeConstraintParameterException($matches);
580 578
 			} else {
581 579
 				throw new UnexpectedValueException(
582
-					'Value of unknown type in object cache (' .
583
-					'cache key: ' . $cacheKey . ', ' .
584
-					'cache map key: ' . $textHash . ', ' .
585
-					'value type: ' . get_debug_type( $matches ) . ')'
580
+					'Value of unknown type in object cache ('.
581
+					'cache key: '.$cacheKey.', '.
582
+					'cache map key: '.$textHash.', '.
583
+					'value type: '.get_debug_type($matches).')'
586 584
 				);
587 585
 			}
588 586
 		} else {
589 587
 			$key = 'wikibase.quality.constraints.regex.cache.miss';
590
-			$this->dataFactory->increment( $key );
591
-			return $this->matchesRegularExpressionWithSparql( $text, $regex );
588
+			$this->dataFactory->increment($key);
589
+			return $this->matchesRegularExpressionWithSparql($text, $regex);
592 590
 		}
593 591
 	}
594 592
 
595
-	private function serializeConstraintParameterException( ConstraintParameterException $cpe ): array {
593
+	private function serializeConstraintParameterException(ConstraintParameterException $cpe): array {
596 594
 		return [
597 595
 			'type' => ConstraintParameterException::class,
598
-			'violationMessage' => $this->violationMessageSerializer->serialize( $cpe->getViolationMessage() ),
596
+			'violationMessage' => $this->violationMessageSerializer->serialize($cpe->getViolationMessage()),
599 597
 		];
600 598
 	}
601 599
 
602
-	private function deserializeConstraintParameterException( array $serialization ): ConstraintParameterException {
600
+	private function deserializeConstraintParameterException(array $serialization): ConstraintParameterException {
603 601
 		$message = $this->violationMessageDeserializer->deserialize(
604 602
 			$serialization['violationMessage']
605 603
 		);
606
-		return new ConstraintParameterException( $message );
604
+		return new ConstraintParameterException($message);
607 605
 	}
608 606
 
609 607
 	/**
@@ -613,25 +611,25 @@  discard block
 block discarded – undo
613 611
 	 * @throws SparqlHelperException if the query times out or some other error occurs
614 612
 	 * @throws ConstraintParameterException if the $regex is invalid
615 613
 	 */
616
-	public function matchesRegularExpressionWithSparql( string $text, string $regex ): bool {
617
-		$textStringLiteral = $this->stringLiteral( $text );
618
-		$regexStringLiteral = $this->stringLiteral( '^(?:' . $regex . ')$' );
614
+	public function matchesRegularExpressionWithSparql(string $text, string $regex): bool {
615
+		$textStringLiteral = $this->stringLiteral($text);
616
+		$regexStringLiteral = $this->stringLiteral('^(?:'.$regex.')$');
619 617
 
620 618
 		$query = <<<EOF
621 619
 SELECT (REGEX($textStringLiteral, $regexStringLiteral) AS ?matches) {}
622 620
 EOF;
623 621
 
624
-		$result = $this->runQuery( $query, $this->primaryEndpoint, false );
622
+		$result = $this->runQuery($query, $this->primaryEndpoint, false);
625 623
 
626 624
 		$vars = $result->getArray()['results']['bindings'][0];
627
-		if ( array_key_exists( 'matches', $vars ) ) {
625
+		if (array_key_exists('matches', $vars)) {
628 626
 			// true or false ⇒ regex okay, text matches or not
629 627
 			return $vars['matches']['value'] === 'true';
630 628
 		} else {
631 629
 			// empty result: regex broken
632 630
 			throw new ConstraintParameterException(
633
-				( new ViolationMessage( 'wbqc-violation-message-parameter-regex' ) )
634
-					->withInlineCode( $regex, Role::CONSTRAINT_PARAMETER_VALUE )
631
+				(new ViolationMessage('wbqc-violation-message-parameter-regex'))
632
+					->withInlineCode($regex, Role::CONSTRAINT_PARAMETER_VALUE)
635 633
 			);
636 634
 		}
637 635
 	}
@@ -639,14 +637,14 @@  discard block
 block discarded – undo
639 637
 	/**
640 638
 	 * Check whether the text content of an error response indicates a query timeout.
641 639
 	 */
642
-	public function isTimeout( string $responseContent ): bool {
643
-		$timeoutRegex = implode( '|', array_map(
644
-			static function ( $fqn ) {
645
-				return preg_quote( $fqn, '/' );
640
+	public function isTimeout(string $responseContent): bool {
641
+		$timeoutRegex = implode('|', array_map(
642
+			static function($fqn) {
643
+				return preg_quote($fqn, '/');
646 644
 			},
647 645
 			$this->timeoutExceptionClasses
648
-		) );
649
-		return (bool)preg_match( '/' . $timeoutRegex . '/', $responseContent );
646
+		));
647
+		return (bool) preg_match('/'.$timeoutRegex.'/', $responseContent);
650 648
 	}
651 649
 
652 650
 	/**
@@ -658,17 +656,17 @@  discard block
 block discarded – undo
658 656
 	 * @return int|boolean the max-age (in seconds)
659 657
 	 * or a plain boolean if no max-age can be determined
660 658
 	 */
661
-	public function getCacheMaxAge( array $responseHeaders ) {
659
+	public function getCacheMaxAge(array $responseHeaders) {
662 660
 		if (
663
-			array_key_exists( 'x-cache-status', $responseHeaders ) &&
664
-			preg_match( '/^hit(?:-.*)?$/', $responseHeaders['x-cache-status'][0] )
661
+			array_key_exists('x-cache-status', $responseHeaders) &&
662
+			preg_match('/^hit(?:-.*)?$/', $responseHeaders['x-cache-status'][0])
665 663
 		) {
666 664
 			$maxage = [];
667 665
 			if (
668
-				array_key_exists( 'cache-control', $responseHeaders ) &&
669
-				preg_match( '/\bmax-age=(\d+)\b/', $responseHeaders['cache-control'][0], $maxage )
666
+				array_key_exists('cache-control', $responseHeaders) &&
667
+				preg_match('/\bmax-age=(\d+)\b/', $responseHeaders['cache-control'][0], $maxage)
670 668
 			) {
671
-				return intval( $maxage[1] );
669
+				return intval($maxage[1]);
672 670
 			} else {
673 671
 				return true;
674 672
 			}
@@ -689,34 +687,34 @@  discard block
 block discarded – undo
689 687
 	 * or SparlHelper::EMPTY_RETRY_AFTER if there is an empty Retry-After
690 688
 	 * or SparlHelper::INVALID_RETRY_AFTER if there is something wrong with the format
691 689
 	 */
692
-	public function getThrottling( MWHttpRequest $request ) {
693
-		$retryAfterValue = $request->getResponseHeader( 'Retry-After' );
694
-		if ( $retryAfterValue === null ) {
690
+	public function getThrottling(MWHttpRequest $request) {
691
+		$retryAfterValue = $request->getResponseHeader('Retry-After');
692
+		if ($retryAfterValue === null) {
695 693
 			return self::NO_RETRY_AFTER;
696 694
 		}
697 695
 
698
-		$trimmedRetryAfterValue = trim( $retryAfterValue );
699
-		if ( $trimmedRetryAfterValue === '' ) {
696
+		$trimmedRetryAfterValue = trim($retryAfterValue);
697
+		if ($trimmedRetryAfterValue === '') {
700 698
 			return self::EMPTY_RETRY_AFTER;
701 699
 		}
702 700
 
703
-		if ( is_numeric( $trimmedRetryAfterValue ) ) {
704
-			$delaySeconds = (int)$trimmedRetryAfterValue;
705
-			if ( $delaySeconds >= 0 ) {
706
-				return $this->getTimestampInFuture( new DateInterval( 'PT' . $delaySeconds . 'S' ) );
701
+		if (is_numeric($trimmedRetryAfterValue)) {
702
+			$delaySeconds = (int) $trimmedRetryAfterValue;
703
+			if ($delaySeconds >= 0) {
704
+				return $this->getTimestampInFuture(new DateInterval('PT'.$delaySeconds.'S'));
707 705
 			}
708 706
 		} else {
709
-			$return = strtotime( $trimmedRetryAfterValue );
710
-			if ( $return !== false ) {
711
-				return new ConvertibleTimestamp( $return );
707
+			$return = strtotime($trimmedRetryAfterValue);
708
+			if ($return !== false) {
709
+				return new ConvertibleTimestamp($return);
712 710
 			}
713 711
 		}
714 712
 		return self::INVALID_RETRY_AFTER;
715 713
 	}
716 714
 
717
-	private function getTimestampInFuture( DateInterval $delta ) {
715
+	private function getTimestampInFuture(DateInterval $delta) {
718 716
 		$now = new ConvertibleTimestamp();
719
-		return new ConvertibleTimestamp( $now->timestamp->add( $delta ) );
717
+		return new ConvertibleTimestamp($now->timestamp->add($delta));
720 718
 	}
721 719
 
722 720
 	/**
@@ -731,64 +729,63 @@  discard block
 block discarded – undo
731 729
 	 *
732 730
 	 * @throws SparqlHelperException if the query times out or some other error occurs
733 731
 	 */
734
-	protected function runQuery( string $query, string $endpoint, bool $needsPrefixes = true ): CachedQueryResults {
735
-		if ( $this->throttlingLock->isLocked( self::EXPIRY_LOCK_ID ) ) {
736
-			$this->dataFactory->increment( 'wikibase.quality.constraints.sparql.throttling' );
732
+	protected function runQuery(string $query, string $endpoint, bool $needsPrefixes = true): CachedQueryResults {
733
+		if ($this->throttlingLock->isLocked(self::EXPIRY_LOCK_ID)) {
734
+			$this->dataFactory->increment('wikibase.quality.constraints.sparql.throttling');
737 735
 			throw new TooManySparqlRequestsException();
738 736
 		}
739 737
 
740
-		if ( $this->sparqlHasWikibaseSupport ) {
738
+		if ($this->sparqlHasWikibaseSupport) {
741 739
 			$needsPrefixes = false;
742 740
 		}
743 741
 
744
-		if ( $needsPrefixes ) {
745
-			$query = $this->prefixes . $query;
742
+		if ($needsPrefixes) {
743
+			$query = $this->prefixes.$query;
746 744
 		}
747
-		$query = "#wbqc\n" . $query;
745
+		$query = "#wbqc\n".$query;
748 746
 
749
-		$url = $endpoint . '?' . http_build_query(
747
+		$url = $endpoint.'?'.http_build_query(
750 748
 			[
751 749
 				'query' => $query,
752 750
 				'format' => 'json',
753 751
 				'maxQueryTimeMillis' => $this->maxQueryTimeMillis,
754 752
 			],
755
-			'', ini_get( 'arg_separator.output' ),
753
+			'', ini_get('arg_separator.output'),
756 754
 			// encode spaces with %20, not +
757 755
 			PHP_QUERY_RFC3986
758 756
 		);
759 757
 
760 758
 		$options = [
761 759
 			'method' => 'GET',
762
-			'timeout' => (int)round( ( $this->maxQueryTimeMillis + 1000 ) / 1000 ),
760
+			'timeout' => (int) round(($this->maxQueryTimeMillis + 1000) / 1000),
763 761
 			'connectTimeout' => 'default',
764 762
 			'userAgent' => $this->defaultUserAgent,
765 763
 		];
766
-		$request = $this->requestFactory->create( $url, $options, __METHOD__ );
767
-		$startTime = microtime( true );
764
+		$request = $this->requestFactory->create($url, $options, __METHOD__);
765
+		$startTime = microtime(true);
768 766
 		$requestStatus = $request->execute();
769
-		$endTime = microtime( true );
767
+		$endTime = microtime(true);
770 768
 		$this->dataFactory->timing(
771 769
 			'wikibase.quality.constraints.sparql.timing',
772
-			( $endTime - $startTime ) * 1000
770
+			($endTime - $startTime) * 1000
773 771
 		);
774 772
 
775
-		$this->guardAgainstTooManyRequestsError( $request );
773
+		$this->guardAgainstTooManyRequestsError($request);
776 774
 
777
-		$maxAge = $this->getCacheMaxAge( $request->getResponseHeaders() );
778
-		if ( $maxAge ) {
779
-			$this->dataFactory->increment( 'wikibase.quality.constraints.sparql.cached' );
775
+		$maxAge = $this->getCacheMaxAge($request->getResponseHeaders());
776
+		if ($maxAge) {
777
+			$this->dataFactory->increment('wikibase.quality.constraints.sparql.cached');
780 778
 		}
781 779
 
782
-		if ( $requestStatus->isOK() ) {
780
+		if ($requestStatus->isOK()) {
783 781
 			$json = $request->getContent();
784
-			$jsonStatus = FormatJson::parse( $json, FormatJson::FORCE_ASSOC );
785
-			if ( $jsonStatus->isOK() ) {
782
+			$jsonStatus = FormatJson::parse($json, FormatJson::FORCE_ASSOC);
783
+			if ($jsonStatus->isOK()) {
786 784
 				return new CachedQueryResults(
787 785
 					$jsonStatus->getValue(),
788 786
 					Metadata::ofCachingMetadata(
789 787
 						$maxAge ?
790
-							CachingMetadata::ofMaximumAgeInSeconds( $maxAge ) :
791
-							CachingMetadata::fresh()
788
+							CachingMetadata::ofMaximumAgeInSeconds($maxAge) : CachingMetadata::fresh()
792 789
 					)
793 790
 				);
794 791
 			} else {
@@ -805,9 +802,9 @@  discard block
 block discarded – undo
805 802
 			// fall through to general error handling
806 803
 		}
807 804
 
808
-		$this->dataFactory->increment( 'wikibase.quality.constraints.sparql.error' );
805
+		$this->dataFactory->increment('wikibase.quality.constraints.sparql.error');
809 806
 
810
-		if ( $this->isTimeout( $request->getContent() ) ) {
807
+		if ($this->isTimeout($request->getContent())) {
811 808
 			$this->dataFactory->increment(
812 809
 				'wikibase.quality.constraints.sparql.error.timeout'
813 810
 			);
@@ -822,29 +819,29 @@  discard block
 block discarded – undo
822 819
 	 * @param MWHttpRequest $request
823 820
 	 * @throws TooManySparqlRequestsException
824 821
 	 */
825
-	private function guardAgainstTooManyRequestsError( MWHttpRequest $request ): void {
826
-		if ( $request->getStatus() !== self::HTTP_TOO_MANY_REQUESTS ) {
822
+	private function guardAgainstTooManyRequestsError(MWHttpRequest $request): void {
823
+		if ($request->getStatus() !== self::HTTP_TOO_MANY_REQUESTS) {
827 824
 			return;
828 825
 		}
829 826
 
830 827
 		$fallbackBlockDuration = $this->sparqlThrottlingFallbackDuration;
831 828
 
832
-		if ( $fallbackBlockDuration < 0 ) {
833
-			throw new InvalidArgumentException( 'Fallback duration must be positive int but is: ' .
834
-				$fallbackBlockDuration );
829
+		if ($fallbackBlockDuration < 0) {
830
+			throw new InvalidArgumentException('Fallback duration must be positive int but is: '.
831
+				$fallbackBlockDuration);
835 832
 		}
836 833
 
837
-		$this->dataFactory->increment( 'wikibase.quality.constraints.sparql.throttling' );
838
-		$throttlingUntil = $this->getThrottling( $request );
839
-		if ( !( $throttlingUntil instanceof ConvertibleTimestamp ) ) {
840
-			$this->loggingHelper->logSparqlHelperTooManyRequestsRetryAfterInvalid( $request );
834
+		$this->dataFactory->increment('wikibase.quality.constraints.sparql.throttling');
835
+		$throttlingUntil = $this->getThrottling($request);
836
+		if (!($throttlingUntil instanceof ConvertibleTimestamp)) {
837
+			$this->loggingHelper->logSparqlHelperTooManyRequestsRetryAfterInvalid($request);
841 838
 			$this->throttlingLock->lock(
842 839
 				self::EXPIRY_LOCK_ID,
843
-				$this->getTimestampInFuture( new DateInterval( 'PT' . $fallbackBlockDuration . 'S' ) )
840
+				$this->getTimestampInFuture(new DateInterval('PT'.$fallbackBlockDuration.'S'))
844 841
 			);
845 842
 		} else {
846
-			$this->loggingHelper->logSparqlHelperTooManyRequestsRetryAfterPresent( $throttlingUntil, $request );
847
-			$this->throttlingLock->lock( self::EXPIRY_LOCK_ID, $throttlingUntil );
843
+			$this->loggingHelper->logSparqlHelperTooManyRequestsRetryAfterPresent($throttlingUntil, $request);
844
+			$this->throttlingLock->lock(self::EXPIRY_LOCK_ID, $throttlingUntil);
848 845
 		}
849 846
 		throw new TooManySparqlRequestsException();
850 847
 	}
Please login to merge, or discard this patch.