enabletimezonescommand.class.php 3.69 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
 * ---------------------------------------------------------------------
 * GLPI - Gestionnaire Libre de Parc Informatique
 * Copyright (C) 2015-2021 Teclib' and contributors.
 *
 * http://glpi-project.org
 *
 * based on GLPI - Gestionnaire Libre de Parc Informatique
 * Copyright (C) 2003-2014 by the INDEPNET Development Team.
 *
 * ---------------------------------------------------------------------
 *
 * LICENSE
 *
 * This file is part of GLPI.
 *
 * GLPI 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.
 *
 * GLPI 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 GLPI. If not, see <http://www.gnu.org/licenses/>.
 * ---------------------------------------------------------------------
 */

namespace Glpi\Console\Database;

if (!defined('GLPI_ROOT')) {
   die("Sorry. You can't access this file directly");
}

use DBConnection;
use Glpi\Console\AbstractCommand;
use Glpi\System\Requirement\DbTimezones;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class EnableTimezonesCommand extends AbstractCommand {

   /**
    * Error code returned if DB configuration file cannot be updated.
    *
    * @var integer
    */
   const ERROR_UNABLE_TO_UPDATE_CONFIG = 1;

   /**
    * Error code returned if prerequisites are missing.
    *
    * @var integer
    */
   const ERROR_MISSING_PREREQUISITES = 2;

   /**
    * Error code returned if some tables are still using datetime field type.
    *
    * @var integer
    */
   const ERROR_TIMESTAMP_FIELDS_REQUIRED = 3;

   protected function configure() {
      parent::configure();

      $this->setName('glpi:database:enable_timezones');
      $this->setAliases(['db:enable_timezones']);
      $this->setDescription(__('Enable timezones usage.'));
   }

   protected function execute(InputInterface $input, OutputInterface $output) {
      $timezones_requirement = new DbTimezones($this->db);

      if (!$timezones_requirement->isValidated()) {
         $message = __('Timezones usage cannot be activated due to following errors:');
         foreach ($timezones_requirement->getValidationMessages() as $validation_message) {
            $message .= "\n - " . $validation_message;
         }
         throw new \Glpi\Console\Exception\EarlyExitException(
            '<error>' . $message . '</error>',
            self::ERROR_MISSING_PREREQUISITES
         );
      }

      if (($datetime_count = $this->db->getTzIncompatibleTables()->count()) > 0) {
         $message = sprintf(__('%1$s columns are not still using datetime field type.'), $datetime_count)
            . ' '
            . sprintf(__('Run the "php bin/console %1$s" command to migrate them.'), 'glpi:migration:timestamps');
         throw new \Glpi\Console\Exception\EarlyExitException(
            '<error>' . $message . '</error>',
            self::ERROR_TIMESTAMP_FIELDS_REQUIRED
         );
      }

      if (!DBConnection::updateConfigProperty(DBConnection::PROPERTY_USE_TIMEZONES, true)) {
         throw new \Glpi\Console\Exception\EarlyExitException(
            '<error>' . __('Unable to update DB configuration file.') . '</error>',
            self::ERROR_UNABLE_TO_UPDATE_CONFIG
         );
      }

      $output->writeln('<info>' . __('Timezone usage has been enabled.') . '</info>');

      return 0; // Success
   }
}