@@ -89,18 +89,18 @@ discard block |
||
89 | 89 | $this->propertyDataTypeLookup = $propertyDataTypeLookup; |
90 | 90 | $this->cache = $cache; |
91 | 91 | |
92 | - $this->entityPrefix = $rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_ENTITY ); |
|
92 | + $this->entityPrefix = $rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_ENTITY); |
|
93 | 93 | $this->prefixes = <<<EOT |
94 | -PREFIX wd: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_ENTITY )}> |
|
95 | -PREFIX wds: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_STATEMENT )}> |
|
96 | -PREFIX wdt: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_DIRECT_CLAIM )}> |
|
97 | -PREFIX wdv: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NS_VALUE )}> |
|
98 | -PREFIX p: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_CLAIM )}> |
|
99 | -PREFIX ps: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_CLAIM_STATEMENT )}> |
|
100 | -PREFIX pq: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_QUALIFIER )}> |
|
101 | -PREFIX pqv: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_QUALIFIER_VALUE )}> |
|
102 | -PREFIX pr: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_REFERENCE )}> |
|
103 | -PREFIX prv: <{$rdfVocabulary->getNamespaceURI( RdfVocabulary::NSP_REFERENCE_VALUE )}> |
|
94 | +PREFIX wd: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_ENTITY)}> |
|
95 | +PREFIX wds: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_STATEMENT)}> |
|
96 | +PREFIX wdt: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_DIRECT_CLAIM)}> |
|
97 | +PREFIX wdv: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NS_VALUE)}> |
|
98 | +PREFIX p: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_CLAIM)}> |
|
99 | +PREFIX ps: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_CLAIM_STATEMENT)}> |
|
100 | +PREFIX pq: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_QUALIFIER)}> |
|
101 | +PREFIX pqv: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_QUALIFIER_VALUE)}> |
|
102 | +PREFIX pr: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_REFERENCE)}> |
|
103 | +PREFIX prv: <{$rdfVocabulary->getNamespaceURI(RdfVocabulary::NSP_REFERENCE_VALUE)}> |
|
104 | 104 | PREFIX wikibase: <http://wikiba.se/ontology#> |
105 | 105 | PREFIX wikibase-beta: <http://wikiba.se/ontology-beta#> |
106 | 106 | EOT; |
@@ -117,21 +117,21 @@ discard block |
||
117 | 117 | * @return CachedBool |
118 | 118 | * @throws SparqlHelperException if the query times out or some other error occurs |
119 | 119 | */ |
120 | - public function hasType( $id, array $classes, $withInstance ) { |
|
121 | - $instanceOfId = $this->config->get( 'WBQualityConstraintsInstanceOfId' ); |
|
122 | - $subclassOfId = $this->config->get( 'WBQualityConstraintsSubclassOfId' ); |
|
120 | + public function hasType($id, array $classes, $withInstance) { |
|
121 | + $instanceOfId = $this->config->get('WBQualityConstraintsInstanceOfId'); |
|
122 | + $subclassOfId = $this->config->get('WBQualityConstraintsSubclassOfId'); |
|
123 | 123 | |
124 | - $path = ( $withInstance ? "wdt:$instanceOfId/" : "" ) . "wdt:$subclassOfId*"; |
|
124 | + $path = ($withInstance ? "wdt:$instanceOfId/" : "")."wdt:$subclassOfId*"; |
|
125 | 125 | |
126 | 126 | $metadatas = []; |
127 | 127 | |
128 | - foreach ( array_chunk( $classes, 20 ) as $classesChunk ) { |
|
129 | - $classesValues = implode( ' ', array_map( |
|
130 | - function( $class ) { |
|
131 | - return 'wd:' . $class; |
|
128 | + foreach (array_chunk($classes, 20) as $classesChunk) { |
|
129 | + $classesValues = implode(' ', array_map( |
|
130 | + function($class) { |
|
131 | + return 'wd:'.$class; |
|
132 | 132 | }, |
133 | 133 | $classesChunk |
134 | - ) ); |
|
134 | + )); |
|
135 | 135 | |
136 | 136 | $query = <<<EOF |
137 | 137 | ASK { |
@@ -142,19 +142,19 @@ discard block |
||
142 | 142 | EOF; |
143 | 143 | // TODO hint:gearing is a workaround for T168973 and can hopefully be removed eventually |
144 | 144 | |
145 | - $result = $this->runQuery( $query ); |
|
145 | + $result = $this->runQuery($query); |
|
146 | 146 | $metadatas[] = $result->getMetadata(); |
147 | - if ( $result->getArray()['boolean'] ) { |
|
147 | + if ($result->getArray()['boolean']) { |
|
148 | 148 | return new CachedBool( |
149 | 149 | true, |
150 | - Metadata::merge( $metadatas ) |
|
150 | + Metadata::merge($metadatas) |
|
151 | 151 | ); |
152 | 152 | } |
153 | 153 | } |
154 | 154 | |
155 | 155 | return new CachedBool( |
156 | 156 | false, |
157 | - Metadata::merge( $metadatas ) |
|
157 | + Metadata::merge($metadatas) |
|
158 | 158 | ); |
159 | 159 | } |
160 | 160 | |
@@ -170,10 +170,10 @@ discard block |
||
170 | 170 | $ignoreDeprecatedStatements |
171 | 171 | ) { |
172 | 172 | $pid = $statement->getPropertyId()->serialize(); |
173 | - $guid = str_replace( '$', '-', $statement->getGuid() ); |
|
173 | + $guid = str_replace('$', '-', $statement->getGuid()); |
|
174 | 174 | |
175 | 175 | $deprecatedFilter = ''; |
176 | - if ( $ignoreDeprecatedStatements ) { |
|
176 | + if ($ignoreDeprecatedStatements) { |
|
177 | 177 | $deprecatedFilter .= 'MINUS { ?otherStatement wikibase:rank wikibase:DeprecatedRank. }'; |
178 | 178 | $deprecatedFilter .= 'MINUS { ?otherStatement wikibase-beta:rank wikibase-beta:DeprecatedRank. }'; |
179 | 179 | } |
@@ -193,9 +193,9 @@ discard block |
||
193 | 193 | LIMIT 10 |
194 | 194 | EOF; |
195 | 195 | |
196 | - $result = $this->runQuery( $query ); |
|
196 | + $result = $this->runQuery($query); |
|
197 | 197 | |
198 | - return $this->getOtherEntities( $result ); |
|
198 | + return $this->getOtherEntities($result); |
|
199 | 199 | } |
200 | 200 | |
201 | 201 | /** |
@@ -220,16 +220,15 @@ discard block |
||
220 | 220 | $dataType = $this->propertyDataTypeLookup->getDataTypeIdForProperty( |
221 | 221 | $snak->getPropertyId() |
222 | 222 | ); |
223 | - list ( $value, $isFullValue ) = $this->getRdfLiteral( $dataType, $dataValue ); |
|
224 | - if ( $isFullValue ) { |
|
223 | + list ($value, $isFullValue) = $this->getRdfLiteral($dataType, $dataValue); |
|
224 | + if ($isFullValue) { |
|
225 | 225 | $prefix .= 'v'; |
226 | 226 | } |
227 | 227 | $path = $type === Context::TYPE_QUALIFIER ? |
228 | - "$prefix:$pid" : |
|
229 | - "prov:wasDerivedFrom/$prefix:$pid"; |
|
228 | + "$prefix:$pid" : "prov:wasDerivedFrom/$prefix:$pid"; |
|
230 | 229 | |
231 | 230 | $deprecatedFilter = ''; |
232 | - if ( $ignoreDeprecatedStatements ) { |
|
231 | + if ($ignoreDeprecatedStatements) { |
|
233 | 232 | $deprecatedFilter = <<< EOF |
234 | 233 | MINUS { ?otherStatement wikibase:rank wikibase:DeprecatedRank. } |
235 | 234 | MINUS { ?otherStatement wikibase-beta:rank wikibase-beta:DeprecatedRank. } |
@@ -250,9 +249,9 @@ discard block |
||
250 | 249 | LIMIT 10 |
251 | 250 | EOF; |
252 | 251 | |
253 | - $result = $this->runQuery( $query ); |
|
252 | + $result = $this->runQuery($query); |
|
254 | 253 | |
255 | - return $this->getOtherEntities( $result ); |
|
254 | + return $this->getOtherEntities($result); |
|
256 | 255 | } |
257 | 256 | |
258 | 257 | /** |
@@ -262,8 +261,8 @@ discard block |
||
262 | 261 | * |
263 | 262 | * @return string |
264 | 263 | */ |
265 | - private function stringLiteral( $text ) { |
|
266 | - return '"' . strtr( $text, [ '"' => '\\"', '\\' => '\\\\' ] ) . '"'; |
|
264 | + private function stringLiteral($text) { |
|
265 | + return '"'.strtr($text, ['"' => '\\"', '\\' => '\\\\']).'"'; |
|
267 | 266 | } |
268 | 267 | |
269 | 268 | /** |
@@ -273,17 +272,17 @@ discard block |
||
273 | 272 | * |
274 | 273 | * @return CachedEntityIds |
275 | 274 | */ |
276 | - private function getOtherEntities( CachedQueryResults $results ) { |
|
277 | - return new CachedEntityIds( array_map( |
|
278 | - function ( $resultBindings ) { |
|
275 | + private function getOtherEntities(CachedQueryResults $results) { |
|
276 | + return new CachedEntityIds(array_map( |
|
277 | + function($resultBindings) { |
|
279 | 278 | $entityIRI = $resultBindings['otherEntity']['value']; |
280 | - $entityPrefixLength = strlen( $this->entityPrefix ); |
|
281 | - if ( substr( $entityIRI, 0, $entityPrefixLength ) === $this->entityPrefix ) { |
|
279 | + $entityPrefixLength = strlen($this->entityPrefix); |
|
280 | + if (substr($entityIRI, 0, $entityPrefixLength) === $this->entityPrefix) { |
|
282 | 281 | try { |
283 | 282 | return $this->entityIdParser->parse( |
284 | - substr( $entityIRI, $entityPrefixLength ) |
|
283 | + substr($entityIRI, $entityPrefixLength) |
|
285 | 284 | ); |
286 | - } catch ( EntityIdParsingException $e ) { |
|
285 | + } catch (EntityIdParsingException $e) { |
|
287 | 286 | // fall through |
288 | 287 | } |
289 | 288 | } |
@@ -291,7 +290,7 @@ discard block |
||
291 | 290 | return null; |
292 | 291 | }, |
293 | 292 | $results->getArray()['results']['bindings'] |
294 | - ), $results->getMetadata() ); |
|
293 | + ), $results->getMetadata()); |
|
295 | 294 | } |
296 | 295 | |
297 | 296 | // @codingStandardsIgnoreStart cyclomatic complexity of this function is too high |
@@ -304,47 +303,47 @@ discard block |
||
304 | 303 | * @return array the literal or IRI as a string in SPARQL syntax, |
305 | 304 | * and a boolean indicating whether it refers to a full value node or not |
306 | 305 | */ |
307 | - private function getRdfLiteral( $dataType, DataValue $dataValue ) { |
|
308 | - switch ( $dataType ) { |
|
306 | + private function getRdfLiteral($dataType, DataValue $dataValue) { |
|
307 | + switch ($dataType) { |
|
309 | 308 | case 'string': |
310 | 309 | case 'external-id': |
311 | - return [ $this->stringLiteral( $dataValue->getValue() ), false ]; |
|
310 | + return [$this->stringLiteral($dataValue->getValue()), false]; |
|
312 | 311 | case 'commonsMedia': |
313 | - $url = $this->rdfVocabulary->getMediaFileURI( $dataValue->getValue() ); |
|
314 | - return [ '<' . $url . '>', false ]; |
|
312 | + $url = $this->rdfVocabulary->getMediaFileURI($dataValue->getValue()); |
|
313 | + return ['<'.$url.'>', false]; |
|
315 | 314 | case 'geo-shape': |
316 | - $url = $this->rdfVocabulary->getGeoShapeURI( $dataValue->getValue() ); |
|
317 | - return [ '<' . $url . '>', false ]; |
|
315 | + $url = $this->rdfVocabulary->getGeoShapeURI($dataValue->getValue()); |
|
316 | + return ['<'.$url.'>', false]; |
|
318 | 317 | case 'tabular-data': |
319 | - $url = $this->rdfVocabulary->getTabularDataURI( $dataValue->getValue() ); |
|
320 | - return [ '<' . $url . '>', false ]; |
|
318 | + $url = $this->rdfVocabulary->getTabularDataURI($dataValue->getValue()); |
|
319 | + return ['<'.$url.'>', false]; |
|
321 | 320 | case 'url': |
322 | 321 | $url = $dataValue->getValue(); |
323 | - if ( !preg_match( '/^[^<>"{}\\\\|^`\\x00-\\x20]*$/D', $url ) ) { |
|
322 | + if (!preg_match('/^[^<>"{}\\\\|^`\\x00-\\x20]*$/D', $url)) { |
|
324 | 323 | // not a valid URL for SPARQL (see SPARQL spec, production 139 IRIREF) |
325 | 324 | // such an URL should never reach us, so just throw |
326 | - throw new InvalidArgumentException( 'invalid URL: ' . $url ); |
|
325 | + throw new InvalidArgumentException('invalid URL: '.$url); |
|
327 | 326 | } |
328 | - return [ '<' . $url . '>', false ]; |
|
327 | + return ['<'.$url.'>', false]; |
|
329 | 328 | case 'wikibase-item': |
330 | 329 | case 'wikibase-property': |
331 | 330 | /** @var EntityIdValue $dataValue */ |
332 | - return [ 'wd:' . $dataValue->getEntityId()->getSerialization(), false ]; |
|
331 | + return ['wd:'.$dataValue->getEntityId()->getSerialization(), false]; |
|
333 | 332 | case 'monolingualtext': |
334 | 333 | /** @var MonolingualTextValue $dataValue */ |
335 | 334 | $lang = $dataValue->getLanguageCode(); |
336 | - if ( !preg_match( '/^[a-zA-Z]+(-[a-zA-Z0-9]+)*$/D', $lang ) ) { |
|
335 | + if (!preg_match('/^[a-zA-Z]+(-[a-zA-Z0-9]+)*$/D', $lang)) { |
|
337 | 336 | // not a valid language tag for SPARQL (see SPARQL spec, production 145 LANGTAG) |
338 | 337 | // such a language tag should never reach us, so just throw |
339 | - throw new InvalidArgumentException( 'invalid language tag: ' . $lang ); |
|
338 | + throw new InvalidArgumentException('invalid language tag: '.$lang); |
|
340 | 339 | } |
341 | - return [ $this->stringLiteral( $dataValue->getText() ) . '@' . $lang, false ]; |
|
340 | + return [$this->stringLiteral($dataValue->getText()).'@'.$lang, false]; |
|
342 | 341 | case 'globe-coordinate': |
343 | 342 | case 'quantity': |
344 | 343 | case 'time': |
345 | - return [ 'wdv:' . $dataValue->getHash(), true ]; |
|
344 | + return ['wdv:'.$dataValue->getHash(), true]; |
|
346 | 345 | default: |
347 | - throw new InvalidArgumentException( 'unknown data type: ' . $dataType ); |
|
346 | + throw new InvalidArgumentException('unknown data type: '.$dataType); |
|
348 | 347 | } |
349 | 348 | } |
350 | 349 | // @codingStandardsIgnoreEnd |
@@ -357,41 +356,41 @@ discard block |
||
357 | 356 | * @throws SparqlHelperException if the query times out or some other error occurs |
358 | 357 | * @throws ConstraintParameterException if the $regex is invalid |
359 | 358 | */ |
360 | - public function matchesRegularExpression( $text, $regex ) { |
|
359 | + public function matchesRegularExpression($text, $regex) { |
|
361 | 360 | // caching wrapper around matchesRegularExpressionWithSparql |
362 | 361 | |
363 | - $textHash = hash( 'sha256', $text ); |
|
364 | - $cacheMapSize = $this->config->get( 'WBQualityConstraintsFormatCacheMapSize' ); |
|
362 | + $textHash = hash('sha256', $text); |
|
363 | + $cacheMapSize = $this->config->get('WBQualityConstraintsFormatCacheMapSize'); |
|
365 | 364 | |
366 | 365 | $cacheMapArray = $this->cache->getWithSetCallback( |
367 | 366 | $this->cache->makeKey( |
368 | 367 | 'WikibaseQualityConstraints', // extension |
369 | 368 | 'regex', // action |
370 | 369 | 'WDQS-Java', // regex flavor |
371 | - hash( 'sha256', $regex ) |
|
370 | + hash('sha256', $regex) |
|
372 | 371 | ), |
373 | 372 | WANObjectCache::TTL_DAY, |
374 | - function( $cacheMapArray ) use ( $text, $regex, $textHash, $cacheMapSize ) { |
|
373 | + function($cacheMapArray) use ($text, $regex, $textHash, $cacheMapSize) { |
|
375 | 374 | // Initialize the cache map if not set |
376 | - if ( $cacheMapArray === false ) { |
|
375 | + if ($cacheMapArray === false) { |
|
377 | 376 | $key = 'wikibase.quality.constraints.regex.cache.refresh.init'; |
378 | - $this->dataFactory->increment( $key ); |
|
377 | + $this->dataFactory->increment($key); |
|
379 | 378 | return []; |
380 | 379 | } |
381 | 380 | |
382 | 381 | $key = 'wikibase.quality.constraints.regex.cache.refresh'; |
383 | - $this->dataFactory->increment( $key ); |
|
384 | - $cacheMap = MapCacheLRU::newFromArray( $cacheMapArray, $cacheMapSize ); |
|
385 | - if ( $cacheMap->has( $textHash ) ) { |
|
382 | + $this->dataFactory->increment($key); |
|
383 | + $cacheMap = MapCacheLRU::newFromArray($cacheMapArray, $cacheMapSize); |
|
384 | + if ($cacheMap->has($textHash)) { |
|
386 | 385 | $key = 'wikibase.quality.constraints.regex.cache.refresh.hit'; |
387 | - $this->dataFactory->increment( $key ); |
|
388 | - $cacheMap->get( $textHash ); // ping cache |
|
386 | + $this->dataFactory->increment($key); |
|
387 | + $cacheMap->get($textHash); // ping cache |
|
389 | 388 | } else { |
390 | 389 | $key = 'wikibase.quality.constraints.regex.cache.refresh.miss'; |
391 | - $this->dataFactory->increment( $key ); |
|
390 | + $this->dataFactory->increment($key); |
|
392 | 391 | $cacheMap->set( |
393 | 392 | $textHash, |
394 | - $this->matchesRegularExpressionWithSparql( $text, $regex ), |
|
393 | + $this->matchesRegularExpressionWithSparql($text, $regex), |
|
395 | 394 | 3 / 8 |
396 | 395 | ); |
397 | 396 | } |
@@ -409,14 +408,14 @@ discard block |
||
409 | 408 | ] |
410 | 409 | ); |
411 | 410 | |
412 | - if ( isset( $cacheMapArray[$textHash] ) ) { |
|
411 | + if (isset($cacheMapArray[$textHash])) { |
|
413 | 412 | $key = 'wikibase.quality.constraints.regex.cache.hit'; |
414 | - $this->dataFactory->increment( $key ); |
|
413 | + $this->dataFactory->increment($key); |
|
415 | 414 | return $cacheMapArray[$textHash]; |
416 | 415 | } else { |
417 | 416 | $key = 'wikibase.quality.constraints.regex.cache.miss'; |
418 | - $this->dataFactory->increment( $key ); |
|
419 | - return $this->matchesRegularExpressionWithSparql( $text, $regex ); |
|
417 | + $this->dataFactory->increment($key); |
|
418 | + return $this->matchesRegularExpressionWithSparql($text, $regex); |
|
420 | 419 | } |
421 | 420 | } |
422 | 421 | |
@@ -431,26 +430,26 @@ discard block |
||
431 | 430 | * @throws SparqlHelperException if the query times out or some other error occurs |
432 | 431 | * @throws ConstraintParameterException if the $regex is invalid |
433 | 432 | */ |
434 | - public function matchesRegularExpressionWithSparql( $text, $regex ) { |
|
435 | - $textStringLiteral = $this->stringLiteral( $text ); |
|
436 | - $regexStringLiteral = $this->stringLiteral( '^' . $regex . '$' ); |
|
433 | + public function matchesRegularExpressionWithSparql($text, $regex) { |
|
434 | + $textStringLiteral = $this->stringLiteral($text); |
|
435 | + $regexStringLiteral = $this->stringLiteral('^'.$regex.'$'); |
|
437 | 436 | |
438 | 437 | $query = <<<EOF |
439 | 438 | SELECT (REGEX($textStringLiteral, $regexStringLiteral) AS ?matches) {} |
440 | 439 | EOF; |
441 | 440 | |
442 | - $result = $this->runQuery( $query ); |
|
441 | + $result = $this->runQuery($query); |
|
443 | 442 | |
444 | 443 | $vars = $result->getArray()['results']['bindings'][0]; |
445 | - if ( array_key_exists( 'matches', $vars ) ) { |
|
444 | + if (array_key_exists('matches', $vars)) { |
|
446 | 445 | // true or false ⇒ regex okay, text matches or not |
447 | 446 | return $vars['matches']['value'] === 'true'; |
448 | 447 | } else { |
449 | 448 | // empty result: regex broken |
450 | 449 | throw new ConstraintParameterException( |
451 | - wfMessage( 'wbqc-violation-message-parameter-regex' ) |
|
452 | - ->rawParams( ConstraintParameterRenderer::formatByRole( Role::CONSTRAINT_PARAMETER_VALUE, |
|
453 | - '<code><nowiki>' . htmlspecialchars( $regex ) . '</nowiki></code>' ) ) |
|
450 | + wfMessage('wbqc-violation-message-parameter-regex') |
|
451 | + ->rawParams(ConstraintParameterRenderer::formatByRole(Role::CONSTRAINT_PARAMETER_VALUE, |
|
452 | + '<code><nowiki>'.htmlspecialchars($regex).'</nowiki></code>')) |
|
454 | 453 | ->escaped() |
455 | 454 | ); |
456 | 455 | } |
@@ -463,14 +462,14 @@ discard block |
||
463 | 462 | * |
464 | 463 | * @return boolean |
465 | 464 | */ |
466 | - public function isTimeout( $responseContent ) { |
|
467 | - $timeoutRegex = implode( '|', array_map( |
|
468 | - function ( $fqn ) { |
|
469 | - return preg_quote( $fqn, '/' ); |
|
465 | + public function isTimeout($responseContent) { |
|
466 | + $timeoutRegex = implode('|', array_map( |
|
467 | + function($fqn) { |
|
468 | + return preg_quote($fqn, '/'); |
|
470 | 469 | }, |
471 | - $this->config->get( 'WBQualityConstraintsSparqlTimeoutExceptionClasses' ) |
|
472 | - ) ); |
|
473 | - return (bool)preg_match( '/' . $timeoutRegex . '/', $responseContent ); |
|
470 | + $this->config->get('WBQualityConstraintsSparqlTimeoutExceptionClasses') |
|
471 | + )); |
|
472 | + return (bool) preg_match('/'.$timeoutRegex.'/', $responseContent); |
|
474 | 473 | } |
475 | 474 | |
476 | 475 | /** |
@@ -482,17 +481,17 @@ discard block |
||
482 | 481 | * @return integer|boolean the max-age (in seconds) |
483 | 482 | * or a plain boolean if no max-age can be determined |
484 | 483 | */ |
485 | - public function getCacheMaxAge( $responseHeaders ) { |
|
484 | + public function getCacheMaxAge($responseHeaders) { |
|
486 | 485 | if ( |
487 | - array_key_exists( 'x-cache-status', $responseHeaders ) && |
|
488 | - preg_match( '/^hit(?:-.*)?$/', $responseHeaders['x-cache-status'][0] ) |
|
486 | + array_key_exists('x-cache-status', $responseHeaders) && |
|
487 | + preg_match('/^hit(?:-.*)?$/', $responseHeaders['x-cache-status'][0]) |
|
489 | 488 | ) { |
490 | 489 | $maxage = []; |
491 | 490 | if ( |
492 | - array_key_exists( 'cache-control', $responseHeaders ) && |
|
493 | - preg_match( '/\bmax-age=(\d+)\b/', $responseHeaders['cache-control'][0], $maxage ) |
|
491 | + array_key_exists('cache-control', $responseHeaders) && |
|
492 | + preg_match('/\bmax-age=(\d+)\b/', $responseHeaders['cache-control'][0], $maxage) |
|
494 | 493 | ) { |
495 | - return intval( $maxage[1] ); |
|
494 | + return intval($maxage[1]); |
|
496 | 495 | } else { |
497 | 496 | return true; |
498 | 497 | } |
@@ -510,59 +509,58 @@ discard block |
||
510 | 509 | * |
511 | 510 | * @throws SparqlHelperException if the query times out or some other error occurs |
512 | 511 | */ |
513 | - public function runQuery( $query ) { |
|
512 | + public function runQuery($query) { |
|
514 | 513 | |
515 | - $endpoint = $this->config->get( 'WBQualityConstraintsSparqlEndpoint' ); |
|
516 | - $maxQueryTimeMillis = $this->config->get( 'WBQualityConstraintsSparqlMaxMillis' ); |
|
517 | - $url = $endpoint . '?' . http_build_query( |
|
514 | + $endpoint = $this->config->get('WBQualityConstraintsSparqlEndpoint'); |
|
515 | + $maxQueryTimeMillis = $this->config->get('WBQualityConstraintsSparqlMaxMillis'); |
|
516 | + $url = $endpoint.'?'.http_build_query( |
|
518 | 517 | [ |
519 | - 'query' => "#wbqc\n" . $this->prefixes . $query, |
|
518 | + 'query' => "#wbqc\n".$this->prefixes.$query, |
|
520 | 519 | 'format' => 'json', |
521 | 520 | 'maxQueryTimeMillis' => $maxQueryTimeMillis, |
522 | 521 | ], |
523 | - null, ini_get( 'arg_separator.output' ), |
|
522 | + null, ini_get('arg_separator.output'), |
|
524 | 523 | // encode spaces with %20, not + |
525 | 524 | PHP_QUERY_RFC3986 |
526 | 525 | ); |
527 | 526 | |
528 | 527 | $options = [ |
529 | 528 | 'method' => 'GET', |
530 | - 'timeout' => (int)round( ( $maxQueryTimeMillis + 1000 ) / 1000 ), |
|
529 | + 'timeout' => (int) round(($maxQueryTimeMillis + 1000) / 1000), |
|
531 | 530 | 'connectTimeout' => 'default', |
532 | 531 | ]; |
533 | - $request = MWHttpRequest::factory( $url, $options ); |
|
534 | - $startTime = microtime( true ); |
|
532 | + $request = MWHttpRequest::factory($url, $options); |
|
533 | + $startTime = microtime(true); |
|
535 | 534 | $status = $request->execute(); |
536 | - $endTime = microtime( true ); |
|
535 | + $endTime = microtime(true); |
|
537 | 536 | $this->dataFactory->timing( |
538 | 537 | 'wikibase.quality.constraints.sparql.timing', |
539 | - ( $endTime - $startTime ) * 1000 |
|
538 | + ($endTime - $startTime) * 1000 |
|
540 | 539 | ); |
541 | 540 | |
542 | - $maxAge = $this->getCacheMaxAge( $request->getResponseHeaders() ); |
|
543 | - if ( $maxAge ) { |
|
544 | - $this->dataFactory->increment( 'wikibase.quality.constraints.sparql.cached' ); |
|
541 | + $maxAge = $this->getCacheMaxAge($request->getResponseHeaders()); |
|
542 | + if ($maxAge) { |
|
543 | + $this->dataFactory->increment('wikibase.quality.constraints.sparql.cached'); |
|
545 | 544 | } |
546 | 545 | |
547 | - if ( $status->isOK() ) { |
|
546 | + if ($status->isOK()) { |
|
548 | 547 | $json = $request->getContent(); |
549 | - $arr = json_decode( $json, true ); |
|
548 | + $arr = json_decode($json, true); |
|
550 | 549 | return new CachedQueryResults( |
551 | 550 | $arr, |
552 | 551 | Metadata::ofCachingMetadata( |
553 | 552 | $maxAge ? |
554 | - CachingMetadata::ofMaximumAgeInSeconds( $maxAge ) : |
|
555 | - CachingMetadata::fresh() |
|
553 | + CachingMetadata::ofMaximumAgeInSeconds($maxAge) : CachingMetadata::fresh() |
|
556 | 554 | ) |
557 | 555 | ); |
558 | 556 | } else { |
559 | - $this->dataFactory->increment( 'wikibase.quality.constraints.sparql.error' ); |
|
557 | + $this->dataFactory->increment('wikibase.quality.constraints.sparql.error'); |
|
560 | 558 | |
561 | 559 | $this->dataFactory->increment( |
562 | 560 | "wikibase.quality.constraints.sparql.error.http.{$request->getStatus()}" |
563 | 561 | ); |
564 | 562 | |
565 | - if ( $this->isTimeout( $request->getContent() ) ) { |
|
563 | + if ($this->isTimeout($request->getContent())) { |
|
566 | 564 | $this->dataFactory->increment( |
567 | 565 | 'wikibase.quality.constraints.sparql.error.timeout' |
568 | 566 | ); |
@@ -60,22 +60,22 @@ discard block |
||
60 | 60 | * @return array first element is the namespace number (default namespace for TitleParser), |
61 | 61 | * second element is a string to prepend to the title before giving it to the TitleParser |
62 | 62 | */ |
63 | - private function getCommonsNamespace( $namespace ) { |
|
63 | + private function getCommonsNamespace($namespace) { |
|
64 | 64 | // for namespace numbers see mediawiki-config repo, wmf-config/InitialiseSettings.php, |
65 | 65 | // 'wgExtraNamespaces' key, 'commonswiki' subkey |
66 | - switch ( $namespace ) { |
|
66 | + switch ($namespace) { |
|
67 | 67 | case '': |
68 | - return [ NS_MAIN, '' ]; |
|
68 | + return [NS_MAIN, '']; |
|
69 | 69 | case 'Creator': |
70 | - return [ 100, '' ]; |
|
70 | + return [100, '']; |
|
71 | 71 | case 'TimedText': |
72 | - return [ 102, '' ]; |
|
72 | + return [102, '']; |
|
73 | 73 | case 'Sequence': |
74 | - return [ 104, '' ]; |
|
74 | + return [104, '']; |
|
75 | 75 | case 'Institution': |
76 | - return [ 106, '' ]; |
|
76 | + return [106, '']; |
|
77 | 77 | default: |
78 | - return [ NS_MAIN, $namespace . ':' ]; |
|
78 | + return [NS_MAIN, $namespace.':']; |
|
79 | 79 | } |
80 | 80 | } |
81 | 81 | |
@@ -87,17 +87,17 @@ discard block |
||
87 | 87 | * |
88 | 88 | * @return CheckResult |
89 | 89 | */ |
90 | - public function checkConstraint( Context $context, Constraint $constraint ) { |
|
90 | + public function checkConstraint(Context $context, Constraint $constraint) { |
|
91 | 91 | $parameters = []; |
92 | 92 | $constraintParameters = $constraint->getConstraintParameters(); |
93 | - $namespace = $this->constraintParameterParser->parseNamespaceParameter( $constraintParameters, $constraint->getConstraintTypeItemId() ); |
|
94 | - $parameters['namespace'] = [ $namespace ]; |
|
93 | + $namespace = $this->constraintParameterParser->parseNamespaceParameter($constraintParameters, $constraint->getConstraintTypeItemId()); |
|
94 | + $parameters['namespace'] = [$namespace]; |
|
95 | 95 | |
96 | 96 | $snak = $context->getSnak(); |
97 | 97 | |
98 | - if ( !$snak instanceof PropertyValueSnak ) { |
|
98 | + if (!$snak instanceof PropertyValueSnak) { |
|
99 | 99 | // nothing to check |
100 | - return new CheckResult( $context, $constraint, $parameters, CheckResult::STATUS_COMPLIANCE, '' ); |
|
100 | + return new CheckResult($context, $constraint, $parameters, CheckResult::STATUS_COMPLIANCE, ''); |
|
101 | 101 | } |
102 | 102 | |
103 | 103 | $dataValue = $snak->getDataValue(); |
@@ -107,49 +107,49 @@ discard block |
||
107 | 107 | * type of $dataValue for properties with 'Commons link' constraint has to be 'string' |
108 | 108 | * parameter $namespace can be null, works for commons galleries |
109 | 109 | */ |
110 | - if ( $dataValue->getType() !== 'string' ) { |
|
111 | - $message = wfMessage( "wbqc-violation-message-value-needed-of-type" ) |
|
110 | + if ($dataValue->getType() !== 'string') { |
|
111 | + $message = wfMessage("wbqc-violation-message-value-needed-of-type") |
|
112 | 112 | ->rawParams( |
113 | - $this->constraintParameterRenderer->formatItemId( $constraint->getConstraintTypeItemId(), Role::CONSTRAINT_TYPE_ITEM ), |
|
114 | - wfMessage( 'datatypes-type-string' )->escaped() |
|
113 | + $this->constraintParameterRenderer->formatItemId($constraint->getConstraintTypeItemId(), Role::CONSTRAINT_TYPE_ITEM), |
|
114 | + wfMessage('datatypes-type-string')->escaped() |
|
115 | 115 | ) |
116 | 116 | ->escaped(); |
117 | - return new CheckResult( $context, $constraint, $parameters, CheckResult::STATUS_VIOLATION, $message ); |
|
117 | + return new CheckResult($context, $constraint, $parameters, CheckResult::STATUS_VIOLATION, $message); |
|
118 | 118 | } |
119 | 119 | |
120 | 120 | $commonsLink = $dataValue->getValue(); |
121 | 121 | |
122 | 122 | try { |
123 | - if ( !$this->commonsLinkIsWellFormed( $commonsLink ) ) { |
|
124 | - throw new MalformedTitleException( 'wbqc-violation-message-commons-link-not-well-formed', $commonsLink ); // caught below |
|
123 | + if (!$this->commonsLinkIsWellFormed($commonsLink)) { |
|
124 | + throw new MalformedTitleException('wbqc-violation-message-commons-link-not-well-formed', $commonsLink); // caught below |
|
125 | 125 | } |
126 | - list ( $defaultNamespace, $prefix ) = $this->getCommonsNamespace( $namespace ); |
|
127 | - $title = $this->titleParser->parseTitle( $prefix . $commonsLink, $defaultNamespace ); |
|
128 | - if ( $this->pageExists( $title ) ) { |
|
126 | + list ($defaultNamespace, $prefix) = $this->getCommonsNamespace($namespace); |
|
127 | + $title = $this->titleParser->parseTitle($prefix.$commonsLink, $defaultNamespace); |
|
128 | + if ($this->pageExists($title)) { |
|
129 | 129 | $message = ''; |
130 | 130 | $status = CheckResult::STATUS_COMPLIANCE; |
131 | 131 | } else { |
132 | - if ( $this->valueIncludesNamespace( $commonsLink, $namespace ) ) { |
|
133 | - throw new MalformedTitleException( 'wbqc-violation-message-commons-link-not-well-formed', $commonsLink ); // caught below |
|
132 | + if ($this->valueIncludesNamespace($commonsLink, $namespace)) { |
|
133 | + throw new MalformedTitleException('wbqc-violation-message-commons-link-not-well-formed', $commonsLink); // caught below |
|
134 | 134 | } else { |
135 | - $message = wfMessage( "wbqc-violation-message-commons-link-no-existent" )->escaped(); |
|
135 | + $message = wfMessage("wbqc-violation-message-commons-link-no-existent")->escaped(); |
|
136 | 136 | $status = CheckResult::STATUS_VIOLATION; |
137 | 137 | } |
138 | 138 | } |
139 | - } catch ( MalformedTitleException $e ) { |
|
140 | - $message = wfMessage( "wbqc-violation-message-commons-link-not-well-formed" )->escaped(); |
|
139 | + } catch (MalformedTitleException $e) { |
|
140 | + $message = wfMessage("wbqc-violation-message-commons-link-not-well-formed")->escaped(); |
|
141 | 141 | $status = CheckResult::STATUS_VIOLATION; |
142 | 142 | } |
143 | 143 | |
144 | - return new CheckResult( $context, $constraint, $parameters, $status, $message ); |
|
144 | + return new CheckResult($context, $constraint, $parameters, $status, $message); |
|
145 | 145 | } |
146 | 146 | |
147 | - public function checkConstraintParameters( Constraint $constraint ) { |
|
147 | + public function checkConstraintParameters(Constraint $constraint) { |
|
148 | 148 | $constraintParameters = $constraint->getConstraintParameters(); |
149 | 149 | $exceptions = []; |
150 | 150 | try { |
151 | - $this->constraintParameterParser->parseNamespaceParameter( $constraintParameters, $constraint->getConstraintTypeItemId() ); |
|
152 | - } catch ( ConstraintParameterException $e ) { |
|
151 | + $this->constraintParameterParser->parseNamespaceParameter($constraintParameters, $constraint->getConstraintTypeItemId()); |
|
152 | + } catch (ConstraintParameterException $e) { |
|
153 | 153 | $exceptions[] = $e; |
154 | 154 | } |
155 | 155 | return $exceptions; |
@@ -160,19 +160,19 @@ discard block |
||
160 | 160 | * |
161 | 161 | * @return bool |
162 | 162 | */ |
163 | - private function pageExists( TitleValue $title ) { |
|
163 | + private function pageExists(TitleValue $title) { |
|
164 | 164 | $commonsWikiId = 'commonswiki'; |
165 | - if ( defined( 'MW_PHPUNIT_TEST' ) ) { |
|
165 | + if (defined('MW_PHPUNIT_TEST')) { |
|
166 | 166 | $commonsWikiId = false; |
167 | 167 | } |
168 | 168 | |
169 | - $dbLoadBalancer = wfGetLB( $commonsWikiId ); |
|
169 | + $dbLoadBalancer = wfGetLB($commonsWikiId); |
|
170 | 170 | $dbConnection = $dbLoadBalancer->getConnection( |
171 | 171 | DB_REPLICA, false, $commonsWikiId ); |
172 | - $row = $dbConnection->selectRow( 'page', '*', [ |
|
172 | + $row = $dbConnection->selectRow('page', '*', [ |
|
173 | 173 | 'page_title' => $title->getDBkey(), |
174 | 174 | 'page_namespace' => $title->getNamespace() |
175 | - ] ); |
|
175 | + ]); |
|
176 | 176 | |
177 | 177 | return $row !== false; |
178 | 178 | } |
@@ -182,9 +182,9 @@ discard block |
||
182 | 182 | * |
183 | 183 | * @return bool |
184 | 184 | */ |
185 | - private function commonsLinkIsWellFormed( $commonsLink ) { |
|
186 | - $toReplace = [ "_", "%20" ]; |
|
187 | - $compareString = trim( str_replace( $toReplace, '', $commonsLink ) ); |
|
185 | + private function commonsLinkIsWellFormed($commonsLink) { |
|
186 | + $toReplace = ["_", "%20"]; |
|
187 | + $compareString = trim(str_replace($toReplace, '', $commonsLink)); |
|
188 | 188 | return $commonsLink === $compareString; |
189 | 189 | } |
190 | 190 | |
@@ -197,9 +197,9 @@ discard block |
||
197 | 197 | * |
198 | 198 | * @return bool |
199 | 199 | */ |
200 | - private function valueIncludesNamespace( $value, $namespace ) { |
|
200 | + private function valueIncludesNamespace($value, $namespace) { |
|
201 | 201 | return $namespace !== '' && |
202 | - strncasecmp( $value, $namespace . ':', strlen( $namespace ) + 1 ) === 0; |
|
202 | + strncasecmp($value, $namespace.':', strlen($namespace) + 1) === 0; |
|
203 | 203 | } |
204 | 204 | |
205 | 205 | } |
@@ -73,44 +73,43 @@ discard block |
||
73 | 73 | ) { |
74 | 74 | $response = []; |
75 | 75 | $metadatas = []; |
76 | - foreach ( $entityIds as $entityId ) { |
|
76 | + foreach ($entityIds as $entityId) { |
|
77 | 77 | $results = $this->delegatingConstraintChecker->checkAgainstConstraintsOnEntityId( |
78 | 78 | $entityId, |
79 | 79 | $constraintIds, |
80 | - [ $this, 'defaultResults' ] |
|
80 | + [$this, 'defaultResults'] |
|
81 | 81 | ); |
82 | - foreach ( $results as $result ) { |
|
82 | + foreach ($results as $result) { |
|
83 | 83 | $metadatas[] = $result->getMetadata(); |
84 | - $resultArray = $this->checkResultToArray( $result ); |
|
85 | - $result->getContext()->storeCheckResultInArray( $resultArray, $response ); |
|
84 | + $resultArray = $this->checkResultToArray($result); |
|
85 | + $result->getContext()->storeCheckResultInArray($resultArray, $response); |
|
86 | 86 | } |
87 | 87 | } |
88 | - foreach ( $claimIds as $claimId ) { |
|
88 | + foreach ($claimIds as $claimId) { |
|
89 | 89 | $results = $this->delegatingConstraintChecker->checkAgainstConstraintsOnClaimId( |
90 | 90 | $claimId, |
91 | 91 | $constraintIds, |
92 | - [ $this, 'defaultResults' ] |
|
92 | + [$this, 'defaultResults'] |
|
93 | 93 | ); |
94 | - foreach ( $results as $result ) { |
|
94 | + foreach ($results as $result) { |
|
95 | 95 | $metadatas[] = $result->getMetadata(); |
96 | - $resultArray = $this->checkResultToArray( $result ); |
|
97 | - $result->getContext()->storeCheckResultInArray( $resultArray, $response ); |
|
96 | + $resultArray = $this->checkResultToArray($result); |
|
97 | + $result->getContext()->storeCheckResultInArray($resultArray, $response); |
|
98 | 98 | } |
99 | 99 | } |
100 | 100 | return new CachedCheckConstraintsResponse( |
101 | 101 | $response, |
102 | - Metadata::merge( $metadatas ) |
|
102 | + Metadata::merge($metadatas) |
|
103 | 103 | ); |
104 | 104 | } |
105 | 105 | |
106 | - public function defaultResults( Context $context ) { |
|
106 | + public function defaultResults(Context $context) { |
|
107 | 107 | return $context->getType() === Context::TYPE_STATEMENT ? |
108 | - [ new NullResult( $context ) ] : |
|
109 | - []; |
|
108 | + [new NullResult($context)] : []; |
|
110 | 109 | } |
111 | 110 | |
112 | - public function checkResultToArray( CheckResult $checkResult ) { |
|
113 | - if ( $checkResult instanceof NullResult ) { |
|
111 | + public function checkResultToArray(CheckResult $checkResult) { |
|
112 | + if ($checkResult instanceof NullResult) { |
|
114 | 113 | return null; |
115 | 114 | } |
116 | 115 | |
@@ -118,10 +117,10 @@ discard block |
||
118 | 117 | $typeItemId = $checkResult->getConstraint()->getConstraintTypeItemId(); |
119 | 118 | $constraintPropertyId = $checkResult->getContext()->getSnak()->getPropertyId(); |
120 | 119 | |
121 | - $title = $this->entityTitleLookup->getTitleForId( $constraintPropertyId ); |
|
122 | - $typeLabel = $this->entityIdLabelFormatter->formatEntityId( new ItemId( $typeItemId ) ); |
|
120 | + $title = $this->entityTitleLookup->getTitleForId($constraintPropertyId); |
|
121 | + $typeLabel = $this->entityIdLabelFormatter->formatEntityId(new ItemId($typeItemId)); |
|
123 | 122 | // TODO link to the statement when possible (T169224) |
124 | - $link = $title->getFullURL() . '#' . $this->config->get( 'WBQualityConstraintsPropertyConstraintId' ); |
|
123 | + $link = $title->getFullURL().'#'.$this->config->get('WBQualityConstraintsPropertyConstraintId'); |
|
125 | 124 | |
126 | 125 | $constraint = [ |
127 | 126 | 'id' => $constraintId, |
@@ -129,11 +128,11 @@ discard block |
||
129 | 128 | 'typeLabel' => $typeLabel, |
130 | 129 | 'link' => $link, |
131 | 130 | ]; |
132 | - if ( $this->config->get( 'WBQualityConstraintsIncludeDetailInApi' ) ) { |
|
131 | + if ($this->config->get('WBQualityConstraintsIncludeDetailInApi')) { |
|
133 | 132 | $parameters = $checkResult->getParameters(); |
134 | 133 | $constraint += [ |
135 | 134 | 'detail' => $parameters, |
136 | - 'detailHTML' => $this->constraintParameterRenderer->formatParameters( $parameters ), |
|
135 | + 'detailHTML' => $this->constraintParameterRenderer->formatParameters($parameters), |
|
137 | 136 | ]; |
138 | 137 | } |
139 | 138 | |
@@ -143,14 +142,14 @@ discard block |
||
143 | 142 | 'constraint' => $constraint |
144 | 143 | ]; |
145 | 144 | $message = $checkResult->getMessage(); |
146 | - if ( $message ) { |
|
145 | + if ($message) { |
|
147 | 146 | $result['message-html'] = $message; |
148 | 147 | } |
149 | - if ( $checkResult->getContext()->getType() === Context::TYPE_STATEMENT ) { |
|
148 | + if ($checkResult->getContext()->getType() === Context::TYPE_STATEMENT) { |
|
150 | 149 | $result['claim'] = $checkResult->getContext()->getSnakStatement()->getGuid(); |
151 | 150 | } |
152 | 151 | $cachingMetadataArray = $checkResult->getMetadata()->getCachingMetadata()->toArray(); |
153 | - if ( $cachingMetadataArray !== null ) { |
|
152 | + if ($cachingMetadataArray !== null) { |
|
154 | 153 | $result['cached'] = $cachingMetadataArray; |
155 | 154 | } |
156 | 155 |