Update "mrpetovan" email address
[friendica.git/.git] / src / Core / Console / Config.php
1 <?php
2
3 /*
4  * To change this license header, choose License Headers in Project Properties.
5  * To change this template file, choose Tools | Templates
6  * and open the template in the editor.
7  */
8
9 namespace Friendica\Core\Console;
10
11 use Asika\SimpleConsole\CommandArgsException;
12 use Friendica\App;
13 use Friendica\Core;
14 use RuntimeException;
15
16 require_once 'include/dba.php';
17 require_once 'include/text.php';
18
19 /**
20  * @brief tool to access the system config from the CLI
21  *
22  * With this script you can access the system configuration of your node from
23  * the CLI. You can do both, reading current values stored in the database and
24  * set new values to config variables.
25  *
26  * Usage:
27  *   If you specify no parameters at the CLI, the script will list all config
28  *   variables defined.
29  *
30  *   If you specify one parameter, the script will list all config variables
31  *   defined in this section of the configuration (e.g. "system").
32  *
33  *   If you specify two parameters, the script will show you the current value
34  *   of the named configuration setting. (e.g. "system loglevel")
35  *
36  *   If you specify three parameters, the named configuration setting will be
37  *   set to the value of the last parameter. (e.g. "system loglevel 0" will
38  *   disable logging)
39  *
40  * @author Tobias Diekershoff <tobias.diekershoff@gmx.net>
41  * @author Hypolite Petovan <hypolite@mrpetovan.com>
42  */
43 class Config extends \Asika\SimpleConsole\Console
44 {
45         protected $helpOptions = ['h', 'help', '?'];
46
47         protected function getHelp()
48         {
49                 $help = <<<HELP
50 console config - Manage site configuration
51 Synopsis
52         bin/console config [-h|--help|-?] [-v]
53         bin/console config <category> [-h|--help|-?] [-v]
54         bin/console config <category> <key> [-h|--help|-?] [-v]
55         bin/console config <category> <key> <value> [-h|--help|-?] [-v]
56
57 Description
58         bin/console config
59                 Lists all config values
60
61         bin/console config <category>
62                 Lists all config values in the provided category
63
64         bin/console config <category> <key>
65                 Shows the value of the provided key in the category
66
67         bin/console config <category> <key> <value>
68                 Sets the value of the provided key in the category
69
70 Notes:
71         Setting config entries which are manually set in config/local.ini.php may result in
72         conflict between database settings and the manual startup settings.
73
74 Options
75     -h|--help|-? Show help information
76     -v           Show more debug information.
77 HELP;
78                 return $help;
79         }
80
81         protected function doExecute()
82         {
83                 $a = get_app();
84
85                 if ($this->getOption('v')) {
86                         $this->out('Executable: ' . $this->executable);
87                         $this->out('Class: ' . __CLASS__);
88                         $this->out('Arguments: ' . var_export($this->args, true));
89                         $this->out('Options: ' . var_export($this->options, true));
90                 }
91
92                 if (count($this->args) > 3) {
93                         throw new CommandArgsException('Too many arguments');
94                 }
95
96                 if (!($a->mode & App::MODE_DBCONFIGAVAILABLE)) {
97                         $this->out('Database isn\'t ready or populated yet, showing file config only');
98                 }
99
100                 if (count($this->args) == 3) {
101                         $cat = $this->getArgument(0);
102                         $key = $this->getArgument(1);
103                         $value = $this->getArgument(2);
104
105                         if (is_array(Core\Config::get($cat, $key))) {
106                                 throw new RuntimeException("$cat.$key is an array and can't be set using this command.");
107                         }
108
109                         $result = Core\Config::set($cat, $key, $value);
110                         if ($result) {
111                                 $this->out("{$cat}.{$key} <= " .
112                                         Core\Config::get($cat, $key));
113                         } else {
114                                 $this->out("Unable to set {$cat}.{$key}");
115                         }
116                 }
117
118                 if (count($this->args) == 2) {
119                         $cat = $this->getArgument(0);
120                         $key = $this->getArgument(1);
121                         $value = Core\Config::get($this->getArgument(0), $this->getArgument(1));
122
123                         if (is_array($value)) {
124                                 foreach ($value as $k => $v) {
125                                         $this->out("{$cat}.{$key}[{$k}] => " . $v);
126                                 }
127                         } else {
128                                 $this->out("{$cat}.{$key} => " . $value);
129                         }
130                 }
131
132                 if (count($this->args) == 1) {
133                         $cat = $this->getArgument(0);
134                         Core\Config::load($cat);
135
136                         if (!is_null($a->config[$cat])) {
137                                 $this->out("[{$cat}]");
138                                 foreach ($a->config[$cat] as $key => $value) {
139                                         if (is_array($value)) {
140                                                 foreach ($value as $k => $v) {
141                                                         $this->out("{$key}[{$k}] => " . $v);
142                                                 }
143                                         } else {
144                                                 $this->out("{$key} => " . $value);
145                                         }
146                                 }
147                         } else {
148                                 $this->out('Config section ' . $this->getArgument(0) . ' returned nothing');
149                         }
150                 }
151
152                 if (count($this->args) == 0) {
153                         Core\Config::load();
154
155                         if (Core\Config::get('system', 'config_adapter') == 'jit' && $a->mode & App::MODE_DBCONFIGAVAILABLE) {
156                                 $this->out('Warning: The JIT (Just In Time) Config adapter doesn\'t support loading the entire configuration, showing file config only');
157                         }
158
159                         foreach ($a->config as $cat => $section) {
160                                 if (is_array($section)) {
161                                         foreach ($section as $key => $value) {
162                                                 if (is_array($value)) {
163                                                         foreach ($value as $k => $v) {
164                                                                 $this->out("{$cat}.{$key}[{$k}] => " . $v);
165                                                         }
166                                                 } else {
167                                                         $this->out("{$cat}.{$key} => " . $value);
168                                                 }
169                                         }
170                                 } else {
171                                         $this->out("config.{$cat} => " . $section);
172                                 }
173                         }
174                 }
175
176                 return 0;
177         }
178 }