]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/AutomaticInstallation.php
Update use statement lists with new Friendica\Database\dba class
[friendica.git] / src / Core / Console / AutomaticInstallation.php
1 <?php
2
3 namespace Friendica\Core\Console;
4
5 use Asika\SimpleConsole\Console;
6 use Friendica\App;
7 use Friendica\Core\Config;
8 use Friendica\Core\Install;
9 use Friendica\Core\Theme;
10 use Friendica\Database\dba;
11 use RuntimeException;
12
13 require_once 'mod/install.php';
14 require_once 'include/dba.php';
15
16 class AutomaticInstallation extends Console
17 {
18         protected function getHelp()
19         {
20                 return <<<HELP
21 Installation - Install Friendica automatically
22 Synopsis
23         bin/console autoinstall [-h|--help|-?] [-v] [-a]
24
25 Description
26     Installs Friendica with data based on the htconfig.php file
27
28 Notes:
29     Not checking .htaccess/URL-Rewrite during CLI installation.
30
31 Options
32     -h|--help|-? Show help information
33     -v           Show more debug information.
34     -a           All setup checks are required (except .htaccess)
35     -f           prepared config file (e.g. ".htconfig.php" itself)
36 HELP;
37         }
38
39         protected function doExecute()
40         {
41                 // Initialise the app
42                 $this->out("Initializing setup...\n");
43
44                 $a = get_app();
45                 $db_host = '';
46                 $db_user = '';
47                 $db_pass = '';
48                 $db_data = '';
49
50                 $config_file = $this->getOption('f', 'htconfig.php');
51
52                 $this->out("Using config $config_file...\n");
53                 require_once $config_file;
54
55                 Install::setInstallMode();
56
57                 $this->out(" Complete!\n\n");
58
59                 // Check basic setup
60                 $this->out("Checking basic setup...\n");
61
62                 $checkResults = [];
63                 $checkResults['basic'] = $this->runBasicChecks($a);
64                 $errorMessage = $this->extractErrors($checkResults['basic']);
65
66                 if ($errorMessage !== '') {
67                         throw new RuntimeException($errorMessage);
68                 }
69
70                 $this->out(" Complete!\n\n");
71
72                 // Check database connection
73                 $this->out("Checking database...\n");
74
75                 $checkResults['db'] = array();
76                 $checkResults['db'][] = $this->runDatabaseCheck($db_host, $db_user, $db_pass, $db_data);
77                 $errorMessage = $this->extractErrors($checkResults['db']);
78
79                 if ($errorMessage !== '') {
80                         throw new RuntimeException($errorMessage);
81                 }
82
83                 $this->out(" Complete!\n\n");
84
85                 // Install database
86                 $this->out("Inserting data into database...\n");
87
88                 $checkResults['data'] = Install::installDatabaseStructure();
89
90                 if ($checkResults['data'] !== '') {
91                         throw new RuntimeException("ERROR: DB Database creation error. Is the DB empty?\n");
92                 }
93
94                 $this->out(" Complete!\n\n");
95
96                 // Install theme
97                 $this->out("Installing theme\n");
98                 if (!empty(Config::get('system', 'theme'))) {
99                         Theme::install(Config::get('system', 'theme'));
100                         $this->out(" Complete\n\n");
101                 } else {
102                         $this->out(" Theme setting is empty. Please check the file htconfig.php\n\n");
103                 }
104
105                 // Copy config file
106                 $this->out("Saving config file...\n");
107                 if ($config_file != '.htconfig.php' && !copy($config_file, '.htconfig.php')) {
108                         throw new RuntimeException("ERROR: Saving config file failed. Please copy '$config_file' to '.htconfig.php' manually.\n");
109                 }
110                 $this->out(" Complete!\n\n");
111                 $this->out("\nInstallation is finished\n");
112
113                 return 0;
114         }
115
116         /**
117          * @param App $app
118          * @return array
119          */
120         private function runBasicChecks($app)
121         {
122                 $checks = [];
123
124                 Install::checkFunctions($checks);
125                 Install::checkImagick($checks);
126                 Install::checkLocalIni($checks);
127                 Install::checkSmarty3($checks);
128                 Install::checkKeys($checks);
129
130                 if (!empty(Config::get('config', 'php_path'))) {
131                         Install::checkPHP(Config::get('config', 'php_path'), $checks);
132                 } else {
133                         throw new RuntimeException(" ERROR: The php_path is not set in the config.\n");
134                 }
135
136                 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
137
138                 return $checks;
139         }
140
141         /**
142          * @param $db_host
143          * @param $db_user
144          * @param $db_pass
145          * @param $db_data
146          * @return array
147          */
148         private function runDatabaseCheck($db_host, $db_user, $db_pass, $db_data)
149         {
150                 $result = array(
151                         'title' => 'MySQL Connection',
152                         'required' => true,
153                         'status' => true,
154                         'help' => '',
155                 );
156
157
158                 if (!dba::connect($db_host, $db_user, $db_pass, $db_data)) {
159                         $result['status'] = false;
160                         $result['help'] = 'Failed, please check your MySQL settings and credentials.';
161                 }
162
163                 return $result;
164         }
165
166         /**
167          * @param array $results
168          * @return string
169          */
170         private function extractErrors($results)
171         {
172                 $errorMessage = '';
173                 $allChecksRequired = $this->getOption('a') !== null;
174
175                 foreach ($results as $result) {
176                         if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
177                                 $errorMessage .= "--------\n";
178                                 $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
179                         }
180                 }
181
182                 return $errorMessage;
183         }
184 }