public function __construct($string, $idSites) { $string = Piwik_Common::unsanitizeInputValue($string); $string = trim($string); if( !Piwik_Archive::isSegmentationEnabled() && !empty($string)) { throw new Exception("The Super User has disabled the use of 'segments' for the anonymous user. Please log in to use Segmentation in the API."); } // As a preventive measure, we restrict the filter size to a safe limit $string = substr($string, 0, self::SEGMENT_TRUNCATE_LIMIT); $this->string = $string; $this->idSites = $idSites; $segment = new Piwik_SegmentExpression($string); $this->segment = $segment; // parse segments $expressions = $segment->parseSubExpressions(); // convert segments name to sql segment // check that user is allowed to view this segment // and apply a filter to the value to match if necessary (to map DB fields format) $cleanedExpressions = array(); foreach($expressions as $expression) { $operand = $expression[Piwik_SegmentExpression::INDEX_OPERAND]; $cleanedExpression = $this->getCleanedExpression($operand); $expression[Piwik_SegmentExpression::INDEX_OPERAND] = $cleanedExpression; $cleanedExpressions[] = $expression; } $segment->setSubExpressionsAfterCleanup($cleanedExpressions); }
/** * @dataProvider getBogusFilters * @group Core * @group SegmentExpression */ public function testBogusFiltersExpectExceptionThrown($bogus) { try { $segment = new Piwik_SegmentExpression($bogus); $segment->parseSubExpressions(); $segment->getSql(); } catch (Exception $e) { return; } $this->fail('Expected exception not raised'); }
public function test_bogusFilters_expectExceptionThrown() { $boguses = array('A=B', 'C!D', '', ' ', ',;,', ',', ',,', '===', '!='); foreach ($boguses as $bogus) { $segment = new Piwik_SegmentExpression($bogus); try { $segment->parseSubExpressions(); $processed = $segment->getSql(); $this->fail('expecting exception ' . $bogus); } catch (Exception $e) { $this->pass(); } } }