fix: [internal] Faster RegexpBehavior

pull/8416/head
Jakub Onderka 2022-05-29 17:02:59 +02:00
parent b5b26455f0
commit 21afe562ef
2 changed files with 11 additions and 8 deletions

View File

@ -9,7 +9,7 @@ class RegexpBehavior extends ModelBehavior
{
private $__allRegexp = null;
public $excluded_types = array('sigma', 'float');
const EXCLUDED_TYPES = array('sigma', 'float');
/**
* replace the current value according to the regexp rules, or block blocklisted regular expressions
@ -21,21 +21,24 @@ class RegexpBehavior extends ModelBehavior
*/
public function runRegexp(Model $Model, $type, $value)
{
if (in_array($type, $this->excluded_types)) {
if (in_array($type, self::EXCLUDED_TYPES, true)) {
return $value;
}
if ($this->__allRegexp === null) {
$regexp = new Regexp();
$this->__allRegexp = $regexp->find('all', array('order' => 'id ASC'));
$this->__allRegexp = array_column($regexp->find('all', [
'order' => 'id ASC',
'fields' => ['type', 'regexp', 'replacement'],
]), 'Regexp');
}
foreach ($this->__allRegexp as $regexp) {
if ($regexp['Regexp']['type'] === 'ALL' || $regexp['Regexp']['type'] === $type) {
if (!empty($regexp['Regexp']['replacement']) && !empty($regexp['Regexp']['regexp'])) {
$value = preg_replace($regexp['Regexp']['regexp'], $regexp['Regexp']['replacement'], $value);
if ($regexp['type'] === 'ALL' || $regexp['type'] === $type) {
if (!empty($regexp['replacement']) && !empty($regexp['regexp'])) {
$value = preg_replace($regexp['regexp'], $regexp['replacement'], $value);
}
if (empty($regexp['Regexp']['replacement']) && preg_match($regexp['Regexp']['regexp'], $value)) {
if (empty($regexp['replacement']) && preg_match($regexp['regexp'], $value)) {
return false;
}
}

View File

@ -5638,7 +5638,7 @@ class Event extends AppModel
} else {
$attribute_type = $attribute['type'];
if (empty($attribute['category'])) {
$attribute['category'] = $this->Attribute->typedefinitions[$attribute_type]['default_category'];
$attribute['category'] = $this->Attribute->typeDefinitions[$attribute_type]['default_category'];
}
}
if (!isset($attribute['to_ids'])) {