| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the ManageWP Worker plugin. |
| 4 |
* |
| 5 |
* (c) ManageWP LLC <contact@managewp.com> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
class MWP_IncrementalBackup_Database_Configuration |
| 12 |
{ |
| 13 |
|
| 14 |
/** |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
private $username; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
private $password; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
private $database; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var bool |
| 31 |
*/ |
| 32 |
private $socket; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var string|null |
| 36 |
*/ |
| 37 |
private $host; |
| 38 |
|
| 39 |
/** |
| 40 |
* @var string|null |
| 41 |
*/ |
| 42 |
private $socketPath; |
| 43 |
|
| 44 |
/** |
| 45 |
* @var string |
| 46 |
*/ |
| 47 |
private $charset = 'utf8'; |
| 48 |
|
| 49 |
/** |
| 50 |
* @var int |
| 51 |
*/ |
| 52 |
private $port; |
| 53 |
|
| 54 |
/** |
| 55 |
* @param array $parameters |
| 56 |
*/ |
| 57 |
public function __construct(array $parameters = array()) |
| 58 |
{ |
| 59 |
if (isset($parameters['username'])) { |
| 60 |
$this->username = $parameters['username']; |
| 61 |
} |
| 62 |
|
| 63 |
if (isset($parameters['password'])) { |
| 64 |
$this->password = $parameters['password']; |
| 65 |
} |
| 66 |
|
| 67 |
if (isset($parameters['socket'])) { |
| 68 |
$this->socket = true; |
| 69 |
$this->socketPath = $parameters['socket']; |
| 70 |
} elseif (isset($parameters['host'])) { |
| 71 |
$this->socket = false; |
| 72 |
$this->host = $parameters['host']; |
| 73 |
} |
| 74 |
|
| 75 |
if (isset($parameters['port'])) { |
| 76 |
$this->port = $parameters['port']; |
| 77 |
} |
| 78 |
|
| 79 |
if (isset($parameters['database'])) { |
| 80 |
$this->database = $parameters['database']; |
| 81 |
} |
| 82 |
|
| 83 |
if (isset($parameters['charset'])) { |
| 84 |
$this->charset = $parameters['charset']; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @return string|null |
| 90 |
*/ |
| 91 |
public function getHost() |
| 92 |
{ |
| 93 |
return $this->host; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* @return string|null |
| 98 |
*/ |
| 99 |
public function getSocketPath() |
| 100 |
{ |
| 101 |
return $this->socketPath; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* @return int |
| 106 |
*/ |
| 107 |
public function getPort() |
| 108 |
{ |
| 109 |
return $this->port; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* @return boolean |
| 114 |
*/ |
| 115 |
public function isSocket() |
| 116 |
{ |
| 117 |
return $this->socket; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* @return string |
| 122 |
*/ |
| 123 |
public function getUsername() |
| 124 |
{ |
| 125 |
return $this->username; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* @return string |
| 130 |
*/ |
| 131 |
public function getPassword() |
| 132 |
{ |
| 133 |
return $this->password; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* @return string |
| 138 |
*/ |
| 139 |
public function getDatabase() |
| 140 |
{ |
| 141 |
return $this->database; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* @return string |
| 146 |
*/ |
| 147 |
public function getCharset() |
| 148 |
{ |
| 149 |
return $this->charset; |
| 150 |
} |
| 151 |
|
| 152 |
public static function createFromWordPressContext(MWP_WordPress_Context $context) |
| 153 |
{ |
| 154 |
return self::createFromArray(array( |
| 155 |
'username' => $context->getConstant('DB_USER'), |
| 156 |
'password' => $context->getConstant('DB_PASSWORD'), |
| 157 |
'database' => $context->getConstant('DB_NAME'), |
| 158 |
'host' => $context->getConstant('DB_HOST'), |
| 159 |
'charset' => $context->hasConstant('DB_CHARSET') ? $context->getConstant('DB_CHARSET') : 'utf8', |
| 160 |
)); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* $parameters array should contain: |
| 165 |
* - username: mysql username |
| 166 |
* - password: mysql password |
| 167 |
* - database: database name |
| 168 |
* - host: host - can contain port or be a mysql socket |
| 169 |
*/ |
| 170 |
public static function createFromArray(array $parameters = array()) |
| 171 |
{ |
| 172 |
$configuration = array( |
| 173 |
'username' => isset($parameters['username']) ? $parameters['username'] : null, |
| 174 |
'password' => isset($parameters['password']) ? $parameters['password'] : null, |
| 175 |
'database' => $parameters['database'], |
| 176 |
); |
| 177 |
|
| 178 |
$databaseHost = $parameters['host']; |
| 179 |
|
| 180 |
$port = 0; |
| 181 |
$host = $databaseHost; |
| 182 |
|
| 183 |
// Handle localhost:3306 cases |
| 184 |
if (strpos($databaseHost, ':') !== false) { |
| 185 |
list($host, $port) = explode(':', $databaseHost); |
| 186 |
$port = (int) $port; |
| 187 |
} |
| 188 |
|
| 189 |
// Database host can be a path to mysql socket |
| 190 |
if (strpos($databaseHost, '/') !== false || strpos($databaseHost, '\\') !== false) { |
| 191 |
$socket = end(explode(':', $databaseHost)); |
| 192 |
|
| 193 |
$configuration['socket'] = $socket; |
| 194 |
} else { |
| 195 |
$configuration['host'] = $host; |
| 196 |
if (!empty($port)) { |
| 197 |
$configuration['port'] = $port; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
if (isset($parameters['charset'])) { |
| 202 |
$configuration['charset'] = $parameters['charset']; |
| 203 |
} |
| 204 |
|
| 205 |
return new MWP_IncrementalBackup_Database_Configuration($configuration); |
| 206 |
} |
| 207 |
} |
| 208 |
|