Merge pull request #6564 from marc1706/ticket/13162

[ticket/13162] Add truncate table functionality to database tools
This commit is contained in:
Marc Alexander 2024-02-23 19:57:49 +01:00 committed by GitHub
commit 12a942747a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
22 changed files with 144 additions and 113 deletions

View file

@ -19,6 +19,7 @@ services:
- '@user' - '@user'
- '@cache.driver' - '@cache.driver'
- '@dbal.conn' - '@dbal.conn'
- '@dbal.tools'
- '@auth' - '@auth'
- '@log' - '@log'
- '@config' - '@config'

View file

@ -26,6 +26,7 @@ services:
arguments: arguments:
- '@config' - '@config'
- '@dbal.conn' - '@dbal.conn'
- '@dbal.tools'
- '@event_dispatcher' - '@event_dispatcher'
- '@language' - '@language'
- '@user' - '@user'

View file

@ -68,9 +68,10 @@ switch ($mode)
if (!$start) if (!$start)
{ {
$db->sql_query('TRUNCATE TABLE ' . POSTS_TABLE); $db_tools = $phpbb_container->get('dbal.tools');
$db->sql_query('TRUNCATE TABLE ' . TOPICS_TABLE);
// $db->sql_query('TRUNCATE TABLE ' . TOPICS_TABLE . '_prefetch'); $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'); $db->sql_query('LOCK TABLES ' . POSTS_TABLE . ' WRITE, ' . TOPICS_TABLE . ' WRITE');

View file

@ -105,7 +105,10 @@ function add_bots($bots)
$result = $db->sql_query($sql); $result = $db->sql_query($sql);
$group_id = (int) $db->sql_fetchfield('group_id', false, $result); $group_id = (int) $db->sql_fetchfield('group_id', false, $result);
$db->sql_freeresult($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) if (!$group_id)
{ {
@ -118,9 +121,6 @@ function add_bots($bots)
} }
foreach ($bots as $bot_name => $bot_ary) foreach ($bots as $bot_name => $bot_ary)
{ {
$user_row = array( $user_row = array(

View file

@ -27,7 +27,7 @@ class acp_forums
function main($id, $mode) function main($id, $mode)
{ {
global $db, $user, $auth, $template, $cache, $request, $phpbb_dispatcher; 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'); $user->add_lang('acp/forums');
$this->tpl_name = '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')))) ($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); 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; $copied_permissions = true;
} }
/* Commented out because of questionable UI workflow - re-visit for 3.0.7 /* 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) if (!empty($forum_perm_from) && $forum_perm_from != $forum_id)
{ {
copy_forum_permissions($forum_perm_from, $forum_id, true); 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(); $auth->acl_clear_prefetch();
$cache->destroy('sql', FORUMS_TABLE); $cache->destroy('sql', FORUMS_TABLE);

View file

@ -574,16 +574,8 @@ class acp_icons
// The user has already selected a smilies_pak file // The user has already selected a smilies_pak file
if ($current == 'delete') if ($current == 'delete')
{ {
switch ($db->get_sql_layer()) $db_tools = $phpbb_container->get('dbal.tools');
{ $db_tools->sql_truncate_table($table);
case 'sqlite3':
$db->sql_query('DELETE FROM ' . $table);
break;
default:
$db->sql_query('TRUNCATE TABLE ' . $table);
break;
}
switch ($mode) switch ($mode)
{ {

View file

@ -281,16 +281,8 @@ class acp_main
break; break;
case 'db_track': case 'db_track':
switch ($db->get_sql_layer()) $db_tools = $phpbb_container->get('dbal.tools');
{ $db_tools->sql_truncate_table(TOPICS_POSTED_TABLE);
case 'sqlite3':
$db->sql_query('DELETE FROM ' . TOPICS_POSTED_TABLE);
break;
default:
$db->sql_query('TRUNCATE TABLE ' . TOPICS_POSTED_TABLE);
break;
}
// This can get really nasty... therefore we only do the last six months // This can get really nasty... therefore we only do the last six months
$get_from_time = time() - (6 * 4 * 7 * 24 * 60 * 60); $get_from_time = time() - (6 * 4 * 7 * 24 * 60 * 60);
@ -370,7 +362,7 @@ class acp_main
// Clear permissions // Clear permissions
$auth->acl_clear_prefetch(); $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'); $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); $tables = array(CONFIRM_TABLE, SESSIONS_TABLE);
$db_tools = $phpbb_container->get('dbal.tools');
foreach ($tables as $table) foreach ($tables as $table)
{ {
switch ($db->get_sql_layer()) $db_tools->sql_truncate_table($table);
{
case 'sqlite3':
$db->sql_query("DELETE FROM $table");
break;
default:
$db->sql_query("TRUNCATE TABLE $table");
break;
}
} }
// let's restore the admin session // let's restore the admin session

View file

@ -679,7 +679,7 @@ class acp_permissions
function set_permissions($mode, $permission_type, $auth_admin, &$user_id, &$group_id) function set_permissions($mode, $permission_type, $auth_admin, &$user_id, &$group_id)
{ {
global $db, $cache, $user, $auth; global $db, $cache, $user, $auth;
global $request; global $request, $phpbb_container;
$psubmit = $request->variable('psubmit', array(0 => array(0 => 0))); $psubmit = $request->variable('psubmit', array(0 => array(0 => 0)));
@ -747,7 +747,7 @@ class acp_permissions
// Do we need to recache the moderator lists? // Do we need to recache the moderator lists?
if ($permission_type == 'm_') 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 // 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) function set_all_permissions($mode, $permission_type, $auth_admin, &$user_id, &$group_id)
{ {
global $db, $cache, $user, $auth; global $db, $cache, $user, $auth;
global $request; global $request, $phpbb_container;
// User or group to be set? // User or group to be set?
$ug_type = (count($user_id)) ? 'user' : 'group'; $ug_type = (count($user_id)) ? 'user' : 'group';
@ -817,7 +817,7 @@ class acp_permissions
// Do we need to recache the moderator lists? // Do we need to recache the moderator lists?
if ($permission_type == 'm_') 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 // 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) 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? // User or group to be set?
$ug_type = (count($user_id)) ? 'user' : 'group'; $ug_type = (count($user_id)) ? 'user' : 'group';
@ -900,7 +900,7 @@ class acp_permissions
// Do we need to recache the moderator lists? // Do we need to recache the moderator lists?
if ($permission_type == 'm_') 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))); $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() 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'); $user->add_lang('acp/forums');
@ -1217,7 +1217,7 @@ class acp_permissions
{ {
if (copy_forum_permissions($src, $dest)) 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(); $auth->acl_clear_prefetch();
$cache->destroy('sql', FORUMS_TABLE); $cache->destroy('sql', FORUMS_TABLE);

View file

@ -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. * must be carried through for the moderators table.
* *
* @param \phpbb\db\driver\driver_interface $db Database connection * @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\cache\driver\driver_interface $cache Cache driver
* @param \phpbb\auth\auth $auth Authentication object * @param \phpbb\auth\auth $auth Authentication object
* @return void * @return void
*/ */
function phpbb_cache_moderators($db, $cache, $auth) function phpbb_cache_moderators($db, $db_tools, $cache, $auth)
{ {
// Remove cached sql results // Remove cached sql results
$cache->destroy('sql', MODERATOR_CACHE_TABLE); $cache->destroy('sql', MODERATOR_CACHE_TABLE);
// Clear table $db_tools->sql_truncate_table(MODERATOR_CACHE_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;
}
// We add moderators who have forum moderator permissions without an explicit ACL_NEVER setting // We add moderators who have forum moderator permissions without an explicit ACL_NEVER setting
$sql_ary = array(); $sql_ary = array();

View file

@ -126,8 +126,8 @@ function tz_select($default = '', $truncate = false)
*/ */
function cache_moderators() function cache_moderators()
{ {
global $db, $cache, $auth; global $db, $cache, $auth, $phpbb_container;
phpbb_cache_moderators($db, $cache, $auth); phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth);
} }
/** /**

View file

@ -1873,18 +1873,11 @@ function update_dynamic_config()
*/ */
function update_topics_posted() function update_topics_posted()
{ {
global $db; global $db, $phpbb_container;
switch ($db->get_sql_layer()) /** @var \phpbb\db\tools\tools_interface $db_tools */
{ $db_tools = $phpbb_container->get('dbal.tools');
case 'sqlite3': $db_tools->sql_truncate_table(TOPICS_POSTED_TABLE);
$db->sql_query('DELETE FROM ' . TOPICS_POSTED_TABLE);
break;
default:
$db->sql_query('TRUNCATE TABLE ' . TOPICS_POSTED_TABLE);
break;
}
// This can get really nasty... therefore we only do the last six months // This can get really nasty... therefore we only do the last six months
$get_from_time = time() - (6 * 4 * 7 * 24 * 60 * 60); $get_from_time = time() - (6 * 4 * 7 * 24 * 60 * 60);

View file

@ -2255,7 +2255,7 @@ function group_delete($group_id, $group_name = false)
include($phpbb_root_path . 'includes/functions_admin.' . $phpEx); 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)); $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; global $phpbb_root_path, $phpEx;
include($phpbb_root_path . 'includes/functions_admin.' . $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) if ($mod_permissions || $admin_permissions)

View file

@ -416,18 +416,11 @@ class convertor
$this->db->sql_freeresult($result); $this->db->sql_freeresult($result);
switch ($this->db->get_sql_layer()) $tools_factory = new \phpbb\db\tools\factory();
{ $db_tools = $tools_factory->get($this->db_doctrine);
case 'sqlite3':
$this->db->sql_query('DELETE FROM ' . $this->session_keys_table);
$this->db->sql_query('DELETE FROM ' . $this->session_table);
break;
default: $db_tools->sql_truncate_table($this->session_keys_table);
$this->db->sql_query('TRUNCATE TABLE ' . $this->session_keys_table); $db_tools->sql_truncate_table($this->session_table);
$this->db->sql_query('TRUNCATE TABLE ' . $this->session_table);
break;
}
return $this->controller_helper->render('installer_convert.html', 'CONVERT_COMPLETE'); return $this->controller_helper->render('installer_convert.html', 'CONVERT_COMPLETE');
} }

