| 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_PhpDumper implements MWP_IncrementalBackup_Database_DumperInterface |
| 12 |
{ |
| 13 |
|
| 14 |
const METHOD_PDO = 1; |
| 15 |
const METHOD_MYSQLI = 2; |
| 16 |
const METHOD_MYSQL = 3; |
| 17 |
|
| 18 |
private $transferSize = 102400; // 100kb |
| 19 |
|
| 20 |
/** |
| 21 |
* @var MWP_IncrementalBackup_Database_Configuration |
| 22 |
*/ |
| 23 |
private $configuration; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var MWP_System_Environment |
| 27 |
*/ |
| 28 |
private $environment; |
| 29 |
|
| 30 |
/** |
| 31 |
* @var MWP_IncrementalBackup_Database_DumpOptions |
| 32 |
*/ |
| 33 |
private $options; |
| 34 |
|
| 35 |
/** |
| 36 |
* @var MWP_IncrementalBackup_Database_ConnectionInterface |
| 37 |
*/ |
| 38 |
private $connection; |
| 39 |
|
| 40 |
/** |
| 41 |
* @param MWP_IncrementalBackup_Database_Configuration $configuration |
| 42 |
* @param MWP_System_Environment $environment |
| 43 |
* @param MWP_IncrementalBackup_Database_DumpOptions $options |
| 44 |
*/ |
| 45 |
public function __construct(MWP_IncrementalBackup_Database_Configuration $configuration, MWP_System_Environment $environment, MWP_IncrementalBackup_Database_DumpOptions $options) |
| 46 |
{ |
| 47 |
$this->configuration = $configuration; |
| 48 |
$this->environment = $environment; |
| 49 |
$this->options = $options; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* {@inheritdoc} |
| 54 |
*/ |
| 55 |
public function dump($table, $realpath) |
| 56 |
{ |
| 57 |
$stream = $this->createStream(array($table)); |
| 58 |
$handle = @fopen($realpath, 'w'); |
| 59 |
if ($handle === false) { |
| 60 |
$error = error_get_last(); |
| 61 |
$message = isset($error['message']) ? $error['message'] : sprintf('Unable to open file %s for writing.', $realpath); |
| 62 |
throw new MWP_Worker_Exception(MWP_Worker_Exception::IO_EXCEPTION, $message, $error); |
| 63 |
} |
| 64 |
|
| 65 |
while (!$stream->eof()) { |
| 66 |
fwrite($handle, $stream->read($this->transferSize)); |
| 67 |
} |
| 68 |
|
| 69 |
fclose($handle); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* {@inheritdoc} |
| 74 |
*/ |
| 75 |
public function createStream(array $tables = array()) |
| 76 |
{ |
| 77 |
if (!$this->connection) { |
| 78 |
$this->createConnection(); |
| 79 |
} |
| 80 |
$this->options->setTables($tables); |
| 81 |
$dumper = new MWP_IncrementalBackup_Database_StreamableQuerySequenceDump($this->connection, $this->options); |
| 82 |
|
| 83 |
return $dumper->createStream(); |
| 84 |
} |
| 85 |
|
| 86 |
private function createConnection() |
| 87 |
{ |
| 88 |
$connectionMethods = $this->options->getConnectionMethods(); |
| 89 |
if (!empty($connectionMethods) && is_array($connectionMethods)) { |
| 90 |
$this->getPreferredConnection($connectionMethods); |
| 91 |
if ($this->connection !== null) { |
| 92 |
return $this->connection; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if ($this->environment->isPdoEnabled()) { |
| 97 |
$this->connection = new MWP_IncrementalBackup_Database_PdoConnection($this->configuration); |
| 98 |
} elseif ($this->environment->isMysqliEnabled()) { |
| 99 |
$this->connection = new MWP_IncrementalBackup_Database_MysqliConnection($this->configuration); |
| 100 |
} elseif ($this->environment->isMysqlEnabled()) { |
| 101 |
$this->connection = new MWP_IncrementalBackup_Database_MysqlConnection($this->configuration); |
| 102 |
} else { |
| 103 |
throw new MWP_IncrementalBackup_Database_Exception_ConnectionException("No mysql drivers available."); |
| 104 |
} |
| 105 |
|
| 106 |
return $this->connection; |
| 107 |
} |
| 108 |
|
| 109 |
private function getPreferredConnection($methods) |
| 110 |
{ |
| 111 |
foreach ($methods as $method) { |
| 112 |
try { |
| 113 |
if ($method == self::METHOD_PDO && $this->environment->isPdoEnabled()) { |
| 114 |
$this->connection = new MWP_IncrementalBackup_Database_PdoConnection($this->configuration); |
| 115 |
} elseif ($method == self::METHOD_MYSQLI && $this->environment->isMysqliEnabled()) { |
| 116 |
$this->connection = new MWP_IncrementalBackup_Database_MysqliConnection($this->configuration); |
| 117 |
} elseif ($method == self::METHOD_MYSQL && $this->environment->isMysqlEnabled()) { |
| 118 |
$this->connection = new MWP_IncrementalBackup_Database_MysqlConnection($this->configuration); |
| 119 |
} |
| 120 |
} catch (Exception $e) { |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|