|
1
|
|
|
<?php |
|
2
|
|
|
namespace phpbu\App\Cli\Executable; |
|
3
|
|
|
|
|
4
|
|
|
use phpbu\App\Cli\Executable; |
|
5
|
|
|
use phpbu\App\Exception; |
|
6
|
|
|
use SebastianFeldmann\Cli\Command\Executable as Cmd; |
|
7
|
|
|
use SebastianFeldmann\Cli\CommandLine; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Mysqlimport Executable class. |
|
11
|
|
|
* |
|
12
|
|
|
* @package phpbu |
|
13
|
|
|
* @subpackage Backup |
|
14
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
15
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
|
16
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
17
|
|
|
* @link http://phpbu.de/ |
|
18
|
|
|
* @since Class available since Release 6.0-dev |
|
19
|
|
|
*/ |
|
20
|
|
|
class Mysqlimport extends Abstraction implements Executable |
|
21
|
|
|
{ |
|
22
|
|
|
use OptionMasker; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Host to connect to |
|
26
|
|
|
* --host <hostname> |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $host; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Port to connect to |
|
34
|
|
|
* --port <number> |
|
35
|
|
|
* |
|
36
|
|
|
* @var int |
|
37
|
|
|
*/ |
|
38
|
|
|
private $port; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The connection protocol |
|
42
|
|
|
* --protocol |
|
43
|
|
|
* |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
private $protocol; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* User to connect with |
|
50
|
|
|
* --user <username> |
|
51
|
|
|
* |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
private $user; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Password to authenticate with |
|
58
|
|
|
* --password <password> |
|
59
|
|
|
* |
|
60
|
|
|
* @var string |
|
61
|
|
|
*/ |
|
62
|
|
|
private $password; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Target database |
|
66
|
|
|
* |
|
67
|
|
|
* @var string |
|
68
|
|
|
*/ |
|
69
|
|
|
private $database; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Name of the source file. |
|
73
|
|
|
* |
|
74
|
|
|
* @var string |
|
75
|
|
|
*/ |
|
76
|
|
|
private $sourceFilename; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Use mysqlimport with compression |
|
80
|
|
|
* -C |
|
81
|
|
|
* |
|
82
|
|
|
* @var bool |
|
83
|
|
|
*/ |
|
84
|
|
|
private $compress = false; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Constructor. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $path |
|
90
|
|
|
*/ |
|
91
|
9 |
|
public function __construct(string $path = '') |
|
92
|
|
|
{ |
|
93
|
9 |
|
$this->setup('mysqlimport', $path); |
|
94
|
9 |
|
$this->setMaskCandidates(['password']); |
|
95
|
9 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Set the source filename and the target database. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $sourceFilename |
|
101
|
|
|
* @param string $targetDatabase |
|
102
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqlimport |
|
103
|
|
|
*/ |
|
104
|
8 |
|
public function setSourceAndTarget(string $sourceFilename, string $targetDatabase) : Mysqlimport |
|
105
|
|
|
{ |
|
106
|
8 |
|
$this->sourceFilename = $sourceFilename; |
|
107
|
8 |
|
$this->database = $targetDatabase; |
|
108
|
8 |
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Set the credentials. |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $user |
|
115
|
|
|
* @param string $password |
|
116
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqlimport |
|
117
|
|
|
*/ |
|
118
|
3 |
|
public function credentials(string $user = '', string $password = '') : Mysqlimport |
|
119
|
|
|
{ |
|
120
|
3 |
|
$this->user = $user; |
|
121
|
3 |
|
$this->password = $password; |
|
122
|
3 |
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Set the hostname. |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $host |
|
129
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqlimport |
|
130
|
|
|
*/ |
|
131
|
2 |
|
public function useHost(string $host) : Mysqlimport |
|
132
|
|
|
{ |
|
133
|
2 |
|
$this->host = $host; |
|
134
|
2 |
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Set the port. |
|
139
|
|
|
* |
|
140
|
|
|
* @param int $port |
|
141
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqlimport |
|
142
|
|
|
*/ |
|
143
|
2 |
|
public function usePort(int $port) : Mysqlimport |
|
144
|
|
|
{ |
|
145
|
2 |
|
$this->port = $port; |
|
146
|
2 |
|
return $this; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Set the connection protocol. |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $protocol |
|
153
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqlimport |
|
154
|
|
|
*/ |
|
155
|
2 |
|
public function useProtocol(string $protocol) : Mysqlimport |
|
156
|
|
|
{ |
|
157
|
2 |
|
$this->protocol = $protocol; |
|
158
|
2 |
|
return $this; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Use '-C' compress mode. |
|
163
|
|
|
* |
|
164
|
|
|
* @param bool $bool |
|
165
|
|
|
* @return \phpbu\App\Cli\Executable\Mysqlimport |
|
166
|
|
|
*/ |
|
167
|
1 |
|
public function useCompression(bool $bool) : Mysqlimport |
|
168
|
|
|
{ |
|
169
|
1 |
|
$this->compress = $bool; |
|
170
|
1 |
|
return $this; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Mysqlimport CommandLine generator. |
|
175
|
|
|
* |
|
176
|
|
|
* @return \SebastianFeldmann\Cli\CommandLine |
|
177
|
|
|
* @throws \phpbu\App\Exception |
|
178
|
|
|
*/ |
|
179
|
9 |
|
protected function createCommandLine(): CommandLine |
|
180
|
|
|
{ |
|
181
|
9 |
|
if (empty($this->database)) { |
|
182
|
1 |
|
throw new Exception('mysqlimport needs a target database to import into'); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
8 |
|
if (empty($this->sourceFilename)) { |
|
186
|
|
|
throw new Exception('mysqlimport needs a source file to import'); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
8 |
|
$process = new CommandLine(); |
|
190
|
8 |
|
$cmd = new Cmd($this->binary); |
|
191
|
8 |
|
$process->addCommand($cmd); |
|
192
|
|
|
|
|
193
|
8 |
|
$cmd->addArgument($this->database); |
|
194
|
8 |
|
$cmd->addArgument($this->sourceFilename); |
|
195
|
8 |
|
$cmd->addOptionIfNotEmpty('--user', $this->user); |
|
196
|
8 |
|
$cmd->addOptionIfNotEmpty('--password', $this->password); |
|
197
|
8 |
|
$cmd->addOptionIfNotEmpty('--host', $this->host); |
|
198
|
8 |
|
$cmd->addOptionIfNotEmpty('--port', $this->port); |
|
199
|
8 |
|
$cmd->addOptionIfNotEmpty('--protocol', $this->protocol); |
|
200
|
8 |
|
$cmd->addOptionIfNotEmpty('-C', $this->compress, false); |
|
201
|
|
|
|
|
202
|
8 |
|
return $process; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|