jenkins-bot has submitted this change and it was merged. ( 
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, 34 insertions(+), 8 deletions(-)

Approvals:
  WMDE-leszek: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/src/ConstraintCheck/Helper/SparqlHelper.php 
b/src/ConstraintCheck/Helper/SparqlHelper.php
index 175318d..4fbd34b 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,20 @@
                                } else {
                                        $key = 
'wikibase.quality.constraints.regex.cache.refresh.miss';
                                        $this->dataFactory->increment( $key );
+                                       try {
+                                               $matches = 
$this->matchesRegularExpressionWithSparql( $text, $regex );
+                                       } catch ( ConstraintParameterException 
$e ) {
+                                               $matches = [
+                                                       'type' => 
ConstraintParameterException::class,
+                                                       'message' => 
$e->getMessage(),
+                                               ];
+                                       } catch ( SparqlHelperException $e ) {
+                                               // don’t cache this
+                                               return $cacheMap->toArray();
+                                       }
                                        $cacheMap->set(
                                                $textHash,
-                                               
$this->matchesRegularExpressionWithSparql( $text, $regex ),
+                                               $matches,
                                                3 / 8
                                        );
                                }
@@ -412,7 +425,20 @@
                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_array( $matches ) &&
+                               $matches['type'] == 
ConstraintParameterException::class ) {
+                               throw new ConstraintParameterException( 
$matches['message'] );
+                       } 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: merged
Gerrit-Change-Id: I1671255279f82acdcdeb42050f0e2332c635718f
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <[email protected]>
Gerrit-Reviewer: Ladsgroup <[email protected]>
Gerrit-Reviewer: Lucas Werkmeister (WMDE) <[email protected]>
Gerrit-Reviewer: WMDE-leszek <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to