Lucas Werkmeister (WMDE) has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/401724 )
Change subject: Catch ConstraintParameterException in SparqlHelper
......................................................................
Catch ConstraintParameterException in SparqlHelper
When an invalid regex is detected, matchesRegularExpressionWithSparql
throws a ConstraintParameterException. matchesRegularExpression needs to
catch this exception in the caching callback, and store the relevant
part (the message) so we can reconstruct the exception from the cached
value.
Bug: T183992
Change-Id: I1671255279f82acdcdeb42050f0e2332c635718f
---
M src/ConstraintCheck/Helper/SparqlHelper.php
1 file changed, 30 insertions(+), 8 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQualityConstraints
refs/changes/24/401724/1
diff --git a/src/ConstraintCheck/Helper/SparqlHelper.php
b/src/ConstraintCheck/Helper/SparqlHelper.php
index 175318d..85c5acd 100644
--- a/src/ConstraintCheck/Helper/SparqlHelper.php
+++ b/src/ConstraintCheck/Helper/SparqlHelper.php
@@ -9,6 +9,7 @@
use InvalidArgumentException;
use MapCacheLRU;
use MediaWiki\MediaWikiServices;
+use MWException;
use MWHttpRequest;
use WANObjectCache;
use Wikibase\DataModel\Entity\EntityId;
@@ -361,15 +362,16 @@
// caching wrapper around matchesRegularExpressionWithSparql
$textHash = hash( 'sha256', $text );
+ $cacheKey = $this->cache->makeKey(
+ 'WikibaseQualityConstraints', // extension
+ 'regex', // action
+ 'WDQS-Java', // regex flavor
+ hash( 'sha256', $regex )
+ );
$cacheMapSize = $this->config->get(
'WBQualityConstraintsFormatCacheMapSize' );
$cacheMapArray = $this->cache->getWithSetCallback(
- $this->cache->makeKey(
- 'WikibaseQualityConstraints', // extension
- 'regex', // action
- 'WDQS-Java', // regex flavor
- hash( 'sha256', $regex )
- ),
+ $cacheKey,
WANObjectCache::TTL_DAY,
function( $cacheMapArray ) use ( $text, $regex,
$textHash, $cacheMapSize ) {
// Initialize the cache map if not set
@@ -389,9 +391,17 @@
} else {
$key =
'wikibase.quality.constraints.regex.cache.refresh.miss';
$this->dataFactory->increment( $key );
+ try {
+ $matches =
$this->matchesRegularExpressionWithSparql( $text, $regex );
+ } catch ( ConstraintParameterException
$e ) {
+ $matches = $e->getMessage();
+ } catch ( SparqlHelperException $e ) {
+ // don’t cache this
+ return $cacheMap->toArray();
+ }
$cacheMap->set(
$textHash,
-
$this->matchesRegularExpressionWithSparql( $text, $regex ),
+ $matches,
3 / 8
);
}
@@ -412,7 +422,19 @@
if ( isset( $cacheMapArray[$textHash] ) ) {
$key = 'wikibase.quality.constraints.regex.cache.hit';
$this->dataFactory->increment( $key );
- return $cacheMapArray[$textHash];
+ $matches = $cacheMapArray[$textHash];
+ if ( is_bool( $matches ) ) {
+ return $matches;
+ } elseif ( is_string( $matches ) ) {
+ throw new ConstraintParameterException(
$matches );
+ } else {
+ throw new MWException(
+ 'Value of unknown type in object cache
(' .
+ 'cache key: ' . $cacheKey . ', ' .
+ 'cache map key: ' . $textHash . ', ' .
+ 'value type: ' . gettype( $matches ) .
')'
+ );
+ }
} else {
$key = 'wikibase.quality.constraints.regex.cache.miss';
$this->dataFactory->increment( $key );
--
To view, visit https://gerrit.wikimedia.org/r/401724
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I1671255279f82acdcdeb42050f0e2332c635718f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits