Tutorial  Reduce attachments.php file resources usage.

Hi.

I use the attachment system quite often as an media system in forums. Take this site for example, I mainly use the system to manage images. I think it is unnecessary to load the whole global.php to serve its purpose, thus I decided to strip it out and only load what I find to be required for the script to properly work.

You can take a look to the attached images, basically the main visual difference should be the error system, which won't render the whole page, but users will be able to use the login form regardless.

This will save you from 2 queries (error load) up to 5 queries (successful load) by file load. If you take into account the number of loaded attachments by page load as n, you will save from n*2 to n*5 queries by page load. So, for example, if you are rendering 10 attached images in a page, you could save from 20 to 50 queries per page load.

This is enough for me to apply the changes.

Open attachment.php, and find:
require_once "./global.php";

Replace with:
$working_dir = dirname(__FILE__);
if(!$working_dir)
{
	$working_dir = '.';
}

$shutdown_queries = $shutdown_functions = array();

require_once $working_dir.'/inc/init.php';

$groupscache = $cache->read('usergroups');

if(!is_array($groupscache))
{
	$cache->update_usergroups();
	$groupscache = $cache->read('usergroups');
}

$current_page = my_strtolower(basename(THIS_SCRIPT));

if(isset($mybb->input['thumbnail']))
{
	define('NO_ONLINE', 1);
}

require_once MYBB_ROOT.'inc/class_session.php';
$session = new session;
$session->init();
$mybb->session = &$session;

$mybb->user['ismoderator'] = is_moderator(0, '', $mybb->user['uid']);

$mybb->post_code = generate_post_check();

if(isset($mybb->input['language']) && $lang->language_exists($mybb->get_input('language')) && verify_post_check($mybb->get_input('my_post_key'), true))
{
	$mybb->settings['bblanguage'] = $mybb->get_input('language');
	// If user is logged in, update their language selection with the new one
	if($mybb->user['uid'])
	{
		if(isset($mybb->cookies['mybblang']))
		{
			my_unsetcookie('mybblang');
		}

		$db->update_query('users', array('language' => $db->escape_string($mybb->settings['bblanguage'])), "uid = '{$mybb->user['uid']}'");
	}
	// Guest = cookie
	else
	{
		my_setcookie('mybblang', $mybb->settings['bblanguage']);
	}
	$mybb->user['language'] = $mybb->settings['bblanguage'];
}
else if(!$mybb->user['uid'] && !empty($mybb->cookies['mybblang']) && $lang->language_exists($mybb->cookies['mybblang']))
{
	$mybb->settings['bblanguage'] = $mybb->cookies['mybblang'];
}
else if(!isset($mybb->settings['bblanguage']))
{
	$mybb->settings['bblanguage'] = 'english';
}

$lang->set_language($mybb->settings['bblanguage']);

$lang->load('global');
$lang->load('messages');

if($mybb->cookies['lockoutexpiry'] && $mybb->cookies['lockoutexpiry'] < TIME_NOW)
{
	my_unsetcookie('lockoutexpiry');
}

Update: As of 1.8.23 the login form isn't displayed within the no permission page or container.
This post was last modified: 22 Jun, 2021, 11:05 pm by Omar G..
Files

Thumbnail(s)

There are currently no posts to display. Be the first one to leave a reply.

Current time: 10 Nov, 2024, 5:20 am