1
|
|
|
<?php |
2
|
|
|
namespace Redaxscript\Console\Command; |
3
|
|
|
|
4
|
|
|
use Redaxscript\Console\Parser; |
5
|
|
|
use Redaxscript\Installer; |
6
|
|
|
use function exec; |
7
|
|
|
use function is_file; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* children class to execute the restore command |
11
|
|
|
* |
12
|
|
|
* @since 3.0.0 |
13
|
|
|
* |
14
|
|
|
* @package Redaxscript |
15
|
|
|
* @category Console |
16
|
|
|
* @author Henry Ruhs |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
class Restore extends CommandAbstract |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* array of the command |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
protected array $_commandArray = |
|
|
|
|
28
|
|
|
[ |
29
|
|
|
'restore' => |
30
|
|
|
[ |
31
|
|
|
'description' => 'Restore command', |
32
|
|
|
'argumentArray' => |
33
|
|
|
[ |
34
|
|
|
'database' => |
35
|
|
|
[ |
36
|
|
|
'description' => 'Restore the database', |
37
|
|
|
'optionArray' => |
38
|
|
|
[ |
39
|
|
|
'directory' => |
40
|
|
|
[ |
41
|
|
|
'description' => 'Required directory' |
42
|
|
|
], |
43
|
|
|
'file' => |
44
|
|
|
[ |
45
|
|
|
'description' => 'Required file' |
46
|
|
|
] |
47
|
|
|
] |
48
|
|
|
] |
49
|
|
|
] |
50
|
|
|
] |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* run the command |
55
|
|
|
* |
56
|
|
|
* @since 3.0.0 |
57
|
|
|
* |
58
|
|
|
* @param string $mode name of the mode |
59
|
|
|
* |
60
|
|
|
* @return string|null |
61
|
|
|
*/ |
62
|
|
|
|
63
|
3 |
|
public function run(string $mode = null) : ?string |
64
|
|
|
{ |
65
|
3 |
|
$parser = new Parser($this->_request); |
66
|
3 |
|
$parser->init($mode); |
67
|
|
|
|
68
|
|
|
/* run command */ |
69
|
|
|
|
70
|
3 |
|
$argumentKey = $parser->getArgument(1); |
71
|
3 |
|
$haltOnError = (bool)$parser->getOption('halt-on-error'); |
72
|
3 |
|
if ($argumentKey === 'database') |
73
|
|
|
{ |
74
|
2 |
|
return $this->_database($parser->getOptionArray()) ? $this->success() : $this->error($haltOnError); |
75
|
|
|
} |
76
|
1 |
|
return $this->getHelp(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* restore the database |
81
|
|
|
* |
82
|
|
|
* @since 3.0.0 |
83
|
|
|
* |
84
|
|
|
* @param array $optionArray |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
|
89
|
2 |
|
protected function _database(array $optionArray = []) : bool |
90
|
|
|
{ |
91
|
2 |
|
$installer = new Installer($this->_registry, $this->_request, $this->_language, $this->_config); |
92
|
2 |
|
$installer->init(); |
93
|
2 |
|
$dbType = $this->_config->get('dbType'); |
94
|
2 |
|
$dbHost = $this->_config->get('dbHost'); |
95
|
2 |
|
$dbName = $this->_config->get('dbName'); |
96
|
2 |
|
$dbUser = $this->_config->get('dbUser'); |
97
|
2 |
|
$dbPassword = $this->_config->get('dbPassword'); |
98
|
2 |
|
$directory = $this->prompt('directory', $optionArray); |
99
|
2 |
|
$file = $this->prompt('file', $optionArray); |
100
|
2 |
|
$path = $directory . DIRECTORY_SEPARATOR . $file; |
101
|
|
|
|
102
|
|
|
/* restore */ |
103
|
|
|
|
104
|
2 |
|
if (is_file($path)) |
105
|
|
|
{ |
106
|
1 |
|
if ($dbType === 'mysql' && $dbHost && $dbName && $dbUser && $dbPassword) |
107
|
|
|
{ |
108
|
|
|
$command = 'mysql --host=' . $dbHost . ' --user=' . $dbUser . ' --password=' . $dbPassword . ' ' . $dbName . ' < ' . $path; |
109
|
|
|
} |
110
|
1 |
|
if ($dbType === 'pgsql' && $dbHost && $dbName && $dbUser && $dbPassword) |
111
|
|
|
{ |
112
|
|
|
$command = 'psql postgres://' . $dbUser . ':' . $dbPassword . '@' . $dbHost . '/' . $dbName . ' < ' . $path; |
113
|
|
|
} |
114
|
1 |
|
if ($dbType === 'sqlite' && $dbHost) |
115
|
|
|
{ |
116
|
1 |
|
$command = 'sqlite3 ' . $dbHost . ' .dump < ' . $path; |
117
|
|
|
} |
118
|
1 |
|
if ($command) |
119
|
|
|
{ |
120
|
1 |
|
$installer->rawDrop(); |
121
|
1 |
|
exec($command, $outputArray, $status); |
122
|
|
|
} |
123
|
1 |
|
return $status === 0; |
124
|
|
|
} |
125
|
1 |
|
return false; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|