1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-07-30 21:40:43 +02:00

[ticket/16643] Refactor installer tasks to use Doctrine DBAL

PHPBB3-16643
This commit is contained in:
Máté Bartus
2021-01-16 11:57:35 +01:00
parent aab2679966
commit 2a57477718
19 changed files with 1102 additions and 676 deletions

View File

@@ -13,10 +13,21 @@
namespace phpbb\install\module\install_data\task;
use phpbb\install\exception\resource_limit_reached_exception;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Statement;
use phpbb\install\database_task;
use phpbb\install\helper\config;
use phpbb\install\helper\container_factory;
use phpbb\install\helper\database;
use phpbb\install\helper\iohandler\iohandler_interface;
use phpbb\install\sequential_task;
use phpbb\language\language;
class add_bots extends \phpbb\install\task_base
class add_bots extends database_task
{
use sequential_task;
/**
* A list of the web-crawlers/bots we recognise by default
*
@@ -106,22 +117,17 @@ class add_bots extends \phpbb\install\task_base
);
/**
* @var \phpbb\db\driver\driver_interface
*/
protected $db;
/**
* @var \phpbb\install\helper\config
* @var config
*/
protected $install_config;
/**
* @var \phpbb\install\helper\iohandler\iohandler_interface
* @var iohandler_interface
*/
protected $io_handler;
/**
* @var \phpbb\language\language
* @var language
*/
protected $language;
@@ -135,31 +141,59 @@ class add_bots extends \phpbb\install\task_base
*/
protected $php_ext;
/**
* @var string
*/
protected $groups_table;
/**
* @var string
*/
protected $bots_table;
/**
* @var DriverStatement|Statement
*/
protected $stmt;
/**
* @var int
*/
protected $group_id;
/**
* Constructor
*
* @param \phpbb\install\helper\config $install_config Installer's config
* @param \phpbb\install\helper\iohandler\iohandler_interface $iohandler Input-output handler for the installer
* @param \phpbb\install\helper\container_factory $container Installer's DI container
* @param \phpbb\language\language $language Language provider
* @param string $phpbb_root_path Relative path to phpBB root
* @param string $php_ext PHP extension
* @param config $install_config Installer's config
* @param database $db_helper Database helper.
* @param iohandler_interface $iohandler Input-output handler for the installer
* @param container_factory $container Installer's DI container
* @param language $language Language provider
* @param string $phpbb_root_path Relative path to phpBB root
* @param string $php_ext PHP extension
*/
public function __construct(\phpbb\install\helper\config $install_config,
\phpbb\install\helper\iohandler\iohandler_interface $iohandler,
\phpbb\install\helper\container_factory $container,
\phpbb\language\language $language,
$phpbb_root_path,
$php_ext)
public function __construct(config $install_config,
database $db_helper,
iohandler_interface $iohandler,
container_factory $container,
language $language,
string $phpbb_root_path,
string $php_ext)
{
parent::__construct(true);
$this->db = $container->get('dbal.conn');
$this->install_config = $install_config;
$this->io_handler = $iohandler;
$this->language = $language;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
$this->bots_table = $container->get_parameter('tables.bots');
$this->groups_table = $container->get_parameter('tables.groups');
parent::__construct(
self::get_doctrine_connection($db_helper, $install_config),
$this->io_handler,
true
);
}
/**
@@ -167,89 +201,84 @@ class add_bots extends \phpbb\install\task_base
*/
public function run()
{
$this->db->sql_return_on_error(true);
$this->group_id = $this->install_config->get('bots_group_id');
if ($this->group_id === false)
{
$sql = 'SELECT group_id FROM ' . $this->groups_table . " WHERE group_name = 'BOTS'";
$result = $this->query($sql);
$sql = 'SELECT group_id
FROM ' . GROUPS_TABLE . "
WHERE group_name = 'BOTS'";
$result = $this->db->sql_query($sql);
$group_id = (int) $this->db->sql_fetchfield('group_id');
$this->db->sql_freeresult($result);
try
{
$this->group_id = (int) $result->fetchOne();
$result->free();
}
catch (Exception $e)
{
$this->group_id = 0;
}
if (!$group_id)
$this->install_config->set('bots_group_id', $this->group_id);
}
if (!$this->group_id)
{
// If we reach this point then something has gone very wrong
$this->io_handler->add_error_message('NO_GROUP');
}
$i = $this->install_config->get('add_bot_index', 0);
$bot_list = array_slice($this->bot_list, $i);
foreach ($bot_list as $bot_name => $bot_ary)
{
$user_row = array(
'user_type' => USER_IGNORE,
'group_id' => $group_id,
'username' => $bot_name,
'user_regdate' => time(),
'user_password' => '',
'user_colour' => '9E8DA7',
'user_email' => '',
'user_lang' => $this->install_config->get('default_lang'),
'user_style' => 1,
'user_timezone' => 'UTC',
'user_dateformat' => $this->language->lang('default_dateformat'),
'user_allow_massemail' => 0,
'user_allow_pm' => 0,
);
if (!function_exists('user_add'))
{
include($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);
}
$user_id = user_add($user_row);
if (!$user_id)
{
// If we can't insert this user then continue to the next one to avoid inconsistent data
$this->io_handler->add_error_message('CONV_ERROR_INSERT_BOT');
$i++;
continue;
}
$sql = 'INSERT INTO ' . BOTS_TABLE . ' ' . $this->db->sql_build_array('INSERT', array(
'bot_active' => 1,
'bot_name' => (string) $bot_name,
'user_id' => (int) $user_id,
'bot_agent' => (string) $bot_ary[0],
'bot_ip' => (string) $bot_ary[1],
));
$this->db->sql_query($sql);
$i++;
// Stop execution if resource limit is reached
if ($this->install_config->get_time_remaining() <= 0 || $this->install_config->get_memory_remaining() <= 0)
{
break;
}
}
$this->install_config->set('add_bot_index', $i);
if ($i < count($this->bot_list))
{
throw new resource_limit_reached_exception();
}
$sql = 'INSERT INTO ' . $this->bots_table . ' '
. '(bot_active, bot_name, user_id, bot_agent, bot_ip) VALUES '
. '(:bot_active, :bot_name, :user_id, :bot_agent, :bot_ip)';
$this->stmt = $this->create_prepared_stmt($sql);
$this->execute($this->install_config, $this->bot_list);
}
/**
* {@inheritdoc}
*/
static public function get_step_count()
protected function execute_step($key, $value) : void
{
if (!function_exists('user_add'))
{
include($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);
}
$user_id = user_add([
'user_type' => USER_IGNORE,
'group_id' => $this->group_id,
'username' => $key,
'user_regdate' => time(),
'user_password' => '',
'user_colour' => '9E8DA7',
'user_email' => '',
'user_lang' => $this->install_config->get('default_lang'),
'user_style' => 1,
'user_timezone' => 'UTC',
'user_dateformat' => $this->language->lang('default_dateformat'),
'user_allow_massemail' => 0,
'user_allow_pm' => 0,
]);
if (!$user_id)
{
// If we can't insert this user then continue to the next one to avoid inconsistent data
$this->io_handler->add_error_message('CONV_ERROR_INSERT_BOT');
return;
}
$this->exec_prepared_stmt($this->stmt, [
'bot_active' => 1,
'bot_name' => (string) $key,
'user_id' => (int) $user_id,
'bot_agent' => (string) $value[0],
'bot_ip' => (string) $value[1],
]);
}
/**
* {@inheritdoc}
*/
static public function get_step_count() : int
{
return 1;
}
@@ -257,7 +286,7 @@ class add_bots extends \phpbb\install\task_base
/**
* {@inheritdoc}
*/
public function get_task_lang_name()
public function get_task_lang_name() : string
{
return 'TASK_ADD_BOTS';
}

View File

@@ -13,39 +13,66 @@
namespace phpbb\install\module\install_data\task;
class add_languages extends \phpbb\install\task_base
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\Statement;
use phpbb\install\database_task;
use phpbb\install\helper\config;
use phpbb\install\helper\container_factory;
use phpbb\install\helper\database;
use phpbb\install\helper\iohandler\iohandler_interface;
use phpbb\install\sequential_task;
use phpbb\language\language_file_helper;
class add_languages extends database_task
{
/**
* @var \phpbb\db\driver\driver_interface
*/
protected $db;
use sequential_task;
/**
* @var \phpbb\install\helper\iohandler\iohandler_interface
* @var config
*/
protected $config;
/**
* @var iohandler_interface
*/
protected $iohandler;
/**
* @var \phpbb\language\language_file_helper
* @var language_file_helper
*/
protected $language_helper;
/**
* @var string
*/
protected $lang_table;
/**
* @var DriverStatement|Statement
*/
protected $stmt;
/**
* Constructor
*
* @param \phpbb\install\helper\iohandler\iohandler_interface $iohandler Installer's input-output handler
* @param \phpbb\install\helper\container_factory $container Installer's DI container
* @param \phpbb\language\language_file_helper $language_helper Language file helper service
* @param config $config Installer config.
* @param database $db_helper Database helper.
* @param iohandler_interface $iohandler Installer's input-output handler
* @param container_factory $container Installer's DI container
* @param language_file_helper $language_helper Language file helper service
*/
public function __construct(\phpbb\install\helper\iohandler\iohandler_interface $iohandler,
\phpbb\install\helper\container_factory $container,
\phpbb\language\language_file_helper $language_helper)
public function __construct(config $config,
database $db_helper,
iohandler_interface $iohandler,
container_factory $container,
language_file_helper $language_helper)
{
$this->db = $container->get('dbal.conn');
$this->config = $config;
$this->iohandler = $iohandler;
$this->language_helper = $language_helper;
$this->lang_table = $container->get_parameter('tables.lang');
parent::__construct(true);
parent::__construct(self::get_doctrine_connection($db_helper, $config), $this->iohandler,true);
}
/**
@@ -53,60 +80,36 @@ class add_languages extends \phpbb\install\task_base
*/
public function run()
{
$this->db->sql_return_on_error(true);
$languages = $this->language_helper->get_available_languages();
$installed_languages = array();
foreach ($languages as $lang_info)
{
$lang_pack = array(
'lang_iso' => $lang_info['iso'],
'lang_dir' => $lang_info['iso'],
'lang_english_name' => htmlspecialchars($lang_info['name'], ENT_COMPAT),
'lang_local_name' => htmlspecialchars($lang_info['local_name'], ENT_COMPAT, 'UTF-8'),
'lang_author' => htmlspecialchars($lang_info['author'], ENT_COMPAT, 'UTF-8'),
);
$this->db->sql_query('INSERT INTO ' . LANG_TABLE . ' ' . $this->db->sql_build_array('INSERT', $lang_pack));
$installed_languages[] = (int) $this->db->sql_nextid();
if ($this->db->get_sql_error_triggered())
{
$error = $this->db->sql_error($this->db->get_sql_error_sql());
$this->iohandler->add_error_message($error['message']);
}
}
$sql = 'SELECT * FROM ' . PROFILE_FIELDS_TABLE;
$result = $this->db->sql_query($sql);
$insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, PROFILE_LANG_TABLE);
while ($row = $this->db->sql_fetchrow($result))
{
foreach ($installed_languages as $lang_id)
{
$insert_buffer->insert(array(
'field_id' => $row['field_id'],
'lang_id' => $lang_id,
// Remove phpbb_ from field name
'lang_name' => strtoupper(substr($row['field_name'], 6)),
'lang_explain' => '',
'lang_default_value' => '',
));
}
}
$this->db->sql_freeresult($result);
$insert_buffer->flush();
$sql = 'INSERT INTO ' . $this->lang_table
. ' (lang_iso, lang_dir, lang_english_name, lang_local_name, lang_author)'
. ' VALUES (:lang_iso, :lang_dir, :lang_english_name, :lang_local_name, :lang_author)';
$this->stmt = $this->create_prepared_stmt($sql);
$this->execute($this->config, $languages);
}
/**
* {@inheritdoc}
*/
static public function get_step_count()
protected function execute_step($key, $value) : void
{
$this->exec_prepared_stmt($this->stmt, [
'lang_iso' => $value['iso'],
'lang_dir' => $value['iso'],
'lang_english_name' => htmlspecialchars($value['name'], ENT_COMPAT),
'lang_local_name' => htmlspecialchars($value['local_name'], ENT_COMPAT, 'UTF-8'),
'lang_author' => htmlspecialchars($value['author'], ENT_COMPAT, 'UTF-8'),
]);
$installed_languages = $this->config->get('installed_languages', []);
array_push($installed_languages, (int) $this->get_last_insert_id());
$this->config->set('installed_languages', $installed_languages);
}
/**
* {@inheritdoc}
*/
static public function get_step_count() : int
{
return 1;
}
@@ -114,7 +117,7 @@ class add_languages extends \phpbb\install\task_base
/**
* {@inheritdoc}
*/
public function get_task_lang_name()
public function get_task_lang_name() : string
{
return 'TASK_ADD_LANGUAGES';
}

View File

@@ -141,7 +141,7 @@ class add_modules extends \phpbb\install\task_base
/**
* Constructor
*
* @parma config $config Installer's config
* @param config $config Installer's config
* @param iohandler_interface $iohandler Installer's input-output handler
* @param container_factory $container Installer's DI container
*/

View File

@@ -13,31 +13,53 @@
namespace phpbb\install\module\install_data\task;
use Doctrine\DBAL\Exception;
use phpbb\auth\auth;
use phpbb\db\driver\driver_interface;
use phpbb\event\dispatcher;
use phpbb\config\config;
use phpbb\install\database_task;
use phpbb\install\helper\config;
use phpbb\install\helper\container_factory;
use phpbb\install\helper\database;
use phpbb\install\helper\iohandler\iohandler_interface;
use phpbb\install\sequential_task;
use phpbb\search\fulltext_native;
use phpbb\user;
class create_search_index extends \phpbb\install\task_base
class create_search_index extends database_task
{
use sequential_task;
/**
* @var auth
*/
protected $auth;
/**
* @var config
* @var \phpbb\config\config
*/
protected $config;
/**
* @var \Doctrine\DBAL\Connection
*/
protected $conn;
/**
* @var driver_interface
*/
protected $db;
/**
* @var config
*/
protected $installer_config;
/**
* @var iohandler_interface
*/
protected $iohandler;
/**
* @var dispatcher
*/
@@ -48,6 +70,11 @@ class create_search_index extends \phpbb\install\task_base
*/
protected $user;
/**
* @var fulltext_native
*/
protected $search_indexer;
/**
* @var string phpBB root path
*/
@@ -58,26 +85,59 @@ class create_search_index extends \phpbb\install\task_base
*/
protected $php_ext;
/**
* @var string
*/
protected $posts_table;
/**
* @var mixed
*/
protected $error;
/**
* Constructor
*
* @param config $config phpBB config
* @param config $config Installer config.
* @param database $db_helper Database helper.
* @param container_factory $container Installer's DI container
* @param iohandler_interface $iohandler IO manager.
* @param string $phpbb_root_path phpBB root path
* @param string $php_ext PHP file extension
*/
public function __construct(config $config, container_factory $container,
$phpbb_root_path, $php_ext)
public function __construct(
config $config,
database $db_helper,
container_factory $container,
iohandler_interface $iohandler,
string $phpbb_root_path,
string $php_ext)
{
$this->conn = self::get_doctrine_connection($db_helper, $config);
$this->auth = $container->get('auth');
$this->config = $config;
$this->config = $container->get('config');
$this->db = $container->get('dbal.conn');
$this->iohandler = $iohandler;
$this->installer_config = $config;
$this->phpbb_dispatcher = $container->get('dispatcher');
$this->user = $container->get('user');
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
parent::__construct(true);
$this->error = false;
$this->search_indexer = new fulltext_native(
$this->error,
$this->phpbb_root_path,
$this->php_ext,
$this->auth,
$this->config,
$this->db,
$this->user,
$this->phpbb_dispatcher
);
parent::__construct($this->conn, $iohandler, true);
}
/**
@@ -88,33 +148,39 @@ class create_search_index extends \phpbb\install\task_base
// Make sure fulltext native load update is set
$this->config->set('fulltext_native_load_upd', 1);
$error = false;
$search = new fulltext_native(
$error,
$this->phpbb_root_path,
$this->php_ext,
$this->auth,
$this->config,
$this->db,
$this->user,
$this->phpbb_dispatcher
);
$sql = 'SELECT post_id, post_subject, post_text, poster_id, forum_id
FROM ' . POSTS_TABLE;
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
try
{
$search->index('post', $row['post_id'], $row['post_text'], $row['post_subject'], $row['poster_id'], $row['forum_id']);
$sql = 'SELECT post_id, post_subject, post_text, poster_id, forum_id FROM ' . $this->posts_table;
$rows = $this->conn->fetchAllAssociative($sql);
}
$this->db->sql_freeresult($result);
catch (Exception $e)
{
$this->iohandler->add_error_message('INST_ERR_DB', $e->getMessage());
$rows = [];
}
$this->execute($this->installer_config, $rows);
}
/**
* {@inheritdoc}
*/
static public function get_step_count()
protected function execute_step($key, $value) : void
{
$this->search_indexer->index(
'post',
$value['post_id'],
$value['post_text'],
$value['post_subject'],
$value['poster_id'],
$value['forum_id']
);
}
/**
* {@inheritdoc}
*/
static public function get_step_count() : int
{
return 1;
}
@@ -122,7 +188,7 @@ class create_search_index extends \phpbb\install\task_base
/**
* {@inheritdoc}
*/
public function get_task_lang_name()
public function get_task_lang_name() : string
{
return 'TASK_CREATE_SEARCH_INDEX';
}

View File

@@ -0,0 +1,151 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\install\module\install_data\task;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Statement;
use phpbb\install\database_task;
use phpbb\install\helper\config;
use phpbb\install\helper\container_factory;
use phpbb\install\helper\database;
use phpbb\install\helper\iohandler\iohandler_interface;
use phpbb\install\sequential_task;
class setup_languages extends database_task
{
use sequential_task;
/**
* @var config
*/
protected $config;
/**
* @var Connection
*/
protected $db;
/**
* @var iohandler_interface
*/
protected $iohandler;
/**
* @var DriverStatement|Statement
*/
protected $stmt;
/**
* @var array
*/
protected $installed_languages;
/**
* @var string
*/
protected $profile_fields_table;
/**
* @var string
*/
protected $profile_lang_table;
/**
* Constructor.
*
* @param config $config
* @param database $db_helper
* @param iohandler_interface $io
* @param container_factory $container
*/
public function __construct(config $config,
database $db_helper,
iohandler_interface $io,
container_factory $container)
{
$this->config = $config;
$this->db = self::get_doctrine_connection($db_helper, $config);
$this->iohandler = $io;
$this->profile_fields_table = $container->get_parameter('tables.profile_fields');
$this->profile_lang_table = $container->get_parameter('tables.profile_fields_language');
parent::__construct($this->db, $io, true);
}
/**
* {@inheritdoc}
*/
public function run()
{
$this->installed_languages = $this->config->get('installed_languages', []);
$profile_fields = $this->config->get('profile_field_rows', false);
if ($profile_fields === false)
{
try
{
$rows = $this->db->fetchAllAssociative('SELECT * FROM ' . $this->profile_fields_table);
}
catch (Exception $e)
{
$this->iohandler->add_error_message('INST_ERR_DB', $e->getMessage());
$rows = [];
}
$this->config->set('profile_field_rows', $rows);
$profile_fields = $rows;
}
$sql = 'INSERT INTO ' . $this->profile_lang_table
. ' (field_id, lang_id, lang_name, lang_explain, lang_default_value)'
. " VALUES (:field_id, :lang_id, :lang_name, '', '')";
$this->stmt = $this->create_prepared_stmt($sql);
$this->execute($this->config, $profile_fields);
}
/**
* {@inheritdoc}
*/
protected function execute_step($key, $value) : void
{
foreach ($this->installed_languages as $lang_id)
{
$this->exec_prepared_stmt($this->stmt, [
'field_id' => $value['field_id'],
'lang_id' => $lang_id,
// Remove phpbb_ from field name
'lang_name' => strtoupper(substr($value['field_name'], 6)),
]);
}
}
/**
* {@inheritdoc}
*/
static public function get_step_count() : int
{
return 1;
}
/**
* {@inheritdoc}
*/
public function get_task_lang_name() : string
{
return 'TASK_SET_LANGUAGES';
}
}