]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/AutomaticInstallation.php
Merge pull request #6942 from annando/worker-fast-commands
[friendica.git] / src / Core / Console / AutomaticInstallation.php
1 <?php
2
3 namespace Friendica\Core\Console;
4
5 use Asika\SimpleConsole\Console;
6 use Friendica\BaseObject;
7 use Friendica\Core\Config;
8 use Friendica\Core\Installer;
9 use Friendica\Core\Theme;
10 use Friendica\Util\Config\ConfigFileLoader;
11 use RuntimeException;
12
13 class AutomaticInstallation extends Console
14 {
15         protected function getHelp()
16         {
17                 return <<<HELP
18 Installation - Install Friendica automatically
19 Synopsis
20         bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
21
22 Description
23     Installs Friendica with data based on the local.config.php file or environment variables
24
25 Notes
26     Not checking .htaccess/URL-Rewrite during CLI installation.
27
28 Options
29     -h|--help|-?            Show help information
30     -v                      Show more debug information.
31     -a                      All setup checks are required (except .htaccess)
32     -f|--file <config>      prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
33     -s|--savedb             Save the DB credentials to the file (if environment variables is used)
34     -H|--dbhost <host>      The host of the mysql/mariadb database (env MYSQL_HOST)
35     -p|--dbport <port>      The port of the mysql/mariadb database (env MYSQL_PORT)
36     -d|--dbdata <database>  The name of the mysql/mariadb database (env MYSQL_DATABASE)
37     -U|--dbuser <username>  The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
38     -P|--dbpass <password>  The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
39     -u|--urlpath <url_path> The URL path of Friendica - f.e. '/friendica' (env FRIENDICA_URL_PATH) 
40     -b|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH) 
41     -A|--admin <mail>       The admin email address of Friendica (env FRIENDICA_ADMIN_MAIL)
42     -T|--tz <timezone>      The timezone of Friendica (env FRIENDICA_TZ)
43     -L|--lang <language>    The language of Friendica (env FRIENDICA_LANG)
44  
45 Environment variables
46    MYSQL_HOST                  The host of the mysql/mariadb database (mandatory if mysql and environment is used)
47    MYSQL_PORT                  The port of the mysql/mariadb database
48    MYSQL_USERNAME|MYSQL_USER   The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
49    MYSQL_PASSWORD              The password of the mysql/mariadb database login
50    MYSQL_DATABASE              The name of the mysql/mariadb database
51    FRIENDICA_URL_PATH          The URL path of Friendica (f.e. '/friendica')
52    FRIENDICA_PHP_PATH          The path of the PHP binary
53    FRIENDICA_ADMIN_MAIL        The admin email address of Friendica (this email will be used for admin access)
54    FRIENDICA_TZ                The timezone of Friendica
55    FRIENDICA_LANG              The langauge of Friendica
56    
57 Examples
58         bin/console autoinstall -f 'input.config.php
59                 Installs Friendica with the prepared 'input.config.php' file
60
61         bin/console autoinstall --savedb
62                 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
63
64         bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
65                 Installs Friendica with a local mysql database with credentials
66 HELP;
67         }
68
69         protected function doExecute()
70         {
71                 // Initialise the app
72                 $this->out("Initializing setup...\n");
73
74                 $a = BaseObject::getApp();
75
76                 $installer = new Installer();
77
78                 $configCache = $a->getConfigCache();
79
80                 $this->out(" Complete!\n\n");
81
82                 // Check Environment
83                 $this->out("Checking environment...\n");
84
85                 $installer->resetChecks();
86
87                 if (!$this->runBasicChecks($installer, $configCache)) {
88                         $errorMessage = $this->extractErrors($installer->getChecks());
89                         throw new RuntimeException($errorMessage);
90                 }
91
92                 $this->out(" Complete!\n\n");
93
94                 // if a config file is set,
95                 $config_file = $this->getOption(['f', 'file']);
96
97                 if (!empty($config_file)) {
98                         if ($config_file != 'config' . DIRECTORY_SEPARATOR . 'local.config.php') {
99                                 // Copy config file
100                                 $this->out("Copying config file...\n");
101                                 if (!copy($a->getBasePath() . DIRECTORY_SEPARATOR . $config_file, $a->getBasePath() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
102                                         throw new RuntimeException("ERROR: Saving config file failed. Please copy '$config_file' to '" . $a->getBasePath() . "'"  . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "local.config.php' manually.\n");
103                                 }
104                         }
105
106                         //reload the config cache
107                         $loader = new ConfigFileLoader($a->getBasePath(), $a->getMode());
108                         $loader->setupCache($configCache);
109
110                 } else {
111                         // Creating config file
112                         $this->out("Creating config file...\n");
113
114                         $save_db = $this->getOption(['s', 'savedb'], false);
115
116                         //$db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? (getenv('MYSQL_HOST') ? getenv('MYSQL_HOST') : Installer::DEFAULT_HOST) : '');
117                         $db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? (getenv('MYSQL_HOST')) : Installer::DEFAULT_HOST);
118                         $db_port = $this->getOption(['p', 'dbport'], ($save_db) ? getenv('MYSQL_PORT') : null);
119                         $configCache->set('database', 'hostname', $db_host . (!empty($db_port) ? ':' . $db_port : ''));
120                         $configCache->set('database', 'database',
121                                 $this->getOption(['d', 'dbdata'],
122                                         ($save_db) ? getenv('MYSQL_DATABASE') : ''));
123                         $configCache->set('database', 'username',
124                                 $this->getOption(['U', 'dbuser'],
125                                         ($save_db) ? getenv('MYSQL_USER') . getenv('MYSQL_USERNAME') : ''));
126                         $configCache->set('database', 'password',
127                                 $this->getOption(['P', 'dbpass'],
128                                         ($save_db) ? getenv('MYSQL_PASSWORD') : ''));
129                         $php_path = $this->getOption(['b', 'phppath'], !empty('FRIENDICA_PHP_PATH') ? getenv('FRIENDICA_PHP_PATH') : null);
130                         if (!empty($php_path)) {
131                                 $configCache->set('config', 'php_path', $php_path);
132                         }
133                         $configCache->set('config', 'admin_email',
134                                 $this->getOption(['A', 'admin'],
135                                         !empty(getenv('FRIENDICA_ADMIN_MAIL')) ? getenv('FRIENDICA_ADMIN_MAIL') : ''));
136                         $configCache->set('system', 'default_timezone',
137                                 $this->getOption(['T', 'tz'],
138                                         !empty(getenv('FRIENDICA_TZ')) ? getenv('FRIENDICA_TZ') : Installer::DEFAULT_TZ));
139                         $configCache->set('system', 'language',
140                                 $this->getOption(['L', 'lang'],
141                                         !empty(getenv('FRIENDICA_LANG')) ? getenv('FRIENDICA_LANG') : Installer::DEFAULT_LANG));
142
143
144                         if (empty($php_path)) {
145                                 $configCache->set('config', 'php_path', $installer->getPHPPath());
146                         }
147
148                         $installer->createConfig(
149                                 $a,
150                                 $configCache,
151                                 $a->getBasePath()
152                         );
153                 }
154
155                 $this->out(" Complete!\n\n");
156
157                 // Check database connection
158                 $this->out("Checking database...\n");
159
160                 $installer->resetChecks();
161
162                 if (!$installer->checkDB($a->getBasePath(), $configCache, $a->getProfiler())) {
163                         $errorMessage = $this->extractErrors($installer->getChecks());
164                         throw new RuntimeException($errorMessage);
165                 }
166
167                 $this->out(" Complete!\n\n");
168
169                 // Install database
170                 $this->out("Inserting data into database...\n");
171
172                 $installer->resetChecks();
173
174                 if (!$installer->installDatabase($a->getBasePath())) {
175                         $errorMessage = $this->extractErrors($installer->getChecks());
176                         throw new RuntimeException($errorMessage);
177                 }
178
179                 $this->out(" Complete!\n\n");
180
181                 // Install theme
182                 $this->out("Installing theme\n");
183                 if (!empty(Config::get('system', 'theme'))) {
184                         Theme::install(Config::get('system', 'theme'));
185                         $this->out(" Complete\n\n");
186                 } else {
187                         $this->out(" Theme setting is empty. Please check the file 'config/local.config.php'\n\n");
188                 }
189
190                 $this->out("\nInstallation is finished\n");
191
192                 return 0;
193         }
194
195         /**
196          * @param Installer                 $installer   The Installer instance
197          * @param Config\Cache\IConfigCache $configCache The config cache
198          *
199          * @return bool true if checks were successfully, otherwise false
200          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
201          */
202         private function runBasicChecks(Installer $installer, Config\Cache\IConfigCache $configCache)
203         {
204                 $checked = true;
205
206                 $installer->resetChecks();
207                 if (!$installer->checkFunctions())              {
208                         $checked = false;
209                 }
210                 if (!$installer->checkImagick()) {
211                         $checked = false;
212                 }
213                 if (!$installer->checkLocalIni()) {
214                         $checked = false;
215                 }
216                 if (!$installer->checkSmarty3()) {
217                         $checked = false;
218                 }
219                 if (!$installer->checkKeys()) {
220                         $checked = false;
221                 }
222
223                 $php_path = null;
224                 if ($configCache->has('config', 'php_path')) {
225                         $php_path = $configCache->get('config', 'php_path');
226                 }
227
228                 if (!$installer->checkPHP($php_path, true)) {
229                         $checked = false;
230                 }
231
232                 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
233
234                 return $checked;
235         }
236
237         /**
238          * @param array $results
239          * @return string
240          */
241         private function extractErrors($results)
242         {
243                 $errorMessage = '';
244                 $allChecksRequired = $this->getOption('a') !== null;
245
246                 foreach ($results as $result) {
247                         if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
248                                 $errorMessage .= "--------\n";
249                                 $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
250                         }
251                 }
252
253                 return $errorMessage;
254         }
255 }