1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Joomla! Statistics Server |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (C) 2013 - 2017 Open Source Matters, Inc. All rights reserved. |
6
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Joomla\StatsServer\Commands; |
10
|
|
|
|
11
|
|
|
use Joomla\Controller\AbstractController; |
12
|
|
|
use Joomla\Database\DatabaseDriver; |
13
|
|
|
use Joomla\Database\Mysql\MysqlDriver; |
14
|
|
|
use Joomla\StatsServer\CommandInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Install command |
18
|
|
|
* |
19
|
|
|
* @method \Joomla\StatsServer\CliApplication getApplication() Get the application object. |
20
|
|
|
* @property-read \Joomla\StatsServer\CliApplication $app Application object |
21
|
|
|
*/ |
22
|
|
|
class InstallCommand extends AbstractController implements CommandInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Database driver. |
26
|
|
|
* |
27
|
|
|
* @var DatabaseDriver |
28
|
|
|
*/ |
29
|
|
|
private $db = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor. |
33
|
|
|
* |
34
|
|
|
* @param DatabaseDriver $db Database driver. |
35
|
|
|
*/ |
36
|
|
|
public function __construct(DatabaseDriver $db) |
37
|
|
|
{ |
38
|
|
|
$this->db = $db; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Execute the controller. |
43
|
|
|
* |
44
|
|
|
* @return boolean |
45
|
|
|
*/ |
46
|
|
|
public function execute() |
47
|
|
|
{ |
48
|
|
|
$this->getApplication()->outputTitle('Installer'); |
49
|
|
|
|
50
|
|
|
// Check for PDO MySQL support |
51
|
|
|
if (!MysqlDriver::isSupported()) |
52
|
|
|
{ |
53
|
|
|
$this->getApplication()->out('<error>PDO with MySQL support is not available on this server!</error>'); |
54
|
|
|
|
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
try |
59
|
|
|
{ |
60
|
|
|
// Check if the database "exists" |
61
|
|
|
$tables = $this->db->getTableList(); |
62
|
|
|
|
63
|
|
|
if (!$this->getApplication()->input->getBool('reinstall', false)) |
64
|
|
|
{ |
65
|
|
|
$this->getApplication()->out() |
66
|
|
|
->out('<fg=black;bg=yellow>WARNING: A database has been found !!</fg=black;bg=yellow>') |
67
|
|
|
->out() |
68
|
|
|
->out('Do you want to reinstall ?') |
69
|
|
|
->out() |
70
|
|
|
->out('1) Yes') |
71
|
|
|
->out('2) No') |
72
|
|
|
->out() |
73
|
|
|
->out('<question>' . g11n3t('Select:') . '</question>', false); |
74
|
|
|
|
75
|
|
|
$in = trim($this->getApplication()->in()); |
76
|
|
|
|
77
|
|
|
if ((int) $in !== 1) |
78
|
|
|
{ |
79
|
|
|
$this->getApplication()->out('<info>Aborting installation.</info>'); |
80
|
|
|
|
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->cleanDatabase($tables); |
86
|
|
|
} |
87
|
|
|
catch (\RuntimeException $e) |
88
|
|
|
{ |
89
|
|
|
// Check if the message is "Could not connect to database." Odds are, this means the DB isn't there or the server is down. |
90
|
|
|
if (strpos($e->getMessage(), 'Could not connect to database.') !== false) |
91
|
|
|
{ |
92
|
|
|
$this->getApplication()->out('No database found.') |
93
|
|
|
->out('Creating the database...', false); |
94
|
|
|
|
95
|
|
|
$this->db->setQuery('CREATE DATABASE ' . $this->db->quoteName($this->getApplication()->get('database.name'))) |
96
|
|
|
->execute(); |
97
|
|
|
|
98
|
|
|
$this->db->select($this->getApplication()->get('database.name')); |
99
|
|
|
|
100
|
|
|
$this->getApplication()->out('<info>Database created.</info>'); |
101
|
|
|
} |
102
|
|
|
else |
103
|
|
|
{ |
104
|
|
|
throw $e; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Perform the installation |
109
|
|
|
$this->processSql() |
110
|
|
|
->getApplication() |
111
|
|
|
->out() |
112
|
|
|
->out('<fg=green;options=bold>Installation has been completed successfully.</fg=green;options=bold>'); |
113
|
|
|
|
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Cleanup the database. |
119
|
|
|
* |
120
|
|
|
* @param array $tables Tables to remove. |
121
|
|
|
* |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
|
|
private function cleanDatabase(array $tables) : InstallCommand |
125
|
|
|
{ |
126
|
|
|
$this->getApplication()->out('Removing existing tables...', false); |
127
|
|
|
|
128
|
|
|
// Foreign key constraint fails fix |
129
|
|
|
$this->db->setQuery('SET FOREIGN_KEY_CHECKS=0') |
130
|
|
|
->execute(); |
131
|
|
|
|
132
|
|
|
foreach ($tables as $table) |
133
|
|
|
{ |
134
|
|
|
$this->db->dropTable($table, true); |
135
|
|
|
$this->getApplication()->out('.', false); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$this->db->setQuery('SET FOREIGN_KEY_CHECKS=1') |
139
|
|
|
->execute(); |
140
|
|
|
|
141
|
|
|
$this->getApplication()->out('<info>Tables removed.</info>'); |
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Process the main SQL file. |
148
|
|
|
* |
149
|
|
|
* @return $this |
150
|
|
|
* |
151
|
|
|
* @throws \UnexpectedValueException |
152
|
|
|
*/ |
153
|
|
|
private function processSql() : InstallCommand |
154
|
|
|
{ |
155
|
|
|
$fName = APPROOT . '/etc/mysql.sql'; |
156
|
|
|
|
157
|
|
|
if (!file_exists($fName)) |
158
|
|
|
{ |
159
|
|
|
throw new \UnexpectedValueException('Install SQL file for MySQL not found.'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$sql = file_get_contents($fName); |
163
|
|
|
|
164
|
|
|
if (!$sql) |
165
|
|
|
{ |
166
|
|
|
throw new \UnexpectedValueException('SQL file corrupted.'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$this->out(sprintf('Creating tables from file %s', realpath($fName)), false); |
|
|
|
|
170
|
|
|
|
171
|
|
|
foreach ($this->db->splitSql($sql) as $query) |
172
|
|
|
{ |
173
|
|
|
$q = trim($this->db->replacePrefix($query)); |
174
|
|
|
|
175
|
|
|
if ('' == trim($q)) |
176
|
|
|
{ |
177
|
|
|
continue; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$this->db->setQuery($q) |
181
|
|
|
->execute(); |
182
|
|
|
|
183
|
|
|
$this->getApplication()->out('.', false); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$this->getApplication()->out('<info>Database tables created successfully.</info>'); |
187
|
|
|
|
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get the command's description |
193
|
|
|
* |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
|
|
public function getDescription() : string |
197
|
|
|
{ |
198
|
|
|
return 'Installs the application.'; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Get the command's title |
203
|
|
|
* |
204
|
|
|
* @return string |
205
|
|
|
*/ |
206
|
|
|
public function getTitle() : string |
207
|
|
|
{ |
208
|
|
|
return 'Install Application'; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.