]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/Config.php
3b77d5400980a8d0cf291a19f60226fd50d390c1
[friendica.git] / src / Core / Console / Config.php
1 <?php\r
2 \r
3 /*\r
4  * To change this license header, choose License Headers in Project Properties.\r
5  * To change this template file, choose Tools | Templates\r
6  * and open the template in the editor.\r
7  */\r
8 \r
9 namespace Friendica\Core\Console;\r
10 \r
11 use Asika\SimpleConsole\CommandArgsException;\r
12 use dba;\r
13 use Friendica\Core;\r
14 \r
15 require_once 'include/dba.php';\r
16 require_once 'include/text.php';\r
17 \r
18 /**\r
19  * @brief tool to access the system config from the CLI\r
20  *\r
21  * With this script you can access the system configuration of your node from\r
22  * the CLI. You can do both, reading current values stored in the database and\r
23  * set new values to config variables.\r
24  *\r
25  * Usage:\r
26  *   If you specify no parameters at the CLI, the script will list all config\r
27  *   variables defined.\r
28  *\r
29  *   If you specify one parameter, the script will list all config variables\r
30  *   defined in this section of the configuration (e.g. "system").\r
31  *\r
32  *   If you specify two parameters, the script will show you the current value\r
33  *   of the named configuration setting. (e.g. "system loglevel")\r
34  *\r
35  *   If you specify three parameters, the named configuration setting will be\r
36  *   set to the value of the last parameter. (e.g. "system loglevel 0" will\r
37  *   disable logging)\r
38  *\r
39  * @author Tobias Diekershoff\r
40  * @author Hypolite Petovan <mrpetovan@gmail.com>\r
41  */\r
42 class Config extends \Asika\SimpleConsole\Console\r
43 {\r
44         protected $helpOptions = ['h', 'help', '?'];\r
45 \r
46         protected function getHelp()\r
47         {\r
48                 $help = <<<HELP\r
49 console config - Manage site configuration\r
50 Synopsis\r
51         bin/console config [-h|--help|-?] [-v]\r
52         bin/console config <category> [-h|--help|-?] [-v]\r
53         bin/console config <category> <key> [-h|--help|-?] [-v]\r
54         bin/console config <category> <key> <value> [-h|--help|-?] [-v]\r
55 \r
56 Description\r
57         bin/console config\r
58                 Lists all config values\r
59 \r
60         bin/console config <category>\r
61                 Lists all config values in the provided category\r
62 \r
63         bin/console config <category> <key>\r
64                 Shows the value of the provided key in the category\r
65 \r
66         bin/console config <category> <key> <value>\r
67                 Set the value of the provided key in the category\r
68 \r
69 Options\r
70     -h|--help|-? Show help information\r
71     -v           Show more debug information.\r
72 HELP;\r
73                 return $help;\r
74         }\r
75 \r
76         protected function doExecute()\r
77         {\r
78                 if ($this->getOption('v')) {\r
79                         $this->out('Executable: ' . $this->executable);\r
80                         $this->out('Class: ' . __CLASS__);\r
81                         $this->out('Arguments: ' . var_export($this->args, true));\r
82                         $this->out('Options: ' . var_export($this->options, true));\r
83                 }\r
84 \r
85                 if (count($this->args) > 3) {\r
86                         throw new CommandArgsException('Too many arguments');\r
87                 }\r
88 \r
89                 require_once '.htconfig.php';\r
90                 $result = dba::connect($db_host, $db_user, $db_pass, $db_data);\r
91                 unset($db_host, $db_user, $db_pass, $db_data);\r
92 \r
93                 if (!$result) {\r
94                         throw new \RuntimeException('Unable to connect to database');\r
95                 }\r
96 \r
97                 if (count($this->args) == 3) {\r
98                         Core\Config::set($this->getArgument(0), $this->getArgument(1), $this->getArgument(2));\r
99                         $this->out("config[{$this->getArgument(0)}][{$this->getArgument(1)}] = " . Core\Config::get($this->getArgument(0),\r
100                                         $this->getArgument(1)));\r
101                 }\r
102 \r
103                 if (count($this->args) == 2) {\r
104                         $this->out("config[{$this->getArgument(0)}][{$this->getArgument(1)}] = " . Core\Config::get($this->getArgument(0),\r
105                                         $this->getArgument(1)));\r
106                 }\r
107 \r
108                 if (count($this->args) == 1) {\r
109                         Core\Config::load($this->getArgument(0));\r
110 \r
111                         $a = get_app();\r
112                         if (!is_null($a->config[$this->getArgument(0)])) {\r
113                                 foreach ($a->config[$this->getArgument(0)] as $k => $x) {\r
114                                         $this->out("config[{$this->getArgument(0)}][{$k}] = " . $x);\r
115                                 }\r
116                         } else {\r
117                                 $this->out('Config section ' . $this->getArgument(0) . ' returned nothing');\r
118                         }\r
119                 }\r
120 \r
121                 if (count($this->args) == 0) {\r
122                         $configs = dba::select('config');\r
123                         foreach ($configs as $config) {\r
124                                 $this->out("config[{$config['cat']}][{$config['k']}] = " . $config['v']);\r
125                         }\r
126                 }\r
127 \r
128                 return 0;\r
129         }\r
130 \r
131 }\r