View file

@ -1022,7 +1022,7 @@ class convertor
$db->sql_query('DELETE FROM ' . SESSIONS_TABLE); $db->sql_query('DELETE FROM ' . SESSIONS_TABLE);
@unlink($phpbb_container->getParameter('core.cache_dir') . 'data_global.' . $phpEx); @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 // 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'])); $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_INSTALL_CONVERTED', false, array($convert->convertor_data['forum_name'], $config['version']));

View file

@ -25,6 +25,9 @@ class purge extends \phpbb\console\command\command
/** @var \phpbb\db\driver\driver_interface */ /** @var \phpbb\db\driver\driver_interface */
protected $db; protected $db;
/** @var \phpbb\db\tools\tools_interface */
protected $db_tools;
/** @var \phpbb\auth\auth */ /** @var \phpbb\auth\auth */
protected $auth; protected $auth;
@ -40,14 +43,17 @@ class purge extends \phpbb\console\command\command
* @param \phpbb\user $user User instance * @param \phpbb\user $user User instance
* @param \phpbb\cache\driver\driver_interface $cache Cache instance * @param \phpbb\cache\driver\driver_interface $cache Cache instance
* @param \phpbb\db\driver\driver_interface $db Database connection * @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\auth\auth $auth Auth instance
* @param \phpbb\log\log_interface $log Logger instance * @param \phpbb\log\log_interface $log Logger instance
* @param \phpbb\config\config $config Config 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->cache = $cache;
$this->db = $db; $this->db = $db;
$this->db_tools = $db_tools;
$this->auth = $auth; $this->auth = $auth;
$this->log = $log; $this->log = $log;
$this->config = $config; $this->config = $config;
@ -82,7 +88,7 @@ class purge extends \phpbb\console\command\command
// Clear permissions // Clear permissions
$this->auth->acl_clear_prefetch(); $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()); $this->log->add('admin', ANONYMOUS, '', 'LOG_PURGE_CACHE', time(), array());

