Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to fix incorrectly deleted fields from containers tables #525

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions inc/containertemplate.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

abstract class PluginFieldsContainerTemplate extends CommonDBTM
{
abstract public static function get_ITEMTYPE(): string;

abstract public static function get_CONTAINER(): string;

public static function install()
{
global $DB;

$default_charset = DBConnection::getDefaultCharset();
$default_collation = DBConnection::getDefaultCollation();
$default_key_sign = DBConnection::getDefaultPrimaryKeySignOption();

$obj = new static();
$table = $obj->getTable();

// create Table
$itemtype = static::get_ITEMTYPE();
$container = static::get_CONTAINER();
if (!$DB->tableExists($table)) {
$query = "CREATE TABLE IF NOT EXISTS `$table` (
`id` INT {$default_key_sign} NOT NULL auto_increment,
`items_id` INT {$default_key_sign} NOT NULL,
`itemtype` VARCHAR(255) DEFAULT '$itemtype',
`plugin_fields_containers_id` INT {$default_key_sign} NOT NULL DEFAULT '$container',
PRIMARY KEY (`id`),
UNIQUE INDEX `itemtype_item_container`
(`itemtype`, `items_id`, `plugin_fields_containers_id`)
) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;";
$DB->query($query) or die($DB->error());
}
}

public static function uninstall()
{
global $DB;

$obj = new static();
return $DB->query("DROP TABLE IF EXISTS `" . $obj->getTable() . "`");
}

public static function addField($fieldname, $type)
{
$migration = new PluginFieldsMigration(0);

$sql_fields = PluginFieldsMigration::getSQLFields($fieldname, $type);
foreach ($sql_fields as $sql_field_name => $sql_field_type) {
$migration->addField(static::getTable(), $sql_field_name, $sql_field_type);
}

$migration->migrationOneTable(static::getTable());
}

public static function removeField($fieldname, $type)
{
$migration = new PluginFieldsMigration(0);

$sql_fields = PluginFieldsMigration::getSQLFields($fieldname, $type);
foreach (array_keys($sql_fields) as $sql_field_name) {
$migration->dropField(static::getTable(), $sql_field_name);
}

$migration->migrationOneTable(static::getTable());
}
}
19 changes: 1 addition & 18 deletions inc/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,6 @@ function prepareInputForAdd($input) {
);
return false;
}

$oldname = $input['name'];
$input['name'] = getForeignKeyFieldForItemType(
PluginFieldsDropdown::getClassname($input['name']));
}

// Before adding, add the ranking of the new field
Expand All @@ -192,10 +188,6 @@ function prepareInputForAdd($input) {
}
}

if (isset($oldname)) {
$input['name'] = $oldname;
}

if (isset($input['allowed_values'])) {
$input['allowed_values'] = Sanitizer::dbEscape(json_encode($input['allowed_values']));
}
Expand All @@ -211,15 +203,10 @@ function pre_deleteItem() {
&& !isset($_SESSION['uninstall_fields'])
&& !isset($_SESSION['delete_container'])) {

if ($this->fields['type'] === "dropdown") {
$oldname = $this->fields['name'];
$this->fields['name'] = getForeignKeyFieldForItemType(
PluginFieldsDropdown::getClassname($this->fields['name']));
}

$container_obj = new PluginFieldsContainer;
$container_obj->getFromDB($this->fields['plugin_fields_containers_id']);
foreach (json_decode($container_obj->fields['itemtypes']) as $itemtype) {

$classname = PluginFieldsContainer::getClassname($itemtype, $container_obj->fields['name']);
$classname::removeField($this->fields['name'], $this->fields['type']);
}
Expand All @@ -232,10 +219,6 @@ function pre_deleteItem() {
'items_id' => $this->fields['id']
]);

if (isset($oldname)) {
$this->fields['name'] = $oldname;
}

if ($this->fields['type'] === "dropdown") {
return PluginFieldsDropdown::destroy($this->fields['name']);
}
Expand Down
100 changes: 100 additions & 0 deletions inc/fixdroppedfieldscommand.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

/**
* -------------------------------------------------------------------------
* Fields plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Fields.
*
* Fields is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Fields is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Fields. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2013-2022 by Fields plugin team.
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
* @link https://github.com/pluginsGLPI/fields
* -------------------------------------------------------------------------
*/

use Glpi\Console\AbstractCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class PluginFieldsFixDroppedFieldsCommand extends AbstractCommand
{
protected function configure()
{
$this->setName('plugin:fields:fixdroppedfields');
$this->setAliases(['fields:fixdroppedfields']);
$this->setDescription(
'Remove fields that were wrongly kept in the database following an '
. 'issue introduced in 1.15.0 and fixed in 1.15.3.'
);

$this->addOption(
"delete",
null,
InputOption::VALUE_NONE,
"Use this option to actually delete data"
);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
// Read option
$delete = $input->getOption("delete");

$fields = PluginFieldsMigration::fixDroppedFields(!$delete);

// No invalid fields found
if (!count($fields)) {
$output->writeln(
__("Everything is in order - no action needed.", 'fields'),
);
return Command::SUCCESS;
}

// Indicate which fields will have been or must be deleted
foreach ($fields as $field) {
if ($delete) {
$info = sprintf(__("-> %s was deleted.", 'fields'), $field);
} else {
$info = sprintf(__("-> %s must be deleted.", 'fields'), $field);
}

$output->writeln($info);
}

// Show extra info in dry-run mode
if (!$delete) {
$fields_found = sprintf(
__("%s field(s) need to be deleted.", 'fields'),
count($fields)
);
$output->writeln($fields_found);

// Print command to do the actual deletion
$next_command = sprintf(
__("Run \"%s\" to delete the found field(s).", 'fields'),
"php bin/console plugin:fields:fixdroppedfields --delete"
);
$output->writeln($next_command);
}

return Command::SUCCESS;
}
}
Loading