mirror of https://github.com/MISP/MISP
chg: [internal] Optimise fetching events by tags
parent
9855fc5ced
commit
ab432a02d6
|
@ -1702,7 +1702,7 @@ class Attribute extends AppModel
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
foreach ($args as $arg) {
|
foreach ($args as $arg) {
|
||||||
if (substr($arg, 0, 1) == '!') {
|
if ($arg[0] === '!') {
|
||||||
$result[1][] = substr($arg, 1);
|
$result[1][] = substr($arg, 1);
|
||||||
} else {
|
} else {
|
||||||
$result[0][] = $arg;
|
$result[0][] = $arg;
|
||||||
|
|
|
@ -5807,7 +5807,7 @@ class Event extends AppModel
|
||||||
} else {
|
} else {
|
||||||
$filters = array();
|
$filters = array();
|
||||||
$args = $this->Attribute->dissectArgs($tagRules);
|
$args = $this->Attribute->dissectArgs($tagRules);
|
||||||
$tagArray = $this->EventTag->Tag->fetchEventTagIds($args[0], $args[1]);
|
$tagArray = $this->EventTag->fetchEventTagIds($args[0], $args[1]);
|
||||||
if (!empty($tagArray[0])) {
|
if (!empty($tagArray[0])) {
|
||||||
$filters[] = ['OR' => ['Event.id' => $tagArray[0]]];
|
$filters[] = ['OR' => ['Event.id' => $tagArray[0]]];
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -141,6 +141,50 @@ class EventTag extends AppModel
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find all of the event Ids that belong to the accepted tags and the rejected tags
|
||||||
|
* @param array $accept
|
||||||
|
* @param array $reject
|
||||||
|
* @return array[]
|
||||||
|
*/
|
||||||
|
public function fetchEventTagIds(array $accept = array(), array $reject = array())
|
||||||
|
{
|
||||||
|
$acceptIds = array();
|
||||||
|
$rejectIds = array();
|
||||||
|
if (!empty($accept)) {
|
||||||
|
$acceptIds = $this->findEventIdsByTagNames($accept);
|
||||||
|
if (empty($acceptIds)) {
|
||||||
|
$acceptIds = [-1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!empty($reject)) {
|
||||||
|
$rejectIds = $this->findEventIdsByTagNames($reject);
|
||||||
|
}
|
||||||
|
return array($acceptIds, $rejectIds);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $tagIdsOrNames
|
||||||
|
* @return array|int|null
|
||||||
|
*/
|
||||||
|
private function findEventIdsByTagNames(array $tagIdsOrNames)
|
||||||
|
{
|
||||||
|
$conditions = [];
|
||||||
|
foreach ($tagIdsOrNames as $tagIdOrName) {
|
||||||
|
if (is_numeric($tagIdOrName)) {
|
||||||
|
$conditions[] = array('Tag.id' => $tagIdOrName);
|
||||||
|
} else {
|
||||||
|
$conditions[] = array('LOWER(Tag.name)' => mb_strtolower($tagIdOrName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $this->find('column', array(
|
||||||
|
'recursive' => -1,
|
||||||
|
'contain' => 'Tag',
|
||||||
|
'conditions' => ['OR' => $conditions],
|
||||||
|
'fields' => ['EventTag.event_id'],
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
public function getSortedTagList($context = false)
|
public function getSortedTagList($context = false)
|
||||||
{
|
{
|
||||||
$conditions = array();
|
$conditions = array();
|
||||||
|
|
|
@ -237,21 +237,14 @@ class Tag extends AppModel
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
// find all of the event Ids that belong to the accepted tags and the rejected tags
|
/**
|
||||||
public function fetchEventTagIds($accept = array(), $reject = array())
|
* @param array $accept
|
||||||
|
* @param array $reject
|
||||||
|
* @deprecated Use EventTag::fetchEventTagIds instead
|
||||||
|
*/
|
||||||
|
public function fetchEventTagIds($accept, $reject)
|
||||||
{
|
{
|
||||||
$acceptIds = array();
|
$this->EventTag->fetchEventTagIds($accept, $reject);
|
||||||
$rejectIds = array();
|
|
||||||
if (!empty($accept)) {
|
|
||||||
$acceptIds = $this->findEventIdsByTagNames($accept);
|
|
||||||
if (empty($acceptIds)) {
|
|
||||||
$acceptIds[] = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!empty($reject)) {
|
|
||||||
$rejectIds = $this->findEventIdsByTagNames($reject);
|
|
||||||
}
|
|
||||||
return array($acceptIds, $rejectIds);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// find all of the tag Ids that belong to the accepted tags and the rejected tags
|
// find all of the tag Ids that belong to the accepted tags and the rejected tags
|
||||||
|
@ -314,29 +307,7 @@ class Tag extends AppModel
|
||||||
return array_values($tag_ids);
|
return array_values($tag_ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function findEventIdsByTagNames($array)
|
|
||||||
{
|
|
||||||
$ids = array();
|
|
||||||
foreach ($array as $a) {
|
|
||||||
if (is_numeric($a)) {
|
|
||||||
$conditions['OR'][] = array('id' => $a);
|
|
||||||
} else {
|
|
||||||
$conditions['OR'][] = array('LOWER(name) like' => strtolower($a));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$params = array(
|
|
||||||
'recursive' => 1,
|
|
||||||
'contain' => 'EventTag',
|
|
||||||
'conditions' => $conditions
|
|
||||||
);
|
|
||||||
$result = $this->find('all', $params);
|
|
||||||
foreach ($result as $tag) {
|
|
||||||
foreach ($tag['EventTag'] as $eventTag) {
|
|
||||||
$ids[] = $eventTag['event_id'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $ids;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $tag
|
* @param array $tag
|
||||||
|
|
Loading…
Reference in New Issue