mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-12 22:38:52 +00:00
Merge pull request #2527 from dhruvgoel92/ticket/11445
[ticket/11445] optimize no of queries in get_global_subscriptions( ) * dhruvgoel92/ticket/11445: [ticket/11445] Move get user's notification code into its own method [ticket/11445] Remove unused foreach key $group_name [ticket/11445] Improve unit tests for get_global_subscriptions [ticket/11445] Optimize no of queries in get_global_subscriptions
This commit is contained in:
commit
7642fbbd63
2 changed files with 56 additions and 15 deletions
|
@ -574,6 +574,34 @@ class manager
|
||||||
return $subscription_methods;
|
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)
|
* Get global subscriptions (item_id = 0)
|
||||||
*
|
*
|
||||||
|
@ -587,28 +615,23 @@ class manager
|
||||||
|
|
||||||
$subscriptions = array();
|
$subscriptions = array();
|
||||||
|
|
||||||
foreach ($this->get_subscription_types() as $group_name => $types)
|
$user_notifications = $this->get_user_notifications($user_id);
|
||||||
|
|
||||||
|
foreach ($this->get_subscription_types() as $types)
|
||||||
{
|
{
|
||||||
foreach ($types as $id => $type)
|
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 (empty($user_notifications[$id]))
|
||||||
if (!$row)
|
|
||||||
{
|
{
|
||||||
// No rows at all, default to ''
|
// No rows at all, default to ''
|
||||||
$subscriptions[$id] = array('');
|
$subscriptions[$id] = array('');
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
do
|
foreach ($user_notifications[$id] as $user_notification)
|
||||||
{
|
{
|
||||||
if (!$row['notify'])
|
if (!$user_notification['notify'])
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -618,12 +641,9 @@ class manager
|
||||||
$subscriptions[$id] = array();
|
$subscriptions[$id] = array();
|
||||||
}
|
}
|
||||||
|
|
||||||
$subscriptions[$id][] = $row['method'];
|
$subscriptions[$id][] = $user_notification['method'];
|
||||||
}
|
}
|
||||||
while ($row = $this->db->sql_fetchrow($result));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->db->sql_freeresult($result);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -71,6 +71,27 @@ class phpbb_notification_test extends phpbb_tests_notification_base
|
||||||
|
|
||||||
public function test_subscriptions()
|
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->notifications->delete_subscription('post', 0, '', 2);
|
||||||
|
|
||||||
$this->assertArrayNotHasKey('post', $this->notifications->get_global_subscriptions(2));
|
$this->assertArrayNotHasKey('post', $this->notifications->get_global_subscriptions(2));
|
||||||
|
|
Loading…
Add table
Reference in a new issue