@@ -162,19 +162,19 @@ discard block |
||
162 | 162 | $this->loggingHelper = $loggingHelper; |
163 | 163 | $this->defaultUserAgent = $defaultUserAgent; |
164 | 164 | $this->requestFactory = $requestFactory; |
165 | - $this->entityPrefix = $rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_ENTITY ); |
|
165 | + $this->entityPrefix = $rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_ENTITY); |
|
166 | 166 | $this->prefixes = <<<EOT |
167 | -PREFIX wd: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_ENTITY )}> |
|
168 | -PREFIX wds: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_STATEMENT )}> |
|
169 | -PREFIX wdt: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_DIRECT_CLAIM )}> |
|
170 | -PREFIX wdv: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_VALUE )}> |
|
171 | -PREFIX p: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_CLAIM )}> |
|
172 | -PREFIX ps: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_CLAIM_STATEMENT )}> |
|
173 | -PREFIX pq: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_QUALIFIER )}> |
|
174 | -PREFIX pqv: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_QUALIFIER_VALUE )}> |
|
175 | -PREFIX pr: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_REFERENCE )}> |
|
176 | -PREFIX prv: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_REFERENCE_VALUE )}> |
|
177 | -PREFIX wikibase: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_ONTOLOGY )}> |
|
167 | +PREFIX wd: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_ENTITY)}> |
|
168 | +PREFIX wds: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_STATEMENT)}> |
|
169 | +PREFIX wdt: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_DIRECT_CLAIM)}> |
|
170 | +PREFIX wdv: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_VALUE)}> |
|
171 | +PREFIX p: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_CLAIM)}> |
|
172 | +PREFIX ps: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_CLAIM_STATEMENT)}> |
|
173 | +PREFIX pq: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_QUALIFIER)}> |
|
174 | +PREFIX pqv: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_QUALIFIER_VALUE)}> |
|
175 | +PREFIX pr: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_REFERENCE)}> |
|
176 | +PREFIX prv: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_REFERENCE_VALUE)}> |
|
177 | +PREFIX wikibase: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_ONTOLOGY)}> |
|
178 | 178 | EOT; |
179 | 179 | } |
180 | 180 | |
@@ -185,22 +185,21 @@ discard block |
||
185 | 185 | * @return CachedBool |
186 | 186 | * @throws SparqlHelperException if the query times out or some other error occurs |
187 | 187 | */ |
188 | - public function hasType( $id, array $classes ) { |
|
189 | - $subclassOfId = $this->config->get( 'WBQualityConstraintsSubclassOfId' ); |
|
188 | + public function hasType($id, array $classes) { |
|
189 | + $subclassOfId = $this->config->get('WBQualityConstraintsSubclassOfId'); |
|
190 | 190 | // TODO hint:gearing is a workaround for T168973 and can hopefully be removed eventually |
191 | - $gearingHint = $this->config->get( 'WBQualityConstraintsSparqlHasWikibaseSupport' ) ? |
|
192 | - ' hint:Prior hint:gearing "forward".' : |
|
193 | - ''; |
|
191 | + $gearingHint = $this->config->get('WBQualityConstraintsSparqlHasWikibaseSupport') ? |
|
192 | + ' hint:Prior hint:gearing "forward".' : ''; |
|
194 | 193 | |
195 | 194 | $metadatas = []; |
196 | 195 | |
197 | - foreach ( array_chunk( $classes, 20 ) as $classesChunk ) { |
|
198 | - $classesValues = implode( ' ', array_map( |
|
199 | - function( $class ) { |
|
200 | - return 'wd:' . $class; |
|
196 | + foreach (array_chunk($classes, 20) as $classesChunk) { |
|
197 | + $classesValues = implode(' ', array_map( |
|
198 | + function($class) { |
|
199 | + return 'wd:'.$class; |
|
201 | 200 | }, |
202 | 201 | $classesChunk |
203 | - ) ); |
|
202 | + )); |
|
204 | 203 | |
205 | 204 | $query = <<<EOF |
206 | 205 | ASK { |
@@ -210,19 +209,19 @@ discard block |
||
210 | 209 | } |
211 | 210 | EOF; |
212 | 211 | |
213 | - $result = $this->runQuery( $query ); |
|
212 | + $result = $this->runQuery($query); |
|
214 | 213 | $metadatas[] = $result->getMetadata(); |
215 | - if ( $result->getArray()['boolean'] ) { |
|
214 | + if ($result->getArray()['boolean']) { |
|
216 | 215 | return new CachedBool( |
217 | 216 | true, |
218 | - Metadata::merge( $metadatas ) |
|
217 | + Metadata::merge($metadatas) |
|
219 | 218 | ); |
220 | 219 | } |
221 | 220 | } |
222 | 221 | |
223 | 222 | return new CachedBool( |
224 | 223 | false, |
225 | - Metadata::merge( $metadatas ) |
|
224 | + Metadata::merge($metadatas) |
|
226 | 225 | ); |
227 | 226 | } |
228 | 227 | |
@@ -238,10 +237,10 @@ discard block |
||
238 | 237 | $ignoreDeprecatedStatements |
239 | 238 | ) { |
240 | 239 | $pid = $statement->getPropertyId()->serialize(); |
241 | - $guid = str_replace( '$', '-', $statement->getGuid() ); |
|
240 | + $guid = str_replace('$', '-', $statement->getGuid()); |
|
242 | 241 | |
243 | 242 | $deprecatedFilter = ''; |
244 | - if ( $ignoreDeprecatedStatements ) { |
|
243 | + if ($ignoreDeprecatedStatements) { |
|
245 | 244 | $deprecatedFilter = 'MINUS { ?otherStatement wikibase:rank wikibase:DeprecatedRank. }'; |
246 | 245 | } |
247 | 246 | |
@@ -260,9 +259,9 @@ discard block |
||
260 | 259 | LIMIT 10 |
261 | 260 | EOF; |
262 | 261 | |
263 | - $result = $this->runQuery( $query ); |
|
262 | + $result = $this->runQuery($query); |
|
264 | 263 | |
265 | - return $this->getOtherEntities( $result ); |
|
264 | + return $this->getOtherEntities($result); |
|
266 | 265 | } |
267 | 266 | |
268 | 267 | /** |
@@ -287,16 +286,15 @@ discard block |
||
287 | 286 | $dataType = $this->propertyDataTypeLookup->getDataTypeIdForProperty( |
288 | 287 | $snak->getPropertyId() |
289 | 288 | ); |
290 | - list( $value, $isFullValue ) = $this->getRdfLiteral( $dataType, $dataValue ); |
|
291 | - if ( $isFullValue ) { |
|
289 | + list($value, $isFullValue) = $this->getRdfLiteral($dataType, $dataValue); |
|
290 | + if ($isFullValue) { |
|
292 | 291 | $prefix .= 'v'; |
293 | 292 | } |
294 | 293 | $path = $type === Context::TYPE_QUALIFIER ? |
295 | - "$prefix:$pid" : |
|
296 | - "prov:wasDerivedFrom/$prefix:$pid"; |
|
294 | + "$prefix:$pid" : "prov:wasDerivedFrom/$prefix:$pid"; |
|
297 | 295 | |
298 | 296 | $deprecatedFilter = ''; |
299 | - if ( $ignoreDeprecatedStatements ) { |
|
297 | + if ($ignoreDeprecatedStatements) { |
|
300 | 298 | $deprecatedFilter = <<< EOF |
301 | 299 | MINUS { ?otherStatement wikibase:rank wikibase:DeprecatedRank. } |
302 | 300 | EOF; |
@@ -316,9 +314,9 @@ discard block |
||
316 | 314 | LIMIT 10 |
317 | 315 | EOF; |
318 | 316 | |
319 | - $result = $this->runQuery( $query ); |
|
317 | + $result = $this->runQuery($query); |
|
320 | 318 | |
321 | - return $this->getOtherEntities( $result ); |
|
319 | + return $this->getOtherEntities($result); |
|
322 | 320 | } |
323 | 321 | |
324 | 322 | /** |
@@ -328,8 +326,8 @@ discard block |
||
328 | 326 | * |
329 | 327 | * @return string |
330 | 328 | */ |
331 | - private function stringLiteral( $text ) { |
|
332 | - return '"' . strtr( $text, [ '"' => '\\"', '\\' => '\\\\' ] ) . '"'; |
|
329 | + private function stringLiteral($text) { |
|
330 | + return '"'.strtr($text, ['"' => '\\"', '\\' => '\\\\']).'"'; |
|
333 | 331 | } |
334 | 332 | |
335 | 333 | /** |
@@ -339,17 +337,17 @@ discard block |
||
339 | 337 | * |
340 | 338 | * @return CachedEntityIds |
341 | 339 | */ |
342 | - private function getOtherEntities( CachedQueryResults $results ) { |
|
343 | - return new CachedEntityIds( array_map( |
|
344 | - function ( $resultBindings ) { |
|
340 | + private function getOtherEntities(CachedQueryResults $results) { |
|
341 | + return new CachedEntityIds(array_map( |
|
342 | + function($resultBindings) { |
|
345 | 343 | $entityIRI = $resultBindings['otherEntity']['value']; |
346 | - $entityPrefixLength = strlen( $this->entityPrefix ); |
|
347 | - if ( substr( $entityIRI, 0, $entityPrefixLength ) === $this->entityPrefix ) { |
|
344 | + $entityPrefixLength = strlen($this->entityPrefix); |
|
345 | + if (substr($entityIRI, 0, $entityPrefixLength) === $this->entityPrefix) { |
|
348 | 346 | try { |
349 | 347 | return $this->entityIdParser->parse( |
350 | - substr( $entityIRI, $entityPrefixLength ) |
|
348 | + substr($entityIRI, $entityPrefixLength) |
|
351 | 349 | ); |
352 | - } catch ( EntityIdParsingException $e ) { |
|
350 | + } catch (EntityIdParsingException $e) { |
|
353 | 351 | // fall through |
354 | 352 | } |
355 | 353 | } |
@@ -357,7 +355,7 @@ discard block |
||
357 | 355 | return null; |
358 | 356 | }, |
359 | 357 | $results->getArray()['results']['bindings'] |
360 | - ), $results->getMetadata() ); |
|
358 | + ), $results->getMetadata()); |
|
361 | 359 | } |
362 | 360 | |
363 | 361 | // @codingStandardsIgnoreStart cyclomatic complexity of this function is too high |
@@ -370,47 +368,47 @@ discard block |
||
370 | 368 | * @return array the literal or IRI as a string in SPARQL syntax, |
371 | 369 | * and a boolean indicating whether it refers to a full value node or not |
372 | 370 | */ |
373 | - private function getRdfLiteral( $dataType, DataValue $dataValue ) { |
|
374 | - switch ( $dataType ) { |
|
371 | + private function getRdfLiteral($dataType, DataValue $dataValue) { |
|
372 | + switch ($dataType) { |
|
375 | 373 | case 'string': |
376 | 374 | case 'external-id': |
377 | - return [ $this->stringLiteral( $dataValue->getValue() ), false ]; |
|
375 | + return [$this->stringLiteral($dataValue->getValue()), false]; |
|
378 | 376 | case 'commonsMedia': |
379 | - $url = $this->rdfVocabulary->getMediaFileURI( $dataValue->getValue() ); |
|
380 | - return [ '<' . $url . '>', false ]; |
|
377 | + $url = $this->rdfVocabulary->getMediaFileURI($dataValue->getValue()); |
|
378 | + return ['<'.$url.'>', false]; |
|
381 | 379 | case 'geo-shape': |
382 | - $url = $this->rdfVocabulary->getGeoShapeURI( $dataValue->getValue() ); |
|
383 | - return [ '<' . $url . '>', false ]; |
|
380 | + $url = $this->rdfVocabulary->getGeoShapeURI($dataValue->getValue()); |
|
381 | + return ['<'.$url.'>', false]; |
|
384 | 382 | case 'tabular-data': |
385 | - $url = $this->rdfVocabulary->getTabularDataURI( $dataValue->getValue() ); |
|
386 | - return [ '<' . $url . '>', false ]; |
|
383 | + $url = $this->rdfVocabulary->getTabularDataURI($dataValue->getValue()); |
|
384 | + return ['<'.$url.'>', false]; |
|
387 | 385 | case 'url': |
388 | 386 | $url = $dataValue->getValue(); |
389 | - if ( !preg_match( '/^[^<>"{}\\\\|^`\\x00-\\x20]*$/D', $url ) ) { |
|
387 | + if (!preg_match('/^[^<>"{}\\\\|^`\\x00-\\x20]*$/D', $url)) { |
|
390 | 388 | // not a valid URL for SPARQL (see SPARQL spec, production 139 IRIREF) |
391 | 389 | // such an URL should never reach us, so just throw |
392 | - throw new InvalidArgumentException( 'invalid URL: ' . $url ); |
|
390 | + throw new InvalidArgumentException('invalid URL: '.$url); |
|
393 | 391 | } |
394 | - return [ '<' . $url . '>', false ]; |
|
392 | + return ['<'.$url.'>', false]; |
|
395 | 393 | case 'wikibase-item': |
396 | 394 | case 'wikibase-property': |
397 | 395 | /** @var EntityIdValue $dataValue */ |
398 | - return [ 'wd:' . $dataValue->getEntityId()->getSerialization(), false ]; |
|
396 | + return ['wd:'.$dataValue->getEntityId()->getSerialization(), false]; |
|
399 | 397 | case 'monolingualtext': |
400 | 398 | /** @var MonolingualTextValue $dataValue */ |
401 | 399 | $lang = $dataValue->getLanguageCode(); |
402 | - if ( !preg_match( '/^[a-zA-Z]+(-[a-zA-Z0-9]+)*$/D', $lang ) ) { |
|
400 | + if (!preg_match('/^[a-zA-Z]+(-[a-zA-Z0-9]+)*$/D', $lang)) { |
|
403 | 401 | // not a valid language tag for SPARQL (see SPARQL spec, production 145 LANGTAG) |
404 | 402 | // such a language tag should never reach us, so just throw |
405 | - throw new InvalidArgumentException( 'invalid language tag: ' . $lang ); |
|
403 | + throw new InvalidArgumentException('invalid language tag: '.$lang); |
|
406 | 404 | } |
407 | - return [ $this->stringLiteral( $dataValue->getText() ) . '@' . $lang, false ]; |
|
405 | + return [$this->stringLiteral($dataValue->getText()).'@'.$lang, false]; |
|
408 | 406 | case 'globe-coordinate': |
409 | 407 | case 'quantity': |
410 | 408 | case 'time': |
411 | - return [ 'wdv:' . $dataValue->getHash(), true ]; |
|
409 | + return ['wdv:'.$dataValue->getHash(), true]; |
|
412 | 410 | default: |
413 | - throw new InvalidArgumentException( 'unknown data type: ' . $dataType ); |
|
411 | + throw new InvalidArgumentException('unknown data type: '.$dataType); |
|
414 | 412 | } |
415 | 413 | } |
416 | 414 | // @codingStandardsIgnoreEnd |
@@ -423,44 +421,44 @@ discard block |
||
423 | 421 | * @throws SparqlHelperException if the query times out or some other error occurs |
424 | 422 | * @throws ConstraintParameterException if the $regex is invalid |
425 | 423 | */ |
426 | - public function matchesRegularExpression( $text, $regex ) { |
|
424 | + public function matchesRegularExpression($text, $regex) { |
|
427 | 425 | // caching wrapper around matchesRegularExpressionWithSparql |
428 | 426 | |
429 | - $textHash = hash( 'sha256', $text ); |
|
427 | + $textHash = hash('sha256', $text); |
|
430 | 428 | $cacheKey = $this->cache->makeKey( |
431 | 429 | 'WikibaseQualityConstraints', // extension |
432 | 430 | 'regex', // action |
433 | 431 | 'WDQS-Java', // regex flavor |
434 | - hash( 'sha256', $regex ) |
|
432 | + hash('sha256', $regex) |
|
435 | 433 | ); |
436 | - $cacheMapSize = $this->config->get( 'WBQualityConstraintsFormatCacheMapSize' ); |
|
434 | + $cacheMapSize = $this->config->get('WBQualityConstraintsFormatCacheMapSize'); |
|
437 | 435 | |
438 | 436 | $cacheMapArray = $this->cache->getWithSetCallback( |
439 | 437 | $cacheKey, |
440 | 438 | WANObjectCache::TTL_DAY, |
441 | - function( $cacheMapArray ) use ( $text, $regex, $textHash, $cacheMapSize ) { |
|
439 | + function($cacheMapArray) use ($text, $regex, $textHash, $cacheMapSize) { |
|
442 | 440 | // Initialize the cache map if not set |
443 | - if ( $cacheMapArray === false ) { |
|
441 | + if ($cacheMapArray === false) { |
|
444 | 442 | $key = 'wikibase.quality.constraints.regex.cache.refresh.init'; |
445 | - $this->dataFactory->increment( $key ); |
|
443 | + $this->dataFactory->increment($key); |
|
446 | 444 | return []; |
447 | 445 | } |
448 | 446 | |
449 | 447 | $key = 'wikibase.quality.constraints.regex.cache.refresh'; |
450 | - $this->dataFactory->increment( $key ); |
|
451 | - $cacheMap = MapCacheLRU::newFromArray( $cacheMapArray, $cacheMapSize ); |
|
452 | - if ( $cacheMap->has( $textHash ) ) { |
|
448 | + $this->dataFactory->increment($key); |
|
449 | + $cacheMap = MapCacheLRU::newFromArray($cacheMapArray, $cacheMapSize); |
|
450 | + if ($cacheMap->has($textHash)) { |
|
453 | 451 | $key = 'wikibase.quality.constraints.regex.cache.refresh.hit'; |
454 | - $this->dataFactory->increment( $key ); |
|
455 | - $cacheMap->get( $textHash ); // ping cache |
|
452 | + $this->dataFactory->increment($key); |
|
453 | + $cacheMap->get($textHash); // ping cache |
|
456 | 454 | } else { |
457 | 455 | $key = 'wikibase.quality.constraints.regex.cache.refresh.miss'; |
458 | - $this->dataFactory->increment( $key ); |
|
456 | + $this->dataFactory->increment($key); |
|
459 | 457 | try { |
460 | - $matches = $this->matchesRegularExpressionWithSparql( $text, $regex ); |
|
461 | - } catch ( ConstraintParameterException $e ) { |
|
462 | - $matches = $this->serializeConstraintParameterException( $e ); |
|
463 | - } catch ( SparqlHelperException $e ) { |
|
458 | + $matches = $this->matchesRegularExpressionWithSparql($text, $regex); |
|
459 | + } catch (ConstraintParameterException $e) { |
|
460 | + $matches = $this->serializeConstraintParameterException($e); |
|
461 | + } catch (SparqlHelperException $e) { |
|
464 | 462 | // don’t cache this |
465 | 463 | return $cacheMap->toArray(); |
466 | 464 | } |
@@ -484,42 +482,42 @@ discard block |
||
484 | 482 | ] |
485 | 483 | ); |
486 | 484 | |
487 | - if ( isset( $cacheMapArray[$textHash] ) ) { |
|
485 | + if (isset($cacheMapArray[$textHash])) { |
|
488 | 486 | $key = 'wikibase.quality.constraints.regex.cache.hit'; |
489 | - $this->dataFactory->increment( $key ); |
|
487 | + $this->dataFactory->increment($key); |
|
490 | 488 | $matches = $cacheMapArray[$textHash]; |
491 | - if ( is_bool( $matches ) ) { |
|
489 | + if (is_bool($matches)) { |
|
492 | 490 | return $matches; |
493 | - } elseif ( is_array( $matches ) && |
|
494 | - $matches['type'] == ConstraintParameterException::class ) { |
|
495 | - throw $this->deserializeConstraintParameterException( $matches ); |
|
491 | + } elseif (is_array($matches) && |
|
492 | + $matches['type'] == ConstraintParameterException::class) { |
|
493 | + throw $this->deserializeConstraintParameterException($matches); |
|
496 | 494 | } else { |
497 | 495 | throw new MWException( |
498 | - 'Value of unknown type in object cache (' . |
|
499 | - 'cache key: ' . $cacheKey . ', ' . |
|
500 | - 'cache map key: ' . $textHash . ', ' . |
|
501 | - 'value type: ' . gettype( $matches ) . ')' |
|
496 | + 'Value of unknown type in object cache ('. |
|
497 | + 'cache key: '.$cacheKey.', '. |
|
498 | + 'cache map key: '.$textHash.', '. |
|
499 | + 'value type: '.gettype($matches).')' |
|
502 | 500 | ); |
503 | 501 | } |
504 | 502 | } else { |
505 | 503 | $key = 'wikibase.quality.constraints.regex.cache.miss'; |
506 | - $this->dataFactory->increment( $key ); |
|
507 | - return $this->matchesRegularExpressionWithSparql( $text, $regex ); |
|
504 | + $this->dataFactory->increment($key); |
|
505 | + return $this->matchesRegularExpressionWithSparql($text, $regex); |
|
508 | 506 | } |
509 | 507 | } |
510 | 508 | |
511 | - private function serializeConstraintParameterException( ConstraintParameterException $cpe ) { |
|
509 | + private function serializeConstraintParameterException(ConstraintParameterException $cpe) { |
|
512 | 510 | return [ |
513 | 511 | 'type' => ConstraintParameterException::class, |
514 | - 'violationMessage' => $this->violationMessageSerializer->serialize( $cpe->getViolationMessage() ), |
|
512 | + 'violationMessage' => $this->violationMessageSerializer->serialize($cpe->getViolationMessage()), |
|
515 | 513 | ]; |
516 | 514 | } |
517 | 515 | |
518 | - private function deserializeConstraintParameterException( array $serialization ) { |
|
516 | + private function deserializeConstraintParameterException(array $serialization) { |
|
519 | 517 | $message = $this->violationMessageDeserializer->deserialize( |
520 | 518 | $serialization['violationMessage'] |
521 | 519 | ); |
522 | - return new ConstraintParameterException( $message ); |
|
520 | + return new ConstraintParameterException($message); |
|
523 | 521 | } |
524 | 522 | |
525 | 523 | /** |
@@ -533,25 +531,25 @@ discard block |
||
533 | 531 | * @throws SparqlHelperException if the query times out or some other error occurs |
534 | 532 | * @throws ConstraintParameterException if the $regex is invalid |
535 | 533 | */ |
536 | - public function matchesRegularExpressionWithSparql( $text, $regex ) { |
|
537 | - $textStringLiteral = $this->stringLiteral( $text ); |
|
538 | - $regexStringLiteral = $this->stringLiteral( '^(?:' . $regex . ')$' ); |
|
534 | + public function matchesRegularExpressionWithSparql($text, $regex) { |
|
535 | + $textStringLiteral = $this->stringLiteral($text); |
|
536 | + $regexStringLiteral = $this->stringLiteral('^(?:'.$regex.')$'); |
|
539 | 537 | |
540 | 538 | $query = <<<EOF |
541 | 539 | SELECT (REGEX($textStringLiteral, $regexStringLiteral) AS ?matches) {} |
542 | 540 | EOF; |
543 | 541 | |
544 | - $result = $this->runQuery( $query, false ); |
|
542 | + $result = $this->runQuery($query, false); |
|
545 | 543 | |
546 | 544 | $vars = $result->getArray()['results']['bindings'][0]; |
547 | - if ( array_key_exists( 'matches', $vars ) ) { |
|
545 | + if (array_key_exists('matches', $vars)) { |
|
548 | 546 | // true or false ⇒ regex okay, text matches or not |
549 | 547 | return $vars['matches']['value'] === 'true'; |
550 | 548 | } else { |
551 | 549 | // empty result: regex broken |
552 | 550 | throw new ConstraintParameterException( |
553 | - ( new ViolationMessage( 'wbqc-violation-message-parameter-regex' ) ) |
|
554 | - ->withInlineCode( $regex, Role::CONSTRAINT_PARAMETER_VALUE ) |
|
551 | + (new ViolationMessage('wbqc-violation-message-parameter-regex')) |
|
552 | + ->withInlineCode($regex, Role::CONSTRAINT_PARAMETER_VALUE) |
|
555 | 553 | ); |
556 | 554 | } |
557 | 555 | } |
@@ -563,14 +561,14 @@ discard block |
||
563 | 561 | * |
564 | 562 | * @return boolean |
565 | 563 | */ |
566 | - public function isTimeout( $responseContent ) { |
|
567 | - $timeoutRegex = implode( '|', array_map( |
|
568 | - function ( $fqn ) { |
|
569 | - return preg_quote( $fqn, '/' ); |
|
564 | + public function isTimeout($responseContent) { |
|
565 | + $timeoutRegex = implode('|', array_map( |
|
566 | + function($fqn) { |
|
567 | + return preg_quote($fqn, '/'); |
|
570 | 568 | }, |
571 | - $this->config->get( 'WBQualityConstraintsSparqlTimeoutExceptionClasses' ) |
|
572 | - ) ); |
|
573 | - return (bool)preg_match( '/' . $timeoutRegex . '/', $responseContent ); |
|
569 | + $this->config->get('WBQualityConstraintsSparqlTimeoutExceptionClasses') |
|
570 | + )); |
|
571 | + return (bool) preg_match('/'.$timeoutRegex.'/', $responseContent); |
|
574 | 572 | } |
575 | 573 | |
576 | 574 | /** |
@@ -582,17 +580,17 @@ discard block |
||
582 | 580 | * @return int|boolean the max-age (in seconds) |
583 | 581 | * or a plain boolean if no max-age can be determined |
584 | 582 | */ |
585 | - public function getCacheMaxAge( $responseHeaders ) { |
|
583 | + public function getCacheMaxAge($responseHeaders) { |
|
586 | 584 | if ( |
587 | - array_key_exists( 'x-cache-status', $responseHeaders ) && |
|
588 | - preg_match( '/^hit(?:-.*)?$/', $responseHeaders['x-cache-status'][0] ) |
|
585 | + array_key_exists('x-cache-status', $responseHeaders) && |
|
586 | + preg_match('/^hit(?:-.*)?$/', $responseHeaders['x-cache-status'][0]) |
|
589 | 587 | ) { |
590 | 588 | $maxage = []; |
591 | 589 | if ( |
592 | - array_key_exists( 'cache-control', $responseHeaders ) && |
|
593 | - preg_match( '/\bmax-age=(\d+)\b/', $responseHeaders['cache-control'][0], $maxage ) |
|
590 | + array_key_exists('cache-control', $responseHeaders) && |
|
591 | + preg_match('/\bmax-age=(\d+)\b/', $responseHeaders['cache-control'][0], $maxage) |
|
594 | 592 | ) { |
595 | - return intval( $maxage[1] ); |
|
593 | + return intval($maxage[1]); |
|
596 | 594 | } else { |
597 | 595 | return true; |
598 | 596 | } |
@@ -613,34 +611,34 @@ discard block |
||
613 | 611 | * or SparlHelper::EMPTY_RETRY_AFTER if there is an empty Retry-After |
614 | 612 | * or SparlHelper::INVALID_RETRY_AFTER if there is something wrong with the format |
615 | 613 | */ |
616 | - public function getThrottling( MWHttpRequest $request ) { |
|
617 | - $retryAfterValue = $request->getResponseHeader( 'Retry-After' ); |
|
618 | - if ( $retryAfterValue === null ) { |
|
614 | + public function getThrottling(MWHttpRequest $request) { |
|
615 | + $retryAfterValue = $request->getResponseHeader('Retry-After'); |
|
616 | + if ($retryAfterValue === null) { |
|
619 | 617 | return self::NO_RETRY_AFTER; |
620 | 618 | } |
621 | 619 | |
622 | - $trimmedRetryAfterValue = trim( $retryAfterValue ); |
|
623 | - if ( empty( $trimmedRetryAfterValue ) ) { |
|
620 | + $trimmedRetryAfterValue = trim($retryAfterValue); |
|
621 | + if (empty($trimmedRetryAfterValue)) { |
|
624 | 622 | return self::EMPTY_RETRY_AFTER; |
625 | 623 | } |
626 | 624 | |
627 | - if ( is_numeric( $trimmedRetryAfterValue ) ) { |
|
628 | - $delaySeconds = (int)$trimmedRetryAfterValue; |
|
629 | - if ( $delaySeconds >= 0 ) { |
|
630 | - return $this->getTimestampInFuture( new DateInterval( 'PT' . $delaySeconds . 'S' ) ); |
|
625 | + if (is_numeric($trimmedRetryAfterValue)) { |
|
626 | + $delaySeconds = (int) $trimmedRetryAfterValue; |
|
627 | + if ($delaySeconds >= 0) { |
|
628 | + return $this->getTimestampInFuture(new DateInterval('PT'.$delaySeconds.'S')); |
|
631 | 629 | } |
632 | 630 | } else { |
633 | - $return = strtotime( $trimmedRetryAfterValue ); |
|
634 | - if ( !empty( $return ) ) { |
|
635 | - return new ConvertibleTimestamp( $return ); |
|
631 | + $return = strtotime($trimmedRetryAfterValue); |
|
632 | + if (!empty($return)) { |
|
633 | + return new ConvertibleTimestamp($return); |
|
636 | 634 | } |
637 | 635 | } |
638 | 636 | return self::INVALID_RETRY_AFTER; |
639 | 637 | } |
640 | 638 | |
641 | - private function getTimestampInFuture( DateInterval $delta ) { |
|
639 | + private function getTimestampInFuture(DateInterval $delta) { |
|
642 | 640 | $now = new ConvertibleTimestamp(); |
643 | - return new ConvertibleTimestamp( $now->timestamp->add( $delta ) ); |
|
641 | + return new ConvertibleTimestamp($now->timestamp->add($delta)); |
|
644 | 642 | } |
645 | 643 | |
646 | 644 | /** |
@@ -654,97 +652,96 @@ discard block |
||
654 | 652 | * |
655 | 653 | * @throws SparqlHelperException if the query times out or some other error occurs |
656 | 654 | */ |
657 | - public function runQuery( $query, $needsPrefixes = true ) { |
|
655 | + public function runQuery($query, $needsPrefixes = true) { |
|
658 | 656 | |
659 | - if ( $this->throttlingLock->isLocked( self::EXPIRY_LOCK_ID ) ) { |
|
660 | - $this->dataFactory->increment( 'wikibase.quality.constraints.sparql.throttling' ); |
|
657 | + if ($this->throttlingLock->isLocked(self::EXPIRY_LOCK_ID)) { |
|
658 | + $this->dataFactory->increment('wikibase.quality.constraints.sparql.throttling'); |
|
661 | 659 | throw new TooManySparqlRequestsException(); |
662 | 660 | } |
663 | 661 | |
664 | - $endpoint = $this->config->get( 'WBQualityConstraintsSparqlEndpoint' ); |
|
665 | - $maxQueryTimeMillis = $this->config->get( 'WBQualityConstraintsSparqlMaxMillis' ); |
|
666 | - $fallbackBlockDuration = (int)$this->config->get( 'WBQualityConstraintsSparqlThrottlingFallbackDuration' ); |
|
662 | + $endpoint = $this->config->get('WBQualityConstraintsSparqlEndpoint'); |
|
663 | + $maxQueryTimeMillis = $this->config->get('WBQualityConstraintsSparqlMaxMillis'); |
|
664 | + $fallbackBlockDuration = (int) $this->config->get('WBQualityConstraintsSparqlThrottlingFallbackDuration'); |
|
667 | 665 | |
668 | - if ( $fallbackBlockDuration < 0 ) { |
|
669 | - throw new InvalidArgumentException( 'Fallback duration must be positive int but is: '. |
|
670 | - $fallbackBlockDuration ); |
|
666 | + if ($fallbackBlockDuration < 0) { |
|
667 | + throw new InvalidArgumentException('Fallback duration must be positive int but is: '. |
|
668 | + $fallbackBlockDuration); |
|
671 | 669 | } |
672 | 670 | |
673 | - if ( $this->config->get( 'WBQualityConstraintsSparqlHasWikibaseSupport' ) ) { |
|
671 | + if ($this->config->get('WBQualityConstraintsSparqlHasWikibaseSupport')) { |
|
674 | 672 | $needsPrefixes = false; |
675 | 673 | } |
676 | 674 | |
677 | - if ( $needsPrefixes ) { |
|
678 | - $query = $this->prefixes . $query; |
|
675 | + if ($needsPrefixes) { |
|
676 | + $query = $this->prefixes.$query; |
|
679 | 677 | } |
680 | - $query = "#wbqc\n" . $query; |
|
678 | + $query = "#wbqc\n".$query; |
|
681 | 679 | |
682 | - $url = $endpoint . '?' . http_build_query( |
|
680 | + $url = $endpoint.'?'.http_build_query( |
|
683 | 681 | [ |
684 | 682 | 'query' => $query, |
685 | 683 | 'format' => 'json', |
686 | 684 | 'maxQueryTimeMillis' => $maxQueryTimeMillis, |
687 | 685 | ], |
688 | - null, ini_get( 'arg_separator.output' ), |
|
686 | + null, ini_get('arg_separator.output'), |
|
689 | 687 | // encode spaces with %20, not + |
690 | 688 | PHP_QUERY_RFC3986 |
691 | 689 | ); |
692 | 690 | |
693 | 691 | $options = [ |
694 | 692 | 'method' => 'GET', |
695 | - 'timeout' => (int)round( ( $maxQueryTimeMillis + 1000 ) / 1000 ), |
|
693 | + 'timeout' => (int) round(($maxQueryTimeMillis + 1000) / 1000), |
|
696 | 694 | 'connectTimeout' => 'default', |
697 | 695 | 'userAgent' => $this->defaultUserAgent, |
698 | 696 | ]; |
699 | - $request = $this->requestFactory->create( $url, $options ); |
|
700 | - $startTime = microtime( true ); |
|
697 | + $request = $this->requestFactory->create($url, $options); |
|
698 | + $startTime = microtime(true); |
|
701 | 699 | $status = $request->execute(); |
702 | - $endTime = microtime( true ); |
|
700 | + $endTime = microtime(true); |
|
703 | 701 | $this->dataFactory->timing( |
704 | 702 | 'wikibase.quality.constraints.sparql.timing', |
705 | - ( $endTime - $startTime ) * 1000 |
|
703 | + ($endTime - $startTime) * 1000 |
|
706 | 704 | ); |
707 | 705 | |
708 | - if ( $request->getStatus() === self::HTTP_TOO_MANY_REQUESTS ) { |
|
709 | - $this->dataFactory->increment( 'wikibase.quality.constraints.sparql.throttling' ); |
|
710 | - $throttlingUntil = $this->getThrottling( $request ); |
|
711 | - if ( !( $throttlingUntil instanceof ConvertibleTimestamp ) ) { |
|
712 | - $this->loggingHelper->logSparqlHelperTooManyRequestsRetryAfterInvalid( $request ); |
|
706 | + if ($request->getStatus() === self::HTTP_TOO_MANY_REQUESTS) { |
|
707 | + $this->dataFactory->increment('wikibase.quality.constraints.sparql.throttling'); |
|
708 | + $throttlingUntil = $this->getThrottling($request); |
|
709 | + if (!($throttlingUntil instanceof ConvertibleTimestamp)) { |
|
710 | + $this->loggingHelper->logSparqlHelperTooManyRequestsRetryAfterInvalid($request); |
|
713 | 711 | $this->throttlingLock->lock( |
714 | 712 | self::EXPIRY_LOCK_ID, |
715 | - $this->getTimestampInFuture( new DateInterval( 'PT' . $fallbackBlockDuration . 'S' ) ) |
|
713 | + $this->getTimestampInFuture(new DateInterval('PT'.$fallbackBlockDuration.'S')) |
|
716 | 714 | ); |
717 | 715 | } else { |
718 | - $this->loggingHelper->logSparqlHelperTooManyRequestsRetryAfterPresent( $throttlingUntil, $request ); |
|
719 | - $this->throttlingLock->lock( self::EXPIRY_LOCK_ID, $throttlingUntil ); |
|
716 | + $this->loggingHelper->logSparqlHelperTooManyRequestsRetryAfterPresent($throttlingUntil, $request); |
|
717 | + $this->throttlingLock->lock(self::EXPIRY_LOCK_ID, $throttlingUntil); |
|
720 | 718 | } |
721 | 719 | throw new TooManySparqlRequestsException(); |
722 | 720 | } |
723 | 721 | |
724 | - $maxAge = $this->getCacheMaxAge( $request->getResponseHeaders() ); |
|
725 | - if ( $maxAge ) { |
|
726 | - $this->dataFactory->increment( 'wikibase.quality.constraints.sparql.cached' ); |
|
722 | + $maxAge = $this->getCacheMaxAge($request->getResponseHeaders()); |
|
723 | + if ($maxAge) { |
|
724 | + $this->dataFactory->increment('wikibase.quality.constraints.sparql.cached'); |
|
727 | 725 | } |
728 | 726 | |
729 | - if ( $status->isOK() ) { |
|
727 | + if ($status->isOK()) { |
|
730 | 728 | $json = $request->getContent(); |
731 | - $arr = json_decode( $json, true ); |
|
729 | + $arr = json_decode($json, true); |
|
732 | 730 | return new CachedQueryResults( |
733 | 731 | $arr, |
734 | 732 | Metadata::ofCachingMetadata( |
735 | 733 | $maxAge ? |
736 | - CachingMetadata::ofMaximumAgeInSeconds( $maxAge ) : |
|
737 | - CachingMetadata::fresh() |
|
734 | + CachingMetadata::ofMaximumAgeInSeconds($maxAge) : CachingMetadata::fresh() |
|
738 | 735 | ) |
739 | 736 | ); |
740 | 737 | } else { |
741 | - $this->dataFactory->increment( 'wikibase.quality.constraints.sparql.error' ); |
|
738 | + $this->dataFactory->increment('wikibase.quality.constraints.sparql.error'); |
|
742 | 739 | |
743 | 740 | $this->dataFactory->increment( |
744 | 741 | "wikibase.quality.constraints.sparql.error.http.{$request->getStatus()}" |
745 | 742 | ); |
746 | 743 | |
747 | - if ( $this->isTimeout( $request->getContent() ) ) { |
|
744 | + if ($this->isTimeout($request->getContent())) { |
|
748 | 745 | $this->dataFactory->increment( |
749 | 746 | 'wikibase.quality.constraints.sparql.error.timeout' |
750 | 747 | ); |