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] ContentHistoryHelper prepared statements #26033

Merged
merged 9 commits into from
Sep 30, 2019
47 changes: 31 additions & 16 deletions libraries/src/Helper/ContentHistoryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Joomla\CMS\Factory;
use Joomla\CMS\Filter\InputFilter;
use Joomla\CMS\Table\Table;
use Joomla\Database\ParameterType;

/**
* Versions helper class, provides methods to perform various tasks relevant
Expand Down Expand Up @@ -54,15 +55,19 @@ public function __construct($typeAlias = null)
*/
public function deleteHistory($table)
{
$key = $table->getKeyName();
$id = $table->$key;
$typeTable = Table::getInstance('Contenttype', 'JTable');
$typeId = $typeTable->getTypeId($this->typeAlias);
$db = Factory::getDbo();
$query = $db->getQuery(true);
$query->delete($db->quoteName('#__ucm_history'))
->where($db->quoteName('ucm_item_id') . ' = ' . (int) $id)
->where($db->quoteName('ucm_type_id') . ' = ' . (int) $typeId);
$id = (int) $table->{$table->getKeyName()};
$typeId = Table::getInstance('Contenttype', 'JTable')->getTypeId($this->typeAlias);
$db = Factory::getDbo();
$query = $db->getQuery(true)
->delete($db->quoteName('#__ucm_history'))
->where(
[
$db->quoteName('ucm_item_id') . ' = :id',
$db->quoteName('ucm_type_id') . ' = :typeId',
]
)
->bind(':id', $id, ParameterType::INTEGER)
SharkyKZ marked this conversation as resolved.
Show resolved Hide resolved
->bind(':typeId', $typeId, ParameterType::INTEGER);
$db->setQuery($query);

return $db->execute();
Expand All @@ -80,14 +85,24 @@ public function deleteHistory($table)
*/
public function getHistory($typeId, $id)
{
// Cast as integer until method is typehinted.
$typeId = (int) $typeId;
$id = (int) $id;

$db = Factory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('h.version_note') . ',' . $db->quoteName('h.save_date') . ',' . $db->quoteName('u.name'))
->from($db->quoteName('#__ucm_history') . ' AS h ')
->leftJoin($db->quoteName('#__users') . ' AS u ON ' . $db->quoteName('u.id') . ' = ' . $db->quoteName('h.editor_user_id'))
->where($db->quoteName('ucm_item_id') . ' = ' . $db->quote($id))
->where($db->quoteName('ucm_type_id') . ' = ' . (int) $typeId)
->order($db->quoteName('save_date') . ' DESC ');
$query = $db->getQuery(true)
->select($db->quoteName(['h.version_note', 'h.save_date', 'u.name']))
->from($db->quoteName('#__ucm_history', 'h'))
->join('LEFT', $db->quoteName('#__users', 'u'), $db->quoteName('u.id') . ' = ' . $db->quoteName('h.editor_user_id'))
->where(
[
$db->quoteName('h.ucm_item_id') . ' = :id',
$db->quoteName('h.ucm_type_id') . ' = :typeId',
]
)
->bind(':id', $id, ParameterType::INTEGER)
->bind(':typeId', $typeId, ParameterType::INTEGER)
->order($db->quoteName('h.save_date') . ' DESC');
$db->setQuery($query);

return $db->loadObjectList();
Expand Down