Merge remote-tracking branch 'upstream/develop' into update-self
[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 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 .htconfig.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                 if ($this->getOption('v')) {
83                         $this->out('Executable: ' . $this->executable);
84                         $this->out('Class: ' . __CLASS__);
85                         $this->out('Arguments: ' . var_export($this->args, true));
86                         $this->out('Options: ' . var_export($this->options, true));
87                 }
88
89                 if (count($this->args) > 3) {
90                         throw new CommandArgsException('Too many arguments');
91                 }
92
93                 require_once '.htconfig.php';
94                 $result = dba::connect($db_host, $db_user, $db_pass, $db_data);
95                 unset($db_host, $db_user, $db_pass, $db_data);
96
97                 if (!$result) {
98                         throw new \RuntimeException('Unable to connect to database');
99                 }
100
101                 if (count($this->args) == 3) {
102                         Core\Config::set($this->getArgument(0), $this->getArgument(1), $this->getArgument(2));
103                         $this->out("config[{$this->getArgument(0)}][{$this->getArgument(1)}] = " . Core\Config::get($this->getArgument(0),
104                                         $this->getArgument(1)));
105                 }
106
107                 if (count($this->args) == 2) {
108                         $this->out("config[{$this->getArgument(0)}][{$this->getArgument(1)}] = " . Core\Config::get($this->getArgument(0),
109                                         $this->getArgument(1)));
110                 }
111
112                 if (count($this->args) == 1) {
113                         Core\Config::load($this->getArgument(0));
114
115                         $a = get_app();
116                         if (!is_null($a->config[$this->getArgument(0)])) {
117                                 foreach ($a->config[$this->getArgument(0)] as $k => $x) {
118                                         $this->out("config[{$this->getArgument(0)}][{$k}] = " . $x);
119                                 }
120                         } else {
121                                 $this->out('Config section ' . $this->getArgument(0) . ' returned nothing');
122                         }
123                 }
124
125                 if (count($this->args) == 0) {
126                         $configs = dba::select('config');
127                         foreach ($configs as $config) {
128                                 $this->out("config[{$config['cat']}][{$config['k']}] = " . $config['v']);
129                         }
130                 }
131
132                 return 0;
133         }
134
135 }