]> git.mxchange.org Git - friendica.git/blob - src/Console/Maintenance.php
Convert class calls into DI calls
[friendica.git] / src / Console / Maintenance.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Console;
23
24 use Friendica\App;
25 use Friendica\Core\Config\Capability\IManageConfigValues;
26
27 /**
28  * Sets maintenance mode for this node
29  */
30 class Maintenance extends \Asika\SimpleConsole\Console
31 {
32         protected $helpOptions = ['h', 'help', '?'];
33
34         /**
35          * @var App\Mode
36          */
37         private $appMode;
38         /**
39          * @var IManageConfigValues
40          */
41         private $config;
42
43         protected function getHelp()
44         {
45                 $help = <<<HELP
46 console maintenance - Sets maintenance mode for this node
47 Usage
48         bin/console maintenance <enable> [<reason>] [-h|--help|-?] [-v]
49
50 Description
51         <enable> cen be either 0 or 1 to disabled or enable the maintenance mode on this node.
52
53         <reason> is a quote-enclosed string with the optional reason for the maintenance mode.
54
55 Examples
56         bin/console maintenance 1
57                 Enables the maintenance mode without setting a reason message
58
59         bin/console maintenance 1 "SSL certification update"
60                 Enables the maintenance mode with setting a reason message
61
62         bin/console maintenance 0
63                 Disables the maintenance mode
64
65 Options
66     -h|--help|-? Show help information
67     -v           Show more debug information.
68 HELP;
69                 return $help;
70         }
71
72         public function __construct(App\Mode $appMode, IManageConfigValues $config, $argv = null)
73         {
74                 parent::__construct($argv);
75
76                 $this->appMode = $appMode;
77                 $this->config = $config;
78         }
79
80         protected function doExecute()
81         {
82                 if ($this->getOption('v')) {
83                         $this->out('Class: ' . __CLASS__);
84                         $this->out('Arguments: ' . var_export($this->args, true));
85                         $this->out('Options: ' . var_export($this->options, true));
86                 }
87
88                 if (count($this->args) == 0) {
89                         $this->out($this->getHelp());
90                         return 0;
91                 }
92
93                 if (count($this->args) > 2) {
94                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
95                 }
96
97                 if ($this->appMode->isInstall()) {
98                         throw new \RuntimeException('Database isn\'t ready or populated yet');
99                 }
100
101                 $enabled = intval($this->getArgument(0));
102
103                 $this->config->set('system', 'maintenance', $enabled);
104
105                 $reason = $this->getArgument(1);
106
107                 if ($enabled && $this->getArgument(1)) {
108                         $this->config->set('system', 'maintenance_reason', $this->getArgument(1));
109                 } else {
110                         $this->config->set('system', 'maintenance_reason', '');
111                 }
112
113                 if ($enabled) {
114                         $mode_str = "maintenance mode";
115                 } else {
116                         $mode_str = "normal mode";
117                 }
118
119                 $this->out('System set in ' . $mode_str);
120
121                 if ($enabled && $reason != '') {
122                         $this->out('Maintenance reason: ' . $reason);
123                 }
124
125                 return 0;
126         }
127
128 }