mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-28 14:18:52 +00:00
Performance increase for get_username_string() (Bug #37545 - Patch by BartVB)
git-svn-id: file:///svn/phpbb/branches/phpBB-3_0_0@9148 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
parent
30021ca5bc
commit
f766dccc3b
2 changed files with 46 additions and 48 deletions
|
@ -104,6 +104,7 @@
|
||||||
<li>[Change] Better handling and finer control for custom profile fields visibility options. (Patch by Highway of Life)</li>
|
<li>[Change] Better handling and finer control for custom profile fields visibility options. (Patch by Highway of Life)</li>
|
||||||
<li>[Change] Performance increase for format_date() (Bug #37575 - Patch by BartVB)</li>
|
<li>[Change] Performance increase for format_date() (Bug #37575 - Patch by BartVB)</li>
|
||||||
<li>[Change] Changed prosilver date separator from 'on' to '»'</li>
|
<li>[Change] Changed prosilver date separator from 'on' to '»'</li>
|
||||||
|
<li>[Change] Performance increase for get_username_string() (Bug #37545 - Patch by BartVB)</li>
|
||||||
<li>[Feature] Added 'AGO' setting to relative date strings. For example: posted 14 minutes ago. (Patch by BartVB)</li>
|
<li>[Feature] Added 'AGO' setting to relative date strings. For example: posted 14 minutes ago. (Patch by BartVB)</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
|
|
@ -1144,6 +1144,7 @@ function truncate_string($string, $max_length = 60, $max_store_length = 255, $al
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get username details for placing into templates.
|
* Get username details for placing into templates.
|
||||||
|
* This function caches all modes on first call, except for no_profile - determined by $user_id/$guest_username combination.
|
||||||
*
|
*
|
||||||
* @param string $mode Can be 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) or no_profile (the same as full but forcing no profile link)
|
* @param string $mode Can be 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) or no_profile (the same as full but forcing no profile link)
|
||||||
* @param int $user_id The users id
|
* @param int $user_id The users id
|
||||||
|
@ -1153,12 +1154,30 @@ function truncate_string($string, $max_length = 60, $max_store_length = 255, $al
|
||||||
* @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 string $custom_profile_url optional parameter to specify a profile url. The user id get appended to this url as &u={user_id}
|
||||||
*
|
*
|
||||||
* @return string A string consisting of what is wanted based on $mode.
|
* @return string A string consisting of what is wanted based on $mode.
|
||||||
|
* @author BartVB, Acyd Burn
|
||||||
*/
|
*/
|
||||||
function get_username_string($mode, $user_id, $username, $username_colour = '', $guest_username = false, $custom_profile_url = false)
|
function get_username_string($mode, $user_id, $username, $username_colour = '', $guest_username = false, $custom_profile_url = false)
|
||||||
{
|
{
|
||||||
|
static $_profile_cache;
|
||||||
|
static $_base_profile_url;
|
||||||
|
|
||||||
|
$cache_key = $user_id . (string) $guest_username;
|
||||||
|
|
||||||
|
// If the get_username_string() function had been executed once with an (to us) unkown mode, all modes are pre-filled and we can just grab it.
|
||||||
|
if (isset($_profile_cache[$cache_key][$mode]))
|
||||||
|
{
|
||||||
|
// If the mode is 'no_profile', we simply construct the TPL code due to calls to this mode being very very rare
|
||||||
|
if ($mode == 'no_profile')
|
||||||
|
{
|
||||||
|
$tpl = (!$_profile_cache[$cache_key][$mode]['colour']) ? '{USERNAME}' : '<span style="color: {USERNAME_COLOUR};" class="username-coloured">{USERNAME}</span>';
|
||||||
|
return str_replace(array('{USERNAME_COLOUR}', '{USERNAME}'), array($_profile_cache[$cache_key][$mode]['colour'], $_profile_cache[$cache_key][$mode]['username']), $tpl);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $_profile_cache[$cache_key][$mode];
|
||||||
|
}
|
||||||
|
|
||||||
global $phpbb_root_path, $phpEx, $user, $auth;
|
global $phpbb_root_path, $phpEx, $user, $auth;
|
||||||
|
|
||||||
$profile_url = '';
|
|
||||||
$username_colour = ($username_colour) ? '#' . $username_colour : '';
|
$username_colour = ($username_colour) ? '#' . $username_colour : '';
|
||||||
|
|
||||||
if ($guest_username === false)
|
if ($guest_username === false)
|
||||||
|
@ -1170,64 +1189,42 @@ function get_username_string($mode, $user_id, $username, $username_colour = '',
|
||||||
$username = ($user_id && $user_id != ANONYMOUS) ? $username : ((!empty($guest_username)) ? $guest_username : $user->lang['GUEST']);
|
$username = ($user_id && $user_id != ANONYMOUS) ? $username : ((!empty($guest_username)) ? $guest_username : $user->lang['GUEST']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only show the link if not anonymous
|
// Build cache for all modes
|
||||||
if ($mode != 'no_profile' && $user_id && $user_id != ANONYMOUS)
|
$_profile_cache[$cache_key]['colour'] = $username_colour;
|
||||||
|
$_profile_cache[$cache_key]['username'] = $username;
|
||||||
|
$_profile_cache[$cache_key]['no_profile'] = true;
|
||||||
|
|
||||||
|
// Profile url - only show if not anonymous and permission to view profile if registered user
|
||||||
|
// For anonymous the link leads to a login page.
|
||||||
|
if ($user_id && $user_id != ANONYMOUS && ($user->data['user_id'] == ANONYMOUS || $auth->acl_get('u_viewprofile')))
|
||||||
{
|
{
|
||||||
// Do not show the link if the user is already logged in but do not have u_viewprofile permissions (relevant for bots mostly).
|
if (empty($_base_profile_url))
|
||||||
// For all others the link leads to a login page or the profile.
|
|
||||||
if ($user->data['user_id'] != ANONYMOUS && !$auth->acl_get('u_viewprofile'))
|
|
||||||
{
|
{
|
||||||
$profile_url = '';
|
$_base_profile_url = append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=viewprofile&u={USER_ID}');
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$profile_url = ($custom_profile_url !== false) ? $custom_profile_url . '&u=' . (int) $user_id : append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=viewprofile&u=' . (int) $user_id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$profile_url = ($custom_profile_url !== false) ? $custom_profile_url . '&u=' . (int) $user_id : str_replace('={USER_ID}', '=' . (int) $user_id, $_base_profile_url);
|
||||||
|
$tpl = (!$username_colour) ? '<a href="{PROFILE_URL}">{USERNAME}</a>' : '<a href="{PROFILE_URL}" style="color: {USERNAME_COLOUR};" class="username-coloured">{USERNAME}</a>';
|
||||||
|
$_profile_cache[$cache_key]['full'] = str_replace(array('{PROFILE_URL}', '{USERNAME_COLOUR}', '{USERNAME}'), array($profile_url, $username_colour, $username), $tpl);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
$tpl = (!$username_colour) ? '{USERNAME}' : '<span style="color: {USERNAME_COLOUR};" class="username-coloured">{USERNAME}</span>';
|
||||||
|
$_profile_cache[$cache_key]['full'] = str_replace(array('{USERNAME_COLOUR}', '{USERNAME}'), array($username_colour, $username), $tpl);
|
||||||
$profile_url = '';
|
$profile_url = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ($mode)
|
// Use the profile url from above
|
||||||
|
$_profile_cache[$cache_key]['profile'] = $profile_url;
|
||||||
|
|
||||||
|
// If - by any chance - no_profile is called before any other mode, we need to do the calculation here
|
||||||
|
if ($mode == 'no_profile')
|
||||||
{
|
{
|
||||||
case 'profile':
|
$tpl = (!$_profile_cache[$cache_key]['colour']) ? '{USERNAME}' : '<span style="color: {USERNAME_COLOUR};" class="username-coloured">{USERNAME}</span>';
|
||||||
return $profile_url;
|
return str_replace(array('{USERNAME_COLOUR}', '{USERNAME}'), array($_profile_cache[$cache_key]['colour'], $_profile_cache[$cache_key]['username']), $tpl);
|
||||||
break;
|
|
||||||
|
|
||||||
case 'username':
|
|
||||||
return $username;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'colour':
|
|
||||||
return $username_colour;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'no_profile':
|
|
||||||
case 'full':
|
|
||||||
default:
|
|
||||||
|
|
||||||
$tpl = '';
|
|
||||||
if (!$profile_url && !$username_colour)
|
|
||||||
{
|
|
||||||
$tpl = '{USERNAME}';
|
|
||||||
}
|
|
||||||
else if (!$profile_url && $username_colour)
|
|
||||||
{
|
|
||||||
$tpl = '<span style="color: {USERNAME_COLOUR};" class="username-coloured">{USERNAME}</span>';
|
|
||||||
}
|
|
||||||
else if ($profile_url && !$username_colour)
|
|
||||||
{
|
|
||||||
$tpl = '<a href="{PROFILE_URL}">{USERNAME}</a>';
|
|
||||||
}
|
|
||||||
else if ($profile_url && $username_colour)
|
|
||||||
{
|
|
||||||
$tpl = '<a href="{PROFILE_URL}" style="color: {USERNAME_COLOUR};" class="username-coloured">{USERNAME}</a>';
|
|
||||||
}
|
|
||||||
|
|
||||||
return str_replace(array('{PROFILE_URL}', '{USERNAME_COLOUR}', '{USERNAME}'), array($profile_url, $username_colour, $username), $tpl);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $_profile_cache[$cache_key][$mode];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue