349 lines
8.4 KiB
PHP
349 lines
8.4 KiB
PHP
<?php
|
|
|
|
/**
|
|
* -------------------------------------------------------------------------
|
|
* UrBackup plugin for GLPI
|
|
* -------------------------------------------------------------------------
|
|
*
|
|
* Install/update process for GLPI 11.
|
|
*
|
|
* GLPI 11 Migration class does not expose createTable()/addTable().
|
|
* Compatible GLPI plugin pattern:
|
|
*
|
|
* - initial schema creation from SQL file using $DB->runFile()
|
|
* - schema evolution using Migration addField(), addKey(), executeMigration()
|
|
*
|
|
* -------------------------------------------------------------------------
|
|
*/
|
|
|
|
use GlpiPlugin\Urbackup\Config;
|
|
use GlpiPlugin\Urbackup\Profile;
|
|
|
|
if (!defined('GLPI_ROOT')) {
|
|
die('Sorry. You cannot access this file directly.');
|
|
}
|
|
|
|
/**
|
|
* Install or update plugin database schema and default data.
|
|
*
|
|
* @return bool
|
|
*/
|
|
function plugin_urbackup_install_process(): bool
|
|
{
|
|
$migration = new Migration(PLUGIN_URBACKUP_VERSION);
|
|
|
|
$migration->displayMessage(__('UrBackup plugin installation', 'urbackup'));
|
|
|
|
plugin_urbackup_install_create_initial_schema($migration);
|
|
|
|
plugin_urbackup_install_update_configs_table($migration);
|
|
plugin_urbackup_install_update_assettypes_table($migration);
|
|
plugin_urbackup_install_update_servers_table($migration);
|
|
plugin_urbackup_install_update_serverassets_table($migration);
|
|
plugin_urbackup_install_update_profiles_table($migration);
|
|
|
|
$migration->executeMigration();
|
|
|
|
Config::ensureDefaultConfiguration();
|
|
Profile::installRights();
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Create initial database schema from SQL file.
|
|
*
|
|
* @param Migration $migration Migration instance
|
|
*
|
|
* @return void
|
|
*/
|
|
function plugin_urbackup_install_create_initial_schema(Migration $migration): void
|
|
{
|
|
global $DB;
|
|
|
|
$required_tables = [
|
|
'glpi_plugin_urbackup_configs',
|
|
'glpi_plugin_urbackup_assettypes',
|
|
'glpi_plugin_urbackup_servers',
|
|
'glpi_plugin_urbackup_serverassets',
|
|
'glpi_plugin_urbackup_profiles',
|
|
];
|
|
|
|
$missing_table_found = false;
|
|
|
|
foreach ($required_tables as $table) {
|
|
if (!$DB->tableExists($table)) {
|
|
$missing_table_found = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!$missing_table_found) {
|
|
return;
|
|
}
|
|
|
|
$migration->displayMessage(__('Creating UrBackup plugin database schema', 'urbackup'));
|
|
|
|
$db_file = PLUGIN_URBACKUP_DIR . '/install/mysql/plugin_urbackup-empty.sql';
|
|
|
|
if (!file_exists($db_file)) {
|
|
$migration->displayMessage(
|
|
sprintf(
|
|
__('Database schema file not found: %s', 'urbackup'),
|
|
$db_file
|
|
)
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
if (!$DB->runFile($db_file)) {
|
|
$migration->displayMessage(__('Error while creating UrBackup plugin database schema', 'urbackup'));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update configs table.
|
|
*
|
|
* @param Migration $migration Migration instance
|
|
*
|
|
* @return void
|
|
*/
|
|
function plugin_urbackup_install_update_configs_table(Migration $migration): void
|
|
{
|
|
global $DB;
|
|
|
|
$table = 'glpi_plugin_urbackup_configs';
|
|
|
|
if (!$DB->tableExists($table)) {
|
|
return;
|
|
}
|
|
|
|
$migration->addField($table, 'name', 'string', [
|
|
'value' => '',
|
|
'after' => 'id',
|
|
]);
|
|
|
|
$migration->addField($table, 'value', 'text', [
|
|
'after' => 'name',
|
|
]);
|
|
|
|
$migration->addField($table, 'date_creation', 'timestamp');
|
|
$migration->addField($table, 'date_mod', 'timestamp');
|
|
|
|
$migration->addKey($table, 'name');
|
|
}
|
|
|
|
/**
|
|
* Update assettypes table.
|
|
*
|
|
* @param Migration $migration Migration instance
|
|
*
|
|
* @return void
|
|
*/
|
|
function plugin_urbackup_install_update_assettypes_table(Migration $migration): void
|
|
{
|
|
global $DB;
|
|
|
|
$table = 'glpi_plugin_urbackup_assettypes';
|
|
|
|
if (!$DB->tableExists($table)) {
|
|
return;
|
|
}
|
|
|
|
$migration->addField($table, 'itemtype', 'string', [
|
|
'value' => '',
|
|
'after' => 'id',
|
|
]);
|
|
|
|
$migration->addField($table, 'is_active', 'bool', [
|
|
'value' => 0,
|
|
'after' => 'itemtype',
|
|
]);
|
|
|
|
$migration->addField($table, 'is_default', 'bool', [
|
|
'value' => 0,
|
|
'after' => 'is_active',
|
|
]);
|
|
|
|
$migration->addField($table, 'date_creation', 'timestamp');
|
|
$migration->addField($table, 'date_mod', 'timestamp');
|
|
|
|
$migration->addKey($table, 'itemtype');
|
|
$migration->addKey($table, 'is_active');
|
|
}
|
|
|
|
/**
|
|
* Update servers table.
|
|
*
|
|
* @param Migration $migration Migration instance
|
|
*
|
|
* @return void
|
|
*/
|
|
function plugin_urbackup_install_update_servers_table(Migration $migration): void
|
|
{
|
|
global $DB;
|
|
|
|
$table = 'glpi_plugin_urbackup_servers';
|
|
|
|
if (!$DB->tableExists($table)) {
|
|
return;
|
|
}
|
|
|
|
$migration->addField($table, 'entities_id', 'integer', [
|
|
'value' => 0,
|
|
'after' => 'id',
|
|
]);
|
|
|
|
$migration->addField($table, 'is_recursive', 'bool', [
|
|
'value' => 0,
|
|
'after' => 'entities_id',
|
|
]);
|
|
|
|
$migration->addField($table, 'name', 'string', [
|
|
'value' => '',
|
|
'after' => 'is_recursive',
|
|
]);
|
|
|
|
$migration->addField($table, 'locations_id', 'integer', [
|
|
'value' => 0,
|
|
'after' => 'name',
|
|
]);
|
|
|
|
$migration->addField($table, 'ip_address', 'string', [
|
|
'value' => '',
|
|
'after' => 'locations_id',
|
|
]);
|
|
|
|
$migration->addField($table, 'port', 'integer', [
|
|
'value' => 55414,
|
|
'after' => 'ip_address',
|
|
]);
|
|
|
|
$migration->addField($table, 'protocol', 'string', [
|
|
'value' => 'http',
|
|
'after' => 'port',
|
|
]);
|
|
|
|
$migration->addField($table, 'server_version', 'string', [
|
|
'after' => 'protocol',
|
|
]);
|
|
|
|
$migration->addField($table, 'api_username', 'string', [
|
|
'after' => 'server_version',
|
|
]);
|
|
|
|
$migration->addField($table, 'api_password', 'text', [
|
|
'after' => 'api_username',
|
|
]);
|
|
|
|
$migration->addField($table, 'ignore_ssl', 'bool', [
|
|
'value' => 0,
|
|
'after' => 'api_password',
|
|
]);
|
|
|
|
$migration->addField($table, 'is_active', 'bool', [
|
|
'value' => 1,
|
|
'after' => 'ignore_ssl',
|
|
]);
|
|
|
|
$migration->addField($table, 'last_api_status', 'bool', [
|
|
'value' => 0,
|
|
'after' => 'is_active',
|
|
]);
|
|
|
|
$migration->addField($table, 'last_api_message', 'text', [
|
|
'after' => 'last_api_status',
|
|
]);
|
|
|
|
$migration->addField($table, 'last_api_check', 'timestamp', [
|
|
'after' => 'last_api_message',
|
|
]);
|
|
|
|
$migration->addField($table, 'comment', 'text', [
|
|
'after' => 'last_api_check',
|
|
]);
|
|
|
|
$migration->addField($table, 'date_creation', 'timestamp');
|
|
$migration->addField($table, 'date_mod', 'timestamp');
|
|
|
|
$migration->addKey($table, 'name');
|
|
$migration->addKey($table, 'entities_id');
|
|
$migration->addKey($table, 'locations_id');
|
|
$migration->addKey($table, 'is_active');
|
|
}
|
|
|
|
/**
|
|
* Update serverassets table.
|
|
*
|
|
* @param Migration $migration Migration instance
|
|
*
|
|
* @return void
|
|
*/
|
|
function plugin_urbackup_install_update_serverassets_table(Migration $migration): void
|
|
{
|
|
global $DB;
|
|
|
|
$table = 'glpi_plugin_urbackup_serverassets';
|
|
|
|
if (!$DB->tableExists($table)) {
|
|
return;
|
|
}
|
|
|
|
$migration->addField($table, 'plugin_urbackup_servers_id', 'integer', [
|
|
'value' => 0,
|
|
'after' => 'id',
|
|
]);
|
|
|
|
$migration->addField($table, 'itemtype', 'string', [
|
|
'value' => '',
|
|
'after' => 'plugin_urbackup_servers_id',
|
|
]);
|
|
|
|
$migration->addField($table, 'items_id', 'integer', [
|
|
'value' => 0,
|
|
'after' => 'itemtype',
|
|
]);
|
|
|
|
$migration->addKey($table, 'plugin_urbackup_servers_id');
|
|
$migration->addKey($table, ['itemtype', 'items_id'], 'item');
|
|
}
|
|
|
|
/**
|
|
* Update profiles table.
|
|
*
|
|
* @param Migration $migration Migration instance
|
|
*
|
|
* @return void
|
|
*/
|
|
function plugin_urbackup_install_update_profiles_table(Migration $migration): void
|
|
{
|
|
global $DB;
|
|
|
|
$table = 'glpi_plugin_urbackup_profiles';
|
|
|
|
if (!$DB->tableExists($table)) {
|
|
return;
|
|
}
|
|
|
|
$migration->addField($table, 'profiles_id', 'integer', [
|
|
'value' => 0,
|
|
'after' => 'id',
|
|
]);
|
|
|
|
$migration->addField($table, 'rightname', 'string', [
|
|
'value' => '',
|
|
'after' => 'profiles_id',
|
|
]);
|
|
|
|
$migration->addField($table, 'rights', 'integer', [
|
|
'value' => 0,
|
|
'after' => 'rightname',
|
|
]);
|
|
|
|
$migration->addField($table, 'date_creation', 'timestamp');
|
|
$migration->addField($table, 'date_mod', 'timestamp');
|
|
|
|
$migration->addKey($table, ['profiles_id', 'rightname'], 'profile_right');
|
|
}
|