@@ -108,18 +108,18 @@ discard block |
||
108 | 108 | $this->violationMessageDeserializer = $violationMessageDeserializer; |
109 | 109 | $this->dataFactory = $dataFactory; |
110 | 110 | |
111 | - $this->entityPrefix = $rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_ENTITY ); |
|
111 | + $this->entityPrefix = $rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_ENTITY); |
|
112 | 112 | $this->prefixes = <<<EOT |
113 | -PREFIX wd: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_ENTITY )}> |
|
114 | -PREFIX wds: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_STATEMENT )}> |
|
115 | -PREFIX wdt: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_DIRECT_CLAIM )}> |
|
116 | -PREFIX wdv: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_VALUE )}> |
|
117 | -PREFIX p: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_CLAIM )}> |
|
118 | -PREFIX ps: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_CLAIM_STATEMENT )}> |
|
119 | -PREFIX pq: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_QUALIFIER )}> |
|
120 | -PREFIX pqv: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_QUALIFIER_VALUE )}> |
|
121 | -PREFIX pr: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_REFERENCE )}> |
|
122 | -PREFIX prv: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_REFERENCE_VALUE )}> |
|
113 | +PREFIX wd: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_ENTITY)}> |
|
114 | +PREFIX wds: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_STATEMENT)}> |
|
115 | +PREFIX wdt: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_DIRECT_CLAIM)}> |
|
116 | +PREFIX wdv: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_VALUE)}> |
|
117 | +PREFIX p: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_CLAIM)}> |
|
118 | +PREFIX ps: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_CLAIM_STATEMENT)}> |
|
119 | +PREFIX pq: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_QUALIFIER)}> |
|
120 | +PREFIX pqv: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_QUALIFIER_VALUE)}> |
|
121 | +PREFIX pr: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_REFERENCE)}> |
|
122 | +PREFIX prv: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_REFERENCE_VALUE)}> |
|
123 | 123 | PREFIX wikibase: <http://wikiba.se/ontology#> |
124 | 124 | PREFIX wikibase-beta: <http://wikiba.se/ontology-beta#> |
125 | 125 | EOT; |
@@ -134,21 +134,21 @@ discard block |
||
134 | 134 | * @return CachedBool |
135 | 135 | * @throws SparqlHelperException if the query times out or some other error occurs |
136 | 136 | */ |
137 | - public function hasType( $id, array $classes, $withInstance ) { |
|
138 | - $instanceOfId = $this->config->get( 'WBQualityConstraintsInstanceOfId' ); |
|
139 | - $subclassOfId = $this->config->get( 'WBQualityConstraintsSubclassOfId' ); |
|
137 | + public function hasType($id, array $classes, $withInstance) { |
|
138 | + $instanceOfId = $this->config->get('WBQualityConstraintsInstanceOfId'); |
|
139 | + $subclassOfId = $this->config->get('WBQualityConstraintsSubclassOfId'); |
|
140 | 140 | |
141 | - $path = ( $withInstance ? "wdt:$instanceOfId/" : "" ) . "wdt:$subclassOfId*"; |
|
141 | + $path = ($withInstance ? "wdt:$instanceOfId/" : "")."wdt:$subclassOfId*"; |
|
142 | 142 | |
143 | 143 | $metadatas = []; |
144 | 144 | |
145 | - foreach ( array_chunk( $classes, 20 ) as $classesChunk ) { |
|
146 | - $classesValues = implode( ' ', array_map( |
|
147 | - function( $class ) { |
|
148 | - return 'wd:' . $class; |
|
145 | + foreach (array_chunk($classes, 20) as $classesChunk) { |
|
146 | + $classesValues = implode(' ', array_map( |
|
147 | + function($class) { |
|
148 | + return 'wd:'.$class; |
|
149 | 149 | }, |
150 | 150 | $classesChunk |
151 | - ) ); |
|
151 | + )); |
|
152 | 152 | |
153 | 153 | $query = <<<EOF |
154 | 154 | ASK { |
@@ -159,19 +159,19 @@ discard block |
||
159 | 159 | EOF; |
160 | 160 | // TODO hint:gearing is a workaround for T168973 and can hopefully be removed eventually |
161 | 161 | |
162 | - $result = $this->runQuery( $query ); |
|
162 | + $result = $this->runQuery($query); |
|
163 | 163 | $metadatas[] = $result->getMetadata(); |
164 | - if ( $result->getArray()['boolean'] ) { |
|
164 | + if ($result->getArray()['boolean']) { |
|
165 | 165 | return new CachedBool( |
166 | 166 | true, |
167 | - Metadata::merge( $metadatas ) |
|
167 | + Metadata::merge($metadatas) |
|
168 | 168 | ); |
169 | 169 | } |
170 | 170 | } |
171 | 171 | |
172 | 172 | return new CachedBool( |
173 | 173 | false, |
174 | - Metadata::merge( $metadatas ) |
|
174 | + Metadata::merge($metadatas) |
|
175 | 175 | ); |
176 | 176 | } |
177 | 177 | |
@@ -187,10 +187,10 @@ discard block |
||
187 | 187 | $ignoreDeprecatedStatements |
188 | 188 | ) { |
189 | 189 | $pid = $statement->getPropertyId()->serialize(); |
190 | - $guid = str_replace( '$', '-', $statement->getGuid() ); |
|
190 | + $guid = str_replace('$', '-', $statement->getGuid()); |
|
191 | 191 | |
192 | 192 | $deprecatedFilter = ''; |
193 | - if ( $ignoreDeprecatedStatements ) { |
|
193 | + if ($ignoreDeprecatedStatements) { |
|
194 | 194 | $deprecatedFilter .= 'MINUS { ?otherStatement wikibase:rank wikibase:DeprecatedRank. }'; |
195 | 195 | $deprecatedFilter .= 'MINUS { ?otherStatement wikibase-beta:rank wikibase-beta:DeprecatedRank. }'; |
196 | 196 | } |
@@ -210,9 +210,9 @@ discard block |
||
210 | 210 | LIMIT 10 |
211 | 211 | EOF; |
212 | 212 | |
213 | - $result = $this->runQuery( $query ); |
|
213 | + $result = $this->runQuery($query); |
|
214 | 214 | |
215 | - return $this->getOtherEntities( $result ); |
|
215 | + return $this->getOtherEntities($result); |
|
216 | 216 | } |
217 | 217 | |
218 | 218 | /** |
@@ -237,16 +237,15 @@ discard block |
||
237 | 237 | $dataType = $this->propertyDataTypeLookup->getDataTypeIdForProperty( |
238 | 238 | $snak->getPropertyId() |
239 | 239 | ); |
240 | - list( $value, $isFullValue ) = $this->getRdfLiteral( $dataType, $dataValue ); |
|
241 | - if ( $isFullValue ) { |
|
240 | + list($value, $isFullValue) = $this->getRdfLiteral($dataType, $dataValue); |
|
241 | + if ($isFullValue) { |
|
242 | 242 | $prefix .= 'v'; |
243 | 243 | } |
244 | 244 | $path = $type === Context::TYPE_QUALIFIER ? |
245 | - "$prefix:$pid" : |
|
246 | - "prov:wasDerivedFrom/$prefix:$pid"; |
|
245 | + "$prefix:$pid" : "prov:wasDerivedFrom/$prefix:$pid"; |
|
247 | 246 | |
248 | 247 | $deprecatedFilter = ''; |
249 | - if ( $ignoreDeprecatedStatements ) { |
|
248 | + if ($ignoreDeprecatedStatements) { |
|
250 | 249 | $deprecatedFilter = <<< EOF |
251 | 250 | MINUS { ?otherStatement wikibase:rank wikibase:DeprecatedRank. } |
252 | 251 | MINUS { ?otherStatement wikibase-beta:rank wikibase-beta:DeprecatedRank. } |
@@ -267,9 +266,9 @@ discard block |
||
267 | 266 | LIMIT 10 |
268 | 267 | EOF; |
269 | 268 | |
270 | - $result = $this->runQuery( $query ); |
|
269 | + $result = $this->runQuery($query); |
|
271 | 270 | |
272 | - return $this->getOtherEntities( $result ); |
|
271 | + return $this->getOtherEntities($result); |
|
273 | 272 | } |
274 | 273 | |
275 | 274 | /** |
@@ -279,8 +278,8 @@ discard block |
||
279 | 278 | * |
280 | 279 | * @return string |
281 | 280 | */ |
282 | - private function stringLiteral( $text ) { |
|
283 | - return '"' . strtr( $text, [ '"' => '\\"', '\\' => '\\\\' ] ) . '"'; |
|
281 | + private function stringLiteral($text) { |
|
282 | + return '"'.strtr($text, ['"' => '\\"', '\\' => '\\\\']).'"'; |
|
284 | 283 | } |
285 | 284 | |
286 | 285 | /** |
@@ -290,17 +289,17 @@ discard block |
||
290 | 289 | * |
291 | 290 | * @return CachedEntityIds |
292 | 291 | */ |
293 | - private function getOtherEntities( CachedQueryResults $results ) { |
|
294 | - return new CachedEntityIds( array_map( |
|
295 | - function ( $resultBindings ) { |
|
292 | + private function getOtherEntities(CachedQueryResults $results) { |
|
293 | + return new CachedEntityIds(array_map( |
|
294 | + function($resultBindings) { |
|
296 | 295 | $entityIRI = $resultBindings['otherEntity']['value']; |
297 | - $entityPrefixLength = strlen( $this->entityPrefix ); |
|
298 | - if ( substr( $entityIRI, 0, $entityPrefixLength ) === $this->entityPrefix ) { |
|
296 | + $entityPrefixLength = strlen($this->entityPrefix); |
|
297 | + if (substr($entityIRI, 0, $entityPrefixLength) === $this->entityPrefix) { |
|
299 | 298 | try { |
300 | 299 | return $this->entityIdParser->parse( |
301 | - substr( $entityIRI, $entityPrefixLength ) |
|
300 | + substr($entityIRI, $entityPrefixLength) |
|
302 | 301 | ); |
303 | - } catch ( EntityIdParsingException $e ) { |
|
302 | + } catch (EntityIdParsingException $e) { |
|
304 | 303 | // fall through |
305 | 304 | } |
306 | 305 | } |
@@ -308,7 +307,7 @@ discard block |
||
308 | 307 | return null; |
309 | 308 | }, |
310 | 309 | $results->getArray()['results']['bindings'] |
311 | - ), $results->getMetadata() ); |
|
310 | + ), $results->getMetadata()); |
|
312 | 311 | } |
313 | 312 | |
314 | 313 | // @codingStandardsIgnoreStart cyclomatic complexity of this function is too high |
@@ -321,47 +320,47 @@ discard block |
||
321 | 320 | * @return array the literal or IRI as a string in SPARQL syntax, |
322 | 321 | * and a boolean indicating whether it refers to a full value node or not |
323 | 322 | */ |
324 | - private function getRdfLiteral( $dataType, DataValue $dataValue ) { |
|
325 | - switch ( $dataType ) { |
|
323 | + private function getRdfLiteral($dataType, DataValue $dataValue) { |
|
324 | + switch ($dataType) { |
|
326 | 325 | case 'string': |
327 | 326 | case 'external-id': |
328 | - return [ $this->stringLiteral( $dataValue->getValue() ), false ]; |
|
327 | + return [$this->stringLiteral($dataValue->getValue()), false]; |
|
329 | 328 | case 'commonsMedia': |
330 | - $url = $this->rdfVocabulary->getMediaFileURI( $dataValue->getValue() ); |
|
331 | - return [ '<' . $url . '>', false ]; |
|
329 | + $url = $this->rdfVocabulary->getMediaFileURI($dataValue->getValue()); |
|
330 | + return ['<'.$url.'>', false]; |
|
332 | 331 | case 'geo-shape': |
333 | - $url = $this->rdfVocabulary->getGeoShapeURI( $dataValue->getValue() ); |
|
334 | - return [ '<' . $url . '>', false ]; |
|
332 | + $url = $this->rdfVocabulary->getGeoShapeURI($dataValue->getValue()); |
|
333 | + return ['<'.$url.'>', false]; |
|
335 | 334 | case 'tabular-data': |
336 | - $url = $this->rdfVocabulary->getTabularDataURI( $dataValue->getValue() ); |
|
337 | - return [ '<' . $url . '>', false ]; |
|
335 | + $url = $this->rdfVocabulary->getTabularDataURI($dataValue->getValue()); |
|
336 | + return ['<'.$url.'>', false]; |
|
338 | 337 | case 'url': |
339 | 338 | $url = $dataValue->getValue(); |
340 | - if ( !preg_match( '/^[^<>"{}\\\\|^`\\x00-\\x20]*$/D', $url ) ) { |
|
339 | + if (!preg_match('/^[^<>"{}\\\\|^`\\x00-\\x20]*$/D', $url)) { |
|
341 | 340 | // not a valid URL for SPARQL (see SPARQL spec, production 139 IRIREF) |
342 | 341 | // such an URL should never reach us, so just throw |
343 | - throw new InvalidArgumentException( 'invalid URL: ' . $url ); |
|
342 | + throw new InvalidArgumentException('invalid URL: '.$url); |
|
344 | 343 | } |
345 | - return [ '<' . $url . '>', false ]; |
|
344 | + return ['<'.$url.'>', false]; |
|
346 | 345 | case 'wikibase-item': |
347 | 346 | case 'wikibase-property': |
348 | 347 | /** @var EntityIdValue $dataValue */ |
349 | - return [ 'wd:' . $dataValue->getEntityId()->getSerialization(), false ]; |
|
348 | + return ['wd:'.$dataValue->getEntityId()->getSerialization(), false]; |
|
350 | 349 | case 'monolingualtext': |
351 | 350 | /** @var MonolingualTextValue $dataValue */ |
352 | 351 | $lang = $dataValue->getLanguageCode(); |
353 | - if ( !preg_match( '/^[a-zA-Z]+(-[a-zA-Z0-9]+)*$/D', $lang ) ) { |
|
352 | + if (!preg_match('/^[a-zA-Z]+(-[a-zA-Z0-9]+)*$/D', $lang)) { |
|
354 | 353 | // not a valid language tag for SPARQL (see SPARQL spec, production 145 LANGTAG) |
355 | 354 | // such a language tag should never reach us, so just throw |
356 | - throw new InvalidArgumentException( 'invalid language tag: ' . $lang ); |
|
355 | + throw new InvalidArgumentException('invalid language tag: '.$lang); |
|
357 | 356 | } |
358 | - return [ $this->stringLiteral( $dataValue->getText() ) . '@' . $lang, false ]; |
|
357 | + return [$this->stringLiteral($dataValue->getText()).'@'.$lang, false]; |
|
359 | 358 | case 'globe-coordinate': |
360 | 359 | case 'quantity': |
361 | 360 | case 'time': |
362 | - return [ 'wdv:' . $dataValue->getHash(), true ]; |
|
361 | + return ['wdv:'.$dataValue->getHash(), true]; |
|
363 | 362 | default: |
364 | - throw new InvalidArgumentException( 'unknown data type: ' . $dataType ); |
|
363 | + throw new InvalidArgumentException('unknown data type: '.$dataType); |
|
365 | 364 | } |
366 | 365 | } |
367 | 366 | // @codingStandardsIgnoreEnd |
@@ -374,44 +373,44 @@ discard block |
||
374 | 373 | * @throws SparqlHelperException if the query times out or some other error occurs |
375 | 374 | * @throws ConstraintParameterException if the $regex is invalid |
376 | 375 | */ |
377 | - public function matchesRegularExpression( $text, $regex ) { |
|
376 | + public function matchesRegularExpression($text, $regex) { |
|
378 | 377 | // caching wrapper around matchesRegularExpressionWithSparql |
379 | 378 | |
380 | - $textHash = hash( 'sha256', $text ); |
|
379 | + $textHash = hash('sha256', $text); |
|
381 | 380 | $cacheKey = $this->cache->makeKey( |
382 | 381 | 'WikibaseQualityConstraints', // extension |
383 | 382 | 'regex', // action |
384 | 383 | 'WDQS-Java', // regex flavor |
385 | - hash( 'sha256', $regex ) |
|
384 | + hash('sha256', $regex) |
|
386 | 385 | ); |
387 | - $cacheMapSize = $this->config->get( 'WBQualityConstraintsFormatCacheMapSize' ); |
|
386 | + $cacheMapSize = $this->config->get('WBQualityConstraintsFormatCacheMapSize'); |
|
388 | 387 | |
389 | 388 | $cacheMapArray = $this->cache->getWithSetCallback( |
390 | 389 | $cacheKey, |
391 | 390 | WANObjectCache::TTL_DAY, |
392 | - function( $cacheMapArray ) use ( $text, $regex, $textHash, $cacheMapSize ) { |
|
391 | + function($cacheMapArray) use ($text, $regex, $textHash, $cacheMapSize) { |
|
393 | 392 | // Initialize the cache map if not set |
394 | - if ( $cacheMapArray === false ) { |
|
393 | + if ($cacheMapArray === false) { |
|
395 | 394 | $key = 'wikibase.quality.constraints.regex.cache.refresh.init'; |
396 | - $this->dataFactory->increment( $key ); |
|
395 | + $this->dataFactory->increment($key); |
|
397 | 396 | return []; |
398 | 397 | } |
399 | 398 | |
400 | 399 | $key = 'wikibase.quality.constraints.regex.cache.refresh'; |
401 | - $this->dataFactory->increment( $key ); |
|
402 | - $cacheMap = MapCacheLRU::newFromArray( $cacheMapArray, $cacheMapSize ); |
|
403 | - if ( $cacheMap->has( $textHash ) ) { |
|
400 | + $this->dataFactory->increment($key); |
|
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,15 +473,15 @@ discard block |
||
474 | 473 | } |
475 | 474 | } |
476 | 475 | |
477 | - private function deserializeConstraintParameterException( array $serialization ) { |
|
478 | - if ( array_key_exists( 'violationMessage', $serialization ) ) { |
|
476 | + private function deserializeConstraintParameterException(array $serialization) { |
|
477 | + if (array_key_exists('violationMessage', $serialization)) { |
|
479 | 478 | $message = $this->violationMessageDeserializer->deserialize( |
480 | 479 | $serialization['violationMessage'] |
481 | 480 | ); |
482 | 481 | } else { |
483 | 482 | $message = $serialization['message']; |
484 | 483 | } |
485 | - return new ConstraintParameterException( $message ); |
|
484 | + return new ConstraintParameterException($message); |
|
486 | 485 | } |
487 | 486 | |
488 | 487 | /** |
@@ -496,26 +495,26 @@ discard block |
||
496 | 495 | * @throws SparqlHelperException if the query times out or some other error occurs |
497 | 496 | * @throws ConstraintParameterException if the $regex is invalid |
498 | 497 | */ |
499 | - public function matchesRegularExpressionWithSparql( $text, $regex ) { |
|
500 | - $textStringLiteral = $this->stringLiteral( $text ); |
|
501 | - $regexStringLiteral = $this->stringLiteral( '^' . $regex . '$' ); |
|
498 | + public function matchesRegularExpressionWithSparql($text, $regex) { |
|
499 | + $textStringLiteral = $this->stringLiteral($text); |
|
500 | + $regexStringLiteral = $this->stringLiteral('^'.$regex.'$'); |
|
502 | 501 | |
503 | 502 | $query = <<<EOF |
504 | 503 | SELECT (REGEX($textStringLiteral, $regexStringLiteral) AS ?matches) {} |
505 | 504 | EOF; |
506 | 505 | |
507 | - $result = $this->runQuery( $query ); |
|
506 | + $result = $this->runQuery($query); |
|
508 | 507 | |
509 | 508 | $vars = $result->getArray()['results']['bindings'][0]; |
510 | - if ( array_key_exists( 'matches', $vars ) ) { |
|
509 | + if (array_key_exists('matches', $vars)) { |
|
511 | 510 | // true or false ⇒ regex okay, text matches or not |
512 | 511 | return $vars['matches']['value'] === 'true'; |
513 | 512 | } else { |
514 | 513 | // empty result: regex broken |
515 | 514 | throw new ConstraintParameterException( |
516 | - wfMessage( 'wbqc-violation-message-parameter-regex' ) |
|
517 | - ->rawParams( ConstraintParameterRenderer::formatByRole( Role::CONSTRAINT_PARAMETER_VALUE, |
|
518 | - '<code><nowiki>' . htmlspecialchars( $regex ) . '</nowiki></code>' ) ) |
|
515 | + wfMessage('wbqc-violation-message-parameter-regex') |
|
516 | + ->rawParams(ConstraintParameterRenderer::formatByRole(Role::CONSTRAINT_PARAMETER_VALUE, |
|
517 | + '<code><nowiki>'.htmlspecialchars($regex).'</nowiki></code>')) |
|
519 | 518 | ->escaped() |
520 | 519 | ); |
521 | 520 | } |
@@ -528,14 +527,14 @@ discard block |
||
528 | 527 | * |
529 | 528 | * @return boolean |
530 | 529 | */ |
531 | - public function isTimeout( $responseContent ) { |
|
532 | - $timeoutRegex = implode( '|', array_map( |
|
533 | - function ( $fqn ) { |
|
534 | - return preg_quote( $fqn, '/' ); |
|
530 | + public function isTimeout($responseContent) { |
|
531 | + $timeoutRegex = implode('|', array_map( |
|
532 | + function($fqn) { |
|
533 | + return preg_quote($fqn, '/'); |
|
535 | 534 | }, |
536 | - $this->config->get( 'WBQualityConstraintsSparqlTimeoutExceptionClasses' ) |
|
537 | - ) ); |
|
538 | - return (bool)preg_match( '/' . $timeoutRegex . '/', $responseContent ); |
|
535 | + $this->config->get('WBQualityConstraintsSparqlTimeoutExceptionClasses') |
|
536 | + )); |
|
537 | + return (bool) preg_match('/'.$timeoutRegex.'/', $responseContent); |
|
539 | 538 | } |
540 | 539 | |
541 | 540 | /** |
@@ -547,17 +546,17 @@ discard block |
||
547 | 546 | * @return integer|boolean the max-age (in seconds) |
548 | 547 | * or a plain boolean if no max-age can be determined |
549 | 548 | */ |
550 | - public function getCacheMaxAge( $responseHeaders ) { |
|
549 | + public function getCacheMaxAge($responseHeaders) { |
|
551 | 550 | if ( |
552 | - array_key_exists( 'x-cache-status', $responseHeaders ) && |
|
553 | - preg_match( '/^hit(?:-.*)?$/', $responseHeaders['x-cache-status'][0] ) |
|
551 | + array_key_exists('x-cache-status', $responseHeaders) && |
|
552 | + preg_match('/^hit(?:-.*)?$/', $responseHeaders['x-cache-status'][0]) |
|
554 | 553 | ) { |
555 | 554 | $maxage = []; |
556 | 555 | if ( |
557 | - array_key_exists( 'cache-control', $responseHeaders ) && |
|
558 | - preg_match( '/\bmax-age=(\d+)\b/', $responseHeaders['cache-control'][0], $maxage ) |
|
556 | + array_key_exists('cache-control', $responseHeaders) && |
|
557 | + preg_match('/\bmax-age=(\d+)\b/', $responseHeaders['cache-control'][0], $maxage) |
|
559 | 558 | ) { |
560 | - return intval( $maxage[1] ); |
|
559 | + return intval($maxage[1]); |
|
561 | 560 | } else { |
562 | 561 | return true; |
563 | 562 | } |
@@ -575,59 +574,58 @@ discard block |
||
575 | 574 | * |
576 | 575 | * @throws SparqlHelperException if the query times out or some other error occurs |
577 | 576 | */ |
578 | - public function runQuery( $query ) { |
|
577 | + public function runQuery($query) { |
|
579 | 578 | |
580 | - $endpoint = $this->config->get( 'WBQualityConstraintsSparqlEndpoint' ); |
|
581 | - $maxQueryTimeMillis = $this->config->get( 'WBQualityConstraintsSparqlMaxMillis' ); |
|
582 | - $url = $endpoint . '?' . http_build_query( |
|
579 | + $endpoint = $this->config->get('WBQualityConstraintsSparqlEndpoint'); |
|
580 | + $maxQueryTimeMillis = $this->config->get('WBQualityConstraintsSparqlMaxMillis'); |
|
581 | + $url = $endpoint.'?'.http_build_query( |
|
583 | 582 | [ |
584 | - 'query' => "#wbqc\n" . $this->prefixes . $query, |
|
583 | + 'query' => "#wbqc\n".$this->prefixes.$query, |
|
585 | 584 | 'format' => 'json', |
586 | 585 | 'maxQueryTimeMillis' => $maxQueryTimeMillis, |
587 | 586 | ], |
588 | - null, ini_get( 'arg_separator.output' ), |
|
587 | + null, ini_get('arg_separator.output'), |
|
589 | 588 | // encode spaces with %20, not + |
590 | 589 | PHP_QUERY_RFC3986 |
591 | 590 | ); |
592 | 591 | |
593 | 592 | $options = [ |
594 | 593 | 'method' => 'GET', |
595 | - 'timeout' => (int)round( ( $maxQueryTimeMillis + 1000 ) / 1000 ), |
|
594 | + 'timeout' => (int) round(($maxQueryTimeMillis + 1000) / 1000), |
|
596 | 595 | 'connectTimeout' => 'default', |
597 | 596 | ]; |
598 | - $request = MWHttpRequest::factory( $url, $options ); |
|
599 | - $startTime = microtime( true ); |
|
597 | + $request = MWHttpRequest::factory($url, $options); |
|
598 | + $startTime = microtime(true); |
|
600 | 599 | $status = $request->execute(); |
601 | - $endTime = microtime( true ); |
|
600 | + $endTime = microtime(true); |
|
602 | 601 | $this->dataFactory->timing( |
603 | 602 | 'wikibase.quality.constraints.sparql.timing', |
604 | - ( $endTime - $startTime ) * 1000 |
|
603 | + ($endTime - $startTime) * 1000 |
|
605 | 604 | ); |
606 | 605 | |
607 | - $maxAge = $this->getCacheMaxAge( $request->getResponseHeaders() ); |
|
608 | - if ( $maxAge ) { |
|
609 | - $this->dataFactory->increment( 'wikibase.quality.constraints.sparql.cached' ); |
|
606 | + $maxAge = $this->getCacheMaxAge($request->getResponseHeaders()); |
|
607 | + if ($maxAge) { |
|
608 | + $this->dataFactory->increment('wikibase.quality.constraints.sparql.cached'); |
|
610 | 609 | } |
611 | 610 | |
612 | - if ( $status->isOK() ) { |
|
611 | + if ($status->isOK()) { |
|
613 | 612 | $json = $request->getContent(); |
614 | - $arr = json_decode( $json, true ); |
|
613 | + $arr = json_decode($json, true); |
|
615 | 614 | return new CachedQueryResults( |
616 | 615 | $arr, |
617 | 616 | Metadata::ofCachingMetadata( |
618 | 617 | $maxAge ? |
619 | - CachingMetadata::ofMaximumAgeInSeconds( $maxAge ) : |
|
620 | - CachingMetadata::fresh() |
|
618 | + CachingMetadata::ofMaximumAgeInSeconds($maxAge) : CachingMetadata::fresh() |
|
621 | 619 | ) |
622 | 620 | ); |
623 | 621 | } else { |
624 | - $this->dataFactory->increment( 'wikibase.quality.constraints.sparql.error' ); |
|
622 | + $this->dataFactory->increment('wikibase.quality.constraints.sparql.error'); |
|
625 | 623 | |
626 | 624 | $this->dataFactory->increment( |
627 | 625 | "wikibase.quality.constraints.sparql.error.http.{$request->getStatus()}" |
628 | 626 | ); |
629 | 627 | |
630 | - if ( $this->isTimeout( $request->getContent() ) ) { |
|
628 | + if ($this->isTimeout($request->getContent())) { |
|
631 | 629 | $this->dataFactory->increment( |
632 | 630 | 'wikibase.quality.constraints.sparql.error.timeout' |
633 | 631 | ); |