From 572debd0e820bfaa4d55b59eb19544fa245aa579 Mon Sep 17 00:00:00 2001 From: Dhruv Date: Sun, 1 Jun 2014 13:38:27 +0530 Subject: [PATCH 1/4] [ticket/11445] Optimize no of queries in get_global_subscriptions PHPBB3-11445 --- phpBB/phpbb/notification/manager.php | 29 ++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/phpBB/phpbb/notification/manager.php b/phpBB/phpbb/notification/manager.php index c3539e76df..8378af2d99 100644 --- a/phpBB/phpbb/notification/manager.php +++ b/phpBB/phpbb/notification/manager.php @@ -587,26 +587,34 @@ class manager $subscriptions = array(); + $sql = 'SELECT method, notify, item_type + FROM ' . $this->user_notifications_table . ' + WHERE user_id = ' . (int) $user_id . ' + AND item_id = 0'; + + $result = $this->db->sql_query($sql); + $rows = array(); + + while ($row = $this->db->sql_fetchrow($result)) + { + $rows[$row['item_type']][] = $row; + } + + $this->db->sql_freeresult($result); + foreach ($this->get_subscription_types() as $group_name => $types) { foreach ($types as $id => $type) { - $sql = 'SELECT method, notify - FROM ' . $this->user_notifications_table . ' - WHERE user_id = ' . (int) $user_id . " - AND item_type = '" . $this->db->sql_escape($id) . "' - AND item_id = 0"; - $result = $this->db->sql_query($sql); - $row = $this->db->sql_fetchrow($result); - if (!$row) + if (empty($rows[$id])) { // No rows at all, default to '' $subscriptions[$id] = array(''); } else { - do + foreach ($rows[$id] as $row) { if (!$row['notify']) { @@ -620,10 +628,7 @@ class manager $subscriptions[$id][] = $row['method']; } - while ($row = $this->db->sql_fetchrow($result)); } - - $this->db->sql_freeresult($result); } } From c7687ba5aa171b4f9ec964897794695ab72cb069 Mon Sep 17 00:00:00 2001 From: Dhruv Date: Sun, 1 Jun 2014 13:44:09 +0530 Subject: [PATCH 2/4] [ticket/11445] Improve unit tests for get_global_subscriptions PHPBB3-11445 --- tests/notification/notification_test.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/notification/notification_test.php b/tests/notification/notification_test.php index 799dcc5e22..27ea8ddb44 100644 --- a/tests/notification/notification_test.php +++ b/tests/notification/notification_test.php @@ -71,6 +71,27 @@ class phpbb_notification_test extends phpbb_tests_notification_base public function test_subscriptions() { + $expected_subscriptions = array( + 'post' => array(''), + 'topic' => array(''), + 'quote' => array(''), + 'bookmark' => array(''), + 'test' => array(''), + 'pm' => array(''), + ); + + $subscriptions = $this->notifications->get_global_subscriptions(2); + + foreach ($expected_subscriptions as $item_type => $methods) + { + $this->assert_array_content_equals($methods, $subscriptions[$item_type]); + } + + foreach ($subscriptions as $item_type => $methods) + { + $this->assert_array_content_equals($methods, $expected_subscriptions[$item_type]); + } + $this->notifications->delete_subscription('post', 0, '', 2); $this->assertArrayNotHasKey('post', $this->notifications->get_global_subscriptions(2)); From ac74dc876ce215b3cc2c13f497cdd6c574bf65bc Mon Sep 17 00:00:00 2001 From: Dhruv Date: Sat, 7 Jun 2014 01:00:09 +0530 Subject: [PATCH 3/4] [ticket/11445] Remove unused foreach key $group_name PHPBB3-11445 --- phpBB/phpbb/notification/manager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/notification/manager.php b/phpBB/phpbb/notification/manager.php index 8378af2d99..ad9cd9a3ff 100644 --- a/phpBB/phpbb/notification/manager.php +++ b/phpBB/phpbb/notification/manager.php @@ -602,7 +602,7 @@ class manager $this->db->sql_freeresult($result); - foreach ($this->get_subscription_types() as $group_name => $types) + foreach ($this->get_subscription_types() as $types) { foreach ($types as $id => $type) { From f5415619ebc49e370a4ec06cd6b664fb2456ccfe Mon Sep 17 00:00:00 2001 From: Dhruv Date: Sat, 7 Jun 2014 01:34:20 +0530 Subject: [PATCH 4/4] [ticket/11445] Move get user's notification code into its own method PHPBB3-11445 --- phpBB/phpbb/notification/manager.php | 51 ++++++++++++++++++---------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/phpBB/phpbb/notification/manager.php b/phpBB/phpbb/notification/manager.php index ad9cd9a3ff..b787b624f6 100644 --- a/phpBB/phpbb/notification/manager.php +++ b/phpBB/phpbb/notification/manager.php @@ -574,6 +574,34 @@ class manager return $subscription_methods; } + + /** + * Get user's notification data + * + * @param int $user_id The user_id of the user to get the notifications for + * + * @return array User's notification + */ + protected function get_user_notifications($user_id) + { + $sql = 'SELECT method, notify, item_type + FROM ' . $this->user_notifications_table . ' + WHERE user_id = ' . (int) $user_id . ' + AND item_id = 0'; + + $result = $this->db->sql_query($sql); + $user_notifications = array(); + + while ($row = $this->db->sql_fetchrow($result)) + { + $user_notifications[$row['item_type']][] = $row; + } + + $this->db->sql_freeresult($result); + + return $user_notifications; + } + /** * Get global subscriptions (item_id = 0) * @@ -587,36 +615,23 @@ class manager $subscriptions = array(); - $sql = 'SELECT method, notify, item_type - FROM ' . $this->user_notifications_table . ' - WHERE user_id = ' . (int) $user_id . ' - AND item_id = 0'; - - $result = $this->db->sql_query($sql); - $rows = array(); - - while ($row = $this->db->sql_fetchrow($result)) - { - $rows[$row['item_type']][] = $row; - } - - $this->db->sql_freeresult($result); + $user_notifications = $this->get_user_notifications($user_id); foreach ($this->get_subscription_types() as $types) { foreach ($types as $id => $type) { - if (empty($rows[$id])) + if (empty($user_notifications[$id])) { // No rows at all, default to '' $subscriptions[$id] = array(''); } else { - foreach ($rows[$id] as $row) + foreach ($user_notifications[$id] as $user_notification) { - if (!$row['notify']) + if (!$user_notification['notify']) { continue; } @@ -626,7 +641,7 @@ class manager $subscriptions[$id] = array(); } - $subscriptions[$id][] = $row['method']; + $subscriptions[$id][] = $user_notification['method']; } } }