Skip to content

Commit

Permalink
Ensure plugin base data is migrated before user data
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne committed Dec 14, 2022
1 parent 226ab8c commit 1e47225
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 72 deletions.
43 changes: 17 additions & 26 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,6 @@ function plugin_fields_install()
$plugin_fields->getFromDBbyDir('fields');
$version = $plugin_fields->fields['version'];

$classesToInstall = [
'PluginFieldsField',
'PluginFieldsDropdown',
'PluginFieldsLabelTranslation',
'PluginFieldsContainer',
'PluginFieldsProfile',
'PluginFieldsStatusOverride',
'PluginFieldsContainerDisplayCondition',
];

$migration = new Migration($version);
if (isCommandLine()) {
Expand All @@ -71,30 +62,30 @@ function plugin_fields_install()
echo "<td align='center'>";
}

//load all classes
$dir = PLUGINFIELDS_DIR . "/inc/";
include_once("{$dir}toolbox.class.php");
$classesToInstall = [
PluginFieldsContainer::class,
PluginFieldsContainerDisplayCondition::class,
PluginFieldsDropdown::class,
PluginFieldsField::class,
PluginFieldsLabelTranslation::class,
PluginFieldsProfile::class,
PluginFieldsStatusOverride::class,
];

// First, install base data
foreach ($classesToInstall as $class) {
if ($plug = isPluginItemType($class)) {
$item = strtolower($plug['class']);
if (file_exists("$dir$item.class.php")) {
include_once("$dir$item.class.php");
}
if (method_exists($class, 'installBaseData')) {
$class::installBaseData($migration, $version);
}
}
$migration->executeMigration();

//install
// Then process specific user classes/tables
foreach ($classesToInstall as $class) {
if ($plug = isPluginItemType($class)) {
$item = strtolower($plug['class']);
if (file_exists("$dir$item.class.php")) {
if (!call_user_func([$class,'install'], $migration, $version)) {
return false;
}
}
if (method_exists($class, 'installUserData')) {
$class::installUserData($migration, $version);
}
}

$migration->executeMigration();

if (!isCommandLine()) {
Expand Down
79 changes: 52 additions & 27 deletions inc/container.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ public static function titleList()
}

/**
* Install or update containers
* Install or update plugin base data.
*
* @param Migration $migration Migration instance
* @param string $version Plugin current version
*
* @return boolean
*/
public static function install(Migration $migration, $version)
public static function installBaseData(Migration $migration, $version)
{
global $DB;

Expand Down Expand Up @@ -114,6 +114,52 @@ public static function install(Migration $migration, $version)
$migration->migrationOneTable($table);
}

return true;
}

/**
* Install or update user data.
*
* @param Migration $migration Migration instance
* @param string $version Plugin current version
*
* @return boolean
*/
public static function installUserData(Migration $migration, $version)
{
global $DB;

// -> 0.90-1.3: generated class moved
// Drop them, they will be regenerated
$obj = new self();
$containers = $obj->find();
foreach ($containers as $container) {
$itemtypes = !empty($container['itemtypes'])
? json_decode($container['itemtypes'], true)
: [];

foreach ($itemtypes as $itemtype) {
$sysname = self::getSystemName($itemtype, $container['name']);
$class_filename = $sysname . ".class.php";
if (file_exists(PLUGINFIELDS_DIR . "/inc/$class_filename")) {
unlink(PLUGINFIELDS_DIR . "/inc/$class_filename");
}

$injclass_filename = $sysname . "injection.class.php";
if (file_exists(PLUGINFIELDS_DIR . "/inc/$injclass_filename")) {
unlink(PLUGINFIELDS_DIR . "/inc/$injclass_filename");
}
}
}

// Regenerate container classes to ensure they can be used
$migration->displayMessage(__("Regenerate containers files", "fields"));
$obj = new self();
$containers = $obj->find();
foreach ($containers as $container) {
self::generateTemplate($container);
}

// Fix containers names that were generated prior to Fields 1.9.2.
$bad_named_containers = $DB->request(
[
Expand Down Expand Up @@ -216,34 +262,13 @@ public static function install(Migration $migration, $version)
}
}

// Ensure data is update before regenerating files.
$migration->executeMigration();

// Regenerate files and install missing tables
$migration->displayMessage(__("Updating generated containers files", "fields"));
$obj = new self();
$containers = $obj->find();

// -> 0.90-1.3: generated class moved
// OLD path: GLPI_ROOT."/plugins/fields/inc/$class_filename"
// NEW path: PLUGINFIELDS_CLASS_PATH . "/$class_filename"
foreach ($containers as $container) {
//First, drop old fields from plugin directories
$itemtypes = !empty($container['itemtypes'])
? json_decode($container['itemtypes'], true)
: [];

foreach ($itemtypes as $itemtype) {
$sysname = self::getSystemName($itemtype, $container['name']);
$class_filename = $sysname . ".class.php";
if (file_exists(PLUGINFIELDS_DIR . "/inc/$class_filename")) {
unlink(PLUGINFIELDS_DIR . "/inc/$class_filename");
}

$injclass_filename = $sysname . "injection.class.php";
if (file_exists(PLUGINFIELDS_DIR . "/inc/$injclass_filename")) {
unlink(PLUGINFIELDS_DIR . "/inc/$injclass_filename");
}
}
}

// Regenerate files and install missing tables
foreach ($containers as $container) {
self::create($container);
}
Expand Down
10 changes: 9 additions & 1 deletion inc/containerdisplaycondition.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ class PluginFieldsContainerDisplayCondition extends CommonDBChild
const SHOW_CONDITION_UNDER = 6;
const SHOW_CONDITION_NOT_UNDER = 7;

public static function install(Migration $migration, $version)
/**
* Install or update plugin base data.
*
* @param Migration $migration Migration instance
* @param string $version Plugin current version
*
* @return boolean
*/
public static function installBaseData(Migration $migration, $version)
{
global $DB;
$default_charset = DBConnection::getDefaultCharset();
Expand Down
25 changes: 14 additions & 11 deletions inc/dropdown.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,22 @@ class PluginFieldsDropdown
public $can_be_translated = true;

/**
* Install or update dropdowns
* Install or update user data.
*
* @param Migration $migration Migration instance
* @param string $version Plugin current version
*
* @return void
* @return boolean
*/
public static function install(Migration $migration, $version)
public static function installUserData(Migration $migration, $version)
{
$toolbox = new PluginFieldsToolbox();
$toolbox->fixFieldsNames($migration, ['type' => 'dropdown']);

$migration->displayMessage(__("Updating generated dropdown files", "fields"));

$obj = new PluginFieldsField();
$fields = $obj->find(['type' => 'dropdown']);

// -> 0.90-1.3: generated class moved
// OLD path: GLPI_ROOT."/plugins/fields/inc/$class_filename"
// NEW path: PLUGINFIELDS_CLASS_PATH . "/$class_filename"
// OLD path: GLPI_ROOT."/plugins/fields/front/$class_filename"
// NEW path: PLUGINFIELDS_FRONT_PATH . "/$class_filename"
$obj = new PluginFieldsField();
$fields = $obj->find(['type' => 'dropdown']);
foreach ($fields as $field) {
//First, drop old fields from plugin directories
$class_filename = $field['name'] . "dropdown.class.php";
Expand All @@ -74,7 +68,16 @@ public static function install(Migration $migration, $version)
}
}

$toolbox = new PluginFieldsToolbox();
$toolbox->fixFieldsNames($migration, ['type' => 'dropdown']);

// Ensure data is update before regenerating files.
$migration->executeMigration();

// Regenerate files and install missing tables
$migration->displayMessage(__("Updating generated dropdown files", "fields"));
$obj = new PluginFieldsField();
$fields = $obj->find(['type' => 'dropdown']);
foreach ($fields as $field) {
self::create($field);
}
Expand Down
4 changes: 2 additions & 2 deletions inc/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ class PluginFieldsField extends CommonDBChild
public static $items_id = 'plugin_fields_containers_id';

/**
* Install or update fields
* Install or update plugin base data.
*
* @param Migration $migration Migration instance
* @param string $version Plugin current version
*
* @return boolean
*/
public static function install(Migration $migration, $version)
public static function installBaseData(Migration $migration, $version)
{
global $DB;

Expand Down
6 changes: 3 additions & 3 deletions inc/labeltranslation.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ class PluginFieldsLabelTranslation extends CommonDBChild
public static $items_id = 'items_id';

/**
* Install or update fields
* Install or update plugin base data.
*
* @param Migration $migration Migration instance
* @param string $version Plugin current version
*
* @return boolean
* @return boolean
*/
public static function install(Migration $migration, $version)
public static function installBaseData(Migration $migration, $version)
{
global $DB;

Expand Down
10 changes: 9 additions & 1 deletion inc/profile.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ class PluginFieldsProfile extends CommonDBRelation
public static $itemtype_2 = Profile::class;
public static $items_id_2 = 'profiles_id';

public static function install(Migration $migration)
/**
* Install or update plugin base data.
*
* @param Migration $migration Migration instance
* @param string $version Plugin current version
*
* @return boolean
*/
public static function installBaseData(Migration $migration, $version)
{
global $DB;

Expand Down
10 changes: 9 additions & 1 deletion inc/statusoverride.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ class PluginFieldsStatusOverride extends CommonDBChild
public static $itemtype = PluginFieldsField::class;
public static $items_id = 'plugin_fields_fields_id';

public static function install(Migration $migration, $version)
/**
* Install or update plugin base data.
*
* @param Migration $migration Migration instance
* @param string $version Plugin current version
*
* @return boolean
*/
public static function installBaseData(Migration $migration, $version)
{
global $DB;

Expand Down

0 comments on commit 1e47225

Please sign in to comment.