From 5a1fe1db12c19def20b78396fa2a73869d5d662b Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Thu, 9 Nov 2023 22:05:42 +0100 Subject: [PATCH 1/4] [ticket/13162] Add truncate table functionality to doctrine tools PHPBB3-13162 --- phpBB/phpbb/db/tools/doctrine.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/phpBB/phpbb/db/tools/doctrine.php b/phpBB/phpbb/db/tools/doctrine.php index 1196ac08be..3f6a11ba60 100644 --- a/phpBB/phpbb/db/tools/doctrine.php +++ b/phpBB/phpbb/db/tools/doctrine.php @@ -15,6 +15,7 @@ namespace phpbb\db\tools; use Doctrine\DBAL\Connection; use Doctrine\DBAL\Exception; +use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Schema\AbstractAsset; use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\Schema\Index; @@ -363,6 +364,24 @@ class doctrine implements tools_interface ); } + /** + * Truncate the table + * + * @param string $table_name + * @return void + */ + public function sql_truncate_table(string $table_name) + { + try + { + $this->connection->executeQuery($this->get_schema_manager()->getDatabasePlatform()->getTruncateTableSQL($table_name)); + } + catch (Exception $e) + { + return; + } + } + /** * Returns an array of indices for either unique and primary keys, or simple indices. * From 0ae7e3f99272f9d9f1480016e0aac92848ee929b Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Fri, 10 Nov 2023 20:27:16 +0100 Subject: [PATCH 2/4] [ticket/13162] Add tests for truncate table via tools PHPBB3-13162 --- tests/dbal/db_tools_test.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/dbal/db_tools_test.php b/tests/dbal/db_tools_test.php index 70295b2165..73543ed97d 100644 --- a/tests/dbal/db_tools_test.php +++ b/tests/dbal/db_tools_test.php @@ -357,6 +357,34 @@ class phpbb_dbal_db_tools_test extends phpbb_database_test_case $this->assertFalse($this->tools->sql_table_exists('prefix_test_table')); } + public function test_truncate_table() + { + $this->tools->sql_create_table('truncate_test_table', + ['COLUMNS' => [ + 'foo' => ['UINT', 42], + ]] + ); + + $this->assertTrue($this->tools->sql_table_exists('truncate_test_table')); + + $sql = 'INSERT INTO truncate_test_table(foo) VALUES(19)'; + $this->db->sql_query($sql); + + $sql = 'SELECT * FROM truncate_test_table'; + $result = $this->db->sql_query($sql); + $rowset = $this->db->sql_fetchrowset($result); + $this->db->sql_freeresult($result); + + $this->assertGreaterThan(0, count($rowset), 'Failed asserting that data exists in truncate_test_table.'); + + $this->tools->sql_truncate_table('truncate_test_table'); + + $result = $this->db->sql_query($sql); + $rowset = $this->db->sql_fetchrowset($result); + $this->db->sql_freeresult($result); + $this->assertEquals(0, count($rowset), 'Failed asserting that truncate was successful for table.'); + } + public function test_perform_schema_changes_drop_tables() { $db_tools = $this->getMockBuilder('\phpbb\db\tools\doctrine') From c31ac7348a462b8cf5141dbfff89aaf9b27547d0 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Fri, 10 Nov 2023 20:29:50 +0100 Subject: [PATCH 3/4] [ticket/13162] Remove unused use statement PHPBB3-13162 --- phpBB/phpbb/db/tools/doctrine.php | 1 - 1 file changed, 1 deletion(-) diff --git a/phpBB/phpbb/db/tools/doctrine.php b/phpBB/phpbb/db/tools/doctrine.php index 3f6a11ba60..fcf14ad13f 100644 --- a/phpBB/phpbb/db/tools/doctrine.php +++ b/phpBB/phpbb/db/tools/doctrine.php @@ -15,7 +15,6 @@ namespace phpbb\db\tools; use Doctrine\DBAL\Connection; use Doctrine\DBAL\Exception; -use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Schema\AbstractAsset; use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\Schema\Index; From 59f387e828131a4a951cd0f2538ed451818aaecd Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sun, 17 Dec 2023 19:49:23 +0100 Subject: [PATCH 4/4] [ticket/13162] Use db tools for truncating PHPBB3-13162 --- .../default/container/services_console.yml | 1 + .../default/container/services_search.yml | 1 + phpBB/develop/fill.php | 7 +-- phpBB/develop/repair_bots.php | 8 ++-- phpBB/includes/acp/acp_forums.php | 6 +-- phpBB/includes/acp/acp_icons.php | 12 +----- phpBB/includes/acp/acp_main.php | 26 +++-------- phpBB/includes/acp/acp_permissions.php | 16 +++---- phpBB/includes/functions_admin.php | 15 ++----- phpBB/includes/functions_compatibility.php | 4 +- phpBB/includes/functions_convert.php | 15 ++----- phpBB/includes/functions_user.php | 7 ++- .../install/convert/controller/convertor.php | 15 ++----- phpBB/install/convert/convertor.php | 2 +- phpBB/phpbb/console/command/cache/purge.php | 10 ++++- phpBB/phpbb/db/tools/doctrine.php | 7 +-- phpBB/phpbb/db/tools/tools_interface.php | 8 ++++ .../install_data/task/create_search_index.php | 8 ++++ .../phpbb/search/backend/fulltext_native.php | 43 ++++++++++--------- tests/console/cache/purge_test.php | 5 ++- tests/search/native_test.php | 5 ++- 21 files changed, 103 insertions(+), 118 deletions(-) diff --git a/phpBB/config/default/container/services_console.yml b/phpBB/config/default/container/services_console.yml index f23d205b8f..038aa360e4 100644 --- a/phpBB/config/default/container/services_console.yml +++ b/phpBB/config/default/container/services_console.yml @@ -19,6 +19,7 @@ services: - '@user' - '@cache.driver' - '@dbal.conn' + - '@dbal.tools' - '@auth' - '@log' - '@config' diff --git a/phpBB/config/default/container/services_search.yml b/phpBB/config/default/container/services_search.yml index 812e41a1f2..71a874f15f 100644 --- a/phpBB/config/default/container/services_search.yml +++ b/phpBB/config/default/container/services_search.yml @@ -26,6 +26,7 @@ services: arguments: - '@config' - '@dbal.conn' + - '@dbal.tools' - '@event_dispatcher' - '@language' - '@user' diff --git a/phpBB/develop/fill.php b/phpBB/develop/fill.php index 2aaafe1e38..dd0292abf5 100644 --- a/phpBB/develop/fill.php +++ b/phpBB/develop/fill.php @@ -68,9 +68,10 @@ switch ($mode) if (!$start) { - $db->sql_query('TRUNCATE TABLE ' . POSTS_TABLE); - $db->sql_query('TRUNCATE TABLE ' . TOPICS_TABLE); -// $db->sql_query('TRUNCATE TABLE ' . TOPICS_TABLE . '_prefetch'); + $db_tools = $phpbb_container->get('dbal.tools'); + + $db_tools->sql_truncate_table(POSTS_TABLE); + $db_tools->sql_truncate_table(TOPICS_TABLE); } $db->sql_query('LOCK TABLES ' . POSTS_TABLE . ' WRITE, ' . TOPICS_TABLE . ' WRITE'); diff --git a/phpBB/develop/repair_bots.php b/phpBB/develop/repair_bots.php index 100cc1e9af..ec638afaa3 100644 --- a/phpBB/develop/repair_bots.php +++ b/phpBB/develop/repair_bots.php @@ -105,7 +105,10 @@ function add_bots($bots) $result = $db->sql_query($sql); $group_id = (int) $db->sql_fetchfield('group_id', false, $result); $db->sql_freeresult($result); - $db->sql_query('TRUNCATE TABLE ' . BOTS_TABLE); + + // Truncate bots table + $db_tools = $phpbb_container->get('dbal.tools'); + $db_tools->sql_truncate_table(BOTS_TABLE); if (!$group_id) { @@ -118,9 +121,6 @@ function add_bots($bots) } - - - foreach ($bots as $bot_name => $bot_ary) { $user_row = array( diff --git a/phpBB/includes/acp/acp_forums.php b/phpBB/includes/acp/acp_forums.php index 077c037484..2df8abed00 100644 --- a/phpBB/includes/acp/acp_forums.php +++ b/phpBB/includes/acp/acp_forums.php @@ -27,7 +27,7 @@ class acp_forums function main($id, $mode) { global $db, $user, $auth, $template, $cache, $request, $phpbb_dispatcher; - global $phpbb_admin_path, $phpbb_root_path, $phpEx, $phpbb_log; + global $phpbb_admin_path, $phpbb_container, $phpbb_root_path, $phpEx, $phpbb_log; $user->add_lang('acp/forums'); $this->tpl_name = 'acp_forums'; @@ -210,7 +210,7 @@ class acp_forums ($action != 'edit' || empty($forum_id) || ($auth->acl_get('a_fauth') && $auth->acl_get('a_authusers') && $auth->acl_get('a_authgroups') && $auth->acl_get('a_mauth')))) { copy_forum_permissions($forum_perm_from, $forum_data['forum_id'], ($action == 'edit') ? true : false); - phpbb_cache_moderators($db, $cache, $auth); + phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth); $copied_permissions = true; } /* Commented out because of questionable UI workflow - re-visit for 3.0.7 @@ -789,7 +789,7 @@ class acp_forums if (!empty($forum_perm_from) && $forum_perm_from != $forum_id) { copy_forum_permissions($forum_perm_from, $forum_id, true); - phpbb_cache_moderators($db, $cache, $auth); + phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth); $auth->acl_clear_prefetch(); $cache->destroy('sql', FORUMS_TABLE); diff --git a/phpBB/includes/acp/acp_icons.php b/phpBB/includes/acp/acp_icons.php index e1c9550b4e..aac42fbd0b 100644 --- a/phpBB/includes/acp/acp_icons.php +++ b/phpBB/includes/acp/acp_icons.php @@ -574,16 +574,8 @@ class acp_icons // The user has already selected a smilies_pak file if ($current == 'delete') { - switch ($db->get_sql_layer()) - { - case 'sqlite3': - $db->sql_query('DELETE FROM ' . $table); - break; - - default: - $db->sql_query('TRUNCATE TABLE ' . $table); - break; - } + $db_tools = $phpbb_container->get('dbal.tools'); + $db_tools->sql_truncate_table($table); switch ($mode) { diff --git a/phpBB/includes/acp/acp_main.php b/phpBB/includes/acp/acp_main.php index 173300ec25..8b34b5fea8 100644 --- a/phpBB/includes/acp/acp_main.php +++ b/phpBB/includes/acp/acp_main.php @@ -281,16 +281,8 @@ class acp_main break; case 'db_track': - switch ($db->get_sql_layer()) - { - case 'sqlite3': - $db->sql_query('DELETE FROM ' . TOPICS_POSTED_TABLE); - break; - - default: - $db->sql_query('TRUNCATE TABLE ' . TOPICS_POSTED_TABLE); - break; - } + $db_tools = $phpbb_container->get('dbal.tools'); + $db_tools->sql_truncate_table(TOPICS_POSTED_TABLE); // This can get really nasty... therefore we only do the last six months $get_from_time = time() - (6 * 4 * 7 * 24 * 60 * 60); @@ -370,7 +362,7 @@ class acp_main // Clear permissions $auth->acl_clear_prefetch(); - phpbb_cache_moderators($db, $cache, $auth); + phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth); $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_PURGE_CACHE'); @@ -388,19 +380,11 @@ class acp_main } $tables = array(CONFIRM_TABLE, SESSIONS_TABLE); + $db_tools = $phpbb_container->get('dbal.tools'); foreach ($tables as $table) { - switch ($db->get_sql_layer()) - { - case 'sqlite3': - $db->sql_query("DELETE FROM $table"); - break; - - default: - $db->sql_query("TRUNCATE TABLE $table"); - break; - } + $db_tools->sql_truncate_table($table); } // let's restore the admin session diff --git a/phpBB/includes/acp/acp_permissions.php b/phpBB/includes/acp/acp_permissions.php index fdab868068..3bb66f2083 100644 --- a/phpBB/includes/acp/acp_permissions.php +++ b/phpBB/includes/acp/acp_permissions.php @@ -679,7 +679,7 @@ class acp_permissions function set_permissions($mode, $permission_type, $auth_admin, &$user_id, &$group_id) { global $db, $cache, $user, $auth; - global $request; + global $request, $phpbb_container; $psubmit = $request->variable('psubmit', array(0 => array(0 => 0))); @@ -747,7 +747,7 @@ class acp_permissions // Do we need to recache the moderator lists? if ($permission_type == 'm_') { - phpbb_cache_moderators($db, $cache, $auth); + phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth); } // Remove users who are now moderators or admins from everyones foes list @@ -768,7 +768,7 @@ class acp_permissions function set_all_permissions($mode, $permission_type, $auth_admin, &$user_id, &$group_id) { global $db, $cache, $user, $auth; - global $request; + global $request, $phpbb_container; // User or group to be set? $ug_type = (count($user_id)) ? 'user' : 'group'; @@ -817,7 +817,7 @@ class acp_permissions // Do we need to recache the moderator lists? if ($permission_type == 'm_') { - phpbb_cache_moderators($db, $cache, $auth); + phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth); } // Remove users who are now moderators or admins from everyones foes list @@ -883,7 +883,7 @@ class acp_permissions */ function remove_permissions($mode, $permission_type, $auth_admin, &$user_id, &$group_id, &$forum_id) { - global $user, $db, $cache, $auth; + global $user, $db, $cache, $auth, $phpbb_container; // User or group to be set? $ug_type = (count($user_id)) ? 'user' : 'group'; @@ -900,7 +900,7 @@ class acp_permissions // Do we need to recache the moderator lists? if ($permission_type == 'm_') { - phpbb_cache_moderators($db, $cache, $auth); + phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth); } $this->log_action($mode, 'del', $permission_type, $ug_type, (($ug_type == 'user') ? $user_id : $group_id), (count($forum_id) ? $forum_id : array(0 => 0))); @@ -1202,7 +1202,7 @@ class acp_permissions */ function copy_forum_permissions() { - global $db, $auth, $cache, $template, $user, $request; + global $db, $auth, $cache, $phpbb_container, $template, $user, $request; $user->add_lang('acp/forums'); @@ -1217,7 +1217,7 @@ class acp_permissions { if (copy_forum_permissions($src, $dest)) { - phpbb_cache_moderators($db, $cache, $auth); + phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth); $auth->acl_clear_prefetch(); $cache->destroy('sql', FORUMS_TABLE); diff --git a/phpBB/includes/functions_admin.php b/phpBB/includes/functions_admin.php index ea9ddbaf3c..a03d7bd2ec 100644 --- a/phpBB/includes/functions_admin.php +++ b/phpBB/includes/functions_admin.php @@ -2490,26 +2490,17 @@ function auto_prune($forum_id, $prune_mode, $prune_flags, $prune_days, $prune_fr * must be carried through for the moderators table. * * @param \phpbb\db\driver\driver_interface $db Database connection +* @param \phpbb\db\tools\tools_interface $db_tools Database tools * @param \phpbb\cache\driver\driver_interface $cache Cache driver * @param \phpbb\auth\auth $auth Authentication object * @return void */ -function phpbb_cache_moderators($db, $cache, $auth) +function phpbb_cache_moderators($db, $db_tools, $cache, $auth) { // Remove cached sql results $cache->destroy('sql', MODERATOR_CACHE_TABLE); - // Clear table - switch ($db->get_sql_layer()) - { - case 'sqlite3': - $db->sql_query('DELETE FROM ' . MODERATOR_CACHE_TABLE); - break; - - default: - $db->sql_query('TRUNCATE TABLE ' . MODERATOR_CACHE_TABLE); - break; - } + $db_tools->sql_truncate_table(MODERATOR_CACHE_TABLE); // We add moderators who have forum moderator permissions without an explicit ACL_NEVER setting $sql_ary = array(); diff --git a/phpBB/includes/functions_compatibility.php b/phpBB/includes/functions_compatibility.php index 95def7b7e9..c5ca053f56 100644 --- a/phpBB/includes/functions_compatibility.php +++ b/phpBB/includes/functions_compatibility.php @@ -126,8 +126,8 @@ function tz_select($default = '', $truncate = false) */ function cache_moderators() { - global $db, $cache, $auth; - phpbb_cache_moderators($db, $cache, $auth); + global $db, $cache, $auth, $phpbb_container; + phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth); } /** diff --git a/phpBB/includes/functions_convert.php b/phpBB/includes/functions_convert.php index 25043edbbf..cf2a659d77 100644 --- a/phpBB/includes/functions_convert.php +++ b/phpBB/includes/functions_convert.php @@ -1873,18 +1873,11 @@ function update_dynamic_config() */ function update_topics_posted() { - global $db; + global $db, $phpbb_container; - switch ($db->get_sql_layer()) - { - case 'sqlite3': - $db->sql_query('DELETE FROM ' . TOPICS_POSTED_TABLE); - break; - - default: - $db->sql_query('TRUNCATE TABLE ' . TOPICS_POSTED_TABLE); - break; - } + /** @var \phpbb\db\tools\tools_interface $db_tools */ + $db_tools = $phpbb_container->get('dbal.tools'); + $db_tools->sql_truncate_table(TOPICS_POSTED_TABLE); // This can get really nasty... therefore we only do the last six months $get_from_time = time() - (6 * 4 * 7 * 24 * 60 * 60); diff --git a/phpBB/includes/functions_user.php b/phpBB/includes/functions_user.php index 340f03c0c0..05b3d83a1e 100644 --- a/phpBB/includes/functions_user.php +++ b/phpBB/includes/functions_user.php @@ -2255,7 +2255,7 @@ function group_delete($group_id, $group_name = false) include($phpbb_root_path . 'includes/functions_admin.' . $phpEx); } - phpbb_cache_moderators($db, $cache, $auth); + phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth); $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_GROUP_DELETE', false, array($group_name)); @@ -3167,7 +3167,10 @@ function group_update_listings($group_id) global $phpbb_root_path, $phpEx; include($phpbb_root_path . 'includes/functions_admin.' . $phpEx); } - phpbb_cache_moderators($db, $cache, $auth); + + global $phpbb_container; + + phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth); } if ($mod_permissions || $admin_permissions) diff --git a/phpBB/install/convert/controller/convertor.php b/phpBB/install/convert/controller/convertor.php index 6c95c7dc1a..279c91d808 100644 --- a/phpBB/install/convert/controller/convertor.php +++ b/phpBB/install/convert/controller/convertor.php @@ -416,18 +416,11 @@ class convertor $this->db->sql_freeresult($result); - switch ($this->db->get_sql_layer()) - { - case 'sqlite3': - $this->db->sql_query('DELETE FROM ' . $this->session_keys_table); - $this->db->sql_query('DELETE FROM ' . $this->session_table); - break; + $tools_factory = new \phpbb\db\tools\factory(); + $db_tools = $tools_factory->get($this->db_doctrine); - default: - $this->db->sql_query('TRUNCATE TABLE ' . $this->session_keys_table); - $this->db->sql_query('TRUNCATE TABLE ' . $this->session_table); - break; - } + $db_tools->sql_truncate_table($this->session_keys_table); + $db_tools->sql_truncate_table($this->session_table); return $this->controller_helper->render('installer_convert.html', 'CONVERT_COMPLETE'); } diff --git a/phpBB/install/convert/convertor.php b/phpBB/install/convert/convertor.php index a90b7337dc..1f09943b50 100644 --- a/phpBB/install/convert/convertor.php +++ b/phpBB/install/convert/convertor.php @@ -1022,7 +1022,7 @@ class convertor $db->sql_query('DELETE FROM ' . SESSIONS_TABLE); @unlink($phpbb_container->getParameter('core.cache_dir') . 'data_global.' . $phpEx); - phpbb_cache_moderators($db, $cache, $auth); + phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth); // And finally, add a note to the log $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_INSTALL_CONVERTED', false, array($convert->convertor_data['forum_name'], $config['version'])); diff --git a/phpBB/phpbb/console/command/cache/purge.php b/phpBB/phpbb/console/command/cache/purge.php index 338bfe362d..5dfc414454 100644 --- a/phpBB/phpbb/console/command/cache/purge.php +++ b/phpBB/phpbb/console/command/cache/purge.php @@ -25,6 +25,9 @@ class purge extends \phpbb\console\command\command /** @var \phpbb\db\driver\driver_interface */ protected $db; + /** @var \phpbb\db\tools\tools_interface */ + protected $db_tools; + /** @var \phpbb\auth\auth */ protected $auth; @@ -40,14 +43,17 @@ class purge extends \phpbb\console\command\command * @param \phpbb\user $user User instance * @param \phpbb\cache\driver\driver_interface $cache Cache instance * @param \phpbb\db\driver\driver_interface $db Database connection + * @param \phpbb\db\tools\tools_interface $db_tools Database tools * @param \phpbb\auth\auth $auth Auth instance * @param \phpbb\log\log_interface $log Logger instance * @param \phpbb\config\config $config Config instance */ - public function __construct(\phpbb\user $user, \phpbb\cache\driver\driver_interface $cache, \phpbb\db\driver\driver_interface $db, \phpbb\auth\auth $auth, \phpbb\log\log_interface $log, \phpbb\config\config $config) + public function __construct(\phpbb\user $user, \phpbb\cache\driver\driver_interface $cache, \phpbb\db\driver\driver_interface $db, + \phpbb\db\tools\tools_interface $db_tools, \phpbb\auth\auth $auth, \phpbb\log\log_interface $log, \phpbb\config\config $config) { $this->cache = $cache; $this->db = $db; + $this->db_tools = $db_tools; $this->auth = $auth; $this->log = $log; $this->config = $config; @@ -82,7 +88,7 @@ class purge extends \phpbb\console\command\command // Clear permissions $this->auth->acl_clear_prefetch(); - phpbb_cache_moderators($this->db, $this->cache, $this->auth); + phpbb_cache_moderators($this->db, $this->db_tools, $this->cache, $this->auth); $this->log->add('admin', ANONYMOUS, '', 'LOG_PURGE_CACHE', time(), array()); diff --git a/phpBB/phpbb/db/tools/doctrine.php b/phpBB/phpbb/db/tools/doctrine.php index fcf14ad13f..bd3f71f83a 100644 --- a/phpBB/phpbb/db/tools/doctrine.php +++ b/phpBB/phpbb/db/tools/doctrine.php @@ -364,12 +364,9 @@ class doctrine implements tools_interface } /** - * Truncate the table - * - * @param string $table_name - * @return void + * {@inheritDoc} */ - public function sql_truncate_table(string $table_name) + public function sql_truncate_table(string $table_name): void { try { diff --git a/phpBB/phpbb/db/tools/tools_interface.php b/phpBB/phpbb/db/tools/tools_interface.php index 498a2a8828..0b8cefd8dd 100644 --- a/phpBB/phpbb/db/tools/tools_interface.php +++ b/phpBB/phpbb/db/tools/tools_interface.php @@ -207,4 +207,12 @@ interface tools_interface * @return bool|string[] True if the statements have been executed */ public function sql_create_primary_key(string $table_name, $column); + + /** + * Truncate the table + * + * @param string $table_name + * @return void + */ + public function sql_truncate_table(string $table_name): void; } diff --git a/phpBB/phpbb/install/module/install_data/task/create_search_index.php b/phpBB/phpbb/install/module/install_data/task/create_search_index.php index e5781b34b3..a8ca6886fa 100644 --- a/phpBB/phpbb/install/module/install_data/task/create_search_index.php +++ b/phpBB/phpbb/install/module/install_data/task/create_search_index.php @@ -16,6 +16,7 @@ namespace phpbb\install\module\install_data\task; use Doctrine\DBAL\Exception; use phpbb\auth\auth; use phpbb\db\driver\driver_interface; +use phpbb\db\tools\tools_interface; use phpbb\event\dispatcher; use phpbb\install\database_task; use phpbb\install\helper\config; @@ -50,6 +51,11 @@ class create_search_index extends database_task */ protected $db; + /** + * @var tools_interface + */ + protected $db_tools; + /** * @var config */ @@ -118,6 +124,7 @@ class create_search_index extends database_task $this->auth = $container->get('auth'); $this->config = $container->get('config'); $this->db = $container->get('dbal.conn'); + $this->db_tools = $container->get('dbal.tools'); $this->iohandler = $iohandler; $this->installer_config = $config; $this->phpbb_dispatcher = $container->get('event_dispatcher'); @@ -130,6 +137,7 @@ class create_search_index extends database_task $this->search_indexer = new fulltext_native( $this->config, $this->db, + $this->db_tools, $this->phpbb_dispatcher, $container->get('language'), $this->user, diff --git a/phpBB/phpbb/search/backend/fulltext_native.php b/phpBB/phpbb/search/backend/fulltext_native.php index 56161d3eb2..641d1e9cfe 100644 --- a/phpBB/phpbb/search/backend/fulltext_native.php +++ b/phpBB/phpbb/search/backend/fulltext_native.php @@ -15,6 +15,7 @@ namespace phpbb\search\backend; use phpbb\config\config; use phpbb\db\driver\driver_interface; +use phpbb\db\tools\tools_interface; use phpbb\event\dispatcher_interface; use phpbb\language\language; use phpbb\user; @@ -87,6 +88,12 @@ class fulltext_native extends base implements search_backend_interface */ protected $php_ext; + /** + * DBAL tools + * @var tools_interface + */ + protected $db_tools; + /** * phpBB event dispatcher object * @var dispatcher_interface @@ -107,6 +114,7 @@ class fulltext_native extends base implements search_backend_interface * * @param config $config Config object * @param driver_interface $db Database object + * @param tools_interface $db_tools Database tools * @param dispatcher_interface $phpbb_dispatcher Event dispatcher object * @param language $language * @param user $user User object @@ -116,13 +124,14 @@ class fulltext_native extends base implements search_backend_interface * @param string $phpbb_root_path phpBB root path * @param string $phpEx PHP file extension */ - public function __construct(config $config, driver_interface $db, dispatcher_interface $phpbb_dispatcher, + public function __construct(config $config, driver_interface $db, tools_interface $db_tools, dispatcher_interface $phpbb_dispatcher, language $language, user $user, string $search_results_table, string $search_wordlist_table, string $search_wordmatch_table, string $phpbb_root_path, string $phpEx) { global $cache; parent::__construct($cache, $config, $db, $user, $search_results_table); + $this->db_tools = $db_tools; $this->phpbb_dispatcher = $phpbb_dispatcher; $this->language = $language; @@ -1610,22 +1619,11 @@ class fulltext_native extends base implements search_backend_interface */ public function delete_index(int &$post_counter = null): ?array { - $sql_queries = []; - - switch ($this->db->get_sql_layer()) - { - case 'sqlite3': - $sql_queries[] = 'DELETE FROM ' . $this->search_wordlist_table; - $sql_queries[] = 'DELETE FROM ' . $this->search_wordmatch_table; - $sql_queries[] = 'DELETE FROM ' . $this->search_results_table; - break; - - default: - $sql_queries[] = 'TRUNCATE TABLE ' . $this->search_wordlist_table; - $sql_queries[] = 'TRUNCATE TABLE ' . $this->search_wordmatch_table; - $sql_queries[] = 'TRUNCATE TABLE ' . $this->search_results_table; - break; - } + $truncate_tables = [ + $this->search_wordlist_table, + $this->search_wordmatch_table, + $this->search_results_table, + ]; $stats = $this->stats; @@ -1633,19 +1631,22 @@ class fulltext_native extends base implements search_backend_interface * Event to modify SQL queries before the native search index is deleted * * @event core.search_native_delete_index_before - * @var array sql_queries Array with queries for deleting the search index + * * @var array stats Array with statistics of the current index (read only) + * @var array truncate_tables Array with tables that will be truncated + * * @since 3.2.3-RC1 + * @changed 4.0.0-a1 Removed sql_queries, only add/remove tables to truncate to truncate_tables */ $vars = array( - 'sql_queries', 'stats', + 'truncate_tables', ); extract($this->phpbb_dispatcher->trigger_event('core.search_native_delete_index_before', compact($vars))); - foreach ($sql_queries as $sql_query) + foreach ($truncate_tables as $table) { - $this->db->sql_query($sql_query); + $this->db_tools->sql_truncate_table($table); } return null; diff --git a/tests/console/cache/purge_test.php b/tests/console/cache/purge_test.php index 84c6ab4249..5a3e075b94 100644 --- a/tests/console/cache/purge_test.php +++ b/tests/console/cache/purge_test.php @@ -22,6 +22,7 @@ class phpbb_console_command_cache_purge_test extends phpbb_test_case protected $cache_dir; protected $cache; protected $db; + protected $db_tools; protected $config; protected $user; @@ -42,6 +43,8 @@ class phpbb_console_command_cache_purge_test extends phpbb_test_case $this->cache = new \phpbb\cache\driver\file($this->cache_dir); $this->db = $this->createMock('\phpbb\db\driver\driver_interface'); + $tools_factory = new \phpbb\db\tools\factory(); + $this->db_tools = $this->createMock('\phpbb\db\tools\doctrine'); $this->config = new \phpbb\config\config(array('assets_version' => 1)); $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); @@ -86,7 +89,7 @@ class phpbb_console_command_cache_purge_test extends phpbb_test_case public function get_command_tester() { $application = new Application(); - $application->add(new purge($this->user, $this->cache, $this->db, $this->createMock('\phpbb\auth\auth'), new \phpbb\log\dummy(), $this->config)); + $application->add(new purge($this->user, $this->cache, $this->db, $this->db_tools, $this->createMock('\phpbb\auth\auth'), new \phpbb\log\dummy(), $this->config)); $command = $application->find('cache:purge'); return new CommandTester($command); diff --git a/tests/search/native_test.php b/tests/search/native_test.php index 90702655d0..234bfff47d 100644 --- a/tests/search/native_test.php +++ b/tests/search/native_test.php @@ -16,6 +16,7 @@ require_once __DIR__ . '/../test_framework/phpbb_search_test_case.php'; class phpbb_search_native_test extends phpbb_search_test_case { protected $db; + protected $db_tools; public function getDataSet() { @@ -34,11 +35,13 @@ class phpbb_search_native_test extends phpbb_search_test_case $user = $this->createMock('\phpbb\user'); $this->db = $this->new_dbal(); + $tools_factory = new \phpbb\db\tools\factory(); + $this->db_tools = $tools_factory->get($this->new_doctrine_dbal()); $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); $class = self::get_search_wrapper('\phpbb\search\backend\fulltext_native'); $config['fulltext_native_min_chars'] = 2; $config['fulltext_native_max_chars'] = 14; - $this->search = new $class($config, $this->db, $phpbb_dispatcher, $language, $user, SEARCH_RESULTS_TABLE, SEARCH_WORDLIST_TABLE, SEARCH_WORDMATCH_TABLE, $phpbb_root_path, $phpEx); + $this->search = new $class($config, $this->db, $this->db_tools, $phpbb_dispatcher, $language, $user, SEARCH_RESULTS_TABLE, SEARCH_WORDLIST_TABLE, SEARCH_WORDMATCH_TABLE, $phpbb_root_path, $phpEx); } public function keywords()