View file

@ -363,6 +363,21 @@ class doctrine implements tools_interface
); );
} }
/**
* {@inheritDoc}
*/
public function sql_truncate_table(string $table_name): void
{
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. * Returns an array of indices for either unique and primary keys, or simple indices.
* *

View file

@ -207,4 +207,12 @@ interface tools_interface
* @return bool|string[] True if the statements have been executed * @return bool|string[] True if the statements have been executed
*/ */
public function sql_create_primary_key(string $table_name, $column); 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;
} }

View file

@ -16,6 +16,7 @@ namespace phpbb\install\module\install_data\task;
use Doctrine\DBAL\Exception; use Doctrine\DBAL\Exception;
use phpbb\auth\auth; use phpbb\auth\auth;
use phpbb\db\driver\driver_interface; use phpbb\db\driver\driver_interface;
use phpbb\db\tools\tools_interface;
use phpbb\event\dispatcher; use phpbb\event\dispatcher;
use phpbb\install\database_task; use phpbb\install\database_task;
use phpbb\install\helper\config; use phpbb\install\helper\config;
@ -50,6 +51,11 @@ class create_search_index extends database_task
*/ */
protected $db; protected $db;
/**
* @var tools_interface
*/
protected $db_tools;
/** /**
* @var config * @var config
*/ */
@ -118,6 +124,7 @@ class create_search_index extends database_task
$this->auth = $container->get('auth'); $this->auth = $container->get('auth');
$this->config = $container->get('config'); $this->config = $container->get('config');
$this->db = $container->get('dbal.conn'); $this->db = $container->get('dbal.conn');
$this->db_tools = $container->get('dbal.tools');
$this->iohandler = $iohandler; $this->iohandler = $iohandler;
$this->installer_config = $config; $this->installer_config = $config;
$this->phpbb_dispatcher = $container->get('event_dispatcher'); $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->search_indexer = new fulltext_native(
$this->config, $this->config,
$this->db, $this->db,
$this->db_tools,
$this->phpbb_dispatcher, $this->phpbb_dispatcher,
$container->get('language'), $container->get('language'),
$this->user, $this->user,

View file

@ -15,6 +15,7 @@ namespace phpbb\search\backend;
use phpbb\config\config; use phpbb\config\config;
use phpbb\db\driver\driver_interface; use phpbb\db\driver\driver_interface;
use phpbb\db\tools\tools_interface;
use phpbb\event\dispatcher_interface; use phpbb\event\dispatcher_interface;
use phpbb\language\language; use phpbb\language\language;
use phpbb\user; use phpbb\user;
@ -87,6 +88,12 @@ class fulltext_native extends base implements search_backend_interface
*/ */
protected $php_ext; protected $php_ext;
/**
* DBAL tools
* @var tools_interface
*/
protected $db_tools;
/** /**
* phpBB event dispatcher object * phpBB event dispatcher object
* @var dispatcher_interface * @var dispatcher_interface
@ -107,6 +114,7 @@ class fulltext_native extends base implements search_backend_interface
* *
* @param config $config Config object * @param config $config Config object
* @param driver_interface $db Database object * @param driver_interface $db Database object
* @param tools_interface $db_tools Database tools
* @param dispatcher_interface $phpbb_dispatcher Event dispatcher object * @param dispatcher_interface $phpbb_dispatcher Event dispatcher object
* @param language $language * @param language $language
* @param user $user User object * @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 $phpbb_root_path phpBB root path
* @param string $phpEx PHP file extension * @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, language $language, user $user, string $search_results_table, string $search_wordlist_table,
string $search_wordmatch_table, string $phpbb_root_path, string $phpEx) string $search_wordmatch_table, string $phpbb_root_path, string $phpEx)
{ {
global $cache; global $cache;
parent::__construct($cache, $config, $db, $user, $search_results_table); parent::__construct($cache, $config, $db, $user, $search_results_table);
$this->db_tools = $db_tools;
$this->phpbb_dispatcher = $phpbb_dispatcher; $this->phpbb_dispatcher = $phpbb_dispatcher;
$this->language = $language; $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 public function delete_index(int &$post_counter = null): ?array
{ {
$sql_queries = []; $truncate_tables = [
$this->search_wordlist_table,
switch ($this->db->get_sql_layer()) $this->search_wordmatch_table,
{ $this->search_results_table,
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;
}
$stats = $this->stats; $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 to modify SQL queries before the native search index is deleted
* *
* @event core.search_native_delete_index_before * @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 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 * @since 3.2.3-RC1
* @changed 4.0.0-a1 Removed sql_queries, only add/remove tables to truncate to truncate_tables
*/ */
$vars = array( $vars = array(
'sql_queries',
'stats', 'stats',
'truncate_tables',
); );
extract($this->phpbb_dispatcher->trigger_event('core.search_native_delete_index_before', compact($vars))); 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; return null;

View file

@ -22,6 +22,7 @@ class phpbb_console_command_cache_purge_test extends phpbb_test_case
protected $cache_dir; protected $cache_dir;
protected $cache; protected $cache;
protected $db; protected $db;
protected $db_tools;
protected $config; protected $config;
protected $user; 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->cache = new \phpbb\cache\driver\file($this->cache_dir);
$this->db = $this->createMock('\phpbb\db\driver\driver_interface'); $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->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)); $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() public function get_command_tester()
{ {
$application = new Application(); $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'); $command = $application->find('cache:purge');
return new CommandTester($command); return new CommandTester($command);

View file

@ -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')); $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() public function test_perform_schema_changes_drop_tables()
{ {
$db_tools = $this->getMockBuilder('\phpbb\db\tools\doctrine') $db_tools = $this->getMockBuilder('\phpbb\db\tools\doctrine')

View file

@ -16,6 +16,7 @@ require_once __DIR__ . '/../test_framework/phpbb_search_test_case.php';
class phpbb_search_native_test extends phpbb_search_test_case class phpbb_search_native_test extends phpbb_search_test_case
{ {
protected $db; protected $db;
protected $db_tools;
public function getDataSet() public function getDataSet()
{ {
@ -34,11 +35,13 @@ class phpbb_search_native_test extends phpbb_search_test_case
$user = $this->createMock('\phpbb\user'); $user = $this->createMock('\phpbb\user');
$this->db = $this->new_dbal(); $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(); $phpbb_dispatcher = new phpbb_mock_event_dispatcher();
$class = self::get_search_wrapper('\phpbb\search\backend\fulltext_native'); $class = self::get_search_wrapper('\phpbb\search\backend\fulltext_native');
$config['fulltext_native_min_chars'] = 2; $config['fulltext_native_min_chars'] = 2;
$config['fulltext_native_max_chars'] = 14; $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() public function keywords()