| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* SQL Backup export rule, apposite for huge queries. |
| 16 |
* |
| 17 |
* @since 1.5 |
| 18 |
*/ |
| 19 |
class VBOBackupExportRuleSqlfile extends VBOBackupExportRuleSql |
| 20 |
{ |
| 21 |
/** |
| 22 |
* The path of the file containing the export queries. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
private $path; |
| 27 |
|
| 28 |
/** |
| 29 |
* Returns the rules instructions. |
| 30 |
* |
| 31 |
* @return mixed |
| 32 |
*/ |
| 33 |
public function getData() |
| 34 |
{ |
| 35 |
// check whether the file has been saved |
| 36 |
if ($this->path) |
| 37 |
{ |
| 38 |
// return an associative array specifying the file path |
| 39 |
// that contains all the export queries |
| 40 |
return [ |
| 41 |
'path' => $this->path, |
| 42 |
]; |
| 43 |
} |
| 44 |
|
| 45 |
// do not import empty files |
| 46 |
return null; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Helper method used to register the query inside the buffer. |
| 51 |
* |
| 52 |
* @param string $query The query to register. |
| 53 |
* |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
protected function registerQuery($query) |
| 57 |
{ |
| 58 |
// register query through parent |
| 59 |
parent::registerQuery($query); |
| 60 |
|
| 61 |
if (!$this->queries) |
| 62 |
{ |
| 63 |
// nothing to export |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
if (!$this->path) |
| 68 |
{ |
| 69 |
// build file path only once |
| 70 |
$this->path = 'database/' . $this->table . '.sql'; |
| 71 |
} |
| 72 |
|
| 73 |
// create buffer to save |
| 74 |
$buffer = trim(implode("\n\n", $this->queries)); |
| 75 |
$buffer = preg_replace("/\)\s*,\s*\(/", "),\n(", $buffer); |
| 76 |
|
| 77 |
// register SQL buffer into the archive |
| 78 |
$saved = $this->archive->addBuffer($buffer . "\n\n", $this->path); |
| 79 |
|
| 80 |
if (!$saved) |
| 81 |
{ |
| 82 |
// an error occurred while writing the dump files |
| 83 |
throw new Exception(sprintf('Unable to write dump into: %s', $this->path), 500); |
| 84 |
} |
| 85 |
|
| 86 |
// reset queries list to avoid duplicates |
| 87 |
$this->queries = []; |
| 88 |
} |
| 89 |
} |
| 90 |
|