| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the ManageWP Worker plugin. |
| 4 |
* |
| 5 |
* (c) ManageWP LLC <[email protected]> |
| 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_PdoConnection implements MWP_IncrementalBackup_Database_ConnectionInterface |
| 12 |
{ |
| 13 |
|
| 14 |
/** |
| 15 |
* @var MWP_IncrementalBackup_Database_Configuration |
| 16 |
*/ |
| 17 |
private $configuration; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var PDO |
| 21 |
*/ |
| 22 |
private $connection; |
| 23 |
|
| 24 |
public function __construct(MWP_IncrementalBackup_Database_Configuration $configuration) |
| 25 |
{ |
| 26 |
$this->configuration = $configuration; |
| 27 |
|
| 28 |
if (!extension_loaded('pdo_mysql')) { |
| 29 |
throw new MWP_IncrementalBackup_Database_Exception_ConnectionException("PDO extension is disabled."); |
| 30 |
} |
| 31 |
|
| 32 |
$options = array( |
| 33 |
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
| 34 |
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, |
| 35 |
); |
| 36 |
|
| 37 |
// Mute the constructor since this error doesn't get thrown as an exception, but as a PHP warning: |
| 38 |
// Warning: PDO::__construct(): The server requested authentication method unknown to the client [mysql_old_password] |
| 39 |
$this->connection = @new PDO(self::getDsn($this->configuration), $this->configuration->getUsername(), $this->configuration->getPassword(), $options); |
| 40 |
|
| 41 |
$this->connection->exec('SET NAMES '.$configuration->getCharset()); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* {@inheritdoc} |
| 46 |
*/ |
| 47 |
public function query($query, $useResult = false) |
| 48 |
{ |
| 49 |
/** @handled constant */ |
| 50 |
$previousAttr = $this->connection->getAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY); |
| 51 |
|
| 52 |
if ($useResult) { |
| 53 |
// Temporarily switch to unbuffered queries |
| 54 |
/** @handled constant */ |
| 55 |
$this->connection->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); |
| 56 |
} |
| 57 |
|
| 58 |
$statement = new MWP_IncrementalBackup_Database_PdoStatement($this->connection->query($query)); |
| 59 |
|
| 60 |
if ($useResult) { |
| 61 |
// Restore configuration |
| 62 |
/** @handled constant */ |
| 63 |
$this->connection->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, $previousAttr); |
| 64 |
} |
| 65 |
|
| 66 |
return $statement; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* {@inheritdoc} |
| 71 |
*/ |
| 72 |
public function quote($value) |
| 73 |
{ |
| 74 |
return $this->connection->quote($value); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @param MWP_IncrementalBackup_Database_Configuration $configuration |
| 79 |
* |
| 80 |
* @return string |
| 81 |
*/ |
| 82 |
private static function getDsn(MWP_IncrementalBackup_Database_Configuration $configuration) |
| 83 |
{ |
| 84 |
$pdoParameters = array( |
| 85 |
'dbname' => $configuration->getDatabase(), |
| 86 |
'charset' => $configuration->getCharset(), |
| 87 |
); |
| 88 |
|
| 89 |
if ($configuration->isSocket()) { |
| 90 |
$pdoParameters['unix_socket'] = $configuration->getSocketPath(); |
| 91 |
} else { |
| 92 |
$pdoParameters['host'] = $configuration->getHost(); |
| 93 |
|
| 94 |
if (($port = $configuration->getPort()) !== null) { |
| 95 |
$pdoParameters['port'] = $configuration->getPort(); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
$parameters = array(); |
| 100 |
foreach ($pdoParameters as $name => $value) { |
| 101 |
$parameters[] = $name.'='.$value; |
| 102 |
} |
| 103 |
|
| 104 |
$dsn = sprintf("mysql:%s", implode(';', $parameters)); |
| 105 |
|
| 106 |
return $dsn; |
| 107 |
} |
| 108 |
} |
| 109 |
|