1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-08-09 02:06:32 +02:00

[ticket/11103] Create user loader class, update for DIC

Create a very basic user loader class to handle querying/storing
user data in a centralized location.

Use DIC collection service for notification types/methods.

Cleanup unused dependencies.

Fix some other issues.

PHPBB3-11103
This commit is contained in:
Nathan Guse
2012-11-20 18:14:48 -06:00
parent d2187424da
commit 2afb8b9df8
30 changed files with 518 additions and 325 deletions

View File

@@ -7,8 +7,6 @@
*
*/
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* @ignore
*/
@@ -23,30 +21,24 @@ if (!defined('IN_PHPBB'))
*/
class phpbb_notification_manager
{
/** @var array */
protected $notification_types = null;
/** @var array */
protected $notification_methods = null;
/** @var ContainerBuilder */
protected $phpbb_container = null;
/** @var phpbb_user_loader */
protected $user_loader = null;
/** @var dbal */
protected $db = null;
/** @var phpbb_cache_service */
protected $cache = null;
/** @var phpbb_template */
protected $template = null;
/** @var phpbb_extension_manager */
protected $extension_manager = null;
/** @var phpbb_user */
protected $user = null;
/** @var phpbb_auth */
protected $auth = null;
/** @var phpbb_config */
protected $config = null;
/** @var string */
protected $phpbb_root_path = null;
@@ -59,23 +51,15 @@ class phpbb_notification_manager
/** @var string */
protected $user_notifications_table = null;
/**
* Users loaded from the DB
*
* @var array Array of user data that we've loaded from the DB
*/
protected $users = array();
public function __construct(ContainerBuilder $phpbb_container, dbal $db, phpbb_cache_driver_interface $cache, phpbb_template $template, phpbb_extension_manager $extension_manager, $user, phpbb_auth $auth, phpbb_config $config, $phpbb_root_path, $php_ext, $notifications_table, $user_notifications_table)
public function __construct($notification_types, $notification_methods, $phpbb_container, phpbb_user_loader $user_loader, dbal $db, $user, $phpbb_root_path, $php_ext, $notifications_table, $user_notifications_table)
{
$this->notification_types = $notification_types;
$this->notification_methods = $notification_methods;
$this->phpbb_container = $phpbb_container;
$this->user_loader = $user_loader;
$this->db = $db;
$this->cache = $cache;
$this->template = $template;
$this->extension_manager = $extension_manager;
$this->user = $user;
$this->auth = $auth;
$this->config = $config;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
@@ -209,7 +193,7 @@ class phpbb_notification_manager
$notifications[$row['notification_id']] = $notification;
}
$this->load_users($user_ids);
$this->user_loader->load_users($user_ids);
// Allow each type to load its own special items
foreach ($load_special as $item_type => $data)
@@ -334,7 +318,7 @@ class phpbb_notification_manager
return $notified_users;
}
$item_id = $item_type::get_item_id($data);
$item_id = $this->get_item_type_class($item_type)->get_item_id($data);
// find out which users want to receive this type of notification
$notify_users = $this->get_item_type_class($item_type)->find_users_for_notification($data, $options);
@@ -363,7 +347,7 @@ class phpbb_notification_manager
return;
}
$item_id = $item_type::get_item_id($data);
$item_id = $this->get_item_type_class($item_type)->get_item_id($data);
$user_ids = array();
$notification_objects = $notification_methods = array();
@@ -428,7 +412,7 @@ class phpbb_notification_manager
$this->db->sql_multi_insert($this->notifications_table, $new_rows);
// We need to load all of the users to send notifications
$this->load_users($user_ids);
$this->user_loader->load_users($user_ids);
// run the queue for each method to send notifications
foreach ($notification_methods as $method)
@@ -467,7 +451,7 @@ class phpbb_notification_manager
}
}
$item_id = $item_type::get_item_id($data);
$item_id = $notification->get_item_id($data);
$update_array = $notification->create_update_array($data);
$sql = 'UPDATE ' . $this->notifications_table . '
@@ -511,7 +495,7 @@ class phpbb_notification_manager
{
$subscription_types = array();
foreach ($this->phpbb_container->findTaggedServiceIds('notification.type') as $type_name => $data)
foreach ($this->notification_types as $type_name => $data)
{
$type = $this->get_item_type_class($type_name);
@@ -547,7 +531,7 @@ class phpbb_notification_manager
{
$subscription_methods = array();
foreach ($this->phpbb_container->findTaggedServiceIds('notification.method') as $method_name => $data)
foreach ($this->notification_methods as $method_name => $data)
{
$method = $this->get_method_class($method_name);
@@ -758,53 +742,12 @@ class phpbb_notification_manager
$this->db->sql_query($sql);
}
/**
* Load user helper
*
* @param array $user_ids
*/
public function load_users($user_ids)
{
$user_ids[] = ANONYMOUS;
// Load the users
$user_ids = array_unique($user_ids);
// Do not load users we already have in $this->users
$user_ids = array_diff($user_ids, array_keys($this->users));
if (sizeof($user_ids))
{
$sql = 'SELECT *
FROM ' . USERS_TABLE . '
WHERE ' . $this->db->sql_in_set('user_id', $user_ids);
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
$this->users[$row['user_id']] = $row;
}
$this->db->sql_freeresult($result);
}
}
/**
* Get a user row from our users cache
*
* @param int $user_id
* @return array
*/
public function get_user($user_id)
{
return (isset($this->users[$user_id])) ? $this->users[$user_id] : $this->users[ANONYMOUS];
}
/**
* Helper to get the notifications item type class and set it up
*/
public function get_item_type_class($item_type, $data = array())
{
$item = $this->phpbb_container->get($item_type);
$item = $this->load_object('notification.type.' . $item_type);
$item->set_initial_data($data);
@@ -816,6 +759,32 @@ class phpbb_notification_manager
*/
public function get_method_class($method_name)
{
return $this->phpbb_container->get($method_name);
return $this->load_object('notification.method.' . $method_name);
}
/**
* Helper to load objects (notification types/methods)
*/
protected function load_object($object_name)
{
// Here we cannot just use ContainerBuilder->get(name)
// The reason for this is because get handles services
// which are initialized once and shared. Here we need
// separate new objects because we pass around objects
// that store row data in each object, which would lead
// to over-writing of data if we used get()
$parameterBag = $this->phpbb_container->getParameterBag();
$definition = $this->phpbb_container->getDefinition($object_name);
$arguments = $this->phpbb_container->resolveServices(
$parameterBag->unescapeValue($parameterBag->resolveValue($definition->getArguments()))
);
$r = new \ReflectionClass($parameterBag->resolveValue($definition->getClass()));
$object = null === $r->getConstructor() ? $r->newInstance() : $r->newInstanceArgs($arguments);
$object->set_notification_manager($this);
return $object;
}
}

View File

@@ -24,6 +24,9 @@ abstract class phpbb_notification_method_base implements phpbb_notification_meth
/** @var phpbb_notification_manager */
protected $notification_manager = null;
/** @var phpbb_user_loader */
protected $user_loader = null;
/** @var dbal */
protected $db = null;
@@ -58,13 +61,11 @@ abstract class phpbb_notification_method_base implements phpbb_notification_meth
*/
protected $queue = array();
public function __construct(phpbb_notification_manager $notification_manager, dbal $db, phpbb_cache_driver_interface $cache, phpbb_template $template, phpbb_extension_manager $extension_manager, $user, phpbb_auth $auth, phpbb_config $config, $phpbb_root_path, $php_ext)
public function __construct(phpbb_user_loader $user_loader, dbal $db, phpbb_cache_driver_interface $cache, $user, phpbb_auth $auth, phpbb_config $config, $phpbb_root_path, $php_ext)
{
$this->notification_manager = $notification_manager;
$this->user_loader = $user_loader;
$this->db = $db;
$this->cache = $cache;
$this->template = $template;
$this->extension_manager = $extension_manager;
$this->user = $user;
$this->auth = $auth;
$this->config = $config;
@@ -72,6 +73,11 @@ abstract class phpbb_notification_method_base implements phpbb_notification_meth
$this->php_ext = $php_ext;
}
public function set_notification_manager(phpbb_notification_manager $notification_manager)
{
$this->notification_manager = $notification_manager;
}
/**
* Add a notification to the queue
*

View File

@@ -82,7 +82,7 @@ class phpbb_notification_method_email extends phpbb_notification_method_base
$banned_users = phpbb_get_banned_user_ids($user_ids);
// Load all the users we need
$this->notification_manager->load_users($user_ids);
$this->user_loader->load_users($user_ids);
// Load the messenger
if (!class_exists('messenger'))
@@ -100,7 +100,7 @@ class phpbb_notification_method_email extends phpbb_notification_method_base
continue;
}
$user = $this->notification_manager->get_user($notification->user_id);
$user = $this->user_loader->get_user($notification->user_id);
if ($user['user_type'] == USER_IGNORE || in_array($notification->user_id, $banned_users))
{

View File

@@ -24,6 +24,9 @@ abstract class phpbb_notification_type_base implements phpbb_notification_type_i
/** @var phpbb_notification_manager */
protected $notification_manager = null;
/** @var phpbb_user_loader */
protected $user_loader = null;
/** @var dbal */
protected $db = null;
@@ -33,9 +36,6 @@ abstract class phpbb_notification_type_base implements phpbb_notification_type_i
/** @var phpbb_template */
protected $template = null;
/** @var phpbb_extension_manager */
protected $extension_manager = null;
/** @var phpbb_user */
protected $user = null;
@@ -84,13 +84,11 @@ abstract class phpbb_notification_type_base implements phpbb_notification_type_i
*/
private $data = array();
public function __construct(phpbb_notification_manager $notification_manager, dbal $db, phpbb_cache_driver_interface $cache, phpbb_template $template, phpbb_extension_manager $extension_manager, $user, phpbb_auth $auth, phpbb_config $config, $phpbb_root_path, $php_ext, $notifications_table, $user_notifications_table)
public function __construct(phpbb_user_loader $user_loader, dbal $db, phpbb_cache_driver_interface $cache, $user, phpbb_auth $auth, phpbb_config $config, $phpbb_root_path, $php_ext, $notifications_table, $user_notifications_table)
{
$this->notification_manager = $notification_manager;
$this->user_loader = $user_loader;
$this->db = $db;
$this->cache = $cache;
$this->template = $template;
$this->extension_manager = $extension_manager;
$this->user = $user;
$this->auth = $auth;
$this->config = $config;
@@ -102,6 +100,11 @@ abstract class phpbb_notification_type_base implements phpbb_notification_type_i
$this->user_notifications_table = $user_notifications_table;
}
public function set_notification_manager(phpbb_notification_manager $notification_manager)
{
$this->notification_manager = $notification_manager;
}
/**
* Set initial data from the database
*
@@ -357,7 +360,7 @@ abstract class phpbb_notification_type_base implements phpbb_notification_type_i
$rowset = $resulting_user_ids = array();
$sql = 'SELECT user_id, method, notify
FROM ' . USER_NOTIFICATIONS_TABLE . '
FROM ' . $this->user_notifications_table . '
WHERE ' . $this->db->sql_in_set('user_id', $user_ids) . "
AND item_type = '" . $this->db->sql_escape($options['item_type']) . "'
AND item_id = " . (int) $options['item_id'];
@@ -394,24 +397,6 @@ abstract class phpbb_notification_type_base implements phpbb_notification_type_i
return $rowset;
}
/**
* Get avatar helper
*
* @param int $user_id
* @return string
*/
protected function get_user_avatar($user_id)
{
$user = $this->notification_manager->get_user($user_id);
if (!function_exists('get_user_avatar'))
{
include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext);
}
return get_user_avatar($user['user_avatar'], $user['user_avatar_type'], $user['user_avatar_width'], $user['user_avatar_height'], $user['username'], false, 'notifications-avatar');
}
/**
* Mark this item read/unread helper
*
@@ -435,7 +420,7 @@ abstract class phpbb_notification_type_base implements phpbb_notification_type_i
return $where;
}
$sql = 'UPDATE ' . NOTIFICATIONS_TABLE . '
$sql = 'UPDATE ' . $this->notifications_table . '
SET unread = ' . (int) $this->unread . '
WHERE ' . $where;
$this->db->sql_query($sql);

View File

@@ -102,7 +102,7 @@ class phpbb_notification_type_bookmark extends phpbb_notification_type_post
// Try to find the users who already have been notified about replies and have not read the topic since and just update their notifications
$update_notifications = array();
$sql = 'SELECT *
FROM ' . NOTIFICATIONS_TABLE . "
FROM ' . $this->notifications_table . "
WHERE item_type = '" . $this->get_type() . "'
AND item_parent_id = " . (int) self::get_item_parent_id($post) . '
AND unread = 1
@@ -114,7 +114,7 @@ class phpbb_notification_type_bookmark extends phpbb_notification_type_post
unset($notify_users[$row['user_id']]);
$notification = $this->notification_manager->get_item_type_class($this->get_type(), $row);
$sql = 'UPDATE ' . NOTIFICATIONS_TABLE . '
$sql = 'UPDATE ' . $this->notifications_table . '
SET ' . $this->db->sql_build_array('UPDATE', $notification->add_responders($post)) . '
WHERE notification_id = ' . $row['notification_id'];
$this->db->sql_query($sql);

View File

@@ -92,7 +92,7 @@ class phpbb_notification_type_pm extends phpbb_notification_type_base
unset($pm['recipients'][$pm['from_user_id']]);
$this->notification_manager->load_users(array_keys($pm['recipients']));
$this->user_loader->load_users(array_keys($pm['recipients']));
return $this->check_user_notification_options(array_keys($pm['recipients']), $options);
}
@@ -102,7 +102,7 @@ class phpbb_notification_type_pm extends phpbb_notification_type_base
*/
public function get_avatar()
{
return $this->get_user_avatar($this->get_data('from_user_id'));
return $this->user_loader->get_avatar($this->get_data('from_user_id'));
}
/**
@@ -112,7 +112,7 @@ class phpbb_notification_type_pm extends phpbb_notification_type_base
*/
public function get_title()
{
$user_data = $this->notification_manager->get_user($this->get_data('from_user_id'));
$user_data = $this->user_loader->get_user($this->get_data('from_user_id'));
$username = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']);
@@ -136,7 +136,7 @@ class phpbb_notification_type_pm extends phpbb_notification_type_base
*/
public function get_email_template_variables()
{
$user_data = $this->notification_manager->get_user($this->get_data('from_user_id'));
$user_data = $this->user_loader->get_user($this->get_data('from_user_id'));
return array(
'AUTHOR_NAME' => htmlspecialchars_decode($user_data['username']),

View File

@@ -123,7 +123,7 @@ class phpbb_notification_type_post extends phpbb_notification_type_base
// Try to find the users who already have been notified about replies and have not read the topic since and just update their notifications
$update_notifications = array();
$sql = 'SELECT *
FROM ' . NOTIFICATIONS_TABLE . "
FROM ' . $this->notifications_table . "
WHERE item_type = '" . $this->get_type() . "'
AND item_parent_id = " . (int) self::get_item_parent_id($post) . '
AND unread = 1
@@ -135,7 +135,7 @@ class phpbb_notification_type_post extends phpbb_notification_type_base
unset($notify_users[$row['user_id']]);
$notification = $this->notification_manager->get_item_type_class($this->get_type(), $row);
$sql = 'UPDATE ' . NOTIFICATIONS_TABLE . '
$sql = 'UPDATE ' . $this->notifications_table . '
SET ' . $this->db->sql_build_array('UPDATE', $notification->add_responders($post)) . '
WHERE notification_id = ' . $row['notification_id'];
$this->db->sql_query($sql);
@@ -150,7 +150,7 @@ class phpbb_notification_type_post extends phpbb_notification_type_base
*/
public function get_avatar()
{
return $this->get_user_avatar($this->get_data('poster_id'));
return $this->user_loader->get_avatar($this->get_data('poster_id'));
}
/**
@@ -181,7 +181,7 @@ class phpbb_notification_type_post extends phpbb_notification_type_base
}
else
{
$user_data = $this->notification_manager->get_user($responder['poster_id']);
$user_data = $this->user_loader->get_user($responder['poster_id']);
$usernames[] = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']);
}
@@ -217,7 +217,7 @@ class phpbb_notification_type_post extends phpbb_notification_type_base
}
else
{
$user_data = $this->notification_manager->get_user($this->get_data('poster_id'));
$user_data = $this->user_loader->get_user($this->get_data('poster_id'));
$username = get_username_string('username', $user_data['user_id'], $user_data['username'], $user_data['user_colour']);
}

View File

@@ -121,7 +121,7 @@ class phpbb_notification_type_quote extends phpbb_notification_type_post
// Try to find the users who already have been notified about replies and have not read the topic since and just update their notifications
$update_notifications = array();
$sql = 'SELECT *
FROM ' . NOTIFICATIONS_TABLE . "
FROM ' . $this->notifications_table . "
WHERE item_type = '" . $this->get_type() . "'
AND item_parent_id = " . (int) self::get_item_parent_id($post) . '
AND unread = 1
@@ -133,7 +133,7 @@ class phpbb_notification_type_quote extends phpbb_notification_type_post
unset($notify_users[$row['user_id']]);
$notification = $this->notification_manager->get_item_type_class($this->get_type(), $row);
$sql = 'UPDATE ' . NOTIFICATIONS_TABLE . '
$sql = 'UPDATE ' . $this->notifications_table . '
SET ' . $this->db->sql_build_array('UPDATE', $notification->add_responders($post)) . '
WHERE notification_id = ' . $row['notification_id'];
$this->db->sql_query($sql);
@@ -152,7 +152,7 @@ class phpbb_notification_type_quote extends phpbb_notification_type_post
{
$old_notifications = array();
$sql = 'SELECT user_id
FROM ' . NOTIFICATIONS_TABLE . "
FROM ' . $this->notifications_table . "
WHERE item_type = '" . $this->get_type() . "'
AND item_id = " . self::get_item_id($post) . '
AND is_enabled = 1';
@@ -182,7 +182,7 @@ class phpbb_notification_type_quote extends phpbb_notification_type_post
// Remove the necessary notifications
if (!empty($remove_notifications))
{
$sql = 'DELETE FROM ' . NOTIFICATIONS_TABLE . "
$sql = 'DELETE FROM ' . $this->notifications_table . "
WHERE item_type = '" . $this->get_type() . "'
AND item_id = " . self::get_item_id($post) . '
AND ' . $this->db->sql_in_set('user_id', $remove_notifications);
@@ -210,7 +210,7 @@ class phpbb_notification_type_quote extends phpbb_notification_type_post
*/
public function get_email_template_variables()
{
$user_data = $this->notification_manager->get_user($this->get_data('poster_id'));
$user_data = $this->user_loader->get_user($this->get_data('poster_id'));
return array_merge(parent::get_email_template_variables(), array(
'AUTHOR_NAME' => htmlspecialchars_decode($user_data['username']),

View File

@@ -160,7 +160,7 @@ class phpbb_notification_type_report_pm extends phpbb_notification_type_pm
{
$this->user->add_lang('mcp');
$user_data = $this->notification_manager->get_user($this->get_data('reporter_id'));
$user_data = $this->user_loader->get_user($this->get_data('reporter_id'));
$username = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']);
@@ -197,7 +197,7 @@ class phpbb_notification_type_report_pm extends phpbb_notification_type_pm
*/
public function get_avatar()
{
return $this->get_user_avatar($this->get_data('reporter_id'));
return $this->user_loader->get_avatar($this->get_data('reporter_id'));
}
/**

View File

@@ -106,7 +106,7 @@ class phpbb_notification_type_report_pm_closed extends phpbb_notification_type_p
*/
public function get_title()
{
$user_data = $this->notification_manager->get_user($this->get_data('closer_id'));
$user_data = $this->user_loader->get_user($this->get_data('closer_id'));
$username = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']);

View File

@@ -127,7 +127,7 @@ class phpbb_notification_type_report_post extends phpbb_notification_type_post_i
{
$this->user->add_lang('mcp');
$user_data = $this->notification_manager->get_user($this->get_data('reporter_id'));
$user_data = $this->user_loader->get_user($this->get_data('reporter_id'));
$username = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']);
@@ -164,7 +164,7 @@ class phpbb_notification_type_report_post extends phpbb_notification_type_post_i
*/
public function get_avatar()
{
return $this->get_user_avatar($this->get_data('reporter_id'));
return $this->user_loader->get_avatar($this->get_data('reporter_id'));
}
/**

View File

@@ -106,7 +106,7 @@ class phpbb_notification_type_report_post_closed extends phpbb_notification_type
*/
public function get_title()
{
$user_data = $this->notification_manager->get_user($this->get_data('closer_id'));
$user_data = $this->user_loader->get_user($this->get_data('closer_id'));
$username = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']);
@@ -122,7 +122,7 @@ class phpbb_notification_type_report_post_closed extends phpbb_notification_type
*/
public function get_avatar()
{
return $this->get_user_avatar($this->get_data('closer_id'));
return $this->user_loader->get_avatar($this->get_data('closer_id'));
}
/**

View File

@@ -126,7 +126,7 @@ class phpbb_notification_type_topic extends phpbb_notification_type_base
*/
public function get_avatar()
{
return $this->get_user_avatar($this->get_data('poster_id'));
return $this->user_loader->get_avatar($this->get_data('poster_id'));
}
/**
@@ -142,7 +142,7 @@ class phpbb_notification_type_topic extends phpbb_notification_type_base
}
else
{
$user_data = $this->notification_manager->get_user($this->get_data('poster_id'));
$user_data = $this->user_loader->get_user($this->get_data('poster_id'));
$username = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']);
}
@@ -178,7 +178,7 @@ class phpbb_notification_type_topic extends phpbb_notification_type_base
}
else
{
$user_data = $this->notification_manager->get_user($this->get_data('poster_id'));
$user_data = $this->user_loader->get_user($this->get_data('poster_id'));
$username = get_username_string('username', $user_data['user_id'], $user_data['username'], $user_data['user_colour']);
}