[ticket/11103] Some improvements to the user loader

PHPBB3-11103
This commit is contained in:
Nathan Guse 2012-12-08 18:40:41 -06:00
parent 53decec6e3
commit 37565f37e4
9 changed files with 129 additions and 41 deletions

View file

@ -112,9 +112,7 @@ class phpbb_notification_type_pm extends phpbb_notification_type_base
*/ */
public function get_title() public function get_title()
{ {
$user_data = $this->user_loader->get_user($this->get_data('from_user_id')); $username = $this->user_loader->get_username($this->get_data('from_user_id'), 'no_profile');
$username = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']);
return $this->user->lang('NOTIFICATION_PM', $username, $this->get_data('message_subject')); return $this->user->lang('NOTIFICATION_PM', $username, $this->get_data('message_subject'));
} }

View file

@ -181,9 +181,7 @@ class phpbb_notification_type_post extends phpbb_notification_type_base
} }
else else
{ {
$user_data = $this->user_loader->get_user($responder['poster_id']); $usernames[] = $this->user_loader->get_username($responder['poster_id'], 'no_profile');
$usernames[] = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']);
} }
} }
@ -217,9 +215,7 @@ class phpbb_notification_type_post extends phpbb_notification_type_base
} }
else else
{ {
$user_data = $this->user_loader->get_user($this->get_data('poster_id')); $username = $this->user_loader->get_username($this->get_data('poster_id'), 'no_profile');
$username = get_username_string('username', $user_data['user_id'], $user_data['username'], $user_data['user_colour']);
} }
return array( return array(

View file

@ -160,9 +160,7 @@ class phpbb_notification_type_report_pm extends phpbb_notification_type_pm
{ {
$this->user->add_lang('mcp'); $this->user->add_lang('mcp');
$user_data = $this->user_loader->get_user($this->get_data('reporter_id')); $username = $this->user_loader->get_username($this->get_data('reporter_id'), 'no_profile');
$username = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']);
if ($this->get_data('report_text')) if ($this->get_data('report_text'))
{ {

View file

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

View file

@ -127,9 +127,7 @@ class phpbb_notification_type_report_post extends phpbb_notification_type_post_i
{ {
$this->user->add_lang('mcp'); $this->user->add_lang('mcp');
$user_data = $this->user_loader->get_user($this->get_data('reporter_id')); $username = $this->user_loader->get_username($this->get_data('reporter_id'), 'no_profile');
$username = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']);
if ($this->get_data('report_text')) if ($this->get_data('report_text'))
{ {

View file

@ -106,9 +106,7 @@ class phpbb_notification_type_report_post_closed extends phpbb_notification_type
*/ */
public function get_title() public function get_title()
{ {
$user_data = $this->user_loader->get_user($this->get_data('closer_id')); $username = $this->user_loader->get_username($this->get_data('closer_id'), 'no_profile');
$username = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']);
return $this->user->lang( return $this->user->lang(
$this->language_key, $this->language_key,

View file

@ -142,9 +142,7 @@ class phpbb_notification_type_topic extends phpbb_notification_type_base
} }
else else
{ {
$user_data = $this->user_loader->get_user($this->get_data('poster_id')); $username = $this->user_loader->get_username($this->get_data('poster_id'), 'no_profile');
$username = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']);
} }
return $this->user->lang( return $this->user->lang(
@ -178,9 +176,7 @@ class phpbb_notification_type_topic extends phpbb_notification_type_base
} }
else else
{ {
$user_data = $this->user_loader->get_user($this->get_data('poster_id')); $username = $this->user_loader->get_username($this->get_data('poster_id'), 'no_profile');
$username = get_username_string('username', $user_data['user_id'], $user_data['username'], $user_data['user_colour']);
} }
return array( return array(

View file

@ -91,27 +91,100 @@ class phpbb_user_loader
} }
} }
/**
* Load a user by username
*
* Stores the full data in the user cache so they do not need to be loaded again
* Returns the user id so you may use get_user() from the returned value
*
* @param string $username Raw username to load (will be cleaned)
* @return int User ID for the username
*/
public function load_user_by_username($username)
{
$sql = 'SELECT *
FROM ' . $this->users_table . "
WHERE username_clean = '" . $this->db->sql_escape(utf8_clean_string($username)) . "'";
$result = $this->db->sql_query($sql);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
if ($row)
{
$this->users[$row['user_id']] = $row;
return $row['user_id'];
}
return ANONYMOUS;
}
/** /**
* Get a user row from our users cache * Get a user row from our users cache
* *
* @param int $user_id * @param int $user_id User ID of the user you want to retreive
* @param bool $query Should we query the database if this user has not yet been loaded?
* Typically this should be left as false and you should make sure
* you load users ahead of time with load_users()
* @return array|bool Row from the database of the user or Anonymous if the user wasn't loaded/does not exist * @return array|bool Row from the database of the user or Anonymous if the user wasn't loaded/does not exist
* or bool False if the anonymous user was not loaded * or bool False if the anonymous user was not loaded
*/ */
public function get_user($user_id) public function get_user($user_id, $query = false)
{ {
return (isset($this->users[$user_id])) ? $this->users[$user_id] : (isset($this->users[ANONYMOUS]) ? $this->users[ANONYMOUS] : false); if (isset($this->users[$user_id]))
{
return $this->users[$user_id];
}
// Query them if we must (if ANONYMOUS is sent as the user_id and we have not loaded Anonymous yet, we must load Anonymous as a last resort)
else if ($query || $user_id == ANONYMOUS)
{
$this->load_users(array($user_id));
return $this->get_user($user_id);
}
return $this->get_user(ANONYMOUS);
}
/**
* Get username
*
* @param int $user_id User ID of the user you want to retreive the username for
* @param string $mode The mode to load (same as get_username_string). One of the following:
* profile (for getting an url to the profile)
* username (for obtaining the username)
* colour (for obtaining the user colour)
* full (for obtaining a html string representing a coloured link to the users profile)
* no_profile (the same as full but forcing no profile link)
* @param string $guest_username Optional parameter to specify the guest username. It will be used in favor of the GUEST language variable then.
* @param string $custom_profile_url Optional parameter to specify a profile url. The user id get appended to this url as &u={user_id}
* @param bool $query Should we query the database if this user has not yet been loaded?
* Typically this should be left as false and you should make sure
* you load users ahead of time with load_users()
* @return string
*/
public function get_username($user_id, $mode, $guest_username = false, $custom_profile_url = false, $query = false)
{
if (!($user = $this->get_user($user_id, $query)))
{
return '';
}
return get_username_string($mode, $user['user_id'], $user['username'], $user['user_colour'], $guest_username, $custom_profile_url);
} }
/** /**
* Get avatar * Get avatar
* *
* @param int $user_id * @param int $user_id User ID of the user you want to retreive the avatar for
* @param bool $query Should we query the database if this user has not yet been loaded?
* Typically this should be left as false and you should make sure
* you load users ahead of time with load_users()
* @return string * @return string
*/ */
public function get_avatar($user_id) public function get_avatar($user_id, $query = false)
{ {
if (!($user = $this->get_user($user_id))) if (!($user = $this->get_user($user_id, $query)))
{ {
return ''; return '';
} }
@ -123,4 +196,36 @@ class phpbb_user_loader
return get_user_avatar($user['user_avatar'], $user['user_avatar_type'], $user['user_avatar_width'], $user['user_avatar_height']); return get_user_avatar($user['user_avatar'], $user['user_avatar_type'], $user['user_avatar_width'], $user['user_avatar_height']);
} }
/**
* Get rank
*
* @param int $user_id User ID of the user you want to retreive the rank for
* @param bool $query Should we query the database if this user has not yet been loaded?
* Typically this should be left as false and you should make sure
* you load users ahead of time with load_users()
* @return array Array with keys 'rank_title', 'rank_img', and 'rank_img_src'
*/
public function get_rank($user_id, $query = false)
{
if (!($user = $this->get_user($user_id, $query)))
{
return '';
}
if (!function_exists('get_user_rank'))
{
include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext);
}
$rank = array(
'rank_title',
'rank_img',
'rank_img_src',
);
get_user_rank($user['user_rank'], (($user['user_id'] == ANONYMOUS) ? false : $user['user_posts']), $rank['rank_title'], $rank['rank_img'], $rank['rank_img_src']);
return $rank;
}
} }

View file

@ -7,6 +7,8 @@
* *
*/ */
include_once(__DIR__ . '/../../phpBB/includes/utf/utf_tools.php');
class phpbb_user_lang_test extends phpbb_database_test_case class phpbb_user_lang_test extends phpbb_database_test_case
{ {
public function getDataSet() public function getDataSet()
@ -18,7 +20,7 @@ class phpbb_user_lang_test extends phpbb_database_test_case
{ {
$db = $this->new_dbal(); $db = $this->new_dbal();
$user_loader = new phpbb_user_loader($db, __DIR__ . '../../phpBB', 'php', 'phpbb_users'); $user_loader = new phpbb_user_loader($db, __DIR__ . '/../../phpBB/', 'php', 'phpbb_users');
$user_loader->load_users(array(2)); $user_loader->load_users(array(2));
@ -35,13 +37,12 @@ class phpbb_user_lang_test extends phpbb_database_test_case
$this->assertEquals(1, $user['user_id']); $this->assertEquals(1, $user['user_id']);
$this->assertEquals('Guest', $user['username']); $this->assertEquals('Guest', $user['username']);
$user_loader->load_users(array(3)); $user = $user_loader->get_user(3, true);
$this->assertEquals(3, $user['user_id']);
$this->assertEquals('Test', $user['username']);
$user = $user_loader->get_user(2); $user_id = $user_loader->load_user_by_username('Test');
$this->assertEquals(2, $user['user_id']); $user = $user_loader->get_user($user_id);
$this->assertEquals('Admin', $user['username']);
$user = $user_loader->get_user(3);
$this->assertEquals(3, $user['user_id']); $this->assertEquals(3, $user['user_id']);
$this->assertEquals('Test', $user['username']); $this->assertEquals('Test', $user['username']);
} }