Skip to content

Commit

Permalink
changed: content_by_tag exluded tags optimization
Browse files Browse the repository at this point in the history
Support for multiple tag names and the universal_categories tag name is
no longer supported
  • Loading branch information
jdalsem committed Oct 9, 2023
1 parent ecea11e commit 16c3e9d
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions views/default/widgets/content_by_tag/content.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
$tags_option = 'and';
}

$tag_names = elgg_extract('tag_names', $vars, ['tags', 'universal_categories']);

$options = [
'type' => 'object',
'subtypes' => $object_subtypes,
Expand All @@ -52,7 +50,7 @@
$options['metadata_name_value_pairs'] = [];
if ($tags_option === 'or') {
$options['metadata_name_value_pairs'][] = [
'name' => $tag_names,
'name' => 'tags',
'value' => $values,
'case_sensitive' => false,
];
Expand All @@ -66,9 +64,9 @@

$options['joins'][] = new JoinClause(QueryBuilder::TABLE_METADATA, $alias, $on);

$options['wheres'][] = function(QueryBuilder $qb) use ($alias, $value, $tag_names) {
$options['wheres'][] = function(QueryBuilder $qb) use ($alias, $value) {
$md = new MetadataWhereClause();
$md->names = $tag_names;
$md->names = 'tags';
$md->values = $value;
$md->case_sensitive = false;
return $md->prepare($qb, $alias);
Expand All @@ -80,14 +78,16 @@
// excluded tags
$excluded_values = is_array($widget->excluded_tags) ? $widget->excluded_tags : elgg_string_to_array((string) $widget->excluded_tags);
if ($excluded_values) {
$options['wheres'][] = function(QueryBuilder $qb, $main_alias) use ($tag_names, $excluded_values) {
$subquery = $qb->subquery('metadata', 'ex_tags');
$subquery->select('DISTINCT entity_guid')
->where($qb->compare('ex_tags.name', 'IN', $tag_names, ELGG_VALUE_STRING))
->andWhere($qb->compare('ex_tags.value', 'IN', $excluded_values, ELGG_VALUE_STRING));

return $qb->compare("{$main_alias}.guid", 'NOT IN', $subquery->getSQL());
};
foreach ($excluded_values as $value) {
$options['wheres'][] = function(QueryBuilder $qb, $main_alias) use ($value) {
$subquery = $qb->subquery('metadata');
$subquery->select('entity_guid')
->where($qb->compare('name', '=', 'tags', ELGG_VALUE_STRING))
->andWhere($qb->compare('value', '=', $value, ELGG_VALUE_STRING));

return $qb->compare("{$main_alias}.guid", 'NOT IN', $subquery->getSQL());
};
}
}

// owner_guids
Expand Down

0 comments on commit 16c3e9d

Please sign in to comment.