Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.0] Add prepared statements for mod tags popular #25043

Merged
merged 14 commits into from Jul 23, 2019
Merged
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 39 additions & 34 deletions modules/mod_tags_popular/Helper/TagsPopularHelper.php
Expand Up @@ -14,6 +14,7 @@
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Helper\ContentHelper;
use Joomla\Database\ParameterType;

/**
* Helper for mod_tags_popular
Expand All @@ -35,36 +36,37 @@ public static function getList(&$params)
{
$db = Factory::getDbo();
$user = Factory::getUser();
$groups = implode(',', $user->getAuthorisedViewLevels());
$groups = $user->getAuthorisedViewLevels();
$timeframe = $params->get('timeframe', 'alltime');
$maximum = $params->get('maximum', 5);
$order_value = $params->get('order_value', 'title');
$nowDate = Factory::getDate()->toSql();
$nullDate = $db->quote($db->getNullDate());
$nullDate = $db->getNullDate();

$query = $db->getQuery(true)
->select(
array(
'MAX(' . $db->quoteName('tag_id') . ') AS tag_id',
' COUNT(*) AS count', 'MAX(t.title) AS title',
'MAX(' . $db->quoteName('t.access') . ') AS access',
'MAX(' . $db->quoteName('t.alias') . ') AS alias',
'MAX(' . $db->quoteName('t.params') . ') AS params',
)
[
'MAX(' . $db->quoteName('tag_id') . ') AS ' . $db->quoteName('tag_id'),
'COUNT(*) AS ' . $db->quoteName('count'),
'MAX(' . $db->quoteName('t.title') . ') AS ' . $db->quoteName('title'),
'MAX(' . $db->quoteName('t.access') . ') AS ' . $db->quoteName('access'),
'MAX(' . $db->quoteName('t.alias') . ') AS ' . $db->quoteName('alias'),
'MAX(' . $db->quoteName('t.params') . ') AS ' . $db->quoteName('params'),
]
)
->group($db->quoteName(array('tag_id', 'title', 'access', 'alias')))
->group($db->quoteName(['tag_id', 'title', 'access', 'alias']))
->from($db->quoteName('#__contentitem_tag_map', 'm'))
->where($db->quoteName('t.access') . ' IN (' . $groups . ')');
->whereIn($db->quoteName('t.access'), $groups);

// Only return published tags
$query->where($db->quoteName('t.published') . ' = 1 ');

// Filter by Parent Tag
$parentTags = $params->get('parentTag', array());
$parentTags = $params->get('parentTag', []);

if ($parentTags)
{
$query->where($db->quoteName('t.parent_id') . ' IN (' . implode(',', $parentTags) . ')');
$query->whereIn($db->quoteName('t.parent_id'), $parentTags);
}

// Optionally filter on language
Expand All @@ -77,30 +79,32 @@ public static function getList(&$params)
$language = ContentHelper::getCurrentLanguage();
}

$query->where($db->quoteName('t.language') . ' IN (' . $db->quote($language) . ', ' . $db->quote('*') . ')');
$query->whereIn($db->quoteName('t.language'), [$language, '*'], ParameterType::STRING);
}

if ($timeframe !== 'alltime')
{
$query->where($db->quoteName('tag_date') . ' > ' . $query->dateAdd($db->quote($nowDate), '-1', strtoupper($timeframe)));
$query->where($db->quoteName('tag_date') . ' > ' . $query->dateAdd(':nowDate1', '-1', strtoupper($timeframe)))
->bind(':nowDate1', $nowDate);
}

$query->join('INNER', $db->quoteName('#__tags', 't') . ' ON ' . $db->quoteName('tag_id') . ' = t.id')
->join(
'INNER',
$db->quoteName('#__ucm_content', 'c') . ' ON ' . $db->quoteName('m.core_content_id') . ' = ' . $db->quoteName('c.core_content_id')
$query->innerJoin($db->quoteName('#__tags', 't'), $db->quoteName('tag_id') . ' = ' . $db->quoteName('t.id'))
HLeithner marked this conversation as resolved.
Show resolved Hide resolved
->innerJoin(
$db->quoteName('#__ucm_content', 'c'), $db->quoteName('m.core_content_id') . ' = ' . $db->quoteName('c.core_content_id')
);

$query->where($db->quoteName('m.type_alias') . ' = ' . $db->quoteName('c.core_type_alias'));

// Only return tags connected to published articles
$query->where($db->quoteName('c.core_state') . ' = 1')
->where('(' . $db->quoteName('c.core_publish_up') . ' = ' . $nullDate
. ' OR ' . $db->quoteName('c.core_publish_up') . ' <= ' . $db->quote($nowDate) . ')'
->where('(' . $db->quoteName('c.core_publish_up') . ' = :nullDate2'
. ' OR ' . $db->quoteName('c.core_publish_up') . ' <= :nowDate2)'
)
->where('(' . $db->quoteName('c.core_publish_down') . ' = ' . $nullDate
. ' OR ' . $db->quoteName('c.core_publish_down') . ' >= ' . $db->quote($nowDate) . ')'
);
->where('(' . $db->quoteName('c.core_publish_down') . ' = :nullDate3'
. ' OR ' . $db->quoteName('c.core_publish_down') . ' >= :nowDate3)'
HLeithner marked this conversation as resolved.
Show resolved Hide resolved
)
->bind([':nullDate2', ':nullDate3'], $nullDate)
->bind([':nowDate2', ':nowDate3'], $nowDate);

// Set query depending on order_value param
if ($order_value === 'rand()')
Expand All @@ -109,7 +113,6 @@ public static function getList(&$params)
}
else
{
$order_value = $db->quoteName($order_value);
$order_direction = $params->get('order_direction', 1) ? 'DESC' : 'ASC';

if ($params->get('order_value', 'title') === 'title')
Expand All @@ -118,22 +121,24 @@ public static function getList(&$params)
$query->order('count DESC');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apply $db->quoteName?

HLeithner marked this conversation as resolved.
Show resolved Hide resolved
$equery = $db->getQuery(true)
->select(
array(
'a.tag_id',
'a.count',
'a.title',
'a.access',
'a.alias',
$db->quoteName(
[
'a.tag_id',
'a.count',
'a.title',
'a.access',
'a.alias',
]
)
)
->from('(' . (string) $query . ') AS a')
->order('a.title ' . $order_direction);
->from('(' . (string) $query . ') AS ' . $db->quoteName('a'))
->order($db->quoteName('a.title') . ' ' . $order_direction);

$query = $equery;
}
else
{
$query->order($order_value . ' ' . $order_direction);
$query->order($db->quoteName($order_value) . ' ' . $order_direction);
}
}

Expand Down