@@ -162,58 +162,58 @@ discard block |
||
162 | 162 | $this->defaultUserAgent = $defaultUserAgent; |
163 | 163 | $this->requestFactory = $requestFactory; |
164 | 164 | $this->entityPrefixes = []; |
165 | - foreach ( $rdfVocabulary->entityNamespaceNames as $namespaceName ) { |
|
166 | - $this->entityPrefixes[] = $rdfVocabulary->getNamespaceURI( $namespaceName ); |
|
165 | + foreach ($rdfVocabulary->entityNamespaceNames as $namespaceName) { |
|
166 | + $this->entityPrefixes[] = $rdfVocabulary->getNamespaceURI($namespaceName); |
|
167 | 167 | } |
168 | 168 | |
169 | - $this->prefixes = $this->getQueryPrefixes( $rdfVocabulary ); |
|
169 | + $this->prefixes = $this->getQueryPrefixes($rdfVocabulary); |
|
170 | 170 | } |
171 | 171 | |
172 | - private function getQueryPrefixes( RdfVocabulary $rdfVocabulary ) { |
|
172 | + private function getQueryPrefixes(RdfVocabulary $rdfVocabulary) { |
|
173 | 173 | // TODO: it would probably be smarter that RdfVocubulary exposed these prefixes somehow |
174 | 174 | $prefixes = ''; |
175 | - foreach ( $rdfVocabulary->entityNamespaceNames as $sourceName => $namespaceName ) { |
|
175 | + foreach ($rdfVocabulary->entityNamespaceNames as $sourceName => $namespaceName) { |
|
176 | 176 | $prefixes .= <<<END |
177 | -PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI( $namespaceName )}>\n |
|
177 | +PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI($namespaceName)}>\n |
|
178 | 178 | END; |
179 | 179 | } |
180 | 180 | $prefixes .= <<<END |
181 | -PREFIX wds: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_STATEMENT )}> |
|
182 | -PREFIX wdv: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_VALUE )}>\n |
|
181 | +PREFIX wds: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_STATEMENT)}> |
|
182 | +PREFIX wdv: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_VALUE)}>\n |
|
183 | 183 | END; |
184 | 184 | |
185 | - foreach ( $rdfVocabulary->propertyNamespaceNames as $sourceName => $sourceNamespaces ) { |
|
185 | + foreach ($rdfVocabulary->propertyNamespaceNames as $sourceName => $sourceNamespaces) { |
|
186 | 186 | $namespaceName = $sourceNamespaces[RdfVocabulary::NSP_DIRECT_CLAIM]; |
187 | 187 | $prefixes .= <<<END |
188 | -PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI( $namespaceName )}>\n |
|
188 | +PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI($namespaceName)}>\n |
|
189 | 189 | END; |
190 | 190 | $namespaceName = $sourceNamespaces[RdfVocabulary::NSP_CLAIM]; |
191 | 191 | $prefixes .= <<<END |
192 | -PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI( $namespaceName )}>\n |
|
192 | +PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI($namespaceName)}>\n |
|
193 | 193 | END; |
194 | 194 | $namespaceName = $sourceNamespaces[RdfVocabulary::NSP_CLAIM_STATEMENT]; |
195 | 195 | $prefixes .= <<<END |
196 | -PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI( $namespaceName )}>\n |
|
196 | +PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI($namespaceName)}>\n |
|
197 | 197 | END; |
198 | 198 | $namespaceName = $sourceNamespaces[RdfVocabulary::NSP_QUALIFIER]; |
199 | 199 | $prefixes .= <<<END |
200 | -PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI( $namespaceName )}>\n |
|
200 | +PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI($namespaceName)}>\n |
|
201 | 201 | END; |
202 | 202 | $namespaceName = $sourceNamespaces[RdfVocabulary::NSP_QUALIFIER_VALUE]; |
203 | 203 | $prefixes .= <<<END |
204 | -PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI( $namespaceName )}>\n |
|
204 | +PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI($namespaceName)}>\n |
|
205 | 205 | END; |
206 | 206 | $namespaceName = $sourceNamespaces[RdfVocabulary::NSP_REFERENCE]; |
207 | 207 | $prefixes .= <<<END |
208 | -PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI( $namespaceName )}>\n |
|
208 | +PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI($namespaceName)}>\n |
|
209 | 209 | END; |
210 | 210 | $namespaceName = $sourceNamespaces[RdfVocabulary::NSP_REFERENCE_VALUE]; |
211 | 211 | $prefixes .= <<<END |
212 | -PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI( $namespaceName )}>\n |
|
212 | +PREFIX {$namespaceName}: <{$rdfVocabulary->getNamespaceURI($namespaceName)}>\n |
|
213 | 213 | END; |
214 | 214 | } |
215 | 215 | $prefixes .= <<<END |
216 | -PREFIX wikibase: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_ONTOLOGY )}>\n |
|
216 | +PREFIX wikibase: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_ONTOLOGY)}>\n |
|
217 | 217 | END; |
218 | 218 | return $prefixes; |
219 | 219 | } |
@@ -225,22 +225,21 @@ discard block |
||
225 | 225 | * @return CachedBool |
226 | 226 | * @throws SparqlHelperException if the query times out or some other error occurs |
227 | 227 | */ |
228 | - public function hasType( $id, array $classes ) { |
|
229 | - $subclassOfId = $this->config->get( 'WBQualityConstraintsSubclassOfId' ); |
|
228 | + public function hasType($id, array $classes) { |
|
229 | + $subclassOfId = $this->config->get('WBQualityConstraintsSubclassOfId'); |
|
230 | 230 | // TODO hint:gearing is a workaround for T168973 and can hopefully be removed eventually |
231 | - $gearingHint = $this->config->get( 'WBQualityConstraintsSparqlHasWikibaseSupport' ) ? |
|
232 | - ' hint:Prior hint:gearing "forward".' : |
|
233 | - ''; |
|
231 | + $gearingHint = $this->config->get('WBQualityConstraintsSparqlHasWikibaseSupport') ? |
|
232 | + ' hint:Prior hint:gearing "forward".' : ''; |
|
234 | 233 | |
235 | 234 | $metadatas = []; |
236 | 235 | |
237 | - foreach ( array_chunk( $classes, 20 ) as $classesChunk ) { |
|
238 | - $classesValues = implode( ' ', array_map( |
|
239 | - function( $class ) { |
|
240 | - return 'wd:' . $class; |
|
236 | + foreach (array_chunk($classes, 20) as $classesChunk) { |
|
237 | + $classesValues = implode(' ', array_map( |
|
238 | + function($class) { |
|
239 | + return 'wd:'.$class; |
|
241 | 240 | }, |
242 | 241 | $classesChunk |
243 | - ) ); |
|
242 | + )); |
|
244 | 243 | |
245 | 244 | $query = <<<EOF |
246 | 245 | ASK { |
@@ -250,19 +249,19 @@ discard block |
||
250 | 249 | } |
251 | 250 | EOF; |
252 | 251 | |
253 | - $result = $this->runQuery( $query ); |
|
252 | + $result = $this->runQuery($query); |
|
254 | 253 | $metadatas[] = $result->getMetadata(); |
255 | - if ( $result->getArray()['boolean'] ) { |
|
254 | + if ($result->getArray()['boolean']) { |
|
256 | 255 | return new CachedBool( |
257 | 256 | true, |
258 | - Metadata::merge( $metadatas ) |
|
257 | + Metadata::merge($metadatas) |
|
259 | 258 | ); |
260 | 259 | } |
261 | 260 | } |
262 | 261 | |
263 | 262 | return new CachedBool( |
264 | 263 | false, |
265 | - Metadata::merge( $metadatas ) |
|
264 | + Metadata::merge($metadatas) |
|
266 | 265 | ); |
267 | 266 | } |
268 | 267 | |
@@ -278,10 +277,10 @@ discard block |
||
278 | 277 | $ignoreDeprecatedStatements |
279 | 278 | ) { |
280 | 279 | $pid = $statement->getPropertyId()->serialize(); |
281 | - $guid = str_replace( '$', '-', $statement->getGuid() ); |
|
280 | + $guid = str_replace('$', '-', $statement->getGuid()); |
|
282 | 281 | |
283 | 282 | $deprecatedFilter = ''; |
284 | - if ( $ignoreDeprecatedStatements ) { |
|
283 | + if ($ignoreDeprecatedStatements) { |
|
285 | 284 | $deprecatedFilter = 'MINUS { ?otherStatement wikibase:rank wikibase:DeprecatedRank. }'; |
286 | 285 | } |
287 | 286 | |
@@ -300,9 +299,9 @@ discard block |
||
300 | 299 | LIMIT 10 |
301 | 300 | EOF; |
302 | 301 | |
303 | - $result = $this->runQuery( $query ); |
|
302 | + $result = $this->runQuery($query); |
|
304 | 303 | |
305 | - return $this->getOtherEntities( $result ); |
|
304 | + return $this->getOtherEntities($result); |
|
306 | 305 | } |
307 | 306 | |
308 | 307 | /** |
@@ -327,16 +326,15 @@ discard block |
||
327 | 326 | $dataType = $this->propertyDataTypeLookup->getDataTypeIdForProperty( |
328 | 327 | $snak->getPropertyId() |
329 | 328 | ); |
330 | - list( $value, $isFullValue ) = $this->getRdfLiteral( $dataType, $dataValue ); |
|
331 | - if ( $isFullValue ) { |
|
329 | + list($value, $isFullValue) = $this->getRdfLiteral($dataType, $dataValue); |
|
330 | + if ($isFullValue) { |
|
332 | 331 | $prefix .= 'v'; |
333 | 332 | } |
334 | 333 | $path = $type === Context::TYPE_QUALIFIER ? |
335 | - "$prefix:$pid" : |
|
336 | - "prov:wasDerivedFrom/$prefix:$pid"; |
|
334 | + "$prefix:$pid" : "prov:wasDerivedFrom/$prefix:$pid"; |
|
337 | 335 | |
338 | 336 | $deprecatedFilter = ''; |
339 | - if ( $ignoreDeprecatedStatements ) { |
|
337 | + if ($ignoreDeprecatedStatements) { |
|
340 | 338 | $deprecatedFilter = <<< EOF |
341 | 339 | MINUS { ?otherStatement wikibase:rank wikibase:DeprecatedRank. } |
342 | 340 | EOF; |
@@ -356,9 +354,9 @@ discard block |
||
356 | 354 | LIMIT 10 |
357 | 355 | EOF; |
358 | 356 | |
359 | - $result = $this->runQuery( $query ); |
|
357 | + $result = $this->runQuery($query); |
|
360 | 358 | |
361 | - return $this->getOtherEntities( $result ); |
|
359 | + return $this->getOtherEntities($result); |
|
362 | 360 | } |
363 | 361 | |
364 | 362 | /** |
@@ -368,8 +366,8 @@ discard block |
||
368 | 366 | * |
369 | 367 | * @return string |
370 | 368 | */ |
371 | - private function stringLiteral( $text ) { |
|
372 | - return '"' . strtr( $text, [ '"' => '\\"', '\\' => '\\\\' ] ) . '"'; |
|
369 | + private function stringLiteral($text) { |
|
370 | + return '"'.strtr($text, ['"' => '\\"', '\\' => '\\\\']).'"'; |
|
373 | 371 | } |
374 | 372 | |
375 | 373 | /** |
@@ -379,18 +377,18 @@ discard block |
||
379 | 377 | * |
380 | 378 | * @return CachedEntityIds |
381 | 379 | */ |
382 | - private function getOtherEntities( CachedQueryResults $results ) { |
|
383 | - return new CachedEntityIds( array_map( |
|
384 | - function ( $resultBindings ) { |
|
380 | + private function getOtherEntities(CachedQueryResults $results) { |
|
381 | + return new CachedEntityIds(array_map( |
|
382 | + function($resultBindings) { |
|
385 | 383 | $entityIRI = $resultBindings['otherEntity']['value']; |
386 | - foreach ( $this->entityPrefixes as $entityPrefix ) { |
|
387 | - $entityPrefixLength = strlen( $entityPrefix ); |
|
388 | - if ( substr( $entityIRI, 0, $entityPrefixLength ) === $entityPrefix ) { |
|
384 | + foreach ($this->entityPrefixes as $entityPrefix) { |
|
385 | + $entityPrefixLength = strlen($entityPrefix); |
|
386 | + if (substr($entityIRI, 0, $entityPrefixLength) === $entityPrefix) { |
|
389 | 387 | try { |
390 | 388 | return $this->entityIdParser->parse( |
391 | - substr( $entityIRI, $entityPrefixLength ) |
|
389 | + substr($entityIRI, $entityPrefixLength) |
|
392 | 390 | ); |
393 | - } catch ( EntityIdParsingException $e ) { |
|
391 | + } catch (EntityIdParsingException $e) { |
|
394 | 392 | // fall through |
395 | 393 | } |
396 | 394 | } |
@@ -401,7 +399,7 @@ discard block |
||
401 | 399 | return null; |
402 | 400 | }, |
403 | 401 | $results->getArray()['results']['bindings'] |
404 | - ), $results->getMetadata() ); |
|
402 | + ), $results->getMetadata()); |
|
405 | 403 | } |
406 | 404 | |
407 | 405 | // @codingStandardsIgnoreStart cyclomatic complexity of this function is too high |
@@ -414,49 +412,49 @@ discard block |
||
414 | 412 | * @return array the literal or IRI as a string in SPARQL syntax, |
415 | 413 | * and a boolean indicating whether it refers to a full value node or not |
416 | 414 | */ |
417 | - private function getRdfLiteral( $dataType, DataValue $dataValue ) { |
|
418 | - switch ( $dataType ) { |
|
415 | + private function getRdfLiteral($dataType, DataValue $dataValue) { |
|
416 | + switch ($dataType) { |
|
419 | 417 | case 'string': |
420 | 418 | case 'external-id': |
421 | - return [ $this->stringLiteral( $dataValue->getValue() ), false ]; |
|
419 | + return [$this->stringLiteral($dataValue->getValue()), false]; |
|
422 | 420 | case 'commonsMedia': |
423 | - $url = $this->rdfVocabulary->getMediaFileURI( $dataValue->getValue() ); |
|
424 | - return [ '<' . $url . '>', false ]; |
|
421 | + $url = $this->rdfVocabulary->getMediaFileURI($dataValue->getValue()); |
|
422 | + return ['<'.$url.'>', false]; |
|
425 | 423 | case 'geo-shape': |
426 | - $url = $this->rdfVocabulary->getGeoShapeURI( $dataValue->getValue() ); |
|
427 | - return [ '<' . $url . '>', false ]; |
|
424 | + $url = $this->rdfVocabulary->getGeoShapeURI($dataValue->getValue()); |
|
425 | + return ['<'.$url.'>', false]; |
|
428 | 426 | case 'tabular-data': |
429 | - $url = $this->rdfVocabulary->getTabularDataURI( $dataValue->getValue() ); |
|
430 | - return [ '<' . $url . '>', false ]; |
|
427 | + $url = $this->rdfVocabulary->getTabularDataURI($dataValue->getValue()); |
|
428 | + return ['<'.$url.'>', false]; |
|
431 | 429 | case 'url': |
432 | 430 | $url = $dataValue->getValue(); |
433 | - if ( !preg_match( '/^[^<>"{}\\\\|^`\\x00-\\x20]*$/D', $url ) ) { |
|
431 | + if (!preg_match('/^[^<>"{}\\\\|^`\\x00-\\x20]*$/D', $url)) { |
|
434 | 432 | // not a valid URL for SPARQL (see SPARQL spec, production 139 IRIREF) |
435 | 433 | // such an URL should never reach us, so just throw |
436 | - throw new InvalidArgumentException( 'invalid URL: ' . $url ); |
|
434 | + throw new InvalidArgumentException('invalid URL: '.$url); |
|
437 | 435 | } |
438 | - return [ '<' . $url . '>', false ]; |
|
436 | + return ['<'.$url.'>', false]; |
|
439 | 437 | case 'wikibase-item': |
440 | 438 | case 'wikibase-property': |
441 | 439 | /** @var EntityIdValue $dataValue */ |
442 | 440 | '@phan-var EntityIdValue $dataValue'; |
443 | - return [ 'wd:' . $dataValue->getEntityId()->getSerialization(), false ]; |
|
441 | + return ['wd:'.$dataValue->getEntityId()->getSerialization(), false]; |
|
444 | 442 | case 'monolingualtext': |
445 | 443 | /** @var MonolingualTextValue $dataValue */ |
446 | 444 | '@phan-var MonolingualTextValue $dataValue'; |
447 | 445 | $lang = $dataValue->getLanguageCode(); |
448 | - if ( !preg_match( '/^[a-zA-Z]+(-[a-zA-Z0-9]+)*$/D', $lang ) ) { |
|
446 | + if (!preg_match('/^[a-zA-Z]+(-[a-zA-Z0-9]+)*$/D', $lang)) { |
|
449 | 447 | // not a valid language tag for SPARQL (see SPARQL spec, production 145 LANGTAG) |
450 | 448 | // such a language tag should never reach us, so just throw |
451 | - throw new InvalidArgumentException( 'invalid language tag: ' . $lang ); |
|
449 | + throw new InvalidArgumentException('invalid language tag: '.$lang); |
|
452 | 450 | } |
453 | - return [ $this->stringLiteral( $dataValue->getText() ) . '@' . $lang, false ]; |
|
451 | + return [$this->stringLiteral($dataValue->getText()).'@'.$lang, false]; |
|
454 | 452 | case 'globe-coordinate': |
455 | 453 | case 'quantity': |
456 | 454 | case 'time': |
457 | - return [ 'wdv:' . $dataValue->getHash(), true ]; |
|
455 | + return ['wdv:'.$dataValue->getHash(), true]; |
|
458 | 456 | default: |
459 | - throw new InvalidArgumentException( 'unknown data type: ' . $dataType ); |
|
457 | + throw new InvalidArgumentException('unknown data type: '.$dataType); |
|
460 | 458 | } |
461 | 459 | } |
462 | 460 | // @codingStandardsIgnoreEnd |
@@ -469,44 +467,44 @@ discard block |
||
469 | 467 | * @throws SparqlHelperException if the query times out or some other error occurs |
470 | 468 | * @throws ConstraintParameterException if the $regex is invalid |
471 | 469 | */ |
472 | - public function matchesRegularExpression( $text, $regex ) { |
|
470 | + public function matchesRegularExpression($text, $regex) { |
|
473 | 471 | // caching wrapper around matchesRegularExpressionWithSparql |
474 | 472 | |
475 | - $textHash = hash( 'sha256', $text ); |
|
473 | + $textHash = hash('sha256', $text); |
|
476 | 474 | $cacheKey = $this->cache->makeKey( |
477 | 475 | 'WikibaseQualityConstraints', // extension |
478 | 476 | 'regex', // action |
479 | 477 | 'WDQS-Java', // regex flavor |
480 | - hash( 'sha256', $regex ) |
|
478 | + hash('sha256', $regex) |
|
481 | 479 | ); |
482 | - $cacheMapSize = $this->config->get( 'WBQualityConstraintsFormatCacheMapSize' ); |
|
480 | + $cacheMapSize = $this->config->get('WBQualityConstraintsFormatCacheMapSize'); |
|
483 | 481 | |
484 | 482 | $cacheMapArray = $this->cache->getWithSetCallback( |
485 | 483 | $cacheKey, |
486 | 484 | WANObjectCache::TTL_DAY, |
487 | - function( $cacheMapArray ) use ( $text, $regex, $textHash, $cacheMapSize ) { |
|
485 | + function($cacheMapArray) use ($text, $regex, $textHash, $cacheMapSize) { |
|
488 | 486 | // Initialize the cache map if not set |
489 | - if ( $cacheMapArray === false ) { |
|
487 | + if ($cacheMapArray === false) { |
|
490 | 488 | $key = 'wikibase.quality.constraints.regex.cache.refresh.init'; |
491 | - $this->dataFactory->increment( $key ); |
|
489 | + $this->dataFactory->increment($key); |
|
492 | 490 | return []; |
493 | 491 | } |
494 | 492 | |
495 | 493 | $key = 'wikibase.quality.constraints.regex.cache.refresh'; |
496 | - $this->dataFactory->increment( $key ); |
|
497 | - $cacheMap = MapCacheLRU::newFromArray( $cacheMapArray, $cacheMapSize ); |
|
498 | - if ( $cacheMap->has( $textHash ) ) { |
|
494 | + $this->dataFactory->increment($key); |
|
495 | + $cacheMap = MapCacheLRU::newFromArray($cacheMapArray, $cacheMapSize); |
|
496 | + if ($cacheMap->has($textHash)) { |
|
499 | 497 | $key = 'wikibase.quality.constraints.regex.cache.refresh.hit'; |
500 | - $this->dataFactory->increment( $key ); |
|
501 | - $cacheMap->get( $textHash ); // ping cache |
|
498 | + $this->dataFactory->increment($key); |
|
499 | + $cacheMap->get($textHash); // ping cache |
|
502 | 500 | } else { |
503 | 501 | $key = 'wikibase.quality.constraints.regex.cache.refresh.miss'; |
504 | - $this->dataFactory->increment( $key ); |
|
502 | + $this->dataFactory->increment($key); |
|
505 | 503 | try { |
506 | - $matches = $this->matchesRegularExpressionWithSparql( $text, $regex ); |
|
507 | - } catch ( ConstraintParameterException $e ) { |
|
508 | - $matches = $this->serializeConstraintParameterException( $e ); |
|
509 | - } catch ( SparqlHelperException $e ) { |
|
504 | + $matches = $this->matchesRegularExpressionWithSparql($text, $regex); |
|
505 | + } catch (ConstraintParameterException $e) { |
|
506 | + $matches = $this->serializeConstraintParameterException($e); |
|
507 | + } catch (SparqlHelperException $e) { |
|
510 | 508 | // don’t cache this |
511 | 509 | return $cacheMap->toArray(); |
512 | 510 | } |
@@ -530,42 +528,42 @@ discard block |
||
530 | 528 | ] |
531 | 529 | ); |
532 | 530 | |
533 | - if ( isset( $cacheMapArray[$textHash] ) ) { |
|
531 | + if (isset($cacheMapArray[$textHash])) { |
|
534 | 532 | $key = 'wikibase.quality.constraints.regex.cache.hit'; |
535 | - $this->dataFactory->increment( $key ); |
|
533 | + $this->dataFactory->increment($key); |
|
536 | 534 | $matches = $cacheMapArray[$textHash]; |
537 | - if ( is_bool( $matches ) ) { |
|
535 | + if (is_bool($matches)) { |
|
538 | 536 | return $matches; |
539 | - } elseif ( is_array( $matches ) && |
|
540 | - $matches['type'] == ConstraintParameterException::class ) { |
|
541 | - throw $this->deserializeConstraintParameterException( $matches ); |
|
537 | + } elseif (is_array($matches) && |
|
538 | + $matches['type'] == ConstraintParameterException::class) { |
|
539 | + throw $this->deserializeConstraintParameterException($matches); |
|
542 | 540 | } else { |
543 | 541 | throw new MWException( |
544 | - 'Value of unknown type in object cache (' . |
|
545 | - 'cache key: ' . $cacheKey . ', ' . |
|
546 | - 'cache map key: ' . $textHash . ', ' . |
|
547 | - 'value type: ' . gettype( $matches ) . ')' |
|
542 | + 'Value of unknown type in object cache ('. |
|
543 | + 'cache key: '.$cacheKey.', '. |
|
544 | + 'cache map key: '.$textHash.', '. |
|
545 | + 'value type: '.gettype($matches).')' |
|
548 | 546 | ); |
549 | 547 | } |
550 | 548 | } else { |
551 | 549 | $key = 'wikibase.quality.constraints.regex.cache.miss'; |
552 | - $this->dataFactory->increment( $key ); |
|
553 | - return $this->matchesRegularExpressionWithSparql( $text, $regex ); |
|
550 | + $this->dataFactory->increment($key); |
|
551 | + return $this->matchesRegularExpressionWithSparql($text, $regex); |
|
554 | 552 | } |
555 | 553 | } |
556 | 554 | |
557 | - private function serializeConstraintParameterException( ConstraintParameterException $cpe ) { |
|
555 | + private function serializeConstraintParameterException(ConstraintParameterException $cpe) { |
|
558 | 556 | return [ |
559 | 557 | 'type' => ConstraintParameterException::class, |
560 | - 'violationMessage' => $this->violationMessageSerializer->serialize( $cpe->getViolationMessage() ), |
|
558 | + 'violationMessage' => $this->violationMessageSerializer->serialize($cpe->getViolationMessage()), |
|
561 | 559 | ]; |
562 | 560 | } |
563 | 561 | |
564 | - private function deserializeConstraintParameterException( array $serialization ) { |
|
562 | + private function deserializeConstraintParameterException(array $serialization) { |
|
565 | 563 | $message = $this->violationMessageDeserializer->deserialize( |
566 | 564 | $serialization['violationMessage'] |
567 | 565 | ); |
568 | - return new ConstraintParameterException( $message ); |
|
566 | + return new ConstraintParameterException($message); |
|
569 | 567 | } |
570 | 568 | |
571 | 569 | /** |
@@ -579,25 +577,25 @@ discard block |
||
579 | 577 | * @throws SparqlHelperException if the query times out or some other error occurs |
580 | 578 | * @throws ConstraintParameterException if the $regex is invalid |
581 | 579 | */ |
582 | - public function matchesRegularExpressionWithSparql( $text, $regex ) { |
|
583 | - $textStringLiteral = $this->stringLiteral( $text ); |
|
584 | - $regexStringLiteral = $this->stringLiteral( '^(?:' . $regex . ')$' ); |
|
580 | + public function matchesRegularExpressionWithSparql($text, $regex) { |
|
581 | + $textStringLiteral = $this->stringLiteral($text); |
|
582 | + $regexStringLiteral = $this->stringLiteral('^(?:'.$regex.')$'); |
|
585 | 583 | |
586 | 584 | $query = <<<EOF |
587 | 585 | SELECT (REGEX($textStringLiteral, $regexStringLiteral) AS ?matches) {} |
588 | 586 | EOF; |
589 | 587 | |
590 | - $result = $this->runQuery( $query, false ); |
|
588 | + $result = $this->runQuery($query, false); |
|
591 | 589 | |
592 | 590 | $vars = $result->getArray()['results']['bindings'][0]; |
593 | - if ( array_key_exists( 'matches', $vars ) ) { |
|
591 | + if (array_key_exists('matches', $vars)) { |
|
594 | 592 | // true or false ⇒ regex okay, text matches or not |
595 | 593 | return $vars['matches']['value'] === 'true'; |
596 | 594 | } else { |
597 | 595 | // empty result: regex broken |
598 | 596 | throw new ConstraintParameterException( |
599 | - ( new ViolationMessage( 'wbqc-violation-message-parameter-regex' ) ) |
|
600 | - ->withInlineCode( $regex, Role::CONSTRAINT_PARAMETER_VALUE ) |
|
597 | + (new ViolationMessage('wbqc-violation-message-parameter-regex')) |
|
598 | + ->withInlineCode($regex, Role::CONSTRAINT_PARAMETER_VALUE) |
|
601 | 599 | ); |
602 | 600 | } |
603 | 601 | } |
@@ -609,14 +607,14 @@ discard block |
||
609 | 607 | * |
610 | 608 | * @return boolean |
611 | 609 | */ |
612 | - public function isTimeout( $responseContent ) { |
|
613 | - $timeoutRegex = implode( '|', array_map( |
|
614 | - function ( $fqn ) { |
|
615 | - return preg_quote( $fqn, '/' ); |
|
610 | + public function isTimeout($responseContent) { |
|
611 | + $timeoutRegex = implode('|', array_map( |
|
612 | + function($fqn) { |
|
613 | + return preg_quote($fqn, '/'); |
|
616 | 614 | }, |
617 | - $this->config->get( 'WBQualityConstraintsSparqlTimeoutExceptionClasses' ) |
|
618 | - ) ); |
|
619 | - return (bool)preg_match( '/' . $timeoutRegex . '/', $responseContent ); |
|
615 | + $this->config->get('WBQualityConstraintsSparqlTimeoutExceptionClasses') |
|
616 | + )); |
|
617 | + return (bool) preg_match('/'.$timeoutRegex.'/', $responseContent); |
|
620 | 618 | } |
621 | 619 | |
622 | 620 | /** |
@@ -628,17 +626,17 @@ discard block |
||
628 | 626 | * @return int|boolean the max-age (in seconds) |
629 | 627 | * or a plain boolean if no max-age can be determined |
630 | 628 | */ |
631 | - public function getCacheMaxAge( $responseHeaders ) { |
|
629 | + public function getCacheMaxAge($responseHeaders) { |
|
632 | 630 | if ( |
633 | - array_key_exists( 'x-cache-status', $responseHeaders ) && |
|
634 | - preg_match( '/^hit(?:-.*)?$/', $responseHeaders['x-cache-status'][0] ) |
|
631 | + array_key_exists('x-cache-status', $responseHeaders) && |
|
632 | + preg_match('/^hit(?:-.*)?$/', $responseHeaders['x-cache-status'][0]) |
|
635 | 633 | ) { |
636 | 634 | $maxage = []; |
637 | 635 | if ( |
638 | - array_key_exists( 'cache-control', $responseHeaders ) && |
|
639 | - preg_match( '/\bmax-age=(\d+)\b/', $responseHeaders['cache-control'][0], $maxage ) |
|
636 | + array_key_exists('cache-control', $responseHeaders) && |
|
637 | + preg_match('/\bmax-age=(\d+)\b/', $responseHeaders['cache-control'][0], $maxage) |
|
640 | 638 | ) { |
641 | - return intval( $maxage[1] ); |
|
639 | + return intval($maxage[1]); |
|
642 | 640 | } else { |
643 | 641 | return true; |
644 | 642 | } |
@@ -659,34 +657,34 @@ discard block |
||
659 | 657 | * or SparlHelper::EMPTY_RETRY_AFTER if there is an empty Retry-After |
660 | 658 | * or SparlHelper::INVALID_RETRY_AFTER if there is something wrong with the format |
661 | 659 | */ |
662 | - public function getThrottling( MWHttpRequest $request ) { |
|
663 | - $retryAfterValue = $request->getResponseHeader( 'Retry-After' ); |
|
664 | - if ( $retryAfterValue === null ) { |
|
660 | + public function getThrottling(MWHttpRequest $request) { |
|
661 | + $retryAfterValue = $request->getResponseHeader('Retry-After'); |
|
662 | + if ($retryAfterValue === null) { |
|
665 | 663 | return self::NO_RETRY_AFTER; |
666 | 664 | } |
667 | 665 | |
668 | - $trimmedRetryAfterValue = trim( $retryAfterValue ); |
|
669 | - if ( empty( $trimmedRetryAfterValue ) ) { |
|
666 | + $trimmedRetryAfterValue = trim($retryAfterValue); |
|
667 | + if (empty($trimmedRetryAfterValue)) { |
|
670 | 668 | return self::EMPTY_RETRY_AFTER; |
671 | 669 | } |
672 | 670 | |
673 | - if ( is_numeric( $trimmedRetryAfterValue ) ) { |
|
674 | - $delaySeconds = (int)$trimmedRetryAfterValue; |
|
675 | - if ( $delaySeconds >= 0 ) { |
|
676 | - return $this->getTimestampInFuture( new DateInterval( 'PT' . $delaySeconds . 'S' ) ); |
|
671 | + if (is_numeric($trimmedRetryAfterValue)) { |
|
672 | + $delaySeconds = (int) $trimmedRetryAfterValue; |
|
673 | + if ($delaySeconds >= 0) { |
|
674 | + return $this->getTimestampInFuture(new DateInterval('PT'.$delaySeconds.'S')); |
|
677 | 675 | } |
678 | 676 | } else { |
679 | - $return = strtotime( $trimmedRetryAfterValue ); |
|
680 | - if ( !empty( $return ) ) { |
|
681 | - return new ConvertibleTimestamp( $return ); |
|
677 | + $return = strtotime($trimmedRetryAfterValue); |
|
678 | + if (!empty($return)) { |
|
679 | + return new ConvertibleTimestamp($return); |
|
682 | 680 | } |
683 | 681 | } |
684 | 682 | return self::INVALID_RETRY_AFTER; |
685 | 683 | } |
686 | 684 | |
687 | - private function getTimestampInFuture( DateInterval $delta ) { |
|
685 | + private function getTimestampInFuture(DateInterval $delta) { |
|
688 | 686 | $now = new ConvertibleTimestamp(); |
689 | - return new ConvertibleTimestamp( $now->timestamp->add( $delta ) ); |
|
687 | + return new ConvertibleTimestamp($now->timestamp->add($delta)); |
|
690 | 688 | } |
691 | 689 | |
692 | 690 | /** |
@@ -700,97 +698,96 @@ discard block |
||
700 | 698 | * |
701 | 699 | * @throws SparqlHelperException if the query times out or some other error occurs |
702 | 700 | */ |
703 | - public function runQuery( $query, $needsPrefixes = true ) { |
|
701 | + public function runQuery($query, $needsPrefixes = true) { |
|
704 | 702 | |
705 | - if ( $this->throttlingLock->isLocked( self::EXPIRY_LOCK_ID ) ) { |
|
706 | - $this->dataFactory->increment( 'wikibase.quality.constraints.sparql.throttling' ); |
|
703 | + if ($this->throttlingLock->isLocked(self::EXPIRY_LOCK_ID)) { |
|
704 | + $this->dataFactory->increment('wikibase.quality.constraints.sparql.throttling'); |
|
707 | 705 | throw new TooManySparqlRequestsException(); |
708 | 706 | } |
709 | 707 | |
710 | - $endpoint = $this->config->get( 'WBQualityConstraintsSparqlEndpoint' ); |
|
711 | - $maxQueryTimeMillis = $this->config->get( 'WBQualityConstraintsSparqlMaxMillis' ); |
|
712 | - $fallbackBlockDuration = (int)$this->config->get( 'WBQualityConstraintsSparqlThrottlingFallbackDuration' ); |
|
708 | + $endpoint = $this->config->get('WBQualityConstraintsSparqlEndpoint'); |
|
709 | + $maxQueryTimeMillis = $this->config->get('WBQualityConstraintsSparqlMaxMillis'); |
|
710 | + $fallbackBlockDuration = (int) $this->config->get('WBQualityConstraintsSparqlThrottlingFallbackDuration'); |
|
713 | 711 | |
714 | - if ( $fallbackBlockDuration < 0 ) { |
|
715 | - throw new InvalidArgumentException( 'Fallback duration must be positive int but is: ' . |
|
716 | - $fallbackBlockDuration ); |
|
712 | + if ($fallbackBlockDuration < 0) { |
|
713 | + throw new InvalidArgumentException('Fallback duration must be positive int but is: '. |
|
714 | + $fallbackBlockDuration); |
|
717 | 715 | } |
718 | 716 | |
719 | - if ( $this->config->get( 'WBQualityConstraintsSparqlHasWikibaseSupport' ) ) { |
|
717 | + if ($this->config->get('WBQualityConstraintsSparqlHasWikibaseSupport')) { |
|
720 | 718 | $needsPrefixes = false; |
721 | 719 | } |
722 | 720 | |
723 | - if ( $needsPrefixes ) { |
|
724 | - $query = $this->prefixes . $query; |
|
721 | + if ($needsPrefixes) { |
|
722 | + $query = $this->prefixes.$query; |
|
725 | 723 | } |
726 | - $query = "#wbqc\n" . $query; |
|
724 | + $query = "#wbqc\n".$query; |
|
727 | 725 | |
728 | - $url = $endpoint . '?' . http_build_query( |
|
726 | + $url = $endpoint.'?'.http_build_query( |
|
729 | 727 | [ |
730 | 728 | 'query' => $query, |
731 | 729 | 'format' => 'json', |
732 | 730 | 'maxQueryTimeMillis' => $maxQueryTimeMillis, |
733 | 731 | ], |
734 | - null, ini_get( 'arg_separator.output' ), |
|
732 | + null, ini_get('arg_separator.output'), |
|
735 | 733 | // encode spaces with %20, not + |
736 | 734 | PHP_QUERY_RFC3986 |
737 | 735 | ); |
738 | 736 | |
739 | 737 | $options = [ |
740 | 738 | 'method' => 'GET', |
741 | - 'timeout' => (int)round( ( $maxQueryTimeMillis + 1000 ) / 1000 ), |
|
739 | + 'timeout' => (int) round(($maxQueryTimeMillis + 1000) / 1000), |
|
742 | 740 | 'connectTimeout' => 'default', |
743 | 741 | 'userAgent' => $this->defaultUserAgent, |
744 | 742 | ]; |
745 | - $request = $this->requestFactory->create( $url, $options ); |
|
746 | - $startTime = microtime( true ); |
|
743 | + $request = $this->requestFactory->create($url, $options); |
|
744 | + $startTime = microtime(true); |
|
747 | 745 | $status = $request->execute(); |
748 | - $endTime = microtime( true ); |
|
746 | + $endTime = microtime(true); |
|
749 | 747 | $this->dataFactory->timing( |
750 | 748 | 'wikibase.quality.constraints.sparql.timing', |
751 | - ( $endTime - $startTime ) * 1000 |
|
749 | + ($endTime - $startTime) * 1000 |
|
752 | 750 | ); |
753 | 751 | |
754 | - if ( $request->getStatus() === self::HTTP_TOO_MANY_REQUESTS ) { |
|
755 | - $this->dataFactory->increment( 'wikibase.quality.constraints.sparql.throttling' ); |
|
756 | - $throttlingUntil = $this->getThrottling( $request ); |
|
757 | - if ( !( $throttlingUntil instanceof ConvertibleTimestamp ) ) { |
|
758 | - $this->loggingHelper->logSparqlHelperTooManyRequestsRetryAfterInvalid( $request ); |
|
752 | + if ($request->getStatus() === self::HTTP_TOO_MANY_REQUESTS) { |
|
753 | + $this->dataFactory->increment('wikibase.quality.constraints.sparql.throttling'); |
|
754 | + $throttlingUntil = $this->getThrottling($request); |
|
755 | + if (!($throttlingUntil instanceof ConvertibleTimestamp)) { |
|
756 | + $this->loggingHelper->logSparqlHelperTooManyRequestsRetryAfterInvalid($request); |
|
759 | 757 | $this->throttlingLock->lock( |
760 | 758 | self::EXPIRY_LOCK_ID, |
761 | - $this->getTimestampInFuture( new DateInterval( 'PT' . $fallbackBlockDuration . 'S' ) ) |
|
759 | + $this->getTimestampInFuture(new DateInterval('PT'.$fallbackBlockDuration.'S')) |
|
762 | 760 | ); |
763 | 761 | } else { |
764 | - $this->loggingHelper->logSparqlHelperTooManyRequestsRetryAfterPresent( $throttlingUntil, $request ); |
|
765 | - $this->throttlingLock->lock( self::EXPIRY_LOCK_ID, $throttlingUntil ); |
|
762 | + $this->loggingHelper->logSparqlHelperTooManyRequestsRetryAfterPresent($throttlingUntil, $request); |
|
763 | + $this->throttlingLock->lock(self::EXPIRY_LOCK_ID, $throttlingUntil); |
|
766 | 764 | } |
767 | 765 | throw new TooManySparqlRequestsException(); |
768 | 766 | } |
769 | 767 | |
770 | - $maxAge = $this->getCacheMaxAge( $request->getResponseHeaders() ); |
|
771 | - if ( $maxAge ) { |
|
772 | - $this->dataFactory->increment( 'wikibase.quality.constraints.sparql.cached' ); |
|
768 | + $maxAge = $this->getCacheMaxAge($request->getResponseHeaders()); |
|
769 | + if ($maxAge) { |
|
770 | + $this->dataFactory->increment('wikibase.quality.constraints.sparql.cached'); |
|
773 | 771 | } |
774 | 772 | |
775 | - if ( $status->isOK() ) { |
|
773 | + if ($status->isOK()) { |
|
776 | 774 | $json = $request->getContent(); |
777 | - $arr = json_decode( $json, true ); |
|
775 | + $arr = json_decode($json, true); |
|
778 | 776 | return new CachedQueryResults( |
779 | 777 | $arr, |
780 | 778 | Metadata::ofCachingMetadata( |
781 | 779 | $maxAge ? |
782 | - CachingMetadata::ofMaximumAgeInSeconds( $maxAge ) : |
|
783 | - CachingMetadata::fresh() |
|
780 | + CachingMetadata::ofMaximumAgeInSeconds($maxAge) : CachingMetadata::fresh() |
|
784 | 781 | ) |
785 | 782 | ); |
786 | 783 | } else { |
787 | - $this->dataFactory->increment( 'wikibase.quality.constraints.sparql.error' ); |
|
784 | + $this->dataFactory->increment('wikibase.quality.constraints.sparql.error'); |
|
788 | 785 | |
789 | 786 | $this->dataFactory->increment( |
790 | 787 | "wikibase.quality.constraints.sparql.error.http.{$request->getStatus()}" |
791 | 788 | ); |
792 | 789 | |
793 | - if ( $this->isTimeout( $request->getContent() ) ) { |
|
790 | + if ($this->isTimeout($request->getContent())) { |
|
794 | 791 | $this->dataFactory->increment( |
795 | 792 | 'wikibase.quality.constraints.sparql.error.timeout' |
796 | 793 | ); |