From b20d98a2294cb76fd298aae2b38d965568df17b9 Mon Sep 17 00:00:00 2001 From: javiexin Date: Sat, 17 Dec 2016 22:27:44 +0100 Subject: [PATCH 1/8] [ticket/14919] Do not directly use globals in acp_extensions PHPBB3-14919 --- phpBB/includes/acp/acp_extensions.php | 131 +++++++++++++------------- 1 file changed, 65 insertions(+), 66 deletions(-) diff --git a/phpBB/includes/acp/acp_extensions.php b/phpBB/includes/acp/acp_extensions.php index 5a2ded91e2..a2436c3f81 100644 --- a/phpBB/includes/acp/acp_extensions.php +++ b/phpBB/includes/acp/acp_extensions.php @@ -22,21 +22,22 @@ if (!defined('IN_PHPBB')) class acp_extensions { var $u_action; + var $tpl_name; - private $db; private $config; private $template; private $user; private $cache; private $log; private $request; + private $php_dispatcher; + private $ext_manager; function main() { // Start the page - global $config, $user, $template, $request, $phpbb_extension_manager, $db, $phpbb_root_path, $phpEx, $phpbb_log, $cache, $phpbb_dispatcher; + global $config, $user, $template, $request, $phpbb_extension_manager, $phpbb_root_path, $phpEx, $phpbb_log, $cache, $phpbb_dispatcher; - $this->db = $db; $this->config = $config; $this->template = $template; $this->user = $user; @@ -44,13 +45,14 @@ class acp_extensions $this->request = $request; $this->log = $phpbb_log; $this->phpbb_dispatcher = $phpbb_dispatcher; + $this->ext_manager = $phpbb_extension_manager; - $user->add_lang(array('install', 'acp/extensions', 'migrator')); + $this->user->add_lang(array('install', 'acp/extensions', 'migrator')); $this->page_title = 'ACP_EXTENSIONS'; - $action = $request->variable('action', 'list'); - $ext_name = $request->variable('ext_name', ''); + $action = $this->request->variable('action', 'list'); + $ext_name = $this->request->variable('ext_name', ''); // What is a safe limit of execution time? Half the max execution time should be safe. $safe_time_limit = (ini_get('max_execution_time') / 2); @@ -72,13 +74,13 @@ class acp_extensions extract($this->phpbb_dispatcher->trigger_event('core.acp_extensions_run_action', compact($vars))); // Cancel action - if ($request->is_set_post('cancel')) + if ($this->request->is_set_post('cancel')) { $action = 'list'; $ext_name = ''; } - if (in_array($action, array('enable', 'disable', 'delete_data')) && !check_link_hash($request->variable('hash', ''), $action . '.' . $ext_name)) + if (in_array($action, array('enable', 'disable', 'delete_data')) && !check_link_hash($this->request->variable('hash', ''), $action . '.' . $ext_name)) { trigger_error('FORM_INVALID', E_USER_WARNING); } @@ -86,7 +88,7 @@ class acp_extensions // If they've specified an extension, let's load the metadata manager and validate it. if ($ext_name) { - $md_manager = new \phpbb\extension\metadata_manager($ext_name, $config, $phpbb_extension_manager, $template, $user, $phpbb_root_path); + $md_manager = $this->ext_manager->create_extension_metadata_manager($ext_name, $this->template); try { @@ -110,12 +112,12 @@ class acp_extensions 'force_unstable' => $force_unstable, )); - confirm_box(false, $user->lang('EXTENSION_FORCE_UNSTABLE_CONFIRM'), $s_hidden_fields); + confirm_box(false, $this->user->lang('EXTENSION_FORCE_UNSTABLE_CONFIRM'), $s_hidden_fields); } else { - $config->set('extension_force_unstable', false); - trigger_error($user->lang['CONFIG_UPDATED'] . adm_back_link($this->u_action)); + $this->config->set('extension_force_unstable', false); + trigger_error($this->user->lang['CONFIG_UPDATED'] . adm_back_link($this->u_action)); } break; @@ -123,17 +125,17 @@ class acp_extensions default: if (confirm_box(true)) { - $config->set('extension_force_unstable', true); - trigger_error($user->lang['CONFIG_UPDATED'] . adm_back_link($this->u_action)); + $this->config->set('extension_force_unstable', true); + trigger_error($this->user->lang['CONFIG_UPDATED'] . adm_back_link($this->u_action)); } - $this->list_enabled_exts($phpbb_extension_manager); - $this->list_disabled_exts($phpbb_extension_manager); - $this->list_available_exts($phpbb_extension_manager); + $this->list_enabled_exts(); + $this->list_disabled_exts(); + $this->list_available_exts(); $this->template->assign_vars(array( 'U_VERSIONCHECK_FORCE' => $this->u_action . '&action=list&versioncheck_force=1', - 'FORCE_UNSTABLE' => $config['extension_force_unstable'], + 'FORCE_UNSTABLE' => $this->config['extension_force_unstable'], 'U_ACTION' => $this->u_action, )); @@ -143,28 +145,28 @@ class acp_extensions case 'enable_pre': if (!$md_manager->validate_dir()) { - trigger_error($user->lang['EXTENSION_DIR_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING); + trigger_error($this->user->lang['EXTENSION_DIR_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING); } if (!$md_manager->validate_enable()) { - trigger_error($user->lang['EXTENSION_NOT_AVAILABLE'] . adm_back_link($this->u_action), E_USER_WARNING); + trigger_error($this->user->lang['EXTENSION_NOT_AVAILABLE'] . adm_back_link($this->u_action), E_USER_WARNING); } - $extension = $phpbb_extension_manager->get_extension($ext_name); + $extension = $this->ext_manager->get_extension($ext_name); if (!$extension->is_enableable()) { - trigger_error($user->lang['EXTENSION_NOT_ENABLEABLE'] . adm_back_link($this->u_action), E_USER_WARNING); + trigger_error($this->user->lang['EXTENSION_NOT_ENABLEABLE'] . adm_back_link($this->u_action), E_USER_WARNING); } - if ($phpbb_extension_manager->is_enabled($ext_name)) + if ($this->ext_manager->is_enabled($ext_name)) { redirect($this->u_action); } $this->tpl_name = 'acp_ext_enable'; - $template->assign_vars(array( + $this->template->assign_vars(array( 'PRE' => true, 'L_CONFIRM_MESSAGE' => $this->user->lang('EXTENSION_ENABLE_CONFIRM', $md_manager->get_metadata('display-name')), 'U_ENABLE' => $this->u_action . '&action=enable&ext_name=' . urlencode($ext_name) . '&hash=' . generate_link_hash('enable.' . $ext_name), @@ -174,55 +176,55 @@ class acp_extensions case 'enable': if (!$md_manager->validate_dir()) { - trigger_error($user->lang['EXTENSION_DIR_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING); + trigger_error($this->user->lang['EXTENSION_DIR_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING); } if (!$md_manager->validate_enable()) { - trigger_error($user->lang['EXTENSION_NOT_AVAILABLE'] . adm_back_link($this->u_action), E_USER_WARNING); + trigger_error($this->user->lang['EXTENSION_NOT_AVAILABLE'] . adm_back_link($this->u_action), E_USER_WARNING); } - $extension = $phpbb_extension_manager->get_extension($ext_name); + $extension = $this->ext_manager->get_extension($ext_name); if (!$extension->is_enableable()) { - trigger_error($user->lang['EXTENSION_NOT_ENABLEABLE'] . adm_back_link($this->u_action), E_USER_WARNING); + trigger_error($this->user->lang['EXTENSION_NOT_ENABLEABLE'] . adm_back_link($this->u_action), E_USER_WARNING); } try { - while ($phpbb_extension_manager->enable_step($ext_name)) + while ($this->ext_manager->enable_step($ext_name)) { // Are we approaching the time limit? If so we want to pause the update and continue after refreshing if ((time() - $start_time) >= $safe_time_limit) { - $template->assign_var('S_NEXT_STEP', true); + $this->template->assign_var('S_NEXT_STEP', true); meta_refresh(0, $this->u_action . '&action=enable&ext_name=' . urlencode($ext_name) . '&hash=' . generate_link_hash('enable.' . $ext_name)); } } - $this->log->add('admin', $user->data['user_id'], $user->ip, 'LOG_EXT_ENABLE', time(), array($ext_name)); + $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_EXT_ENABLE', time(), array($ext_name)); } catch (\phpbb\db\migration\exception $e) { - $template->assign_var('MIGRATOR_ERROR', $e->getLocalisedMessage($user)); + $this->template->assign_var('MIGRATOR_ERROR', $e->getLocalisedMessage($this->user)); } $this->tpl_name = 'acp_ext_enable'; - $template->assign_vars(array( + $this->template->assign_vars(array( 'U_RETURN' => $this->u_action . '&action=list', )); break; case 'disable_pre': - if (!$phpbb_extension_manager->is_enabled($ext_name)) + if (!$this->ext_manager->is_enabled($ext_name)) { redirect($this->u_action); } $this->tpl_name = 'acp_ext_disable'; - $template->assign_vars(array( + $this->template->assign_vars(array( 'PRE' => true, 'L_CONFIRM_MESSAGE' => $this->user->lang('EXTENSION_DISABLE_CONFIRM', $md_manager->get_metadata('display-name')), 'U_DISABLE' => $this->u_action . '&action=disable&ext_name=' . urlencode($ext_name) . '&hash=' . generate_link_hash('disable.' . $ext_name), @@ -230,38 +232,38 @@ class acp_extensions break; case 'disable': - if (!$phpbb_extension_manager->is_enabled($ext_name)) + if (!$this->ext_manager->is_enabled($ext_name)) { redirect($this->u_action); } - while ($phpbb_extension_manager->disable_step($ext_name)) + while ($this->ext_manager->disable_step($ext_name)) { // Are we approaching the time limit? If so we want to pause the update and continue after refreshing if ((time() - $start_time) >= $safe_time_limit) { - $template->assign_var('S_NEXT_STEP', true); + $this->template->assign_var('S_NEXT_STEP', true); meta_refresh(0, $this->u_action . '&action=disable&ext_name=' . urlencode($ext_name) . '&hash=' . generate_link_hash('disable.' . $ext_name)); } } - $this->log->add('admin', $user->data['user_id'], $user->ip, 'LOG_EXT_DISABLE', time(), array($ext_name)); + $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_EXT_DISABLE', time(), array($ext_name)); $this->tpl_name = 'acp_ext_disable'; - $template->assign_vars(array( + $this->template->assign_vars(array( 'U_RETURN' => $this->u_action . '&action=list', )); break; case 'delete_data_pre': - if ($phpbb_extension_manager->is_enabled($ext_name)) + if ($this->ext_manager->is_enabled($ext_name)) { redirect($this->u_action); } $this->tpl_name = 'acp_ext_delete_data'; - $template->assign_vars(array( + $this->template->assign_vars(array( 'PRE' => true, 'L_CONFIRM_MESSAGE' => $this->user->lang('EXTENSION_DELETE_DATA_CONFIRM', $md_manager->get_metadata('display-name')), 'U_PURGE' => $this->u_action . '&action=delete_data&ext_name=' . urlencode($ext_name) . '&hash=' . generate_link_hash('delete_data.' . $ext_name), @@ -269,33 +271,33 @@ class acp_extensions break; case 'delete_data': - if ($phpbb_extension_manager->is_enabled($ext_name)) + if ($this->ext_manager->is_enabled($ext_name)) { redirect($this->u_action); } try { - while ($phpbb_extension_manager->purge_step($ext_name)) + while ($this->ext_manager->purge_step($ext_name)) { // Are we approaching the time limit? If so we want to pause the update and continue after refreshing if ((time() - $start_time) >= $safe_time_limit) { - $template->assign_var('S_NEXT_STEP', true); + $this->template->assign_var('S_NEXT_STEP', true); meta_refresh(0, $this->u_action . '&action=delete_data&ext_name=' . urlencode($ext_name) . '&hash=' . generate_link_hash('delete_data.' . $ext_name)); } } - $this->log->add('admin', $user->data['user_id'], $user->ip, 'LOG_EXT_PURGE', time(), array($ext_name)); + $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_EXT_PURGE', time(), array($ext_name)); } catch (\phpbb\db\migration\exception $e) { - $template->assign_var('MIGRATOR_ERROR', $e->getLocalisedMessage($user)); + $this->template->assign_var('MIGRATOR_ERROR', $e->getLocalisedMessage($this->user)); } $this->tpl_name = 'acp_ext_delete_data'; - $template->assign_vars(array( + $this->template->assign_vars(array( 'U_RETURN' => $this->u_action . '&action=list', )); break; @@ -306,9 +308,9 @@ class acp_extensions try { - $updates_available = $this->version_check($md_manager, $request->variable('versioncheck_force', false)); + $updates_available = $this->version_check($md_manager, $this->request->variable('versioncheck_force', false)); - $template->assign_vars(array( + $this->template->assign_vars(array( 'S_UP_TO_DATE' => empty($updates_available), 'S_VERSIONCHECK' => true, 'UP_TO_DATE_MSG' => $this->user->lang(empty($updates_available) ? 'UP_TO_DATE' : 'NOT_UP_TO_DATE', $md_manager->get_metadata('display-name')), @@ -316,18 +318,18 @@ class acp_extensions foreach ($updates_available as $branch => $version_data) { - $template->assign_block_vars('updates_available', $version_data); + $this->template->assign_block_vars('updates_available', $version_data); } } catch (\RuntimeException $e) { - $template->assign_vars(array( + $this->template->assign_vars(array( 'S_VERSIONCHECK_STATUS' => $e->getCode(), - 'VERSIONCHECK_FAIL_REASON' => ($e->getMessage() !== $user->lang('VERSIONCHECK_FAIL')) ? $e->getMessage() : '', + 'VERSIONCHECK_FAIL_REASON' => ($e->getMessage() !== $this->user->lang('VERSIONCHECK_FAIL')) ? $e->getMessage() : '', )); } - $template->assign_vars(array( + $this->template->assign_vars(array( 'U_BACK' => $this->u_action . '&action=list', 'U_VERSIONCHECK_FORCE' => $this->u_action . '&action=details&versioncheck_force=1&ext_name=' . urlencode($md_manager->get_metadata('name')), )); @@ -340,16 +342,15 @@ class acp_extensions /** * Lists all the enabled extensions and dumps to the template * - * @param $phpbb_extension_manager An instance of the extension manager * @return null */ - public function list_enabled_exts(\phpbb\extension\manager $phpbb_extension_manager) + public function list_enabled_exts() { $enabled_extension_meta_data = array(); - foreach ($phpbb_extension_manager->all_enabled() as $name => $location) + foreach ($this->ext_manager->all_enabled() as $name => $location) { - $md_manager = $phpbb_extension_manager->create_extension_metadata_manager($name, $this->template); + $md_manager = $this->ext_manager->create_extension_metadata_manager($name, $this->template); try { @@ -397,16 +398,15 @@ class acp_extensions /** * Lists all the disabled extensions and dumps to the template * - * @param $phpbb_extension_manager An instance of the extension manager * @return null */ - public function list_disabled_exts(\phpbb\extension\manager $phpbb_extension_manager) + public function list_disabled_exts() { $disabled_extension_meta_data = array(); - foreach ($phpbb_extension_manager->all_disabled() as $name => $location) + foreach ($this->ext_manager->all_disabled() as $name => $location) { - $md_manager = $phpbb_extension_manager->create_extension_metadata_manager($name, $this->template); + $md_manager = $this->ext_manager->create_extension_metadata_manager($name, $this->template); try { @@ -455,18 +455,17 @@ class acp_extensions /** * Lists all the available extensions and dumps to the template * - * @param $phpbb_extension_manager An instance of the extension manager * @return null */ - public function list_available_exts(\phpbb\extension\manager $phpbb_extension_manager) + public function list_available_exts() { - $uninstalled = array_diff_key($phpbb_extension_manager->all_available(), $phpbb_extension_manager->all_configured()); + $uninstalled = array_diff_key($this->ext_manager->all_available(), $this->ext_manager->all_configured()); $available_extension_meta_data = array(); foreach ($uninstalled as $name => $location) { - $md_manager = $phpbb_extension_manager->create_extension_metadata_manager($name, $this->template); + $md_manager = $this->ext_manager->create_extension_metadata_manager($name, $this->template); try { From 84d2d767b92fe7f66e86eaec4b00368411a63edb Mon Sep 17 00:00:00 2001 From: javiexin Date: Mon, 19 Dec 2016 15:51:43 +0100 Subject: [PATCH 2/8] [ticket/14919] Do not directly use globals in acp_extensions Declare all used variables (u_action, tpl_name, page_title). PHPBB3-14919 --- phpBB/includes/acp/acp_extensions.php | 1 + 1 file changed, 1 insertion(+) diff --git a/phpBB/includes/acp/acp_extensions.php b/phpBB/includes/acp/acp_extensions.php index a2436c3f81..acd2dc60c3 100644 --- a/phpBB/includes/acp/acp_extensions.php +++ b/phpBB/includes/acp/acp_extensions.php @@ -23,6 +23,7 @@ class acp_extensions { var $u_action; var $tpl_name; + var $page_title; private $config; private $template; From 21803dc99beab3b22a9abd115f7727fdcaf6f33b Mon Sep 17 00:00:00 2001 From: javiexin Date: Sun, 25 Dec 2016 14:34:52 +0100 Subject: [PATCH 3/8] [ticket/14919] Do not directly use globals in acp_extensions Declare all used variables (u_action, tpl_name, page_title). Fix typo. PHPBB3-14919 --- phpBB/includes/acp/acp_extensions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/includes/acp/acp_extensions.php b/phpBB/includes/acp/acp_extensions.php index acd2dc60c3..1bf0c23718 100644 --- a/phpBB/includes/acp/acp_extensions.php +++ b/phpBB/includes/acp/acp_extensions.php @@ -31,7 +31,7 @@ class acp_extensions private $cache; private $log; private $request; - private $php_dispatcher; + private $phpbb_dispatcher; private $ext_manager; function main() From 17f8c53ef3c1101d8f90752b70f78a4f5c1c9ae2 Mon Sep 17 00:00:00 2001 From: javiexin Date: Sun, 12 Feb 2017 13:46:37 +0100 Subject: [PATCH 4/8] [ticket/15087] Optimize creation of metadata objects by caching Code for [ticket/14919] Do not directly use globals in acp_extensions for phpbb 3.2. PHPBB3-15087 --- phpBB/includes/acp/acp_extensions.php | 144 +++++++++++++------------- 1 file changed, 71 insertions(+), 73 deletions(-) diff --git a/phpBB/includes/acp/acp_extensions.php b/phpBB/includes/acp/acp_extensions.php index e915fa8c75..e01d95167a 100644 --- a/phpBB/includes/acp/acp_extensions.php +++ b/phpBB/includes/acp/acp_extensions.php @@ -25,35 +25,36 @@ if (!defined('IN_PHPBB')) class acp_extensions { var $u_action; + var $tpl_name; + var $page_title; - private $db; private $config; private $template; private $user; - private $cache; private $log; private $request; + private $phpbb_dispatcher; + private $ext_manager; function main() { // Start the page - global $config, $user, $template, $request, $phpbb_extension_manager, $db, $phpbb_root_path, $phpbb_log, $cache, $phpbb_dispatcher; + global $config, $user, $template, $request, $phpbb_extension_manager, $phpbb_root_path, $phpbb_log, $phpbb_dispatcher; - $this->db = $db; $this->config = $config; $this->template = $template; $this->user = $user; - $this->cache = $cache; $this->request = $request; $this->log = $phpbb_log; $this->phpbb_dispatcher = $phpbb_dispatcher; + $this->ext_manager = $phpbb_extension_manager; - $user->add_lang(array('install', 'acp/extensions', 'migrator')); + $this->user->add_lang(array('install', 'acp/extensions', 'migrator')); $this->page_title = 'ACP_EXTENSIONS'; - $action = $request->variable('action', 'list'); - $ext_name = $request->variable('ext_name', ''); + $action = $this->request->variable('action', 'list'); + $ext_name = $this->request->variable('ext_name', ''); // What is a safe limit of execution time? Half the max execution time should be safe. $safe_time_limit = (ini_get('max_execution_time') / 2); @@ -75,13 +76,13 @@ class acp_extensions extract($this->phpbb_dispatcher->trigger_event('core.acp_extensions_run_action', compact($vars))); // Cancel action - if ($request->is_set_post('cancel')) + if ($this->request->is_set_post('cancel')) { $action = 'list'; $ext_name = ''; } - if (in_array($action, array('enable', 'disable', 'delete_data')) && !check_link_hash($request->variable('hash', ''), $action . '.' . $ext_name)) + if (in_array($action, array('enable', 'disable', 'delete_data')) && !check_link_hash($this->request->variable('hash', ''), $action . '.' . $ext_name)) { trigger_error('FORM_INVALID', E_USER_WARNING); } @@ -89,7 +90,7 @@ class acp_extensions // If they've specified an extension, let's load the metadata manager and validate it. if ($ext_name) { - $md_manager = new \phpbb\extension\metadata_manager($ext_name, $config, $phpbb_extension_manager, $phpbb_root_path); + $md_manager = $this->ext_manager->create_extension_metadata_manager($ext_name); try { @@ -114,12 +115,12 @@ class acp_extensions 'force_unstable' => $force_unstable, )); - confirm_box(false, $user->lang('EXTENSION_FORCE_UNSTABLE_CONFIRM'), $s_hidden_fields); + confirm_box(false, $this->user->lang('EXTENSION_FORCE_UNSTABLE_CONFIRM'), $s_hidden_fields); } else { - $config->set('extension_force_unstable', false); - trigger_error($user->lang['CONFIG_UPDATED'] . adm_back_link($this->u_action)); + $this->config->set('extension_force_unstable', false); + trigger_error($this->user->lang['CONFIG_UPDATED'] . adm_back_link($this->u_action)); } break; @@ -127,17 +128,17 @@ class acp_extensions default: if (confirm_box(true)) { - $config->set('extension_force_unstable', true); - trigger_error($user->lang['CONFIG_UPDATED'] . adm_back_link($this->u_action)); + $this->config->set('extension_force_unstable', true); + trigger_error($this->user->lang['CONFIG_UPDATED'] . adm_back_link($this->u_action)); } - $this->list_enabled_exts($phpbb_extension_manager); - $this->list_disabled_exts($phpbb_extension_manager); - $this->list_available_exts($phpbb_extension_manager); + $this->list_enabled_exts(); + $this->list_disabled_exts(); + $this->list_available_exts(); $this->template->assign_vars(array( 'U_VERSIONCHECK_FORCE' => $this->u_action . '&action=list&versioncheck_force=1', - 'FORCE_UNSTABLE' => $config['extension_force_unstable'], + 'FORCE_UNSTABLE' => $this->config['extension_force_unstable'], 'U_ACTION' => $this->u_action, )); @@ -147,28 +148,28 @@ class acp_extensions case 'enable_pre': if (!$md_manager->validate_dir()) { - trigger_error($user->lang['EXTENSION_DIR_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING); + trigger_error($this->user->lang['EXTENSION_DIR_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING); } if (!$md_manager->validate_enable()) { - trigger_error($user->lang['EXTENSION_NOT_AVAILABLE'] . adm_back_link($this->u_action), E_USER_WARNING); + trigger_error($this->user->lang['EXTENSION_NOT_AVAILABLE'] . adm_back_link($this->u_action), E_USER_WARNING); } - $extension = $phpbb_extension_manager->get_extension($ext_name); + $extension = $this->ext_manager->get_extension($ext_name); if (!$extension->is_enableable()) { - trigger_error($user->lang['EXTENSION_NOT_ENABLEABLE'] . adm_back_link($this->u_action), E_USER_WARNING); + trigger_error($this->user->lang['EXTENSION_NOT_ENABLEABLE'] . adm_back_link($this->u_action), E_USER_WARNING); } - if ($phpbb_extension_manager->is_enabled($ext_name)) + if ($this->ext_manager->is_enabled($ext_name)) { redirect($this->u_action); } $this->tpl_name = 'acp_ext_enable'; - $template->assign_vars(array( + $this->template->assign_vars(array( 'PRE' => true, 'L_CONFIRM_MESSAGE' => $this->user->lang('EXTENSION_ENABLE_CONFIRM', $md_manager->get_metadata('display-name')), 'U_ENABLE' => $this->u_action . '&action=enable&ext_name=' . urlencode($ext_name) . '&hash=' . generate_link_hash('enable.' . $ext_name), @@ -178,55 +179,55 @@ class acp_extensions case 'enable': if (!$md_manager->validate_dir()) { - trigger_error($user->lang['EXTENSION_DIR_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING); + trigger_error($this->user->lang['EXTENSION_DIR_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING); } if (!$md_manager->validate_enable()) { - trigger_error($user->lang['EXTENSION_NOT_AVAILABLE'] . adm_back_link($this->u_action), E_USER_WARNING); + trigger_error($this->user->lang['EXTENSION_NOT_AVAILABLE'] . adm_back_link($this->u_action), E_USER_WARNING); } - $extension = $phpbb_extension_manager->get_extension($ext_name); + $extension = $this->ext_manager->get_extension($ext_name); if (!$extension->is_enableable()) { - trigger_error($user->lang['EXTENSION_NOT_ENABLEABLE'] . adm_back_link($this->u_action), E_USER_WARNING); + trigger_error($this->user->lang['EXTENSION_NOT_ENABLEABLE'] . adm_back_link($this->u_action), E_USER_WARNING); } try { - while ($phpbb_extension_manager->enable_step($ext_name)) + while ($this->ext_manager->enable_step($ext_name)) { // Are we approaching the time limit? If so we want to pause the update and continue after refreshing if ((time() - $start_time) >= $safe_time_limit) { - $template->assign_var('S_NEXT_STEP', true); + $this->template->assign_var('S_NEXT_STEP', true); meta_refresh(0, $this->u_action . '&action=enable&ext_name=' . urlencode($ext_name) . '&hash=' . generate_link_hash('enable.' . $ext_name)); } } - $this->log->add('admin', $user->data['user_id'], $user->ip, 'LOG_EXT_ENABLE', time(), array($ext_name)); + $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_EXT_ENABLE', time(), array($ext_name)); } catch (\phpbb\db\migration\exception $e) { - $template->assign_var('MIGRATOR_ERROR', $e->getLocalisedMessage($user)); + $this->template->assign_var('MIGRATOR_ERROR', $e->getLocalisedMessage($this->user)); } $this->tpl_name = 'acp_ext_enable'; - $template->assign_vars(array( + $this->template->assign_vars(array( 'U_RETURN' => $this->u_action . '&action=list', )); break; case 'disable_pre': - if (!$phpbb_extension_manager->is_enabled($ext_name)) + if (!$this->ext_manager->is_enabled($ext_name)) { redirect($this->u_action); } $this->tpl_name = 'acp_ext_disable'; - $template->assign_vars(array( + $this->template->assign_vars(array( 'PRE' => true, 'L_CONFIRM_MESSAGE' => $this->user->lang('EXTENSION_DISABLE_CONFIRM', $md_manager->get_metadata('display-name')), 'U_DISABLE' => $this->u_action . '&action=disable&ext_name=' . urlencode($ext_name) . '&hash=' . generate_link_hash('disable.' . $ext_name), @@ -234,38 +235,38 @@ class acp_extensions break; case 'disable': - if (!$phpbb_extension_manager->is_enabled($ext_name)) + if (!$this->ext_manager->is_enabled($ext_name)) { redirect($this->u_action); } - while ($phpbb_extension_manager->disable_step($ext_name)) + while ($this->ext_manager->disable_step($ext_name)) { // Are we approaching the time limit? If so we want to pause the update and continue after refreshing if ((time() - $start_time) >= $safe_time_limit) { - $template->assign_var('S_NEXT_STEP', true); + $this->template->assign_var('S_NEXT_STEP', true); meta_refresh(0, $this->u_action . '&action=disable&ext_name=' . urlencode($ext_name) . '&hash=' . generate_link_hash('disable.' . $ext_name)); } } - $this->log->add('admin', $user->data['user_id'], $user->ip, 'LOG_EXT_DISABLE', time(), array($ext_name)); + $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_EXT_DISABLE', time(), array($ext_name)); $this->tpl_name = 'acp_ext_disable'; - $template->assign_vars(array( + $this->template->assign_vars(array( 'U_RETURN' => $this->u_action . '&action=list', )); break; case 'delete_data_pre': - if ($phpbb_extension_manager->is_enabled($ext_name)) + if ($this->ext_manager->is_enabled($ext_name)) { redirect($this->u_action); } $this->tpl_name = 'acp_ext_delete_data'; - $template->assign_vars(array( + $this->template->assign_vars(array( 'PRE' => true, 'L_CONFIRM_MESSAGE' => $this->user->lang('EXTENSION_DELETE_DATA_CONFIRM', $md_manager->get_metadata('display-name')), 'U_PURGE' => $this->u_action . '&action=delete_data&ext_name=' . urlencode($ext_name) . '&hash=' . generate_link_hash('delete_data.' . $ext_name), @@ -273,75 +274,75 @@ class acp_extensions break; case 'delete_data': - if ($phpbb_extension_manager->is_enabled($ext_name)) + if ($this->ext_manager->is_enabled($ext_name)) { redirect($this->u_action); } try { - while ($phpbb_extension_manager->purge_step($ext_name)) + while ($this->ext_manager->purge_step($ext_name)) { // Are we approaching the time limit? If so we want to pause the update and continue after refreshing if ((time() - $start_time) >= $safe_time_limit) { - $template->assign_var('S_NEXT_STEP', true); + $this->template->assign_var('S_NEXT_STEP', true); meta_refresh(0, $this->u_action . '&action=delete_data&ext_name=' . urlencode($ext_name) . '&hash=' . generate_link_hash('delete_data.' . $ext_name)); } } - $this->log->add('admin', $user->data['user_id'], $user->ip, 'LOG_EXT_PURGE', time(), array($ext_name)); + $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_EXT_PURGE', time(), array($ext_name)); } catch (\phpbb\db\migration\exception $e) { - $template->assign_var('MIGRATOR_ERROR', $e->getLocalisedMessage($user)); + $this->template->assign_var('MIGRATOR_ERROR', $e->getLocalisedMessage($this->user)); } $this->tpl_name = 'acp_ext_delete_data'; - $template->assign_vars(array( + $this->template->assign_vars(array( 'U_RETURN' => $this->u_action . '&action=list', )); break; case 'details': // Output it to the template - $md_manager->output_template_data($template); + $md_manager->output_template_data($this->template); $meta = $md_manager->get_metadata('all'); if (isset($meta['extra']['version-check'])) { try { - $updates_available = $phpbb_extension_manager->version_check($md_manager, $request->variable('versioncheck_force', false), $this->config['extension_force_unstable'] ? 'unstable' : null); + $updates_available = $this->ext_manager->version_check($md_manager, $this->request->variable('versioncheck_force', false), $this->config['extension_force_unstable'] ? 'unstable' : null); - $template->assign_vars(array( + $this->template->assign_vars(array( 'S_UP_TO_DATE' => empty($updates_available), 'UP_TO_DATE_MSG' => $this->user->lang(empty($updates_available) ? 'UP_TO_DATE' : 'NOT_UP_TO_DATE', $md_manager->get_metadata('display-name')), )); foreach ($updates_available as $branch => $version_data) { - $template->assign_block_vars('updates_available', $version_data); + $this->template->assign_block_vars('updates_available', $version_data); } } catch (exception_interface $e) { $message = call_user_func_array(array($this->user, 'lang'), array_merge(array($e->getMessage()), $e->get_parameters())); - $template->assign_vars(array( + $this->template->assign_vars(array( 'S_VERSIONCHECK_FAIL' => true, 'VERSIONCHECK_FAIL_REASON' => ($e->getMessage() !== 'VERSIONCHECK_FAIL') ? $message : '', )); } - $template->assign_var('S_VERSIONCHECK', true); + $this->template->assign_var('S_VERSIONCHECK', true); } else { - $template->assign_var('S_VERSIONCHECK', false); + $this->template->assign_var('S_VERSIONCHECK', false); } - $template->assign_vars(array( + $this->template->assign_vars(array( 'U_BACK' => $this->u_action . '&action=list', 'U_VERSIONCHECK_FORCE' => $this->u_action . '&action=details&versioncheck_force=1&ext_name=' . urlencode($md_manager->get_metadata('name')), )); @@ -354,16 +355,15 @@ class acp_extensions /** * Lists all the enabled extensions and dumps to the template * - * @param $phpbb_extension_manager An instance of the extension manager * @return null */ - public function list_enabled_exts(\phpbb\extension\manager $phpbb_extension_manager) + public function list_enabled_exts() { $enabled_extension_meta_data = array(); - foreach ($phpbb_extension_manager->all_enabled() as $name => $location) + foreach ($this->ext_manager->all_enabled() as $name => $location) { - $md_manager = $phpbb_extension_manager->create_extension_metadata_manager($name); + $md_manager = $this->ext_manager->create_extension_metadata_manager($name); try { @@ -378,7 +378,7 @@ class acp_extensions try { $force_update = $this->request->variable('versioncheck_force', false); - $updates = $phpbb_extension_manager->version_check($md_manager, $force_update, !$force_update); + $updates = $this->ext_manager->version_check($md_manager, $force_update, !$force_update); $enabled_extension_meta_data[$name]['S_UP_TO_DATE'] = empty($updates); $enabled_extension_meta_data[$name]['S_VERSIONCHECK'] = true; @@ -426,16 +426,15 @@ class acp_extensions /** * Lists all the disabled extensions and dumps to the template * - * @param $phpbb_extension_manager An instance of the extension manager * @return null */ - public function list_disabled_exts(\phpbb\extension\manager $phpbb_extension_manager) + public function list_disabled_exts() { $disabled_extension_meta_data = array(); - foreach ($phpbb_extension_manager->all_disabled() as $name => $location) + foreach ($this->ext_manager->all_disabled() as $name => $location) { - $md_manager = $phpbb_extension_manager->create_extension_metadata_manager($name); + $md_manager = $this->ext_manager->create_extension_metadata_manager($name); try { @@ -448,7 +447,7 @@ class acp_extensions if (isset($meta['extra']['version-check'])) { $force_update = $this->request->variable('versioncheck_force', false); - $updates = $phpbb_extension_manager->version_check($md_manager, $force_update, !$force_update); + $updates = $this->ext_manager->version_check($md_manager, $force_update, !$force_update); $disabled_extension_meta_data[$name]['S_UP_TO_DATE'] = empty($updates); $disabled_extension_meta_data[$name]['S_VERSIONCHECK'] = true; @@ -496,18 +495,17 @@ class acp_extensions /** * Lists all the available extensions and dumps to the template * - * @param $phpbb_extension_manager An instance of the extension manager * @return null */ - public function list_available_exts(\phpbb\extension\manager $phpbb_extension_manager) + public function list_available_exts() { - $uninstalled = array_diff_key($phpbb_extension_manager->all_available(), $phpbb_extension_manager->all_configured()); + $uninstalled = array_diff_key($this->ext_manager->all_available(), $this->ext_manager->all_configured()); $available_extension_meta_data = array(); foreach ($uninstalled as $name => $location) { - $md_manager = $phpbb_extension_manager->create_extension_metadata_manager($name); + $md_manager = $this->ext_manager->create_extension_metadata_manager($name); try { @@ -520,7 +518,7 @@ class acp_extensions if (isset($meta['extra']['version-check'])) { $force_update = $this->request->variable('versioncheck_force', false); - $updates = $phpbb_extension_manager->version_check($md_manager, $force_update, !$force_update); + $updates = $this->ext_manager->version_check($md_manager, $force_update, !$force_update); $available_extension_meta_data[$name]['S_UP_TO_DATE'] = empty($updates); $available_extension_meta_data[$name]['S_VERSIONCHECK'] = true; From 5266821e1b5b271e69b00d717b9e9b1f77b65b9f Mon Sep 17 00:00:00 2001 From: javiexin Date: Sun, 12 Feb 2017 14:10:58 +0100 Subject: [PATCH 5/8] [ticket/15087] Optimize creation of metadata objects by caching Code for [ticket/14938] Inconsistency in ext_mgr all_available vs is_available for phpbb 3.2. PHPBB3-15087 --- phpBB/phpbb/extension/manager.php | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/phpBB/phpbb/extension/manager.php b/phpBB/phpbb/extension/manager.php index b2b60aaa9b..22522d0785 100644 --- a/phpBB/phpbb/extension/manager.php +++ b/phpBB/phpbb/extension/manager.php @@ -431,25 +431,11 @@ class manager if ($file_info->isFile() && $file_info->getFilename() == 'composer.json') { $ext_name = $iterator->getInnerIterator()->getSubPath(); - $composer_file = $iterator->getPath() . '/composer.json'; - - // Ignore the extension if there is no composer.json. - if (!is_readable($composer_file) || !($ext_info = file_get_contents($composer_file))) - { - continue; - } - - $ext_info = json_decode($ext_info, true); $ext_name = str_replace(DIRECTORY_SEPARATOR, '/', $ext_name); - - // Ignore the extension if directory depth is not correct or if the directory structure - // does not match the name value specified in composer.json. - if (substr_count($ext_name, '/') !== 1 || !isset($ext_info['name']) || $ext_name != $ext_info['name']) + if ($this->is_available($ext_name)) { - continue; + $available[$ext_name] = $this->get_extension_path($ext_name, true); } - - $available[$ext_name] = $this->phpbb_root_path . 'ext/' . $ext_name . '/'; } } ksort($available); @@ -527,7 +513,15 @@ class manager */ public function is_available($name) { - return file_exists($this->get_extension_path($name, true)); + $md_manager = $this->create_extension_metadata_manager($name); + try + { + return $md_manager->get_metadata('all') && $md_manager->validate_enable(); + } + catch (\phpbb\extension\exception $e) + { + return false; + } } /** From d3a687df270268b4cbbe3ae0d10e8c4e2ae29235 Mon Sep 17 00:00:00 2001 From: javiexin Date: Sun, 12 Feb 2017 14:39:26 +0100 Subject: [PATCH 6/8] [ticket/15087] Optimize creation of metadata objects by caching Code for [ticket/15080] Save unneeded file loads for extension metadata for phpbb 3.2. PHPBB3-15087 --- phpBB/phpbb/extension/metadata_manager.php | 64 ++++++---------------- 1 file changed, 16 insertions(+), 48 deletions(-) diff --git a/phpBB/phpbb/extension/metadata_manager.php b/phpBB/phpbb/extension/metadata_manager.php index 348b3c2cdf..089f9a7a32 100644 --- a/phpBB/phpbb/extension/metadata_manager.php +++ b/phpBB/phpbb/extension/metadata_manager.php @@ -81,13 +81,11 @@ class metadata_manager */ public function get_metadata($element = 'all') { - $this->set_metadata_file(); - - // Fetch the metadata - $this->fetch_metadata(); - - // Clean the metadata - $this->clean_metadata_array(); + // Fetch and clean the metadata if not done yet + if ($this->metadata === array()) + { + $this->fetch_metadata_from_file(); + } switch ($element) { @@ -121,52 +119,32 @@ class metadata_manager } /** - * Sets the filepath of the metadata file + * Sets the path of the metadata file, gets its contents and cleans loaded file * * @throws \phpbb\extension\exception */ - private function set_metadata_file() + private function fetch_metadata_from_file() { $ext_filepath = $this->extension_manager->get_extension_path($this->ext_name); - $metadata_filepath = $this->phpbb_root_path . $ext_filepath . 'composer.json'; - - $this->metadata_file = $metadata_filepath; + $this->metadata_file = $this->phpbb_root_path . $ext_filepath . 'composer.json'; if (!file_exists($this->metadata_file)) { throw new \phpbb\extension\exception('FILE_NOT_FOUND', array($this->metadata_file)); } - } - /** - * Gets the contents of the composer.json file - * - * @return bool True if success, throws an exception on failure - * @throws \phpbb\extension\exception - */ - private function fetch_metadata() - { - if (!file_exists($this->metadata_file)) + if (!($file_contents = file_get_contents($this->metadata_file))) { - throw new \phpbb\extension\exception('FILE_NOT_FOUND', array($this->metadata_file)); + throw new \phpbb\extension\exception('FILE_CONTENT_ERR', array($this->metadata_file)); } - else + + if (($metadata = json_decode($file_contents, true)) === null) { - if (!($file_contents = file_get_contents($this->metadata_file))) - { - throw new \phpbb\extension\exception('FILE_CONTENT_ERR', array($this->metadata_file)); - } - - if (($metadata = json_decode($file_contents, true)) === null) - { - throw new \phpbb\extension\exception('FILE_JSON_DECODE_ERR', array($this->metadata_file)); - } - - array_walk_recursive($metadata, array($this, 'sanitize_json')); - $this->metadata = $metadata; - - return true; + throw new \phpbb\extension\exception('FILE_JSON_DECODE_ERR', array($this->metadata_file)); } + + array_walk_recursive($metadata, array($this, 'sanitize_json')); + $this->metadata = $metadata; } /** @@ -180,16 +158,6 @@ class metadata_manager $value = htmlspecialchars($value); } - /** - * This array handles the cleaning of the array - * - * @return array Contains the cleaned metadata array - */ - private function clean_metadata_array() - { - return $this->metadata; - } - /** * Validate fields * From 2e3d90e05b7b08f6df89b9e3a98b1716d52cf340 Mon Sep 17 00:00:00 2001 From: javiexin Date: Sun, 12 Feb 2017 18:33:06 +0100 Subject: [PATCH 7/8] [ticket/15087] Optimize creation of metadata objects by caching Caching is done in ext_manager, and metadata_manager is further simplified by reducing the number of parameters needed. Also, move template output function from metadata_manager to acp_extensions, where it belongs. PHPBB3-15087 --- phpBB/includes/acp/acp_extensions.php | 45 ++++++++++- phpBB/phpbb/console/command/update/check.php | 2 +- phpBB/phpbb/extension/manager.php | 33 +++++--- phpBB/phpbb/extension/metadata_manager.php | 85 ++++---------------- 4 files changed, 79 insertions(+), 86 deletions(-) diff --git a/phpBB/includes/acp/acp_extensions.php b/phpBB/includes/acp/acp_extensions.php index e01d95167a..8ce910c242 100644 --- a/phpBB/includes/acp/acp_extensions.php +++ b/phpBB/includes/acp/acp_extensions.php @@ -307,23 +307,23 @@ class acp_extensions case 'details': // Output it to the template - $md_manager->output_template_data($this->template); - $meta = $md_manager->get_metadata('all'); + $this->output_metadata_to_template($meta); + if (isset($meta['extra']['version-check'])) { try { $updates_available = $this->ext_manager->version_check($md_manager, $this->request->variable('versioncheck_force', false), $this->config['extension_force_unstable'] ? 'unstable' : null); - $this->template->assign_vars(array( + $this->template->assign_vars(array( 'S_UP_TO_DATE' => empty($updates_available), 'UP_TO_DATE_MSG' => $this->user->lang(empty($updates_available) ? 'UP_TO_DATE' : 'NOT_UP_TO_DATE', $md_manager->get_metadata('display-name')), )); foreach ($updates_available as $branch => $version_data) { - $this->template->assign_block_vars('updates_available', $version_data); + $this->template->assign_block_vars('updates_available', $version_data); } } catch (exception_interface $e) @@ -583,4 +583,41 @@ class acp_extensions { return strnatcasecmp($val1['META_DISPLAY_NAME'], $val2['META_DISPLAY_NAME']); } + + /** + * Outputs extension metadata into the template + * + * @param array $metadata Array with all metadata for the extension + * @return null + */ + public function output_metadata_to_template($metadata) + { + $this->template->assign_vars(array( + 'META_NAME' => $metadata['name'], + 'META_TYPE' => $metadata['type'], + 'META_DESCRIPTION' => (isset($metadata['description'])) ? $metadata['description'] : '', + 'META_HOMEPAGE' => (isset($metadata['homepage'])) ? $metadata['homepage'] : '', + 'META_VERSION' => $metadata['version'], + 'META_TIME' => (isset($metadata['time'])) ? $metadata['time'] : '', + 'META_LICENSE' => $metadata['license'], + + 'META_REQUIRE_PHP' => (isset($metadata['require']['php'])) ? $metadata['require']['php'] : '', + 'META_REQUIRE_PHP_FAIL' => (isset($metadata['require']['php'])) ? false : true, + + 'META_REQUIRE_PHPBB' => (isset($metadata['extra']['soft-require']['phpbb/phpbb'])) ? $metadata['extra']['soft-require']['phpbb/phpbb'] : '', + 'META_REQUIRE_PHPBB_FAIL' => (isset($metadata['extra']['soft-require']['phpbb/phpbb'])) ? false : true, + + 'META_DISPLAY_NAME' => (isset($metadata['extra']['display-name'])) ? $metadata['extra']['display-name'] : '', + )); + + foreach ($metadata['authors'] as $author) + { + $this->template->assign_block_vars('meta_authors', array( + 'AUTHOR_NAME' => $author['name'], + 'AUTHOR_EMAIL' => (isset($author['email'])) ? $author['email'] : '', + 'AUTHOR_HOMEPAGE' => (isset($author['homepage'])) ? $author['homepage'] : '', + 'AUTHOR_ROLE' => (isset($author['role'])) ? $author['role'] : '', + )); + } + } } diff --git a/phpBB/phpbb/console/command/update/check.php b/phpBB/phpbb/console/command/update/check.php index 1f1cfa25d2..ed8ad79eea 100644 --- a/phpBB/phpbb/console/command/update/check.php +++ b/phpBB/phpbb/console/command/update/check.php @@ -134,7 +134,7 @@ class check extends \phpbb\console\command\command try { $ext_manager = $this->phpbb_container->get('ext.manager'); - $md_manager = $ext_manager->create_extension_metadata_manager($ext_name, null); + $md_manager = $ext_manager->create_extension_metadata_manager($ext_name); $updates_available = $ext_manager->version_check($md_manager, $recheck, false, $stability); $metadata = $md_manager->get_metadata('all'); diff --git a/phpBB/phpbb/extension/manager.php b/phpBB/phpbb/extension/manager.php index 22522d0785..5cafd9319a 100644 --- a/phpBB/phpbb/extension/manager.php +++ b/phpBB/phpbb/extension/manager.php @@ -152,7 +152,12 @@ class manager */ public function create_extension_metadata_manager($name) { - return new \phpbb\extension\metadata_manager($name, $this->config, $this, $this->phpbb_root_path); + if (!isset($this->extensions[$name]['metadata'])) + { + $metadata = new \phpbb\extension\metadata_manager($name, $this->get_extension_path($name, true)); + $this->extensions[$name]['metadata'] = $metadata; + } + return $this->extensions[$name]['metadata']; } /** @@ -168,7 +173,7 @@ class manager public function enable_step($name) { // ignore extensions that are already enabled - if (isset($this->extensions[$name]) && $this->extensions[$name]['ext_active']) + if ($this->is_enabled($name)) { return false; } @@ -258,7 +263,7 @@ class manager public function disable_step($name) { // ignore extensions that are already disabled - if (!isset($this->extensions[$name]) || !$this->extensions[$name]['ext_active']) + if ($this->is_disabled($name)) { return false; } @@ -336,8 +341,8 @@ class manager */ public function purge_step($name) { - // ignore extensions that do not exist - if (!isset($this->extensions[$name])) + // ignore extensions that are not configured + if (!$this->is_configured($name)) { return false; } @@ -458,8 +463,12 @@ class manager $configured = array(); foreach ($this->extensions as $name => $data) { - $data['ext_path'] = ($phpbb_relative ? $this->phpbb_root_path : '') . $data['ext_path']; - $configured[$name] = $data; + if ($this->is_configured($name)) + { + unset($data['metadata']); + $data['ext_path'] = ($phpbb_relative ? $this->phpbb_root_path : '') . $data['ext_path']; + $configured[$name] = $data; + } } return $configured; } @@ -476,7 +485,7 @@ class manager $enabled = array(); foreach ($this->extensions as $name => $data) { - if ($data['ext_active']) + if ($this->is_enabled($name)) { $enabled[$name] = ($phpbb_relative ? $this->phpbb_root_path : '') . $data['ext_path']; } @@ -497,7 +506,7 @@ class manager $disabled = array(); foreach ($this->extensions as $name => $data) { - if (!$data['ext_active']) + if ($this->is_disabled($name)) { $disabled[$name] = ($phpbb_relative ? $this->phpbb_root_path : '') . $data['ext_path']; } @@ -532,7 +541,7 @@ class manager */ public function is_enabled($name) { - return isset($this->extensions[$name]) && $this->extensions[$name]['ext_active']; + return isset($this->extensions[$name]['ext_active']) && $this->extensions[$name]['ext_active']; } /** @@ -543,7 +552,7 @@ class manager */ public function is_disabled($name) { - return isset($this->extensions[$name]) && !$this->extensions[$name]['ext_active']; + return isset($this->extensions[$name]['ext_active']) && !$this->extensions[$name]['ext_active']; } /** @@ -557,7 +566,7 @@ class manager */ public function is_configured($name) { - return isset($this->extensions[$name]); + return isset($this->extensions[$name]['ext_active']); } /** diff --git a/phpBB/phpbb/extension/metadata_manager.php b/phpBB/phpbb/extension/metadata_manager.php index 089f9a7a32..2dec6944b7 100644 --- a/phpBB/phpbb/extension/metadata_manager.php +++ b/phpBB/phpbb/extension/metadata_manager.php @@ -18,24 +18,6 @@ namespace phpbb\extension; */ class metadata_manager { - /** - * phpBB Config instance - * @var \phpbb\config\config - */ - protected $config; - - /** - * phpBB Extension Manager - * @var \phpbb\extension\manager - */ - protected $extension_manager; - - /** - * phpBB root path - * @var string - */ - protected $phpbb_root_path; - /** * Name (including vendor) of the extension * @var string @@ -58,19 +40,13 @@ class metadata_manager * Creates the metadata manager * * @param string $ext_name Name (including vendor) of the extension - * @param \phpbb\config\config $config phpBB Config instance - * @param \phpbb\extension\manager $extension_manager An instance of the phpBB extension manager - * @param string $phpbb_root_path Path to the phpbb includes directory. + * @param string $ext_path Path to the extension directory including root path */ - public function __construct($ext_name, \phpbb\config\config $config, \phpbb\extension\manager $extension_manager, $phpbb_root_path) + public function __construct($ext_name, $ext_path) { - $this->config = $config; - $this->extension_manager = $extension_manager; - $this->phpbb_root_path = $phpbb_root_path; - $this->ext_name = $ext_name; $this->metadata = array(); - $this->metadata_file = ''; + $this->metadata_file = $ext_path . 'composer.json'; } /** @@ -119,15 +95,12 @@ class metadata_manager } /** - * Sets the path of the metadata file, gets its contents and cleans loaded file + * Gets the metadata file contents and cleans loaded file * * @throws \phpbb\extension\exception */ private function fetch_metadata_from_file() { - $ext_filepath = $this->extension_manager->get_extension_path($this->ext_name); - $this->metadata_file = $this->phpbb_root_path . $ext_filepath . 'composer.json'; - if (!file_exists($this->metadata_file)) { throw new \phpbb\extension\exception('FILE_NOT_FOUND', array($this->metadata_file)); @@ -182,9 +155,19 @@ class metadata_manager case 'all': $this->validate('display'); - if (!$this->validate_enable()) + if (!$this->validate_dir()) { - throw new \phpbb\extension\exception('META_FIELD_NOT_SET', array($name)); + throw new \phpbb\extension\exception('EXTENSION_DIR_INVALID'); + } + + if (!$this->validate_require_phpbb()) + { + throw new \phpbb\extension\exception('META_FIELD_NOT_SET', array('soft-require')); + } + + if (!$this->validate_require_php()) + { + throw new \phpbb\extension\exception('META_FIELD_NOT_SET', array('require php')); } break; @@ -296,40 +279,4 @@ class metadata_manager return true; } - - /** - * Outputs the metadata into the template - * - * @param \phpbb\template\template $template phpBB Template instance - */ - public function output_template_data(\phpbb\template\template $template) - { - $template->assign_vars(array( - 'META_NAME' => $this->metadata['name'], - 'META_TYPE' => $this->metadata['type'], - 'META_DESCRIPTION' => (isset($this->metadata['description'])) ? $this->metadata['description'] : '', - 'META_HOMEPAGE' => (isset($this->metadata['homepage'])) ? $this->metadata['homepage'] : '', - 'META_VERSION' => (isset($this->metadata['version'])) ? $this->metadata['version'] : '', - 'META_TIME' => (isset($this->metadata['time'])) ? $this->metadata['time'] : '', - 'META_LICENSE' => $this->metadata['license'], - - 'META_REQUIRE_PHP' => (isset($this->metadata['require']['php'])) ? $this->metadata['require']['php'] : '', - 'META_REQUIRE_PHP_FAIL' => !$this->validate_require_php(), - - 'META_REQUIRE_PHPBB' => (isset($this->metadata['extra']['soft-require']['phpbb/phpbb'])) ? $this->metadata['extra']['soft-require']['phpbb/phpbb'] : '', - 'META_REQUIRE_PHPBB_FAIL' => !$this->validate_require_phpbb(), - - 'META_DISPLAY_NAME' => (isset($this->metadata['extra']['display-name'])) ? $this->metadata['extra']['display-name'] : '', - )); - - foreach ($this->metadata['authors'] as $author) - { - $template->assign_block_vars('meta_authors', array( - 'AUTHOR_NAME' => $author['name'], - 'AUTHOR_EMAIL' => (isset($author['email'])) ? $author['email'] : '', - 'AUTHOR_HOMEPAGE' => (isset($author['homepage'])) ? $author['homepage'] : '', - 'AUTHOR_ROLE' => (isset($author['role'])) ? $author['role'] : '', - )); - } - } } From 7000db304995ae575d14f84d4f4f3c874d09c3e0 Mon Sep 17 00:00:00 2001 From: javiexin Date: Sun, 12 Feb 2017 19:25:42 +0100 Subject: [PATCH 8/8] [ticket/15087] Optimize creation of metadata objects by caching Fix constructor in test cases. PHPBB3-15087 --- tests/extension/metadata_manager_test.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/extension/metadata_manager_test.php b/tests/extension/metadata_manager_test.php index ce675f0d36..8ef41f3673 100644 --- a/tests/extension/metadata_manager_test.php +++ b/tests/extension/metadata_manager_test.php @@ -364,9 +364,7 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case { return new phpbb_mock_metadata_manager( $ext_name, - $this->config, - $this->extension_manager, - $this->phpbb_root_path + $this->extension_manager->get_extension_path($ext_name, true) ); } }