From 17f5c6bf71f560be2f4e2ecabae83ab2973bec47 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 18 Feb 2012 12:00:12 +0100 Subject: [PATCH 01/18] [ticket/10605] Check for orphan privmsgs when deleting a user Also moved the hole code into a new function. PHPBB3-10605 --- phpBB/includes/functions_privmsgs.php | 116 ++++++++++++++++++++++++++ phpBB/includes/functions_user.php | 58 +------------ 2 files changed, 120 insertions(+), 54 deletions(-) diff --git a/phpBB/includes/functions_privmsgs.php b/phpBB/includes/functions_privmsgs.php index c40ceb088f..30cff8ed72 100644 --- a/phpBB/includes/functions_privmsgs.php +++ b/phpBB/includes/functions_privmsgs.php @@ -1083,6 +1083,122 @@ function delete_pm($user_id, $msg_ids, $folder_id) return true; } +/** +* Delete all PM(s) for a given user and delete the ones without references +*/ +function delete_user_pms($user_id) +{ + global $db, $user, $phpbb_root_path, $phpEx; + + $user_id = (int) $user_id; + + if (!$user_id) + { + return false; + } + + // Get PM Information for later deleting + $sql = 'SELECT msg_id, author_id, folder_id, pm_unread, pm_new + FROM ' . PRIVMSGS_TO_TABLE . ' + WHERE user_id = ' . $user_id . ' + OR (author_id = ' . $user_id . ' + AND folder_id = ' . PRIVMSGS_NO_BOX . ')'; + $result = $db->sql_query($sql); + + $undelivered_msg = $undelivered_user = $delete_rows = array(); + $num_unread = $num_new = $num_deleted = 0; + while ($row = $db->sql_fetchrow($result)) + { + if ($row['author_id'] == $user_id && $row['folder_id'] == PRIVMSGS_NO_BOX) + { + // Undelivered messages + $undelivered_msg[] = $row['msg_id']; + $undelivered_user[$row['user_id']][] = true; + } + + $delete_rows[$row['msg_id']] = 1; + } + $db->sql_freeresult($result); + + if (!sizeof($delete_rows)) + { + return false; + } + + $db->sql_transaction('begin'); + + if (sizeof($undelivered_msg)) + { + $sql = 'DELETE FROM ' . PRIVMSGS_TABLE . ' + WHERE ' . $db->sql_in_set('msg_id', $undelivered_msg); + $db->sql_query($sql); + } + + foreach ($undelivered_user as $_user_id => $ary) + { + if ($_user_id == $user_id) + { + continue; + } + + $sql = 'UPDATE ' . USERS_TABLE . ' + SET user_new_privmsg = user_new_privmsg - ' . sizeof($ary) . ', + user_unread_privmsg = user_unread_privmsg - ' . sizeof($ary) . ' + WHERE user_id = ' . $_user_id; + $db->sql_query($sql); + } + + // Delete private message data + $sql = 'DELETE FROM ' . PRIVMSGS_TO_TABLE . " + WHERE user_id = $user_id + AND " . $db->sql_in_set('msg_id', array_keys($delete_rows)); + $db->sql_query($sql); + + // Now we have to check which messages we can delete completely + $sql = 'SELECT msg_id + FROM ' . PRIVMSGS_TO_TABLE . ' + WHERE ' . $db->sql_in_set('msg_id', array_keys($delete_rows)); + $result = $db->sql_query($sql); + + while ($row = $db->sql_fetchrow($result)) + { + unset($delete_rows[$row['msg_id']]); + } + $db->sql_freeresult($result); + + $delete_ids = array_keys($delete_rows); + + if (sizeof($delete_ids)) + { + // Check if there are any attachments we need to remove + if (!function_exists('delete_attachments')) + { + include($phpbb_root_path . 'includes/functions_admin.' . $phpEx); + } + + delete_attachments('message', $delete_ids, false); + + $sql = 'DELETE FROM ' . PRIVMSGS_TABLE . ' + WHERE ' . $db->sql_in_set('msg_id', $delete_ids); + $db->sql_query($sql); + } + + // Set the remaining author id to anonymous - this way users are still able to read messages from users being removed + $sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . ' + SET author_id = ' . ANONYMOUS . ' + WHERE author_id = ' . $user_id; + $db->sql_query($sql); + + $sql = 'UPDATE ' . PRIVMSGS_TABLE . ' + SET author_id = ' . ANONYMOUS . ' + WHERE author_id = ' . $user_id; + $db->sql_query($sql); + + $db->sql_transaction('commit'); + + return true; +} + /** * Rebuild message header */ diff --git a/phpBB/includes/functions_user.php b/phpBB/includes/functions_user.php index 6b5cca8abb..20923ea495 100644 --- a/phpBB/includes/functions_user.php +++ b/phpBB/includes/functions_user.php @@ -528,62 +528,12 @@ function user_delete($mode, $user_id, $post_username = false) WHERE session_user_id = ' . $user_id; $db->sql_query($sql); - // Remove any undelivered mails... - $sql = 'SELECT msg_id, user_id - FROM ' . PRIVMSGS_TO_TABLE . ' - WHERE author_id = ' . $user_id . ' - AND folder_id = ' . PRIVMSGS_NO_BOX; - $result = $db->sql_query($sql); - - $undelivered_msg = $undelivered_user = array(); - while ($row = $db->sql_fetchrow($result)) + // Clean the private messages tables from the user + if (!function_exists('delete_user_pms')) { - $undelivered_msg[] = $row['msg_id']; - $undelivered_user[$row['user_id']][] = true; - } - $db->sql_freeresult($result); - - if (sizeof($undelivered_msg)) - { - $sql = 'DELETE FROM ' . PRIVMSGS_TABLE . ' - WHERE ' . $db->sql_in_set('msg_id', $undelivered_msg); - $db->sql_query($sql); - } - - $sql = 'DELETE FROM ' . PRIVMSGS_TO_TABLE . ' - WHERE author_id = ' . $user_id . ' - AND folder_id = ' . PRIVMSGS_NO_BOX; - $db->sql_query($sql); - - // Delete all to-information - $sql = 'DELETE FROM ' . PRIVMSGS_TO_TABLE . ' - WHERE user_id = ' . $user_id; - $db->sql_query($sql); - - // Set the remaining author id to anonymous - this way users are still able to read messages from users being removed - $sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . ' - SET author_id = ' . ANONYMOUS . ' - WHERE author_id = ' . $user_id; - $db->sql_query($sql); - - $sql = 'UPDATE ' . PRIVMSGS_TABLE . ' - SET author_id = ' . ANONYMOUS . ' - WHERE author_id = ' . $user_id; - $db->sql_query($sql); - - foreach ($undelivered_user as $_user_id => $ary) - { - if ($_user_id == $user_id) - { - continue; - } - - $sql = 'UPDATE ' . USERS_TABLE . ' - SET user_new_privmsg = user_new_privmsg - ' . sizeof($ary) . ', - user_unread_privmsg = user_unread_privmsg - ' . sizeof($ary) . ' - WHERE user_id = ' . $_user_id; - $db->sql_query($sql); + include($phpbb_root_path . 'includes/functions_privmsgs.' . $phpEx); } + delete_user_pms($user_id); $db->sql_transaction('commit'); From 45f39c6d1f2adc318d521bd9eb75d80f0750fdb8 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 18 Feb 2012 12:16:21 +0100 Subject: [PATCH 02/18] [ticket/10605] Delete orphan private messages on update PHPBB3-10605 --- phpBB/install/database_update.php | 42 +++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/phpBB/install/database_update.php b/phpBB/install/database_update.php index a1b7dcd47f..b6298ca651 100644 --- a/phpBB/install/database_update.php +++ b/phpBB/install/database_update.php @@ -2024,6 +2024,48 @@ function change_database_data(&$no_updates, $version) // No changes from 3.0.10-RC3 to 3.0.10 case '3.0.10-RC3': break; + + // Changes from 3.0.10 to 3.0.11-RC1 + case '3.0.10': + // Delete orphan private messages + $batch_size = 500; + + $sql_array = array( + 'SELECT' => 'p.msg_id', + 'FROM' => array( + PRIVMSGS_TABLE => 'p', + ), + 'LEFT_JOIN' => array( + array( + 'FROM' => array(PRIVMSGS_TO_TABLE, 't'), + 'ON' => 'p.msg_id = t.msg_id', + ), + ), + 'WHERE' => 't.user_id IS NULL'; + $sql = $db->sql_build_query('SELECT', $sql_array); + + do + { + $result = $db->sql_query_limit($sql, $batch_size); + + $delete_pms = array(); + while ($row = $db->sql_fetchrow($result)) + { + $delete_pms[] = (int) $row['msg_id']; + } + $db->sql_freeresult($result); + + if (!empty($delete_pms)) + { + $sql = 'DELETE FROM ' . PRIVMSGS_TABLE . ' + WHERE ' . $db->sql_in_set('msg_id', $delete_pms); + $db->sql_query($sql); + } + } + while (sizeof($delete_pms) == $batch_size); + + $no_updates = false; + break; } } From ba6943a6a0ea50af772dc6e94f13b56292cd9f37 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 12 Mar 2012 10:11:52 +0100 Subject: [PATCH 03/18] [ticket/10605] Prefix function with phpbb_ and use true instead of 1 PHPBB3-10605 --- phpBB/includes/functions_privmsgs.php | 4 ++-- phpBB/includes/functions_user.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/phpBB/includes/functions_privmsgs.php b/phpBB/includes/functions_privmsgs.php index 30cff8ed72..34f16ea9da 100644 --- a/phpBB/includes/functions_privmsgs.php +++ b/phpBB/includes/functions_privmsgs.php @@ -1086,7 +1086,7 @@ function delete_pm($user_id, $msg_ids, $folder_id) /** * Delete all PM(s) for a given user and delete the ones without references */ -function delete_user_pms($user_id) +function phpbb_delete_user_pms($user_id) { global $db, $user, $phpbb_root_path, $phpEx; @@ -1116,7 +1116,7 @@ function delete_user_pms($user_id) $undelivered_user[$row['user_id']][] = true; } - $delete_rows[$row['msg_id']] = 1; + $delete_rows[$row['msg_id']] = true; } $db->sql_freeresult($result); diff --git a/phpBB/includes/functions_user.php b/phpBB/includes/functions_user.php index 20923ea495..92a7b8e0e9 100644 --- a/phpBB/includes/functions_user.php +++ b/phpBB/includes/functions_user.php @@ -529,11 +529,11 @@ function user_delete($mode, $user_id, $post_username = false) $db->sql_query($sql); // Clean the private messages tables from the user - if (!function_exists('delete_user_pms')) + if (!function_exists('phpbb_delete_user_pms')) { include($phpbb_root_path . 'includes/functions_privmsgs.' . $phpEx); } - delete_user_pms($user_id); + phpbb_delete_user_pms($user_id); $db->sql_transaction('commit'); From e8830f605f73a0c8fa5c3ea579ee18f295b81600 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Tue, 27 Mar 2012 01:18:02 +0200 Subject: [PATCH 04/18] [ticket/10605] Use unset() instead of checking user_id over and over again. PHPBB3-10605 --- phpBB/includes/functions_privmsgs.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/phpBB/includes/functions_privmsgs.php b/phpBB/includes/functions_privmsgs.php index 34f16ea9da..59dea50094 100644 --- a/phpBB/includes/functions_privmsgs.php +++ b/phpBB/includes/functions_privmsgs.php @@ -1134,13 +1134,9 @@ function phpbb_delete_user_pms($user_id) $db->sql_query($sql); } + unset($undelivered_user[$user_id]); foreach ($undelivered_user as $_user_id => $ary) { - if ($_user_id == $user_id) - { - continue; - } - $sql = 'UPDATE ' . USERS_TABLE . ' SET user_new_privmsg = user_new_privmsg - ' . sizeof($ary) . ', user_unread_privmsg = user_unread_privmsg - ' . sizeof($ary) . ' From 2203bc204e55c33e2e9b212eeb0c7bf2e3060851 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Tue, 27 Mar 2012 01:29:29 +0200 Subject: [PATCH 05/18] [ticket/10605] Turn $undelivered_user into a real array of counters. PHPBB3-10605 --- phpBB/includes/functions_privmsgs.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/phpBB/includes/functions_privmsgs.php b/phpBB/includes/functions_privmsgs.php index 59dea50094..352bd4d15d 100644 --- a/phpBB/includes/functions_privmsgs.php +++ b/phpBB/includes/functions_privmsgs.php @@ -1113,7 +1113,15 @@ function phpbb_delete_user_pms($user_id) { // Undelivered messages $undelivered_msg[] = $row['msg_id']; - $undelivered_user[$row['user_id']][] = true; + + if (isset($undelivered_user[$row['user_id']])) + { + ++$undelivered_user[$row['user_id']]; + } + else + { + $undelivered_user[$row['user_id']] = 1; + } } $delete_rows[$row['msg_id']] = true; @@ -1135,11 +1143,11 @@ function phpbb_delete_user_pms($user_id) } unset($undelivered_user[$user_id]); - foreach ($undelivered_user as $_user_id => $ary) + foreach ($undelivered_user as $_user_id => $count) { $sql = 'UPDATE ' . USERS_TABLE . ' - SET user_new_privmsg = user_new_privmsg - ' . sizeof($ary) . ', - user_unread_privmsg = user_unread_privmsg - ' . sizeof($ary) . ' + SET user_new_privmsg = user_new_privmsg - ' . $count . ', + user_unread_privmsg = user_unread_privmsg - ' . $count . ' WHERE user_id = ' . $_user_id; $db->sql_query($sql); } From dbc7a69ad2bf6b062a4d7a40d62be29a410b5c18 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Tue, 27 Mar 2012 01:30:52 +0200 Subject: [PATCH 06/18] [ticket/10605] Remove unused variable declarations. PHPBB3-10605 --- phpBB/includes/functions_privmsgs.php | 1 - 1 file changed, 1 deletion(-) diff --git a/phpBB/includes/functions_privmsgs.php b/phpBB/includes/functions_privmsgs.php index 352bd4d15d..4ff0c53420 100644 --- a/phpBB/includes/functions_privmsgs.php +++ b/phpBB/includes/functions_privmsgs.php @@ -1106,7 +1106,6 @@ function phpbb_delete_user_pms($user_id) $result = $db->sql_query($sql); $undelivered_msg = $undelivered_user = $delete_rows = array(); - $num_unread = $num_new = $num_deleted = 0; while ($row = $db->sql_fetchrow($result)) { if ($row['author_id'] == $user_id && $row['folder_id'] == PRIVMSGS_NO_BOX) From 9040f18d7cf9de137acbac6072dc2ffd3cede1b5 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Tue, 27 Mar 2012 01:35:01 +0200 Subject: [PATCH 07/18] [ticket/10605] Remove unnecessary array_keys calls on $delete_rows. PHPBB3-10605 --- phpBB/includes/functions_privmsgs.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/phpBB/includes/functions_privmsgs.php b/phpBB/includes/functions_privmsgs.php index 4ff0c53420..00029a1986 100644 --- a/phpBB/includes/functions_privmsgs.php +++ b/phpBB/includes/functions_privmsgs.php @@ -1123,7 +1123,7 @@ function phpbb_delete_user_pms($user_id) } } - $delete_rows[$row['msg_id']] = true; + $delete_rows[(int) $row['msg_id']] = (int) $row['msg_id']; } $db->sql_freeresult($result); @@ -1154,13 +1154,13 @@ function phpbb_delete_user_pms($user_id) // Delete private message data $sql = 'DELETE FROM ' . PRIVMSGS_TO_TABLE . " WHERE user_id = $user_id - AND " . $db->sql_in_set('msg_id', array_keys($delete_rows)); + AND " . $db->sql_in_set('msg_id', $delete_rows); $db->sql_query($sql); // Now we have to check which messages we can delete completely $sql = 'SELECT msg_id FROM ' . PRIVMSGS_TO_TABLE . ' - WHERE ' . $db->sql_in_set('msg_id', array_keys($delete_rows)); + WHERE ' . $db->sql_in_set('msg_id', $delete_rows); $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) From dd53d0576d8256e37074cc23dd5b9769acc12d1a Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Tue, 27 Mar 2012 01:39:49 +0200 Subject: [PATCH 08/18] [ticket/10605] Remove unnecessary $delete_ids array. PHPBB3-10605 --- phpBB/includes/functions_privmsgs.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/phpBB/includes/functions_privmsgs.php b/phpBB/includes/functions_privmsgs.php index 00029a1986..23f582641b 100644 --- a/phpBB/includes/functions_privmsgs.php +++ b/phpBB/includes/functions_privmsgs.php @@ -1169,9 +1169,7 @@ function phpbb_delete_user_pms($user_id) } $db->sql_freeresult($result); - $delete_ids = array_keys($delete_rows); - - if (sizeof($delete_ids)) + if (!empty($delete_rows)) { // Check if there are any attachments we need to remove if (!function_exists('delete_attachments')) @@ -1179,10 +1177,10 @@ function phpbb_delete_user_pms($user_id) include($phpbb_root_path . 'includes/functions_admin.' . $phpEx); } - delete_attachments('message', $delete_ids, false); + delete_attachments('message', $delete_rows, false); $sql = 'DELETE FROM ' . PRIVMSGS_TABLE . ' - WHERE ' . $db->sql_in_set('msg_id', $delete_ids); + WHERE ' . $db->sql_in_set('msg_id', $delete_rows); $db->sql_query($sql); } From ad073d22b9e543d5842af7b8ef94fe7e6b443504 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Tue, 27 Mar 2012 01:43:09 +0200 Subject: [PATCH 09/18] [ticket/10605] Break long comment into multiple lines 80 chars short. PHPBB3-10605 --- phpBB/includes/functions_privmsgs.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/phpBB/includes/functions_privmsgs.php b/phpBB/includes/functions_privmsgs.php index 23f582641b..e7beae3fab 100644 --- a/phpBB/includes/functions_privmsgs.php +++ b/phpBB/includes/functions_privmsgs.php @@ -1184,7 +1184,8 @@ function phpbb_delete_user_pms($user_id) $db->sql_query($sql); } - // Set the remaining author id to anonymous - this way users are still able to read messages from users being removed + // Set the remaining author id to anonymous + // This way users are still able to read messages from users being removed $sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . ' SET author_id = ' . ANONYMOUS . ' WHERE author_id = ' . $user_id; From 9c8aab4d32a001ae66fe005b7124737fc5ad7dd6 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Tue, 27 Mar 2012 02:02:17 +0200 Subject: [PATCH 10/18] [ticket/10605] Rename $delete_rows to $delete_ids. PHPBB3-10605 --- phpBB/includes/functions_privmsgs.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/phpBB/includes/functions_privmsgs.php b/phpBB/includes/functions_privmsgs.php index e7beae3fab..576da4d439 100644 --- a/phpBB/includes/functions_privmsgs.php +++ b/phpBB/includes/functions_privmsgs.php @@ -1105,7 +1105,7 @@ function phpbb_delete_user_pms($user_id) AND folder_id = ' . PRIVMSGS_NO_BOX . ')'; $result = $db->sql_query($sql); - $undelivered_msg = $undelivered_user = $delete_rows = array(); + $undelivered_msg = $undelivered_user = $delete_ids = array(); while ($row = $db->sql_fetchrow($result)) { if ($row['author_id'] == $user_id && $row['folder_id'] == PRIVMSGS_NO_BOX) @@ -1123,11 +1123,11 @@ function phpbb_delete_user_pms($user_id) } } - $delete_rows[(int) $row['msg_id']] = (int) $row['msg_id']; + $delete_ids[(int) $row['msg_id']] = (int) $row['msg_id']; } $db->sql_freeresult($result); - if (!sizeof($delete_rows)) + if (empty($delete_ids)) { return false; } @@ -1154,22 +1154,22 @@ function phpbb_delete_user_pms($user_id) // Delete private message data $sql = 'DELETE FROM ' . PRIVMSGS_TO_TABLE . " WHERE user_id = $user_id - AND " . $db->sql_in_set('msg_id', $delete_rows); + AND " . $db->sql_in_set('msg_id', $delete_ids); $db->sql_query($sql); // Now we have to check which messages we can delete completely $sql = 'SELECT msg_id FROM ' . PRIVMSGS_TO_TABLE . ' - WHERE ' . $db->sql_in_set('msg_id', $delete_rows); + WHERE ' . $db->sql_in_set('msg_id', $delete_ids); $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { - unset($delete_rows[$row['msg_id']]); + unset($delete_ids[$row['msg_id']]); } $db->sql_freeresult($result); - if (!empty($delete_rows)) + if (!empty($delete_ids)) { // Check if there are any attachments we need to remove if (!function_exists('delete_attachments')) @@ -1177,10 +1177,10 @@ function phpbb_delete_user_pms($user_id) include($phpbb_root_path . 'includes/functions_admin.' . $phpEx); } - delete_attachments('message', $delete_rows, false); + delete_attachments('message', $delete_ids, false); $sql = 'DELETE FROM ' . PRIVMSGS_TABLE . ' - WHERE ' . $db->sql_in_set('msg_id', $delete_rows); + WHERE ' . $db->sql_in_set('msg_id', $delete_ids); $db->sql_query($sql); } From 0397b462174887bda3fea4bcebf9a26f6b591a3e Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 27 Mar 2012 17:29:03 +0200 Subject: [PATCH 11/18] [ticket/10605] Split query to be able to use indexes PHPBB3-10605 --- phpBB/includes/functions_privmsgs.php | 34 ++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/phpBB/includes/functions_privmsgs.php b/phpBB/includes/functions_privmsgs.php index 576da4d439..dd81e8f92d 100644 --- a/phpBB/includes/functions_privmsgs.php +++ b/phpBB/includes/functions_privmsgs.php @@ -1098,11 +1098,11 @@ function phpbb_delete_user_pms($user_id) } // Get PM Information for later deleting + // The two queries where split, so we can use our indexes + // Part 1: get PMs the user received $sql = 'SELECT msg_id, author_id, folder_id, pm_unread, pm_new FROM ' . PRIVMSGS_TO_TABLE . ' - WHERE user_id = ' . $user_id . ' - OR (author_id = ' . $user_id . ' - AND folder_id = ' . PRIVMSGS_NO_BOX . ')'; + WHERE user_id = ' . $user_id; $result = $db->sql_query($sql); $undelivered_msg = $undelivered_user = $delete_ids = array(); @@ -1127,6 +1127,34 @@ function phpbb_delete_user_pms($user_id) } $db->sql_freeresult($result); + // Part 2: get PMs the user sent + $sql = 'SELECT msg_id, author_id, folder_id, pm_unread, pm_new + FROM ' . PRIVMSGS_TO_TABLE . ' + WHERE author_id = ' . $user_id . ' + AND folder_id = ' . PRIVMSGS_NO_BOX; + $result = $db->sql_query($sql); + + while ($row = $db->sql_fetchrow($result)) + { + if ($row['author_id'] == $user_id && $row['folder_id'] == PRIVMSGS_NO_BOX) + { + // Undelivered messages + $undelivered_msg[] = $row['msg_id']; + + if (isset($undelivered_user[$row['user_id']])) + { + ++$undelivered_user[$row['user_id']]; + } + else + { + $undelivered_user[$row['user_id']] = 1; + } + } + + $delete_ids[(int) $row['msg_id']] = (int) $row['msg_id']; + } + $db->sql_freeresult($result); + if (empty($delete_ids)) { return false; From b9324577aca6f7f7632b609e975e510e25d8ec86 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 27 Mar 2012 17:32:55 +0200 Subject: [PATCH 12/18] =?UTF-8?q?[ticket/10605]=20Reset=20user=C2=B4s=20pm?= =?UTF-8?q?=20count=20to=200=20when=20deleting=20his=20PMs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PHPBB3-10605 --- phpBB/includes/functions_privmsgs.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/phpBB/includes/functions_privmsgs.php b/phpBB/includes/functions_privmsgs.php index dd81e8f92d..14a6d83b2a 100644 --- a/phpBB/includes/functions_privmsgs.php +++ b/phpBB/includes/functions_privmsgs.php @@ -1169,7 +1169,17 @@ function phpbb_delete_user_pms($user_id) $db->sql_query($sql); } - unset($undelivered_user[$user_id]); + // Reset the userīs pm count to 0 + if (isset($undelivered_user[$user_id])) + { + $sql = 'UPDATE ' . USERS_TABLE . ' + SET user_new_privmsg = 0, + user_unread_privmsg = 0 + WHERE user_id = ' . $user_id; + $db->sql_query($sql); + unset($undelivered_user[$user_id]); + } + foreach ($undelivered_user as $_user_id => $count) { $sql = 'UPDATE ' . USERS_TABLE . ' From 03e382f99c61c12655998ac8bd798fca7a34c099 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Thu, 5 Apr 2012 17:34:37 -0400 Subject: [PATCH 13/18] [ticket/10605] Fix syntax error in database updater. PHPBB3-10605 --- phpBB/install/database_update.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/install/database_update.php b/phpBB/install/database_update.php index b6298ca651..6088a58fc4 100644 --- a/phpBB/install/database_update.php +++ b/phpBB/install/database_update.php @@ -2041,7 +2041,7 @@ function change_database_data(&$no_updates, $version) 'ON' => 'p.msg_id = t.msg_id', ), ), - 'WHERE' => 't.user_id IS NULL'; + 'WHERE' => 't.user_id IS NULL'); $sql = $db->sql_build_query('SELECT', $sql_array); do From de1e343c5b408da09448b3150cfec2de75afd04a Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Thu, 5 Apr 2012 17:34:54 -0400 Subject: [PATCH 14/18] [ticket/10605] Add a section for updating from 3.0.10 to schema updates. Without it data updates do not appear to be run. PHPBB3-10605 --- phpBB/install/database_update.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/phpBB/install/database_update.php b/phpBB/install/database_update.php index 6088a58fc4..116e6ea9a6 100644 --- a/phpBB/install/database_update.php +++ b/phpBB/install/database_update.php @@ -993,6 +993,8 @@ function database_update_info() '3.0.10-RC2' => array(), // No changes from 3.0.10-RC3 to 3.0.10 '3.0.10-RC3' => array(), + // No changes from 3.0.10 to 3.0.11-RC1 + '3.0.10' => array(), /** @todo DROP LOGIN_ATTEMPT_TABLE.attempt_id in 3.0.11-RC1 */ ); From 2b2286a46c56b8a9e5444fd06eff263f880e0c78 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Thu, 5 Apr 2012 17:35:27 -0400 Subject: [PATCH 15/18] [ticket/10605] Fix left join usage. Previously this produced broken SQL: SELECT p.msg_id FROM phpbb_privmsgs p LEFT JOIN 0 phpbb_privmsgs_to ON (p.msg_id = t.msg_id) WHERE t.user_id IS NULL LIMIT 500 OFFSET 0 PHPBB3-10605 --- phpBB/install/database_update.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/install/database_update.php b/phpBB/install/database_update.php index 116e6ea9a6..0737061887 100644 --- a/phpBB/install/database_update.php +++ b/phpBB/install/database_update.php @@ -2039,7 +2039,7 @@ function change_database_data(&$no_updates, $version) ), 'LEFT_JOIN' => array( array( - 'FROM' => array(PRIVMSGS_TO_TABLE, 't'), + 'FROM' => array(PRIVMSGS_TO_TABLE => 't'), 'ON' => 'p.msg_id = t.msg_id', ), ), From 4b6b41a1e5cb3d4ba7c896dfa4d72082aa2f069e Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 13 Apr 2012 16:26:41 +0200 Subject: [PATCH 16/18] [ticket/10605] Add parameter documentation to phpbb_delete_user_pms PHPBB3-10605 --- phpBB/includes/functions_privmsgs.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/phpBB/includes/functions_privmsgs.php b/phpBB/includes/functions_privmsgs.php index 14a6d83b2a..00bec11569 100644 --- a/phpBB/includes/functions_privmsgs.php +++ b/phpBB/includes/functions_privmsgs.php @@ -1085,6 +1085,10 @@ function delete_pm($user_id, $msg_ids, $folder_id) /** * Delete all PM(s) for a given user and delete the ones without references +* +* @param int $user_id ID of the user whose private messages we want to delete +* +* @return boolean False if there were no pms found, true otherwise. */ function phpbb_delete_user_pms($user_id) { From f71a9d369c84ec938408a2134b09ea259f6cbbab Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 14 May 2012 00:34:42 +0200 Subject: [PATCH 17/18] [ticket/10605] Put end of array on its own line because start of array is too. PHPBB3-10605 --- phpBB/install/database_update.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/phpBB/install/database_update.php b/phpBB/install/database_update.php index 0737061887..2a361aae26 100644 --- a/phpBB/install/database_update.php +++ b/phpBB/install/database_update.php @@ -2043,7 +2043,8 @@ function change_database_data(&$no_updates, $version) 'ON' => 'p.msg_id = t.msg_id', ), ), - 'WHERE' => 't.user_id IS NULL'); + 'WHERE' => 't.user_id IS NULL', + ); $sql = $db->sql_build_query('SELECT', $sql_array); do From 95e1d4e9db8d2174c8ee5c6bb35be06abc57e3cf Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 14 May 2012 00:36:18 +0200 Subject: [PATCH 18/18] [ticket/10605] Use database updater function _sql() instead of $db->sql_query() PHPBB3-10605 --- phpBB/install/database_update.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/install/database_update.php b/phpBB/install/database_update.php index 2a361aae26..95a0282878 100644 --- a/phpBB/install/database_update.php +++ b/phpBB/install/database_update.php @@ -2062,7 +2062,7 @@ function change_database_data(&$no_updates, $version) { $sql = 'DELETE FROM ' . PRIVMSGS_TABLE . ' WHERE ' . $db->sql_in_set('msg_id', $delete_pms); - $db->sql_query($sql); + _sql($sql, $errored, $error_ary); } } while (sizeof($delete_pms) == $batch_size);