diff options
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/CustomDimensions/Dimension/Extraction.php | 27 | ||||
-rw-r--r-- | plugins/CustomDimensions/tests/Integration/Dimension/ExtractionTest.php | 14 |
2 files changed, 32 insertions, 9 deletions
diff --git a/plugins/CustomDimensions/Dimension/Extraction.php b/plugins/CustomDimensions/Dimension/Extraction.php index 040992fd1a..48b574a98f 100644 --- a/plugins/CustomDimensions/Dimension/Extraction.php +++ b/plugins/CustomDimensions/Dimension/Extraction.php @@ -16,6 +16,7 @@ use Piwik\Url; use Piwik\UrlHelper; use Piwik\Piwik; use Exception; +use Piwik\Validators\Regex; class Extraction { @@ -50,6 +51,10 @@ class Extraction throw new Exception("You need to group exactly one part of the regular expression inside round brackets, eg 'index_(.+).html'"); } } + + //validate regex pattern + $validator = new Regex(); + $validator->validate($this->formatPattern()); } public static function getSupportedDimensions() @@ -104,6 +109,20 @@ class Extraction return null; } + $regex = $this->formatPattern(); + + if (preg_match($regex, (string) $value, $matches)) { + // we could improve performance here I reckon by combining all patterns of all configs see eg http://nikic.github.io/2014/02/18/Fast-request-routing-using-regular-expressions.html + + if (array_key_exists(1, $matches)) { + return $matches[1]; + } + } + } + + // format pattern to matomo format + private function formatPattern () { + $pattern = $this->pattern; if ($this->dimension === 'urlparam') { $pattern = '\?.*' . $pattern . '=([^&]*)'; @@ -114,12 +133,6 @@ class Extraction $regex .= 'i'; } - if (preg_match($regex, (string) $value, $matches)) { - // we could improve performance here I reckon by combining all patterns of all configs see eg http://nikic.github.io/2014/02/18/Fast-request-routing-using-regular-expressions.html - - if (array_key_exists(1, $matches)) { - return $matches[1]; - } - } + return $regex; } }
\ No newline at end of file diff --git a/plugins/CustomDimensions/tests/Integration/Dimension/ExtractionTest.php b/plugins/CustomDimensions/tests/Integration/Dimension/ExtractionTest.php index eb9f73a75f..aea34880dd 100644 --- a/plugins/CustomDimensions/tests/Integration/Dimension/ExtractionTest.php +++ b/plugins/CustomDimensions/tests/Integration/Dimension/ExtractionTest.php @@ -8,6 +8,7 @@ namespace Piwik\Plugins\CustomDimensions\tests\Integration\Dimension; +use Piwik\Piwik; use Piwik\Plugins\CustomDimensions\Dimension\Extraction; use Piwik\Tests\Framework\Fixture; use Piwik\Tests\Framework\TestCase\IntegrationTestCase; @@ -185,6 +186,14 @@ class ExtractionTest extends IntegrationTestCase $this->buildExtraction('anyInvalid', '/ref(.+)')->check(); } + public function test_check_shouldFailWhenInvalidRegGiven() + { + $check = '/foo(*)/'; + $this->expectException(\Exception::class); + $this->expectExceptionMessage(Piwik::translate('General_ValidatorErrorNoValidRegex', array($check))); + $this->buildExtraction('url', $check)->check(); + } + /** * @dataProvider getInvalidPatterns */ @@ -199,8 +208,9 @@ class ExtractionTest extends IntegrationTestCase public function test_check_shouldNotFailWhenValidCombinationsAreGiven() { $this->expectNotToPerformAssertions(); - $this->buildExtraction('url', 'index_(+).html')->check(); - $this->buildExtraction('action_name', 'index_(+).html')->check(); + $this->buildExtraction('urlparam', 'index')->check(); // does not have to contain brackets + $this->buildExtraction('url', 'index_(.+).html')->check(); + $this->buildExtraction('action_name', 'index_(.+).html')->check(); $this->buildExtraction('url', '')->check(); // empty value is allowed $this->buildExtraction('urlparam', 'index')->check(); // does not have to contain brackets } |