mirror of https://github.com/MISP/MISP
chg: [internal] Optimise Tag::findTagIdsByTagNames
parent
ab432a02d6
commit
9ae7d88f23
|
@ -2554,9 +2554,8 @@ class AppModel extends Model
|
|||
}
|
||||
|
||||
// generate a generic subquery - options needs to include conditions
|
||||
public function subQueryGenerator($model, $options, $lookupKey, $negation = false)
|
||||
protected function subQueryGenerator(AppModel $model, array $options, $lookupKey, $negation = false)
|
||||
{
|
||||
$db = $model->getDataSource();
|
||||
$defaults = array(
|
||||
'fields' => array('*'),
|
||||
'table' => $model->table,
|
||||
|
@ -2569,17 +2568,15 @@ class AppModel extends Model
|
|||
'recursive' => -1
|
||||
);
|
||||
$params = array();
|
||||
foreach (array_keys($defaults) as $key) {
|
||||
foreach ($defaults as $key => $defaultValue) {
|
||||
if (isset($options[$key])) {
|
||||
$params[$key] = $options[$key];
|
||||
} else {
|
||||
$params[$key] = $defaults[$key];
|
||||
$params[$key] = $defaultValue;
|
||||
}
|
||||
}
|
||||
$subQuery = $db->buildStatement(
|
||||
$params,
|
||||
$model
|
||||
);
|
||||
$db = $model->getDataSource();
|
||||
$subQuery = $db->buildStatement($params, $model);
|
||||
if ($negation) {
|
||||
$subQuery = $lookupKey . ' NOT IN (' . $subQuery . ') ';
|
||||
} else {
|
||||
|
|
|
@ -1229,6 +1229,7 @@ class Attribute extends AppModel
|
|||
if (empty($params['tags'])) {
|
||||
return $conditions;
|
||||
}
|
||||
/** @var Tag $tag */
|
||||
$tag = ClassRegistry::init('Tag');
|
||||
$params['tags'] = $this->dissectArgs($params['tags']);
|
||||
foreach (array(0, 1, 2) as $tag_operator) {
|
||||
|
@ -1683,13 +1684,13 @@ class Attribute extends AppModel
|
|||
// array 1 will have all of the non negated terms and array 2 all the negated terms
|
||||
public function dissectArgs($args)
|
||||
{
|
||||
$result = array(0 => array(), 1 => array(), 2 => array());
|
||||
if (empty($args)) {
|
||||
return array(0 => array(), 1 => array(), 2 => array());
|
||||
return $result;
|
||||
}
|
||||
if (!is_array($args)) {
|
||||
$args = explode('&&', $args);
|
||||
}
|
||||
$result = array(0 => array(), 1 => array(), 2 => array());
|
||||
if (isset($args['OR']) || isset($args['NOT']) || isset($args['AND'])) {
|
||||
if (!empty($args['OR'])) {
|
||||
$result[0] = $args['OR'];
|
||||
|
|
|
@ -277,38 +277,40 @@ class Tag extends AppModel
|
|||
return array($acceptIds, $rejectIds);
|
||||
}
|
||||
|
||||
// pass a list of tag names to receive a list of matched tag IDs
|
||||
/**
|
||||
* pass a list of tag names to receive a list of matched tag IDs
|
||||
* @param string|array $array
|
||||
* @return array|int|null
|
||||
*/
|
||||
public function findTagIdsByTagNames($array)
|
||||
{
|
||||
$ids = array();
|
||||
$tag_ids = array();
|
||||
if (!is_array($array)) {
|
||||
$array = array($array);
|
||||
}
|
||||
foreach ($array as $k => $tag) {
|
||||
$tagIds = [];
|
||||
$tagNames = [];
|
||||
foreach ($array as $tag) {
|
||||
if (is_numeric($tag)) {
|
||||
$tag_ids[] = $tag;
|
||||
unset($array[$k]);
|
||||
$tagIds[] = $tag;
|
||||
} else {
|
||||
$tagNames[] = $tag;
|
||||
}
|
||||
}
|
||||
$array = array_values($array);
|
||||
if (!empty($array)) {
|
||||
foreach ($array as $a) {
|
||||
$conditions['OR'][] = array('Tag.name like' => $a);
|
||||
if (!empty($tagNames)) {
|
||||
$conditions = [];
|
||||
foreach ($tagNames as $tagName) {
|
||||
$conditions[] = array('Tag.name LIKE' => $tagName);
|
||||
}
|
||||
$params = array(
|
||||
'recursive' => 1,
|
||||
'conditions' => $conditions,
|
||||
'fields' => array('Tag.id', 'Tag.id')
|
||||
);
|
||||
$result = $this->find('list', $params);
|
||||
$tag_ids = array_merge($result, $tag_ids);
|
||||
$result = $this->find('column', array(
|
||||
'recursive' => 1,
|
||||
'conditions' => ['OR' => $conditions],
|
||||
'fields' => array('Tag.id')
|
||||
));
|
||||
$tagIds = array_merge($result, $tagIds);
|
||||
}
|
||||
return array_values($tag_ids);
|
||||
return $tagIds;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param array $tag
|
||||
* @param array $user
|
||||
|
|
Loading…
Reference in New Issue