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