|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * ------------------------------------------------------------------------- |
| 5 | + * Fields plugin for GLPI |
| 6 | + * ------------------------------------------------------------------------- |
| 7 | + * |
| 8 | + * LICENSE |
| 9 | + * |
| 10 | + * This file is part of Fields. |
| 11 | + * |
| 12 | + * Fields is free software; you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU General Public License as published by |
| 14 | + * the Free Software Foundation; either version 2 of the License, or |
| 15 | + * (at your option) any later version. |
| 16 | + * |
| 17 | + * Fields is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU General Public License for more details. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU General Public License |
| 23 | + * along with Fields. If not, see <http://www.gnu.org/licenses/>. |
| 24 | + * ------------------------------------------------------------------------- |
| 25 | + * @copyright Copyright (C) 2013-2022 by Fields plugin team. |
| 26 | + * @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html |
| 27 | + * @link https://github.com/pluginsGLPI/fields |
| 28 | + * ------------------------------------------------------------------------- |
| 29 | + */ |
| 30 | + |
| 31 | +use Glpi\Console\AbstractCommand; |
| 32 | +use Symfony\Component\Console\Command\Command; |
| 33 | +use Symfony\Component\Console\Input\InputInterface; |
| 34 | +use Symfony\Component\Console\Input\InputOption; |
| 35 | +use Symfony\Component\Console\Output\OutputInterface; |
| 36 | + |
| 37 | +class PluginFieldsCheckDatabaseCommand extends AbstractCommand |
| 38 | +{ |
| 39 | + protected function configure() |
| 40 | + { |
| 41 | + $this->setName('plugin:fields:check_database'); |
| 42 | + $this->setDescription(__('Check database to detect inconsistencies.', 'fields')); |
| 43 | + $this->setHelp( |
| 44 | + __('This command will chec database to detect following inconsistencies:', 'fields') |
| 45 | + . "\n" |
| 46 | + . sprintf( |
| 47 | + __('- some deleted fields may still be present in database (bug introduced in %s and fixed in version %s)', 'fields'), |
| 48 | + '1.15.0', |
| 49 | + '1.15.3' |
| 50 | + ) |
| 51 | + ); |
| 52 | + |
| 53 | + $this->addOption( |
| 54 | + 'fix', |
| 55 | + null, |
| 56 | + InputOption::VALUE_NONE, |
| 57 | + __('Use this option to actually fix database', 'fields') |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 62 | + { |
| 63 | + // Read option |
| 64 | + $fix = $input->getOption('fix'); |
| 65 | + |
| 66 | + $dead_fields = PluginFieldsMigration::checkDeadFields($fix); |
| 67 | + $dead_fields_count = count($dead_fields, COUNT_RECURSIVE) - count($dead_fields); |
| 68 | + |
| 69 | + // No invalid fields found |
| 70 | + if ($dead_fields_count === 0) { |
| 71 | + $output->writeln( |
| 72 | + '<info>' . __('Everything is in order - no action needed.', 'fields') . '</info>', |
| 73 | + ); |
| 74 | + return Command::SUCCESS; |
| 75 | + } |
| 76 | + |
| 77 | + // Indicate which fields will have been or must be deleted |
| 78 | + $error = $fix |
| 79 | + ? sprintf(__('Database was containing %s gone field(s).', 'fields'), $dead_fields_count) |
| 80 | + : sprintf(__('Database contains %s gone field(s).', 'fields'), $dead_fields_count); |
| 81 | + $output->writeln('<error>' . $error . '</error>', OutputInterface::VERBOSITY_QUIET); |
| 82 | + |
| 83 | + foreach ($dead_fields as $table => $fields) { |
| 84 | + foreach ($fields as $field) { |
| 85 | + $info = $fix |
| 86 | + ? sprintf(__('-> "%s.%s" has been deleted.', 'fields'), $table, $field) |
| 87 | + : sprintf(__('-> "%s.%s" should be deleted.', 'fields'), $table, $field); |
| 88 | + $output->writeln($info); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Show extra info in dry-run mode |
| 93 | + if (!$fix) { |
| 94 | + // Print command to do the actual deletion |
| 95 | + $next_command = sprintf( |
| 96 | + __('Run "%s" to delete the found field(s).', 'fields'), |
| 97 | + sprintf("php bin/console %s --fix", $this->getName()) |
| 98 | + ); |
| 99 | + $output->writeln( |
| 100 | + '<comment>' . $next_command . '</comment>', |
| 101 | + OutputInterface::VERBOSITY_QUIET |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + return Command::SUCCESS; |
| 106 | + } |
| 107 | +} |
0 commit comments