Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/worker/src/MWP/Incremen.../Database
File: StreamableQuerySequenceDump.php
<?php
[0] Fix | Delete
/*
[1] Fix | Delete
* This file is part of the ManageWP Worker plugin.
[2] Fix | Delete
*
[3] Fix | Delete
* (c) ManageWP LLC <contact@managewp.com>
[4] Fix | Delete
*
[5] Fix | Delete
* For the full copyright and license information, please view the LICENSE
[6] Fix | Delete
* file that was distributed with this source code.
[7] Fix | Delete
*/
[8] Fix | Delete
[9] Fix | Delete
class MWP_IncrementalBackup_Database_StreamableQuerySequenceDump
[10] Fix | Delete
{
[11] Fix | Delete
[12] Fix | Delete
/**
[13] Fix | Delete
* @var MWP_IncrementalBackup_Database_ConnectionInterface
[14] Fix | Delete
*/
[15] Fix | Delete
private $connection;
[16] Fix | Delete
[17] Fix | Delete
/**
[18] Fix | Delete
* @var MWP_IncrementalBackup_Database_DumpOptions
[19] Fix | Delete
*/
[20] Fix | Delete
private $options;
[21] Fix | Delete
[22] Fix | Delete
public function __construct(MWP_IncrementalBackup_Database_ConnectionInterface $connection, MWP_IncrementalBackup_Database_DumpOptions $options)
[23] Fix | Delete
{
[24] Fix | Delete
$this->connection = $connection;
[25] Fix | Delete
$this->options = $options;
[26] Fix | Delete
}
[27] Fix | Delete
[28] Fix | Delete
/**
[29] Fix | Delete
* @return MWP_IncrementalBackup_Database_ConnectionInterface
[30] Fix | Delete
*/
[31] Fix | Delete
protected function getConnection()
[32] Fix | Delete
{
[33] Fix | Delete
return $this->connection;
[34] Fix | Delete
}
[35] Fix | Delete
[36] Fix | Delete
/**
[37] Fix | Delete
* @inherit
[38] Fix | Delete
*/
[39] Fix | Delete
public function createStream()
[40] Fix | Delete
{
[41] Fix | Delete
$stream = new MWP_Stream_Append();
[42] Fix | Delete
[43] Fix | Delete
$stream->addStream(MWP_Stream_Stream::factory("
[44] Fix | Delete
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
[45] Fix | Delete
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
[46] Fix | Delete
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
[47] Fix | Delete
/*!40101 SET NAMES utf8 */;
[48] Fix | Delete
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
[49] Fix | Delete
/*!40103 SET TIME_ZONE='+00:00' */;
[50] Fix | Delete
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
[51] Fix | Delete
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
[52] Fix | Delete
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
[53] Fix | Delete
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;\n\n"
[54] Fix | Delete
));
[55] Fix | Delete
[56] Fix | Delete
$allTables = self::arrayColumn($this->getConnection()->query('SHOW TABLES')->fetchAll());
[57] Fix | Delete
$tables = array_intersect($allTables, $this->options->getTables() ? $this->options->getTables() : $allTables);
[58] Fix | Delete
[59] Fix | Delete
foreach ($tables as $tableName) {
[60] Fix | Delete
$stream->addStream(
[61] Fix | Delete
new MWP_Stream_Callable(array($this, 'streamCreateTable'), array($tableName))
[62] Fix | Delete
);
[63] Fix | Delete
}
[64] Fix | Delete
[65] Fix | Delete
$stream->addStream(MWP_Stream_Stream::factory("
[66] Fix | Delete
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
[67] Fix | Delete
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
[68] Fix | Delete
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
[69] Fix | Delete
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
[70] Fix | Delete
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
[71] Fix | Delete
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
[72] Fix | Delete
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
[73] Fix | Delete
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;\n"
[74] Fix | Delete
));
[75] Fix | Delete
[76] Fix | Delete
return $stream;
[77] Fix | Delete
}
[78] Fix | Delete
[79] Fix | Delete
public function streamCreateTable($length, $tableName)
[80] Fix | Delete
{
[81] Fix | Delete
// Get the SHOW CREATE TABLE part
[82] Fix | Delete
$content = $this->getConnection()
[83] Fix | Delete
->query("SHOW CREATE TABLE `{$tableName}`;")
[84] Fix | Delete
->fetchAll();
[85] Fix | Delete
[86] Fix | Delete
if (!is_array($content)) {
[87] Fix | Delete
return new MWP_Stream_Buffer();
[88] Fix | Delete
}
[89] Fix | Delete
[90] Fix | Delete
$stream = new MWP_Stream_Append();
[91] Fix | Delete
[92] Fix | Delete
foreach ($content as $entry) {
[93] Fix | Delete
// Add drop table query
[94] Fix | Delete
if ($this->options->isDropTables()) {
[95] Fix | Delete
$stream->addStream(MWP_Stream_Stream::factory("DROP TABLE IF EXISTS `$tableName`;\n"));
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
// Add create table query
[99] Fix | Delete
$stream->addStream(MWP_Stream_Stream::factory("
[100] Fix | Delete
/*!40101 SET @saved_cs_client = @@character_set_client */;
[101] Fix | Delete
/*!40101 SET character_set_client = utf8 */;\n"
[102] Fix | Delete
));
[103] Fix | Delete
$stream->addStream(MWP_Stream_Stream::factory($entry['Create Table'].";\n"));
[104] Fix | Delete
$stream->addStream(MWP_Stream_Stream::factory("/*!40101 SET character_set_client = @saved_cs_client */;\n\n"));
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
// Export content
[108] Fix | Delete
$stream->addStream(
[109] Fix | Delete
new MWP_Stream_Callable(array($this, 'createExportTableStream'), array($tableName))
[110] Fix | Delete
);
[111] Fix | Delete
[112] Fix | Delete
return $stream;
[113] Fix | Delete
}
[114] Fix | Delete
[115] Fix | Delete
public function createExportTableStream($length, $tableName)
[116] Fix | Delete
{
[117] Fix | Delete
$stream = new MWP_Stream_Append();
[118] Fix | Delete
[119] Fix | Delete
$columns = $this->getConnection()
[120] Fix | Delete
->query("SHOW COLUMNS IN `{$tableName}`;")
[121] Fix | Delete
->fetchAll();
[122] Fix | Delete
[123] Fix | Delete
if (is_array($columns)) {
[124] Fix | Delete
$columns = $this->repack($columns, 'Field');
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
$query = $this->selectAllDataQuery($tableName, $columns);
[128] Fix | Delete
$statement = $this->getConnection()->query($query, true);
[129] Fix | Delete
[130] Fix | Delete
// Go through row by row
[131] Fix | Delete
if (!$this->options->isSkipLockTables()) {
[132] Fix | Delete
$stream->addStream(MWP_Stream_Stream::factory("LOCK TABLES `$tableName` WRITE;\n"));
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
$stream->addStream(MWP_Stream_Stream::factory("/*!40000 ALTER TABLE `$tableName` DISABLE KEYS */;\n"));
[136] Fix | Delete
[137] Fix | Delete
$stream->addStream(
[138] Fix | Delete
new MWP_Stream_Callable(array($this, 'createExportRowStream'), array($statement, $tableName, $columns))
[139] Fix | Delete
);
[140] Fix | Delete
[141] Fix | Delete
$stream->addStream(MWP_Stream_Stream::factory("\n"));
[142] Fix | Delete
$stream->addStream(MWP_Stream_Stream::factory("/*!40000 ALTER TABLE `$tableName` ENABLE KEYS */;\n"));
[143] Fix | Delete
[144] Fix | Delete
if (!$this->options->isSkipLockTables()) {
[145] Fix | Delete
$stream->addStream(MWP_Stream_Stream::factory("UNLOCK TABLES;\n"));
[146] Fix | Delete
}
[147] Fix | Delete
[148] Fix | Delete
return $stream;
[149] Fix | Delete
}
[150] Fix | Delete
[151] Fix | Delete
public function createExportRowStream($length, MWP_IncrementalBackup_Database_StatementInterface $statement, $tableName, $columns)
[152] Fix | Delete
{
[153] Fix | Delete
$row = $statement->fetch();
[154] Fix | Delete
if (!$row) {
[155] Fix | Delete
// This statement is using unbuffered queries and MUST be closed explicitly.
[156] Fix | Delete
$statement->close();
[157] Fix | Delete
[158] Fix | Delete
return false;
[159] Fix | Delete
}
[160] Fix | Delete
[161] Fix | Delete
return $this->createRowInsertStatement($tableName, $row, $columns)."\n";
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
/**
[165] Fix | Delete
* Repacks an array by making a key of a particular column
[166] Fix | Delete
*
[167] Fix | Delete
* @param array $array
[168] Fix | Delete
* @param $column
[169] Fix | Delete
*
[170] Fix | Delete
* @return array
[171] Fix | Delete
*/
[172] Fix | Delete
protected function repack(array $array, $column)
[173] Fix | Delete
{
[174] Fix | Delete
$repacked = array();
[175] Fix | Delete
foreach ($array as $element) {
[176] Fix | Delete
$repacked[$element[$column]] = $element;
[177] Fix | Delete
}
[178] Fix | Delete
[179] Fix | Delete
return $repacked;
[180] Fix | Delete
}
[181] Fix | Delete
[182] Fix | Delete
/**
[183] Fix | Delete
* Creates an SQL statement for fetching all data from a particular table
[184] Fix | Delete
*
[185] Fix | Delete
* @param $tableName
[186] Fix | Delete
* @param $columnData
[187] Fix | Delete
*
[188] Fix | Delete
* @return string
[189] Fix | Delete
*/
[190] Fix | Delete
protected function selectAllDataQuery($tableName, $columnData)
[191] Fix | Delete
{
[192] Fix | Delete
$columns = array();
[193] Fix | Delete
foreach ($columnData as $columnName => $metadata) {
[194] Fix | Delete
if (strpos($metadata['Type'], 'blob') !== false) {
[195] Fix | Delete
$fullColumnName = "`{$tableName}`.`{$columnName}`";
[196] Fix | Delete
$columns[] = "HEX($fullColumnName) as `{$columnName}`";
[197] Fix | Delete
} else {
[198] Fix | Delete
$columns[] = "`{$tableName}`.`{$columnName}`";
[199] Fix | Delete
}
[200] Fix | Delete
}
[201] Fix | Delete
$cols = join(', ', $columns);
[202] Fix | Delete
$sql = "SELECT $cols FROM `$tableName`;";
[203] Fix | Delete
[204] Fix | Delete
return $sql;
[205] Fix | Delete
}
[206] Fix | Delete
[207] Fix | Delete
/**
[208] Fix | Delete
* Creates an sql statement for row insertion
[209] Fix | Delete
*
[210] Fix | Delete
* @param string $tableName
[211] Fix | Delete
* @param array $row
[212] Fix | Delete
* @param array $columns
[213] Fix | Delete
*
[214] Fix | Delete
* @return string
[215] Fix | Delete
*/
[216] Fix | Delete
protected function createRowInsertStatement($tableName, array $row, array $columns = array())
[217] Fix | Delete
{
[218] Fix | Delete
$values = $this->createRowInsertValues($row, $columns);
[219] Fix | Delete
$joined = join(', ', $values);
[220] Fix | Delete
$sql = "INSERT INTO `$tableName` VALUES($joined);";
[221] Fix | Delete
[222] Fix | Delete
return $sql;
[223] Fix | Delete
}
[224] Fix | Delete
[225] Fix | Delete
protected function createRowInsertValues($row, $columns)
[226] Fix | Delete
{
[227] Fix | Delete
$values = array();
[228] Fix | Delete
[229] Fix | Delete
foreach ($row as $columnName => $value) {
[230] Fix | Delete
$type = $columns[$columnName]['Type'];
[231] Fix | Delete
[232] Fix | Delete
// Used to determine if the column is enum in case some of the allowed values contain reserved type identifiers
[233] Fix | Delete
$trimmedType = strtolower(trim($type));
[234] Fix | Delete
[235] Fix | Delete
// If it should not be enclosed
[236] Fix | Delete
if ($value === null) {
[237] Fix | Delete
$values[] = 'null';
[238] Fix | Delete
} elseif (strpos($trimmedType, 'enum') !== 0 &&
[239] Fix | Delete
(strpos($type, 'int') !== false
[240] Fix | Delete
|| strpos($type, 'float') !== false
[241] Fix | Delete
|| strpos($type, 'double') !== false
[242] Fix | Delete
|| strpos($type, 'decimal') !== false
[243] Fix | Delete
|| strpos($type, 'bool') !== false)
[244] Fix | Delete
) {
[245] Fix | Delete
$values[] = $value;
[246] Fix | Delete
} elseif (strpos($type, 'blob') !== false) {
[247] Fix | Delete
$values[] = strlen($value) ? ('0x'.$value) : "''";
[248] Fix | Delete
} else {
[249] Fix | Delete
$values[] = $this->getConnection()->quote($value);
[250] Fix | Delete
}
[251] Fix | Delete
}
[252] Fix | Delete
[253] Fix | Delete
return $values;
[254] Fix | Delete
}
[255] Fix | Delete
[256] Fix | Delete
private static function arrayColumn($array, $columnIndex = 0)
[257] Fix | Delete
{
[258] Fix | Delete
$result = array();
[259] Fix | Delete
foreach ($array as $arr) {
[260] Fix | Delete
if (!is_array($arr)) {
[261] Fix | Delete
continue;
[262] Fix | Delete
}
[263] Fix | Delete
$arr = array_values($arr);
[264] Fix | Delete
$result[] = $arr[$columnIndex];
[265] Fix | Delete
}
[266] Fix | Delete
return $result;
[267] Fix | Delete
}
[268] Fix | Delete
}
[269] Fix | Delete
[270] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function