1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\CustomDimensions\Dimension;
use Piwik\Common;
use Piwik\Tracker\Request;
use Piwik\Tracker\Action;
use Piwik\Piwik;
use Exception;
use Piwik\Validators\Regex;
class Extraction
{
private $dimension = '';
private $pattern = '';
private $caseSensitive = true;
public function __construct($dimension, $pattern)
{
$this->dimension = $dimension;
$this->pattern = $pattern;
}
public function toArray()
{
return array('dimension' => $this->dimension, 'pattern' => $this->pattern);
}
public function check()
{
$dimensions = $this->getSupportedDimensions();
if (!array_key_exists($this->dimension, $dimensions)) {
$dimensions = implode(', ', array_keys($dimensions));
throw new Exception("Invald dimension '$this->dimension' used in an extraction. Available dimensions are: " . $dimensions);
}
//Count the number of non-capturing groups in order to omit them from being counted as capturing groups
$ncgCount = substr_count($this->pattern, '(?');
if (!empty($this->pattern) && $this->dimension !== 'urlparam') {
// make sure there is exactly one ( followed by one )
if (
1 !== substr_count($this->pattern, '(') - $ncgCount ||
1 !== substr_count($this->pattern, ')') - $ncgCount ||
1 !== substr_count($this->pattern, ')', strpos($this->pattern, '(')) - $ncgCount
) {
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()
{
return array(
'url' => Piwik::translate('Actions_ColumnPageURL'),
'urlparam' => Piwik::translate('CustomDimensions_PageUrlParam'),
'action_name' => Piwik::translate('Goals_PageTitle'),
);
}
public function setCaseSensitive($caseSensitive)
{
$this->caseSensitive = (bool) $caseSensitive;
}
public function extract(Request $request)
{
$value = $this->getValueForDimension($request);
$value = $this->extractValue($value);
return $value;
}
private function getValueForDimension(Request $request)
{
/** @var Action $action */
$action = $request->getMetadata('Actions', 'action');
if (in_array($this->dimension, array('url', 'urlparam'))) {
if (!empty($action)) {
$dimension = $action->getActionUrlRaw();
} else {
$dimension = $request->getParam('url');
}
} elseif ($this->dimension === 'action_name' && !empty($action)) {
$dimension = $action->getActionName();
} else {
$dimension = $request->getParam($this->dimension);
}
if (!empty($dimension)) {
$dimension = Common::unsanitizeInputValue($dimension);
}
return $dimension;
}
private function extractValue($value)
{
if (!isset($value) || '' === $value) {
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 https://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 . '=([^&]*)';
}
$regex = '/' . str_replace('/', '\/', $pattern) . '/';
if (!$this->caseSensitive) {
$regex .= 'i';
}
return $regex;
}
}
|