@@ -107,18 +107,18 @@ discard block |
||
107 | 107 | $this->violationMessageDeserializer = $violationMessageDeserializer; |
108 | 108 | $this->dataFactory = $dataFactory; |
109 | 109 | |
110 | - $this->entityPrefix = $rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_ENTITY ); |
|
110 | + $this->entityPrefix = $rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_ENTITY); |
|
111 | 111 | $this->prefixes = <<<EOT |
112 | -PREFIX wd: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_ENTITY )}> |
|
113 | -PREFIX wds: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_STATEMENT )}> |
|
114 | -PREFIX wdt: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_DIRECT_CLAIM )}> |
|
115 | -PREFIX wdv: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_VALUE )}> |
|
116 | -PREFIX p: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_CLAIM )}> |
|
117 | -PREFIX ps: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_CLAIM_STATEMENT )}> |
|
118 | -PREFIX pq: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_QUALIFIER )}> |
|
119 | -PREFIX pqv: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_QUALIFIER_VALUE )}> |
|
120 | -PREFIX pr: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_REFERENCE )}> |
|
121 | -PREFIX prv: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_REFERENCE_VALUE )}> |
|
112 | +PREFIX wd: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_ENTITY)}> |
|
113 | +PREFIX wds: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_STATEMENT)}> |
|
114 | +PREFIX wdt: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_DIRECT_CLAIM)}> |
|
115 | +PREFIX wdv: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_VALUE)}> |
|
116 | +PREFIX p: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_CLAIM)}> |
|
117 | +PREFIX ps: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_CLAIM_STATEMENT)}> |
|
118 | +PREFIX pq: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_QUALIFIER)}> |
|
119 | +PREFIX pqv: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_QUALIFIER_VALUE)}> |
|
120 | +PREFIX pr: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_REFERENCE)}> |
|
121 | +PREFIX prv: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_REFERENCE_VALUE)}> |
|
122 | 122 | PREFIX wikibase: <http://wikiba.se/ontology#> |
123 | 123 | PREFIX wikibase-beta: <http://wikiba.se/ontology-beta#> |
124 | 124 | EOT; |
@@ -133,21 +133,21 @@ discard block |
||
133 | 133 | * @return CachedBool |
134 | 134 | * @throws SparqlHelperException if the query times out or some other error occurs |
135 | 135 | */ |
136 | - public function hasType( $id, array $classes, $withInstance ) { |
|
137 | - $instanceOfId = $this->config->get( 'WBQualityConstraintsInstanceOfId' ); |
|
138 | - $subclassOfId = $this->config->get( 'WBQualityConstraintsSubclassOfId' ); |
|
136 | + public function hasType($id, array $classes, $withInstance) { |
|
137 | + $instanceOfId = $this->config->get('WBQualityConstraintsInstanceOfId'); |
|
138 | + $subclassOfId = $this->config->get('WBQualityConstraintsSubclassOfId'); |
|
139 | 139 | |
140 | - $path = ( $withInstance ? "wdt:$instanceOfId/" : "" ) . "wdt:$subclassOfId*"; |
|
140 | + $path = ($withInstance ? "wdt:$instanceOfId/" : "")."wdt:$subclassOfId*"; |
|
141 | 141 | |
142 | 142 | $metadatas = []; |
143 | 143 | |
144 | - foreach ( array_chunk( $classes, 20 ) as $classesChunk ) { |
|
145 | - $classesValues = implode( ' ', array_map( |
|
146 | - function( $class ) { |
|
147 | - return 'wd:' . $class; |
|
144 | + foreach (array_chunk($classes, 20) as $classesChunk) { |
|
145 | + $classesValues = implode(' ', array_map( |
|
146 | + function($class) { |
|
147 | + return 'wd:'.$class; |
|
148 | 148 | }, |
149 | 149 | $classesChunk |
150 | - ) ); |
|
150 | + )); |
|
151 | 151 | |
152 | 152 | $query = <<<EOF |
153 | 153 | ASK { |
@@ -158,19 +158,19 @@ discard block |
||
158 | 158 | EOF; |
159 | 159 | // TODO hint:gearing is a workaround for T168973 and can hopefully be removed eventually |
160 | 160 | |
161 | - $result = $this->runQuery( $query ); |
|
161 | + $result = $this->runQuery($query); |
|
162 | 162 | $metadatas[] = $result->getMetadata(); |
163 | - if ( $result->getArray()['boolean'] ) { |
|
163 | + if ($result->getArray()['boolean']) { |
|
164 | 164 | return new CachedBool( |
165 | 165 | true, |
166 | - Metadata::merge( $metadatas ) |
|
166 | + Metadata::merge($metadatas) |
|
167 | 167 | ); |
168 | 168 | } |
169 | 169 | } |
170 | 170 | |
171 | 171 | return new CachedBool( |
172 | 172 | false, |
173 | - Metadata::merge( $metadatas ) |
|
173 | + Metadata::merge($metadatas) |
|
174 | 174 | ); |
175 | 175 | } |
176 | 176 | |
@@ -186,10 +186,10 @@ discard block |
||
186 | 186 | $ignoreDeprecatedStatements |
187 | 187 | ) { |
188 | 188 | $pid = $statement->getPropertyId()->serialize(); |
189 | - $guid = str_replace( '$', '-', $statement->getGuid() ); |
|
189 | + $guid = str_replace('$', '-', $statement->getGuid()); |
|
190 | 190 | |
191 | 191 | $deprecatedFilter = ''; |
192 | - if ( $ignoreDeprecatedStatements ) { |
|
192 | + if ($ignoreDeprecatedStatements) { |
|
193 | 193 | $deprecatedFilter .= 'MINUS { ?otherStatement wikibase:rank wikibase:DeprecatedRank. }'; |
194 | 194 | $deprecatedFilter .= 'MINUS { ?otherStatement wikibase-beta:rank wikibase-beta:DeprecatedRank. }'; |
195 | 195 | } |
@@ -209,9 +209,9 @@ discard block |
||
209 | 209 | LIMIT 10 |
210 | 210 | EOF; |
211 | 211 | |
212 | - $result = $this->runQuery( $query ); |
|
212 | + $result = $this->runQuery($query); |
|
213 | 213 | |
214 | - return $this->getOtherEntities( $result ); |
|
214 | + return $this->getOtherEntities($result); |
|
215 | 215 | } |
216 | 216 | |
217 | 217 | /** |
@@ -236,16 +236,15 @@ discard block |
||
236 | 236 | $dataType = $this->propertyDataTypeLookup->getDataTypeIdForProperty( |
237 | 237 | $snak->getPropertyId() |
238 | 238 | ); |
239 | - list( $value, $isFullValue ) = $this->getRdfLiteral( $dataType, $dataValue ); |
|
240 | - if ( $isFullValue ) { |
|
239 | + list($value, $isFullValue) = $this->getRdfLiteral($dataType, $dataValue); |
|
240 | + if ($isFullValue) { |
|
241 | 241 | $prefix .= 'v'; |
242 | 242 | } |
243 | 243 | $path = $type === Context::TYPE_QUALIFIER ? |
244 | - "$prefix:$pid" : |
|
245 | - "prov:wasDerivedFrom/$prefix:$pid"; |
|
244 | + "$prefix:$pid" : "prov:wasDerivedFrom/$prefix:$pid"; |
|
246 | 245 | |
247 | 246 | $deprecatedFilter = ''; |
248 | - if ( $ignoreDeprecatedStatements ) { |
|
247 | + if ($ignoreDeprecatedStatements) { |
|
249 | 248 | $deprecatedFilter = <<< EOF |
250 | 249 | MINUS { ?otherStatement wikibase:rank wikibase:DeprecatedRank. } |
251 | 250 | MINUS { ?otherStatement wikibase-beta:rank wikibase-beta:DeprecatedRank. } |
@@ -266,9 +265,9 @@ discard block |
||
266 | 265 | LIMIT 10 |
267 | 266 | EOF; |
268 | 267 | |
269 | - $result = $this->runQuery( $query ); |
|
268 | + $result = $this->runQuery($query); |
|
270 | 269 | |
271 | - return $this->getOtherEntities( $result ); |
|
270 | + return $this->getOtherEntities($result); |
|
272 | 271 | } |
273 | 272 | |
274 | 273 | /** |
@@ -278,8 +277,8 @@ discard block |
||
278 | 277 | * |
279 | 278 | * @return string |
280 | 279 | */ |
281 | - private function stringLiteral( $text ) { |
|
282 | - return '"' . strtr( $text, [ '"' => '\\"', '\\' => '\\\\' ] ) . '"'; |
|
280 | + private function stringLiteral($text) { |
|
281 | + return '"'.strtr($text, ['"' => '\\"', '\\' => '\\\\']).'"'; |
|
283 | 282 | } |
284 | 283 | |
285 | 284 | /** |
@@ -289,17 +288,17 @@ discard block |
||
289 | 288 | * |
290 | 289 | * @return CachedEntityIds |
291 | 290 | */ |
292 | - private function getOtherEntities( CachedQueryResults $results ) { |
|
293 | - return new CachedEntityIds( array_map( |
|
294 | - function ( $resultBindings ) { |
|
291 | + private function getOtherEntities(CachedQueryResults $results) { |
|
292 | + return new CachedEntityIds(array_map( |
|
293 | + function($resultBindings) { |
|
295 | 294 | $entityIRI = $resultBindings['otherEntity']['value']; |
296 | - $entityPrefixLength = strlen( $this->entityPrefix ); |
|
297 | - if ( substr( $entityIRI, 0, $entityPrefixLength ) === $this->entityPrefix ) { |
|
295 | + $entityPrefixLength = strlen($this->entityPrefix); |
|
296 | + if (substr($entityIRI, 0, $entityPrefixLength) === $this->entityPrefix) { |
|
298 | 297 | try { |
299 | 298 | return $this->entityIdParser->parse( |
300 | - substr( $entityIRI, $entityPrefixLength ) |
|
299 | + substr($entityIRI, $entityPrefixLength) |
|
301 | 300 | ); |
302 | - } catch ( EntityIdParsingException $e ) { |
|
301 | + } catch (EntityIdParsingException $e) { |
|
303 | 302 | // fall through |
304 | 303 | } |
305 | 304 | } |
@@ -307,7 +306,7 @@ discard block |
||
307 | 306 | return null; |
308 | 307 | }, |
309 | 308 | $results->getArray()['results']['bindings'] |
310 | - ), $results->getMetadata() ); |
|
309 | + ), $results->getMetadata()); |
|
311 | 310 | } |
312 | 311 | |
313 | 312 | // @codingStandardsIgnoreStart cyclomatic complexity of this function is too high |
@@ -320,47 +319,47 @@ discard block |
||
320 | 319 | * @return array the literal or IRI as a string in SPARQL syntax, |
321 | 320 | * and a boolean indicating whether it refers to a full value node or not |
322 | 321 | */ |
323 | - private function getRdfLiteral( $dataType, DataValue $dataValue ) { |
|
324 | - switch ( $dataType ) { |
|
322 | + private function getRdfLiteral($dataType, DataValue $dataValue) { |
|
323 | + switch ($dataType) { |
|
325 | 324 | case 'string': |
326 | 325 | case 'external-id': |
327 | - return [ $this->stringLiteral( $dataValue->getValue() ), false ]; |
|
326 | + return [$this->stringLiteral($dataValue->getValue()), false]; |
|
328 | 327 | case 'commonsMedia': |
329 | - $url = $this->rdfVocabulary->getMediaFileURI( $dataValue->getValue() ); |
|
330 | - return [ '<' . $url . '>', false ]; |
|
328 | + $url = $this->rdfVocabulary->getMediaFileURI($dataValue->getValue()); |
|
329 | + return ['<'.$url.'>', false]; |
|
331 | 330 | case 'geo-shape': |
332 | - $url = $this->rdfVocabulary->getGeoShapeURI( $dataValue->getValue() ); |
|
333 | - return [ '<' . $url . '>', false ]; |
|
331 | + $url = $this->rdfVocabulary->getGeoShapeURI($dataValue->getValue()); |
|
332 | + return ['<'.$url.'>', false]; |
|
334 | 333 | case 'tabular-data': |
335 | - $url = $this->rdfVocabulary->getTabularDataURI( $dataValue->getValue() ); |
|
336 | - return [ '<' . $url . '>', false ]; |
|
334 | + $url = $this->rdfVocabulary->getTabularDataURI($dataValue->getValue()); |
|
335 | + return ['<'.$url.'>', false]; |
|
337 | 336 | case 'url': |
338 | 337 | $url = $dataValue->getValue(); |
339 | - if ( !preg_match( '/^[^<>"{}\\\\|^`\\x00-\\x20]*$/D', $url ) ) { |
|
338 | + if (!preg_match('/^[^<>"{}\\\\|^`\\x00-\\x20]*$/D', $url)) { |
|
340 | 339 | // not a valid URL for SPARQL (see SPARQL spec, production 139 IRIREF) |
341 | 340 | // such an URL should never reach us, so just throw |
342 | - throw new InvalidArgumentException( 'invalid URL: ' . $url ); |
|
341 | + throw new InvalidArgumentException('invalid URL: '.$url); |
|
343 | 342 | } |
344 | - return [ '<' . $url . '>', false ]; |
|
343 | + return ['<'.$url.'>', false]; |
|
345 | 344 | case 'wikibase-item': |
346 | 345 | case 'wikibase-property': |
347 | 346 | /** @var EntityIdValue $dataValue */ |
348 | - return [ 'wd:' . $dataValue->getEntityId()->getSerialization(), false ]; |
|
347 | + return ['wd:'.$dataValue->getEntityId()->getSerialization(), false]; |
|
349 | 348 | case 'monolingualtext': |
350 | 349 | /** @var MonolingualTextValue $dataValue */ |
351 | 350 | $lang = $dataValue->getLanguageCode(); |
352 | - if ( !preg_match( '/^[a-zA-Z]+(-[a-zA-Z0-9]+)*$/D', $lang ) ) { |
|
351 | + if (!preg_match('/^[a-zA-Z]+(-[a-zA-Z0-9]+)*$/D', $lang)) { |
|
353 | 352 | // not a valid language tag for SPARQL (see SPARQL spec, production 145 LANGTAG) |
354 | 353 | // such a language tag should never reach us, so just throw |
355 | - throw new InvalidArgumentException( 'invalid language tag: ' . $lang ); |
|
354 | + throw new InvalidArgumentException('invalid language tag: '.$lang); |
|
356 | 355 | } |
357 | - return [ $this->stringLiteral( $dataValue->getText() ) . '@' . $lang, false ]; |
|
356 | + return [$this->stringLiteral($dataValue->getText()).'@'.$lang, false]; |
|
358 | 357 | case 'globe-coordinate': |
359 | 358 | case 'quantity': |
360 | 359 | case 'time': |
361 | - return [ 'wdv:' . $dataValue->getHash(), true ]; |
|
360 | + return ['wdv:'.$dataValue->getHash(), true]; |
|
362 | 361 | default: |
363 | - throw new InvalidArgumentException( 'unknown data type: ' . $dataType ); |
|
362 | + throw new InvalidArgumentException('unknown data type: '.$dataType); |
|
364 | 363 | } |
365 | 364 | } |
366 | 365 | // @codingStandardsIgnoreEnd |
@@ -373,45 +372,45 @@ discard block |
||
373 | 372 | * @throws SparqlHelperException if the query times out or some other error occurs |
374 | 373 | * @throws ConstraintParameterException if the $regex is invalid |
375 | 374 | */ |
376 | - public function matchesRegularExpression( $text, $regex ) { |
|
375 | + public function matchesRegularExpression($text, $regex) { |
|
377 | 376 | // caching wrapper around matchesRegularExpressionWithSparql |
378 | 377 | |
379 | - $textHash = hash( 'sha256', $text ); |
|
378 | + $textHash = hash('sha256', $text); |
|
380 | 379 | $cacheKey = $this->cache->makeKey( |
381 | 380 | 'WikibaseQualityConstraints', // extension |
382 | 381 | 'regex', // action |
383 | 382 | 'WDQS-Java', // regex flavor |
384 | - hash( 'sha256', $regex ) |
|
383 | + hash('sha256', $regex) |
|
385 | 384 | ); |
386 | - $cacheMapSize = $this->config->get( 'WBQualityConstraintsFormatCacheMapSize' ); |
|
385 | + $cacheMapSize = $this->config->get('WBQualityConstraintsFormatCacheMapSize'); |
|
387 | 386 | |
388 | 387 | $cacheMapArray = $this->cache->getWithSetCallback( |
389 | 388 | $cacheKey, |
390 | 389 | WANObjectCache::TTL_DAY, |
391 | - function( $cacheMapArray ) use ( $text, $regex, $textHash, $cacheMapSize ) { |
|
390 | + function($cacheMapArray) use ($text, $regex, $textHash, $cacheMapSize) { |
|
392 | 391 | // Initialize the cache map if not set |
393 | - if ( $cacheMapArray === false ) { |
|
392 | + if ($cacheMapArray === false) { |
|
394 | 393 | $key = 'wikibase.quality.constraints.regex.cache.refresh.init'; |
395 | - $this->dataFactory->increment( $key ); |
|
394 | + $this->dataFactory->increment($key); |
|
396 | 395 | return []; |
397 | 396 | } |
398 | 397 | |
399 | 398 | $key = 'wikibase.quality.constraints.regex.cache.refresh'; |
400 | - $this->dataFactory->increment( $key ); |
|
401 | - $cacheMapArray = $this->cleanupConstraintParameterExceptions( $cacheMapArray ); |
|
402 | - $cacheMap = MapCacheLRU::newFromArray( $cacheMapArray, $cacheMapSize ); |
|
403 | - if ( $cacheMap->has( $textHash ) ) { |
|
399 | + $this->dataFactory->increment($key); |
|
400 | + $cacheMapArray = $this->cleanupConstraintParameterExceptions($cacheMapArray); |
|
401 | + $cacheMap = MapCacheLRU::newFromArray($cacheMapArray, $cacheMapSize); |
|
402 | + if ($cacheMap->has($textHash)) { |
|
404 | 403 | $key = 'wikibase.quality.constraints.regex.cache.refresh.hit'; |
405 | - $this->dataFactory->increment( $key ); |
|
406 | - $cacheMap->get( $textHash ); // ping cache |
|
404 | + $this->dataFactory->increment($key); |
|
405 | + $cacheMap->get($textHash); // ping cache |
|
407 | 406 | } else { |
408 | 407 | $key = 'wikibase.quality.constraints.regex.cache.refresh.miss'; |
409 | - $this->dataFactory->increment( $key ); |
|
408 | + $this->dataFactory->increment($key); |
|
410 | 409 | try { |
411 | - $matches = $this->matchesRegularExpressionWithSparql( $text, $regex ); |
|
412 | - } catch ( ConstraintParameterException $e ) { |
|
413 | - $matches = $this->serializeConstraintParameterException( $e ); |
|
414 | - } catch ( SparqlHelperException $e ) { |
|
410 | + $matches = $this->matchesRegularExpressionWithSparql($text, $regex); |
|
411 | + } catch (ConstraintParameterException $e) { |
|
412 | + $matches = $this->serializeConstraintParameterException($e); |
|
413 | + } catch (SparqlHelperException $e) { |
|
415 | 414 | // don’t cache this |
416 | 415 | return $cacheMap->toArray(); |
417 | 416 | } |
@@ -435,36 +434,36 @@ discard block |
||
435 | 434 | ] |
436 | 435 | ); |
437 | 436 | |
438 | - if ( isset( $cacheMapArray[$textHash] ) ) { |
|
437 | + if (isset($cacheMapArray[$textHash])) { |
|
439 | 438 | $key = 'wikibase.quality.constraints.regex.cache.hit'; |
440 | - $this->dataFactory->increment( $key ); |
|
439 | + $this->dataFactory->increment($key); |
|
441 | 440 | $matches = $cacheMapArray[$textHash]; |
442 | - if ( is_bool( $matches ) ) { |
|
441 | + if (is_bool($matches)) { |
|
443 | 442 | return $matches; |
444 | - } elseif ( is_array( $matches ) && |
|
445 | - $matches['type'] == ConstraintParameterException::class ) { |
|
446 | - throw $this->deserializeConstraintParameterException( $matches ); |
|
443 | + } elseif (is_array($matches) && |
|
444 | + $matches['type'] == ConstraintParameterException::class) { |
|
445 | + throw $this->deserializeConstraintParameterException($matches); |
|
447 | 446 | } else { |
448 | 447 | throw new MWException( |
449 | - 'Value of unknown type in object cache (' . |
|
450 | - 'cache key: ' . $cacheKey . ', ' . |
|
451 | - 'cache map key: ' . $textHash . ', ' . |
|
452 | - 'value type: ' . gettype( $matches ) . ')' |
|
448 | + 'Value of unknown type in object cache ('. |
|
449 | + 'cache key: '.$cacheKey.', '. |
|
450 | + 'cache map key: '.$textHash.', '. |
|
451 | + 'value type: '.gettype($matches).')' |
|
453 | 452 | ); |
454 | 453 | } |
455 | 454 | } else { |
456 | 455 | $key = 'wikibase.quality.constraints.regex.cache.miss'; |
457 | - $this->dataFactory->increment( $key ); |
|
458 | - return $this->matchesRegularExpressionWithSparql( $text, $regex ); |
|
456 | + $this->dataFactory->increment($key); |
|
457 | + return $this->matchesRegularExpressionWithSparql($text, $regex); |
|
459 | 458 | } |
460 | 459 | } |
461 | 460 | |
462 | - private function serializeConstraintParameterException( ConstraintParameterException $cpe ) { |
|
461 | + private function serializeConstraintParameterException(ConstraintParameterException $cpe) { |
|
463 | 462 | $message = $cpe->getViolationMessage(); |
464 | - if ( $message instanceof ViolationMessage ) { |
|
463 | + if ($message instanceof ViolationMessage) { |
|
465 | 464 | return [ |
466 | 465 | 'type' => ConstraintParameterException::class, |
467 | - 'violationMessage' => $this->violationMessageSerializer->serialize( $message ), |
|
466 | + 'violationMessage' => $this->violationMessageSerializer->serialize($message), |
|
468 | 467 | ]; |
469 | 468 | } else { |
470 | 469 | return [ |
@@ -474,19 +473,19 @@ discard block |
||
474 | 473 | } |
475 | 474 | } |
476 | 475 | |
477 | - private function isViolationMessageSerialization( array $serialization ) { |
|
478 | - return array_key_exists( 'violationMessage', $serialization ); |
|
476 | + private function isViolationMessageSerialization(array $serialization) { |
|
477 | + return array_key_exists('violationMessage', $serialization); |
|
479 | 478 | } |
480 | 479 | |
481 | - private function deserializeConstraintParameterException( array $serialization ) { |
|
482 | - if ( $this->isViolationMessageSerialization( $serialization ) ) { |
|
480 | + private function deserializeConstraintParameterException(array $serialization) { |
|
481 | + if ($this->isViolationMessageSerialization($serialization)) { |
|
483 | 482 | $message = $this->violationMessageDeserializer->deserialize( |
484 | 483 | $serialization['violationMessage'] |
485 | 484 | ); |
486 | 485 | } else { |
487 | 486 | $message = $serialization['message']; |
488 | 487 | } |
489 | - return new ConstraintParameterException( $message ); |
|
488 | + return new ConstraintParameterException($message); |
|
490 | 489 | } |
491 | 490 | |
492 | 491 | /** |
@@ -496,13 +495,13 @@ discard block |
||
496 | 495 | * @param array $cacheMapArray {@link MapCacheLRU::toArray} |
497 | 496 | * @return array |
498 | 497 | */ |
499 | - private function cleanupConstraintParameterExceptions( array $cacheMapArray ) { |
|
498 | + private function cleanupConstraintParameterExceptions(array $cacheMapArray) { |
|
500 | 499 | $cleanedCacheMapArray = []; |
501 | - foreach ( $cacheMapArray as $key => $value ) { |
|
500 | + foreach ($cacheMapArray as $key => $value) { |
|
502 | 501 | if ( |
503 | - is_array( $value ) && |
|
502 | + is_array($value) && |
|
504 | 503 | $value['type'] === ConstraintParameterException::class && |
505 | - !$this->isViolationMessageSerialization( $value ) |
|
504 | + !$this->isViolationMessageSerialization($value) |
|
506 | 505 | ) { |
507 | 506 | continue; |
508 | 507 | } |
@@ -522,25 +521,25 @@ discard block |
||
522 | 521 | * @throws SparqlHelperException if the query times out or some other error occurs |
523 | 522 | * @throws ConstraintParameterException if the $regex is invalid |
524 | 523 | */ |
525 | - public function matchesRegularExpressionWithSparql( $text, $regex ) { |
|
526 | - $textStringLiteral = $this->stringLiteral( $text ); |
|
527 | - $regexStringLiteral = $this->stringLiteral( '^' . $regex . '$' ); |
|
524 | + public function matchesRegularExpressionWithSparql($text, $regex) { |
|
525 | + $textStringLiteral = $this->stringLiteral($text); |
|
526 | + $regexStringLiteral = $this->stringLiteral('^'.$regex.'$'); |
|
528 | 527 | |
529 | 528 | $query = <<<EOF |
530 | 529 | SELECT (REGEX($textStringLiteral, $regexStringLiteral) AS ?matches) {} |
531 | 530 | EOF; |
532 | 531 | |
533 | - $result = $this->runQuery( $query ); |
|
532 | + $result = $this->runQuery($query); |
|
534 | 533 | |
535 | 534 | $vars = $result->getArray()['results']['bindings'][0]; |
536 | - if ( array_key_exists( 'matches', $vars ) ) { |
|
535 | + if (array_key_exists('matches', $vars)) { |
|
537 | 536 | // true or false ⇒ regex okay, text matches or not |
538 | 537 | return $vars['matches']['value'] === 'true'; |
539 | 538 | } else { |
540 | 539 | // empty result: regex broken |
541 | 540 | throw new ConstraintParameterException( |
542 | - ( new ViolationMessage( 'wbqc-violation-message-parameter-regex' ) ) |
|
543 | - ->withInlineCode( $regex, Role::CONSTRAINT_PARAMETER_VALUE ) |
|
541 | + (new ViolationMessage('wbqc-violation-message-parameter-regex')) |
|
542 | + ->withInlineCode($regex, Role::CONSTRAINT_PARAMETER_VALUE) |
|
544 | 543 | ); |
545 | 544 | } |
546 | 545 | } |
@@ -552,14 +551,14 @@ discard block |
||
552 | 551 | * |
553 | 552 | * @return boolean |
554 | 553 | */ |
555 | - public function isTimeout( $responseContent ) { |
|
556 | - $timeoutRegex = implode( '|', array_map( |
|
557 | - function ( $fqn ) { |
|
558 | - return preg_quote( $fqn, '/' ); |
|
554 | + public function isTimeout($responseContent) { |
|
555 | + $timeoutRegex = implode('|', array_map( |
|
556 | + function($fqn) { |
|
557 | + return preg_quote($fqn, '/'); |
|
559 | 558 | }, |
560 | - $this->config->get( 'WBQualityConstraintsSparqlTimeoutExceptionClasses' ) |
|
561 | - ) ); |
|
562 | - return (bool)preg_match( '/' . $timeoutRegex . '/', $responseContent ); |
|
559 | + $this->config->get('WBQualityConstraintsSparqlTimeoutExceptionClasses') |
|
560 | + )); |
|
561 | + return (bool) preg_match('/'.$timeoutRegex.'/', $responseContent); |
|
563 | 562 | } |
564 | 563 | |
565 | 564 | /** |
@@ -571,17 +570,17 @@ discard block |
||
571 | 570 | * @return integer|boolean the max-age (in seconds) |
572 | 571 | * or a plain boolean if no max-age can be determined |
573 | 572 | */ |
574 | - public function getCacheMaxAge( $responseHeaders ) { |
|
573 | + public function getCacheMaxAge($responseHeaders) { |
|
575 | 574 | if ( |
576 | - array_key_exists( 'x-cache-status', $responseHeaders ) && |
|
577 | - preg_match( '/^hit(?:-.*)?$/', $responseHeaders['x-cache-status'][0] ) |
|
575 | + array_key_exists('x-cache-status', $responseHeaders) && |
|
576 | + preg_match('/^hit(?:-.*)?$/', $responseHeaders['x-cache-status'][0]) |
|
578 | 577 | ) { |
579 | 578 | $maxage = []; |
580 | 579 | if ( |
581 | - array_key_exists( 'cache-control', $responseHeaders ) && |
|
582 | - preg_match( '/\bmax-age=(\d+)\b/', $responseHeaders['cache-control'][0], $maxage ) |
|
580 | + array_key_exists('cache-control', $responseHeaders) && |
|
581 | + preg_match('/\bmax-age=(\d+)\b/', $responseHeaders['cache-control'][0], $maxage) |
|
583 | 582 | ) { |
584 | - return intval( $maxage[1] ); |
|
583 | + return intval($maxage[1]); |
|
585 | 584 | } else { |
586 | 585 | return true; |
587 | 586 | } |
@@ -599,59 +598,58 @@ discard block |
||
599 | 598 | * |
600 | 599 | * @throws SparqlHelperException if the query times out or some other error occurs |
601 | 600 | */ |
602 | - public function runQuery( $query ) { |
|
601 | + public function runQuery($query) { |
|
603 | 602 | |
604 | - $endpoint = $this->config->get( 'WBQualityConstraintsSparqlEndpoint' ); |
|
605 | - $maxQueryTimeMillis = $this->config->get( 'WBQualityConstraintsSparqlMaxMillis' ); |
|
606 | - $url = $endpoint . '?' . http_build_query( |
|
603 | + $endpoint = $this->config->get('WBQualityConstraintsSparqlEndpoint'); |
|
604 | + $maxQueryTimeMillis = $this->config->get('WBQualityConstraintsSparqlMaxMillis'); |
|
605 | + $url = $endpoint.'?'.http_build_query( |
|
607 | 606 | [ |
608 | - 'query' => "#wbqc\n" . $this->prefixes . $query, |
|
607 | + 'query' => "#wbqc\n".$this->prefixes.$query, |
|
609 | 608 | 'format' => 'json', |
610 | 609 | 'maxQueryTimeMillis' => $maxQueryTimeMillis, |
611 | 610 | ], |
612 | - null, ini_get( 'arg_separator.output' ), |
|
611 | + null, ini_get('arg_separator.output'), |
|
613 | 612 | // encode spaces with %20, not + |
614 | 613 | PHP_QUERY_RFC3986 |
615 | 614 | ); |
616 | 615 | |
617 | 616 | $options = [ |
618 | 617 | 'method' => 'GET', |
619 | - 'timeout' => (int)round( ( $maxQueryTimeMillis + 1000 ) / 1000 ), |
|
618 | + 'timeout' => (int) round(($maxQueryTimeMillis + 1000) / 1000), |
|
620 | 619 | 'connectTimeout' => 'default', |
621 | 620 | ]; |
622 | - $request = MWHttpRequest::factory( $url, $options ); |
|
623 | - $startTime = microtime( true ); |
|
621 | + $request = MWHttpRequest::factory($url, $options); |
|
622 | + $startTime = microtime(true); |
|
624 | 623 | $status = $request->execute(); |
625 | - $endTime = microtime( true ); |
|
624 | + $endTime = microtime(true); |
|
626 | 625 | $this->dataFactory->timing( |
627 | 626 | 'wikibase.quality.constraints.sparql.timing', |
628 | - ( $endTime - $startTime ) * 1000 |
|
627 | + ($endTime - $startTime) * 1000 |
|
629 | 628 | ); |
630 | 629 | |
631 | - $maxAge = $this->getCacheMaxAge( $request->getResponseHeaders() ); |
|
632 | - if ( $maxAge ) { |
|
633 | - $this->dataFactory->increment( 'wikibase.quality.constraints.sparql.cached' ); |
|
630 | + $maxAge = $this->getCacheMaxAge($request->getResponseHeaders()); |
|
631 | + if ($maxAge) { |
|
632 | + $this->dataFactory->increment('wikibase.quality.constraints.sparql.cached'); |
|
634 | 633 | } |
635 | 634 | |
636 | - if ( $status->isOK() ) { |
|
635 | + if ($status->isOK()) { |
|
637 | 636 | $json = $request->getContent(); |
638 | - $arr = json_decode( $json, true ); |
|
637 | + $arr = json_decode($json, true); |
|
639 | 638 | return new CachedQueryResults( |
640 | 639 | $arr, |
641 | 640 | Metadata::ofCachingMetadata( |
642 | 641 | $maxAge ? |
643 | - CachingMetadata::ofMaximumAgeInSeconds( $maxAge ) : |
|
644 | - CachingMetadata::fresh() |
|
642 | + CachingMetadata::ofMaximumAgeInSeconds($maxAge) : CachingMetadata::fresh() |
|
645 | 643 | ) |
646 | 644 | ); |
647 | 645 | } else { |
648 | - $this->dataFactory->increment( 'wikibase.quality.constraints.sparql.error' ); |
|
646 | + $this->dataFactory->increment('wikibase.quality.constraints.sparql.error'); |
|
649 | 647 | |
650 | 648 | $this->dataFactory->increment( |
651 | 649 | "wikibase.quality.constraints.sparql.error.http.{$request->getStatus()}" |
652 | 650 | ); |
653 | 651 | |
654 | - if ( $this->isTimeout( $request->getContent() ) ) { |
|
652 | + if ($this->isTimeout($request->getContent())) { |
|
655 | 653 | $this->dataFactory->increment( |
656 | 654 | 'wikibase.quality.constraints.sparql.error.timeout' |
657 | 655 | ); |