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