Commit 30ca3035 authored by Robert Lyon's avatar Robert Lyon Committed by Gerrit Code Review
Browse files

Merge "More SQL optimisation (Bug #1358092)"

parents dbebbb81 a9b36c0f
...@@ -75,12 +75,12 @@ if ($markasread) { ...@@ -75,12 +75,12 @@ if ($markasread) {
$message = get_string('markedasread', 'activity'); $message = get_string('markedasread', 'activity');
} }
else if ($delete) { else if ($delete) {
$ids = array(); $rawids = array();
$deleteunread = 0; // Remember the number of unread messages being deleted $deleteunread = 0; // Remember the number of unread messages being deleted
foreach ($_GET as $k => $v) { foreach ($_GET as $k => $v) {
if (preg_match('/^delete\-([a-zA-Z_]+)\-(\d+)$/',$k,$m)) { if (preg_match('/^delete\-([a-zA-Z_]+)\-(\d+)$/',$k,$m)) {
$table = $m[1]; $table = $m[1];
$ids[$table][] = $m[2]; $rawids[$table][] = $m[2];
if (isset($_GET['unread-' . $table . '-' . $m[2]])) { if (isset($_GET['unread-' . $table . '-' . $m[2]])) {
$deleteunread++; $deleteunread++;
} }
...@@ -88,35 +88,39 @@ else if ($delete) { ...@@ -88,35 +88,39 @@ else if ($delete) {
} }
db_begin(); db_begin();
$countdeleted = 0; $countdeleted = 0;
foreach ($ids as $table => $idspertable) { foreach ($rawids as $table => $idspertable) {
if ('artefact_multirecipient_notification' === $table) { if ('artefact_multirecipient_notification' === $table) {
delete_messages_mr($idspertable, $USER->get('id')); delete_messages_mr($idspertable, $USER->get('id'));
} }
else if ('notification_internal_activity' === $table) { else if ('notification_internal_activity' === $table) {
$strids = join(',', array_map('db_quote', $idspertable));
$userid = $USER->get('id'); $userid = $USER->get('id');
// Remove parent pointers to messages we're about to delete // Ignore message ids that do not belong to the current user to stop
// Use temp table in subselect for Mysql compat. // hacking of the form allowing the deletion of messages owned by other users.
execute_sql(" $rawstrids = join(',', array_map('db_quote', $idspertable));
UPDATE {notification_internal_activity} $ids = get_column_sql(
SET parent = NULL "SELECT id FROM {notification_internal_activity}
WHERE parent IN ( WHERE id IN ($rawstrids) AND usr = ?",
SELECT id FROM (
SELECT id FROM {notification_internal_activity} WHERE id IN ($strids) AND usr = ?
) AS temp
)",
array($userid) array($userid)
); );
delete_records_select(
'notification_internal_activity', if ($ids) {
"id IN ($strids) AND usr = ?", $strids = join(',', $ids);
array($userid) // Remove parent pointers to messages we're about to delete
); execute_sql("
if ($deleteunread) { UPDATE {notification_internal_activity}
$newunread = $USER->add_unread(-$deleteunread); SET parent = NULL
WHERE parent IN ($strids)"
);
delete_records_select(
'notification_internal_activity',
"id IN ($strids)"
);
if ($deleteunread) {
$newunread = $USER->add_unread(-1 * $deleteunread);
}
} }
} }
$countdeleted += count($idspertable); $countdeleted += ($ids) ? count($ids) : count($idspertable);
} }
db_commit(); db_commit();
$message = get_string('deletednotifications1', 'activity', $countdeleted); $message = get_string('deletednotifications1', 'activity', $countdeleted);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment