@@ -90,18 +90,18 @@ discard block |
||
90 | 90 | $this->propertyDataTypeLookup = $propertyDataTypeLookup; |
91 | 91 | $this->cache = $cache; |
92 | 92 | |
93 | - $this->entityPrefix = $rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_ENTITY ); |
|
93 | + $this->entityPrefix = $rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_ENTITY); |
|
94 | 94 | $this->prefixes = <<<EOT |
95 | -PREFIX wd: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_ENTITY )}> |
|
96 | -PREFIX wds: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_STATEMENT )}> |
|
97 | -PREFIX wdt: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_DIRECT_CLAIM )}> |
|
98 | -PREFIX wdv: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_VALUE )}> |
|
99 | -PREFIX p: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_CLAIM )}> |
|
100 | -PREFIX ps: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_CLAIM_STATEMENT )}> |
|
101 | -PREFIX pq: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_QUALIFIER )}> |
|
102 | -PREFIX pqv: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_QUALIFIER_VALUE )}> |
|
103 | -PREFIX pr: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_REFERENCE )}> |
|
104 | -PREFIX prv: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_REFERENCE_VALUE )}> |
|
95 | +PREFIX wd: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_ENTITY)}> |
|
96 | +PREFIX wds: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_STATEMENT)}> |
|
97 | +PREFIX wdt: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_DIRECT_CLAIM)}> |
|
98 | +PREFIX wdv: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_VALUE)}> |
|
99 | +PREFIX p: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_CLAIM)}> |
|
100 | +PREFIX ps: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_CLAIM_STATEMENT)}> |
|
101 | +PREFIX pq: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_QUALIFIER)}> |
|
102 | +PREFIX pqv: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_QUALIFIER_VALUE)}> |
|
103 | +PREFIX pr: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_REFERENCE)}> |
|
104 | +PREFIX prv: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_REFERENCE_VALUE)}> |
|
105 | 105 | PREFIX wikibase: <http://wikiba.se/ontology#> |
106 | 106 | PREFIX wikibase-beta: <http://wikiba.se/ontology-beta#> |
107 | 107 | EOT; |
@@ -118,21 +118,21 @@ discard block |
||
118 | 118 | * @return CachedBool |
119 | 119 | * @throws SparqlHelperException if the query times out or some other error occurs |
120 | 120 | */ |
121 | - public function hasType( $id, array $classes, $withInstance ) { |
|
122 | - $instanceOfId = $this->config->get( 'WBQualityConstraintsInstanceOfId' ); |
|
123 | - $subclassOfId = $this->config->get( 'WBQualityConstraintsSubclassOfId' ); |
|
121 | + public function hasType($id, array $classes, $withInstance) { |
|
122 | + $instanceOfId = $this->config->get('WBQualityConstraintsInstanceOfId'); |
|
123 | + $subclassOfId = $this->config->get('WBQualityConstraintsSubclassOfId'); |
|
124 | 124 | |
125 | - $path = ( $withInstance ? "wdt:$instanceOfId/" : "" ) . "wdt:$subclassOfId*"; |
|
125 | + $path = ($withInstance ? "wdt:$instanceOfId/" : "")."wdt:$subclassOfId*"; |
|
126 | 126 | |
127 | 127 | $metadatas = []; |
128 | 128 | |
129 | - foreach ( array_chunk( $classes, 20 ) as $classesChunk ) { |
|
130 | - $classesValues = implode( ' ', array_map( |
|
131 | - function( $class ) { |
|
132 | - return 'wd:' . $class; |
|
129 | + foreach (array_chunk($classes, 20) as $classesChunk) { |
|
130 | + $classesValues = implode(' ', array_map( |
|
131 | + function($class) { |
|
132 | + return 'wd:'.$class; |
|
133 | 133 | }, |
134 | 134 | $classesChunk |
135 | - ) ); |
|
135 | + )); |
|
136 | 136 | |
137 | 137 | $query = <<<EOF |
138 | 138 | ASK { |
@@ -143,19 +143,19 @@ discard block |
||
143 | 143 | EOF; |
144 | 144 | // TODO hint:gearing is a workaround for T168973 and can hopefully be removed eventually |
145 | 145 | |
146 | - $result = $this->runQuery( $query ); |
|
146 | + $result = $this->runQuery($query); |
|
147 | 147 | $metadatas[] = $result->getMetadata(); |
148 | - if ( $result->getArray()['boolean'] ) { |
|
148 | + if ($result->getArray()['boolean']) { |
|
149 | 149 | return new CachedBool( |
150 | 150 | true, |
151 | - Metadata::merge( $metadatas ) |
|
151 | + Metadata::merge($metadatas) |
|
152 | 152 | ); |
153 | 153 | } |
154 | 154 | } |
155 | 155 | |
156 | 156 | return new CachedBool( |
157 | 157 | false, |
158 | - Metadata::merge( $metadatas ) |
|
158 | + Metadata::merge($metadatas) |
|
159 | 159 | ); |
160 | 160 | } |
161 | 161 | |
@@ -171,10 +171,10 @@ discard block |
||
171 | 171 | $ignoreDeprecatedStatements |
172 | 172 | ) { |
173 | 173 | $pid = $statement->getPropertyId()->serialize(); |
174 | - $guid = str_replace( '$', '-', $statement->getGuid() ); |
|
174 | + $guid = str_replace('$', '-', $statement->getGuid()); |
|
175 | 175 | |
176 | 176 | $deprecatedFilter = ''; |
177 | - if ( $ignoreDeprecatedStatements ) { |
|
177 | + if ($ignoreDeprecatedStatements) { |
|
178 | 178 | $deprecatedFilter .= 'MINUS { ?otherStatement wikibase:rank wikibase:DeprecatedRank. }'; |
179 | 179 | $deprecatedFilter .= 'MINUS { ?otherStatement wikibase-beta:rank wikibase-beta:DeprecatedRank. }'; |
180 | 180 | } |
@@ -194,9 +194,9 @@ discard block |
||
194 | 194 | LIMIT 10 |
195 | 195 | EOF; |
196 | 196 | |
197 | - $result = $this->runQuery( $query ); |
|
197 | + $result = $this->runQuery($query); |
|
198 | 198 | |
199 | - return $this->getOtherEntities( $result ); |
|
199 | + return $this->getOtherEntities($result); |
|
200 | 200 | } |
201 | 201 | |
202 | 202 | /** |
@@ -221,16 +221,15 @@ discard block |
||
221 | 221 | $dataType = $this->propertyDataTypeLookup->getDataTypeIdForProperty( |
222 | 222 | $snak->getPropertyId() |
223 | 223 | ); |
224 | - list ( $value, $isFullValue ) = $this->getRdfLiteral( $dataType, $dataValue ); |
|
225 | - if ( $isFullValue ) { |
|
224 | + list ($value, $isFullValue) = $this->getRdfLiteral($dataType, $dataValue); |
|
225 | + if ($isFullValue) { |
|
226 | 226 | $prefix .= 'v'; |
227 | 227 | } |
228 | 228 | $path = $type === Context::TYPE_QUALIFIER ? |
229 | - "$prefix:$pid" : |
|
230 | - "prov:wasDerivedFrom/$prefix:$pid"; |
|
229 | + "$prefix:$pid" : "prov:wasDerivedFrom/$prefix:$pid"; |
|
231 | 230 | |
232 | 231 | $deprecatedFilter = ''; |
233 | - if ( $ignoreDeprecatedStatements ) { |
|
232 | + if ($ignoreDeprecatedStatements) { |
|
234 | 233 | $deprecatedFilter = <<< EOF |
235 | 234 | MINUS { ?otherStatement wikibase:rank wikibase:DeprecatedRank. } |
236 | 235 | MINUS { ?otherStatement wikibase-beta:rank wikibase-beta:DeprecatedRank. } |
@@ -251,9 +250,9 @@ discard block |
||
251 | 250 | LIMIT 10 |
252 | 251 | EOF; |
253 | 252 | |
254 | - $result = $this->runQuery( $query ); |
|
253 | + $result = $this->runQuery($query); |
|
255 | 254 | |
256 | - return $this->getOtherEntities( $result ); |
|
255 | + return $this->getOtherEntities($result); |
|
257 | 256 | } |
258 | 257 | |
259 | 258 | /** |
@@ -263,8 +262,8 @@ discard block |
||
263 | 262 | * |
264 | 263 | * @return string |
265 | 264 | */ |
266 | - private function stringLiteral( $text ) { |
|
267 | - return '"' . strtr( $text, [ '"' => '\\"', '\\' => '\\\\' ] ) . '"'; |
|
265 | + private function stringLiteral($text) { |
|
266 | + return '"'.strtr($text, ['"' => '\\"', '\\' => '\\\\']).'"'; |
|
268 | 267 | } |
269 | 268 | |
270 | 269 | /** |
@@ -274,17 +273,17 @@ discard block |
||
274 | 273 | * |
275 | 274 | * @return CachedEntityIds |
276 | 275 | */ |
277 | - private function getOtherEntities( CachedQueryResults $results ) { |
|
278 | - return new CachedEntityIds( array_map( |
|
279 | - function ( $resultBindings ) { |
|
276 | + private function getOtherEntities(CachedQueryResults $results) { |
|
277 | + return new CachedEntityIds(array_map( |
|
278 | + function($resultBindings) { |
|
280 | 279 | $entityIRI = $resultBindings['otherEntity']['value']; |
281 | - $entityPrefixLength = strlen( $this->entityPrefix ); |
|
282 | - if ( substr( $entityIRI, 0, $entityPrefixLength ) === $this->entityPrefix ) { |
|
280 | + $entityPrefixLength = strlen($this->entityPrefix); |
|
281 | + if (substr($entityIRI, 0, $entityPrefixLength) === $this->entityPrefix) { |
|
283 | 282 | try { |
284 | 283 | return $this->entityIdParser->parse( |
285 | - substr( $entityIRI, $entityPrefixLength ) |
|
284 | + substr($entityIRI, $entityPrefixLength) |
|
286 | 285 | ); |
287 | - } catch ( EntityIdParsingException $e ) { |
|
286 | + } catch (EntityIdParsingException $e) { |
|
288 | 287 | // fall through |
289 | 288 | } |
290 | 289 | } |
@@ -292,7 +291,7 @@ discard block |
||
292 | 291 | return null; |
293 | 292 | }, |
294 | 293 | $results->getArray()['results']['bindings'] |
295 | - ), $results->getMetadata() ); |
|
294 | + ), $results->getMetadata()); |
|
296 | 295 | } |
297 | 296 | |
298 | 297 | // @codingStandardsIgnoreStart cyclomatic complexity of this function is too high |
@@ -305,47 +304,47 @@ discard block |
||
305 | 304 | * @return array the literal or IRI as a string in SPARQL syntax, |
306 | 305 | * and a boolean indicating whether it refers to a full value node or not |
307 | 306 | */ |
308 | - private function getRdfLiteral( $dataType, DataValue $dataValue ) { |
|
309 | - switch ( $dataType ) { |
|
307 | + private function getRdfLiteral($dataType, DataValue $dataValue) { |
|
308 | + switch ($dataType) { |
|
310 | 309 | case 'string': |
311 | 310 | case 'external-id': |
312 | - return [ $this->stringLiteral( $dataValue->getValue() ), false ]; |
|
311 | + return [$this->stringLiteral($dataValue->getValue()), false]; |
|
313 | 312 | case 'commonsMedia': |
314 | - $url = $this->rdfVocabulary->getMediaFileURI( $dataValue->getValue() ); |
|
315 | - return [ '<' . $url . '>', false ]; |
|
313 | + $url = $this->rdfVocabulary->getMediaFileURI($dataValue->getValue()); |
|
314 | + return ['<'.$url.'>', false]; |
|
316 | 315 | case 'geo-shape': |
317 | - $url = $this->rdfVocabulary->getGeoShapeURI( $dataValue->getValue() ); |
|
318 | - return [ '<' . $url . '>', false ]; |
|
316 | + $url = $this->rdfVocabulary->getGeoShapeURI($dataValue->getValue()); |
|
317 | + return ['<'.$url.'>', false]; |
|
319 | 318 | case 'tabular-data': |
320 | - $url = $this->rdfVocabulary->getTabularDataURI( $dataValue->getValue() ); |
|
321 | - return [ '<' . $url . '>', false ]; |
|
319 | + $url = $this->rdfVocabulary->getTabularDataURI($dataValue->getValue()); |
|
320 | + return ['<'.$url.'>', false]; |
|
322 | 321 | case 'url': |
323 | 322 | $url = $dataValue->getValue(); |
324 | - if ( !preg_match( '/^[^<>"{}\\\\|^`\\x00-\\x20]*$/D', $url ) ) { |
|
323 | + if (!preg_match('/^[^<>"{}\\\\|^`\\x00-\\x20]*$/D', $url)) { |
|
325 | 324 | // not a valid URL for SPARQL (see SPARQL spec, production 139 IRIREF) |
326 | 325 | // such an URL should never reach us, so just throw |
327 | - throw new InvalidArgumentException( 'invalid URL: ' . $url ); |
|
326 | + throw new InvalidArgumentException('invalid URL: '.$url); |
|
328 | 327 | } |
329 | - return [ '<' . $url . '>', false ]; |
|
328 | + return ['<'.$url.'>', false]; |
|
330 | 329 | case 'wikibase-item': |
331 | 330 | case 'wikibase-property': |
332 | 331 | /** @var EntityIdValue $dataValue */ |
333 | - return [ 'wd:' . $dataValue->getEntityId()->getSerialization(), false ]; |
|
332 | + return ['wd:'.$dataValue->getEntityId()->getSerialization(), false]; |
|
334 | 333 | case 'monolingualtext': |
335 | 334 | /** @var MonolingualTextValue $dataValue */ |
336 | 335 | $lang = $dataValue->getLanguageCode(); |
337 | - if ( !preg_match( '/^[a-zA-Z]+(-[a-zA-Z0-9]+)*$/D', $lang ) ) { |
|
336 | + if (!preg_match('/^[a-zA-Z]+(-[a-zA-Z0-9]+)*$/D', $lang)) { |
|
338 | 337 | // not a valid language tag for SPARQL (see SPARQL spec, production 145 LANGTAG) |
339 | 338 | // such a language tag should never reach us, so just throw |
340 | - throw new InvalidArgumentException( 'invalid language tag: ' . $lang ); |
|
339 | + throw new InvalidArgumentException('invalid language tag: '.$lang); |
|
341 | 340 | } |
342 | - return [ $this->stringLiteral( $dataValue->getText() ) . '@' . $lang, false ]; |
|
341 | + return [$this->stringLiteral($dataValue->getText()).'@'.$lang, false]; |
|
343 | 342 | case 'globe-coordinate': |
344 | 343 | case 'quantity': |
345 | 344 | case 'time': |
346 | - return [ 'wdv:' . $dataValue->getHash(), true ]; |
|
345 | + return ['wdv:'.$dataValue->getHash(), true]; |
|
347 | 346 | default: |
348 | - throw new InvalidArgumentException( 'unknown data type: ' . $dataType ); |
|
347 | + throw new InvalidArgumentException('unknown data type: '.$dataType); |
|
349 | 348 | } |
350 | 349 | } |
351 | 350 | // @codingStandardsIgnoreEnd |
@@ -358,47 +357,47 @@ discard block |
||
358 | 357 | * @throws SparqlHelperException if the query times out or some other error occurs |
359 | 358 | * @throws ConstraintParameterException if the $regex is invalid |
360 | 359 | */ |
361 | - public function matchesRegularExpression( $text, $regex ) { |
|
360 | + public function matchesRegularExpression($text, $regex) { |
|
362 | 361 | // caching wrapper around matchesRegularExpressionWithSparql |
363 | 362 | |
364 | - $textHash = hash( 'sha256', $text ); |
|
363 | + $textHash = hash('sha256', $text); |
|
365 | 364 | $cacheKey = $this->cache->makeKey( |
366 | 365 | 'WikibaseQualityConstraints', // extension |
367 | 366 | 'regex', // action |
368 | 367 | 'WDQS-Java', // regex flavor |
369 | - hash( 'sha256', $regex ) |
|
368 | + hash('sha256', $regex) |
|
370 | 369 | ); |
371 | - $cacheMapSize = $this->config->get( 'WBQualityConstraintsFormatCacheMapSize' ); |
|
370 | + $cacheMapSize = $this->config->get('WBQualityConstraintsFormatCacheMapSize'); |
|
372 | 371 | |
373 | 372 | $cacheMapArray = $this->cache->getWithSetCallback( |
374 | 373 | $cacheKey, |
375 | 374 | WANObjectCache::TTL_DAY, |
376 | - function( $cacheMapArray ) use ( $text, $regex, $textHash, $cacheMapSize ) { |
|
375 | + function($cacheMapArray) use ($text, $regex, $textHash, $cacheMapSize) { |
|
377 | 376 | // Initialize the cache map if not set |
378 | - if ( $cacheMapArray === false ) { |
|
377 | + if ($cacheMapArray === false) { |
|
379 | 378 | $key = 'wikibase.quality.constraints.regex.cache.refresh.init'; |
380 | - $this->dataFactory->increment( $key ); |
|
379 | + $this->dataFactory->increment($key); |
|
381 | 380 | return []; |
382 | 381 | } |
383 | 382 | |
384 | 383 | $key = 'wikibase.quality.constraints.regex.cache.refresh'; |
385 | - $this->dataFactory->increment( $key ); |
|
386 | - $cacheMap = MapCacheLRU::newFromArray( $cacheMapArray, $cacheMapSize ); |
|
387 | - if ( $cacheMap->has( $textHash ) ) { |
|
384 | + $this->dataFactory->increment($key); |
|
385 | + $cacheMap = MapCacheLRU::newFromArray($cacheMapArray, $cacheMapSize); |
|
386 | + if ($cacheMap->has($textHash)) { |
|
388 | 387 | $key = 'wikibase.quality.constraints.regex.cache.refresh.hit'; |
389 | - $this->dataFactory->increment( $key ); |
|
390 | - $cacheMap->get( $textHash ); // ping cache |
|
388 | + $this->dataFactory->increment($key); |
|
389 | + $cacheMap->get($textHash); // ping cache |
|
391 | 390 | } else { |
392 | 391 | $key = 'wikibase.quality.constraints.regex.cache.refresh.miss'; |
393 | - $this->dataFactory->increment( $key ); |
|
392 | + $this->dataFactory->increment($key); |
|
394 | 393 | try { |
395 | - $matches = $this->matchesRegularExpressionWithSparql( $text, $regex ); |
|
396 | - } catch ( ConstraintParameterException $e ) { |
|
394 | + $matches = $this->matchesRegularExpressionWithSparql($text, $regex); |
|
395 | + } catch (ConstraintParameterException $e) { |
|
397 | 396 | $matches = [ |
398 | 397 | 'type' => ConstraintParameterException::class, |
399 | 398 | 'message' => $e->getMessage(), |
400 | 399 | ]; |
401 | - } catch ( SparqlHelperException $e ) { |
|
400 | + } catch (SparqlHelperException $e) { |
|
402 | 401 | // don’t cache this |
403 | 402 | return $cacheMap->toArray(); |
404 | 403 | } |
@@ -422,27 +421,27 @@ discard block |
||
422 | 421 | ] |
423 | 422 | ); |
424 | 423 | |
425 | - if ( isset( $cacheMapArray[$textHash] ) ) { |
|
424 | + if (isset($cacheMapArray[$textHash])) { |
|
426 | 425 | $key = 'wikibase.quality.constraints.regex.cache.hit'; |
427 | - $this->dataFactory->increment( $key ); |
|
426 | + $this->dataFactory->increment($key); |
|
428 | 427 | $matches = $cacheMapArray[$textHash]; |
429 | - if ( is_bool( $matches ) ) { |
|
428 | + if (is_bool($matches)) { |
|
430 | 429 | return $matches; |
431 | - } elseif ( is_array( $matches ) && |
|
432 | - $matches['type'] == ConstraintParameterException::class ) { |
|
433 | - throw new ConstraintParameterException( $matches['message'] ); |
|
430 | + } elseif (is_array($matches) && |
|
431 | + $matches['type'] == ConstraintParameterException::class) { |
|
432 | + throw new ConstraintParameterException($matches['message']); |
|
434 | 433 | } else { |
435 | 434 | throw new MWException( |
436 | - 'Value of unknown type in object cache (' . |
|
437 | - 'cache key: ' . $cacheKey . ', ' . |
|
438 | - 'cache map key: ' . $textHash . ', ' . |
|
439 | - 'value type: ' . gettype( $matches ) . ')' |
|
435 | + 'Value of unknown type in object cache ('. |
|
436 | + 'cache key: '.$cacheKey.', '. |
|
437 | + 'cache map key: '.$textHash.', '. |
|
438 | + 'value type: '.gettype($matches).')' |
|
440 | 439 | ); |
441 | 440 | } |
442 | 441 | } else { |
443 | 442 | $key = 'wikibase.quality.constraints.regex.cache.miss'; |
444 | - $this->dataFactory->increment( $key ); |
|
445 | - return $this->matchesRegularExpressionWithSparql( $text, $regex ); |
|
443 | + $this->dataFactory->increment($key); |
|
444 | + return $this->matchesRegularExpressionWithSparql($text, $regex); |
|
446 | 445 | } |
447 | 446 | } |
448 | 447 | |
@@ -457,26 +456,26 @@ discard block |
||
457 | 456 | * @throws SparqlHelperException if the query times out or some other error occurs |
458 | 457 | * @throws ConstraintParameterException if the $regex is invalid |
459 | 458 | */ |
460 | - public function matchesRegularExpressionWithSparql( $text, $regex ) { |
|
461 | - $textStringLiteral = $this->stringLiteral( $text ); |
|
462 | - $regexStringLiteral = $this->stringLiteral( '^' . $regex . '$' ); |
|
459 | + public function matchesRegularExpressionWithSparql($text, $regex) { |
|
460 | + $textStringLiteral = $this->stringLiteral($text); |
|
461 | + $regexStringLiteral = $this->stringLiteral('^'.$regex.'$'); |
|
463 | 462 | |
464 | 463 | $query = <<<EOF |
465 | 464 | SELECT (REGEX($textStringLiteral, $regexStringLiteral) AS ?matches) {} |
466 | 465 | EOF; |
467 | 466 | |
468 | - $result = $this->runQuery( $query ); |
|
467 | + $result = $this->runQuery($query); |
|
469 | 468 | |
470 | 469 | $vars = $result->getArray()['results']['bindings'][0]; |
471 | - if ( array_key_exists( 'matches', $vars ) ) { |
|
470 | + if (array_key_exists('matches', $vars)) { |
|
472 | 471 | // true or false ⇒ regex okay, text matches or not |
473 | 472 | return $vars['matches']['value'] === 'true'; |
474 | 473 | } else { |
475 | 474 | // empty result: regex broken |
476 | 475 | throw new ConstraintParameterException( |
477 | - wfMessage( 'wbqc-violation-message-parameter-regex' ) |
|
478 | - ->rawParams( ConstraintParameterRenderer::formatByRole( Role::CONSTRAINT_PARAMETER_VALUE, |
|
479 | - '<code><nowiki>' . htmlspecialchars( $regex ) . '</nowiki></code>' ) ) |
|
476 | + wfMessage('wbqc-violation-message-parameter-regex') |
|
477 | + ->rawParams(ConstraintParameterRenderer::formatByRole(Role::CONSTRAINT_PARAMETER_VALUE, |
|
478 | + '<code><nowiki>'.htmlspecialchars($regex).'</nowiki></code>')) |
|
480 | 479 | ->escaped() |
481 | 480 | ); |
482 | 481 | } |
@@ -489,14 +488,14 @@ discard block |
||
489 | 488 | * |
490 | 489 | * @return boolean |
491 | 490 | */ |
492 | - public function isTimeout( $responseContent ) { |
|
493 | - $timeoutRegex = implode( '|', array_map( |
|
494 | - function ( $fqn ) { |
|
495 | - return preg_quote( $fqn, '/' ); |
|
491 | + public function isTimeout($responseContent) { |
|
492 | + $timeoutRegex = implode('|', array_map( |
|
493 | + function($fqn) { |
|
494 | + return preg_quote($fqn, '/'); |
|
496 | 495 | }, |
497 | - $this->config->get( 'WBQualityConstraintsSparqlTimeoutExceptionClasses' ) |
|
498 | - ) ); |
|
499 | - return (bool)preg_match( '/' . $timeoutRegex . '/', $responseContent ); |
|
496 | + $this->config->get('WBQualityConstraintsSparqlTimeoutExceptionClasses') |
|
497 | + )); |
|
498 | + return (bool) preg_match('/'.$timeoutRegex.'/', $responseContent); |
|
500 | 499 | } |
501 | 500 | |
502 | 501 | /** |
@@ -508,17 +507,17 @@ discard block |
||
508 | 507 | * @return integer|boolean the max-age (in seconds) |
509 | 508 | * or a plain boolean if no max-age can be determined |
510 | 509 | */ |
511 | - public function getCacheMaxAge( $responseHeaders ) { |
|
510 | + public function getCacheMaxAge($responseHeaders) { |
|
512 | 511 | if ( |
513 | - array_key_exists( 'x-cache-status', $responseHeaders ) && |
|
514 | - preg_match( '/^hit(?:-.*)?$/', $responseHeaders['x-cache-status'][0] ) |
|
512 | + array_key_exists('x-cache-status', $responseHeaders) && |
|
513 | + preg_match('/^hit(?:-.*)?$/', $responseHeaders['x-cache-status'][0]) |
|
515 | 514 | ) { |
516 | 515 | $maxage = []; |
517 | 516 | if ( |
518 | - array_key_exists( 'cache-control', $responseHeaders ) && |
|
519 | - preg_match( '/\bmax-age=(\d+)\b/', $responseHeaders['cache-control'][0], $maxage ) |
|
517 | + array_key_exists('cache-control', $responseHeaders) && |
|
518 | + preg_match('/\bmax-age=(\d+)\b/', $responseHeaders['cache-control'][0], $maxage) |
|
520 | 519 | ) { |
521 | - return intval( $maxage[1] ); |
|
520 | + return intval($maxage[1]); |
|
522 | 521 | } else { |
523 | 522 | return true; |
524 | 523 | } |
@@ -536,59 +535,58 @@ discard block |
||
536 | 535 | * |
537 | 536 | * @throws SparqlHelperException if the query times out or some other error occurs |
538 | 537 | */ |
539 | - public function runQuery( $query ) { |
|
538 | + public function runQuery($query) { |
|
540 | 539 | |
541 | - $endpoint = $this->config->get( 'WBQualityConstraintsSparqlEndpoint' ); |
|
542 | - $maxQueryTimeMillis = $this->config->get( 'WBQualityConstraintsSparqlMaxMillis' ); |
|
543 | - $url = $endpoint . '?' . http_build_query( |
|
540 | + $endpoint = $this->config->get('WBQualityConstraintsSparqlEndpoint'); |
|
541 | + $maxQueryTimeMillis = $this->config->get('WBQualityConstraintsSparqlMaxMillis'); |
|
542 | + $url = $endpoint.'?'.http_build_query( |
|
544 | 543 | [ |
545 | - 'query' => "#wbqc\n" . $this->prefixes . $query, |
|
544 | + 'query' => "#wbqc\n".$this->prefixes.$query, |
|
546 | 545 | 'format' => 'json', |
547 | 546 | 'maxQueryTimeMillis' => $maxQueryTimeMillis, |
548 | 547 | ], |
549 | - null, ini_get( 'arg_separator.output' ), |
|
548 | + null, ini_get('arg_separator.output'), |
|
550 | 549 | // encode spaces with %20, not + |
551 | 550 | PHP_QUERY_RFC3986 |
552 | 551 | ); |
553 | 552 | |
554 | 553 | $options = [ |
555 | 554 | 'method' => 'GET', |
556 | - 'timeout' => (int)round( ( $maxQueryTimeMillis + 1000 ) / 1000 ), |
|
555 | + 'timeout' => (int) round(($maxQueryTimeMillis + 1000) / 1000), |
|
557 | 556 | 'connectTimeout' => 'default', |
558 | 557 | ]; |
559 | - $request = MWHttpRequest::factory( $url, $options ); |
|
560 | - $startTime = microtime( true ); |
|
558 | + $request = MWHttpRequest::factory($url, $options); |
|
559 | + $startTime = microtime(true); |
|
561 | 560 | $status = $request->execute(); |
562 | - $endTime = microtime( true ); |
|
561 | + $endTime = microtime(true); |
|
563 | 562 | $this->dataFactory->timing( |
564 | 563 | 'wikibase.quality.constraints.sparql.timing', |
565 | - ( $endTime - $startTime ) * 1000 |
|
564 | + ($endTime - $startTime) * 1000 |
|
566 | 565 | ); |
567 | 566 | |
568 | - $maxAge = $this->getCacheMaxAge( $request->getResponseHeaders() ); |
|
569 | - if ( $maxAge ) { |
|
570 | - $this->dataFactory->increment( 'wikibase.quality.constraints.sparql.cached' ); |
|
567 | + $maxAge = $this->getCacheMaxAge($request->getResponseHeaders()); |
|
568 | + if ($maxAge) { |
|
569 | + $this->dataFactory->increment('wikibase.quality.constraints.sparql.cached'); |
|
571 | 570 | } |
572 | 571 | |
573 | - if ( $status->isOK() ) { |
|
572 | + if ($status->isOK()) { |
|
574 | 573 | $json = $request->getContent(); |
575 | - $arr = json_decode( $json, true ); |
|
574 | + $arr = json_decode($json, true); |
|
576 | 575 | return new CachedQueryResults( |
577 | 576 | $arr, |
578 | 577 | Metadata::ofCachingMetadata( |
579 | 578 | $maxAge ? |
580 | - CachingMetadata::ofMaximumAgeInSeconds( $maxAge ) : |
|
581 | - CachingMetadata::fresh() |
|
579 | + CachingMetadata::ofMaximumAgeInSeconds($maxAge) : CachingMetadata::fresh() |
|
582 | 580 | ) |
583 | 581 | ); |
584 | 582 | } else { |
585 | - $this->dataFactory->increment( 'wikibase.quality.constraints.sparql.error' ); |
|
583 | + $this->dataFactory->increment('wikibase.quality.constraints.sparql.error'); |
|
586 | 584 | |
587 | 585 | $this->dataFactory->increment( |
588 | 586 | "wikibase.quality.constraints.sparql.error.http.{$request->getStatus()}" |
589 | 587 | ); |
590 | 588 | |
591 | - if ( $this->isTimeout( $request->getContent() ) ) { |
|
589 | + if ($this->isTimeout($request->getContent())) { |
|
592 | 590 | $this->dataFactory->increment( |
593 | 591 | 'wikibase.quality.constraints.sparql.error.timeout' |
594 | 592 | ); |