sqlfile.php
92 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 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 | VAPLoader::import('libraries.backup.export.rule.sql'); |
| 15 | |
| 16 | /** |
| 17 | * SQL Backup export rule, apposite for huge queries. |
| 18 | * |
| 19 | * @since 1.7.1 |
| 20 | */ |
| 21 | class VAPBackupExportRuleSqlfile extends VAPBackupExportRuleSql |
| 22 | { |
| 23 | /** |
| 24 | * The path of the file containing the export queries. |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | private $path; |
| 29 | |
| 30 | /** |
| 31 | * Returns the rules instructions. |
| 32 | * |
| 33 | * @return mixed |
| 34 | */ |
| 35 | public function getData() |
| 36 | { |
| 37 | // check whether the file has been saved |
| 38 | if ($this->path) |
| 39 | { |
| 40 | // return an associative array specifying the file path |
| 41 | // that contains all the export queries |
| 42 | return [ |
| 43 | 'path' => $this->path, |
| 44 | ]; |
| 45 | } |
| 46 | |
| 47 | // do not import empty files |
| 48 | return null; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Helper method used to register the query inside the buffer. |
| 53 | * |
| 54 | * @param string $query The query to register. |
| 55 | * |
| 56 | * @return void |
| 57 | */ |
| 58 | protected function registerQuery($query) |
| 59 | { |
| 60 | // register query through parent |
| 61 | parent::registerQuery($query); |
| 62 | |
| 63 | if (!$this->queries) |
| 64 | { |
| 65 | // nothing to export |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | if (!$this->path) |
| 70 | { |
| 71 | // build file path only once |
| 72 | $this->path = 'database/' . $this->table . '.sql'; |
| 73 | } |
| 74 | |
| 75 | // create buffer to save |
| 76 | $buffer = trim(implode("\n\n", $this->queries)); |
| 77 | $buffer = preg_replace("/\)\s*,\s*\(/", "),\n(", $buffer); |
| 78 | |
| 79 | // register SQL buffer into the archive |
| 80 | $saved = $this->archive->addBuffer($buffer . "\n\n", $this->path); |
| 81 | |
| 82 | if (!$saved) |
| 83 | { |
| 84 | // an error occurred while writing the dump files |
| 85 | throw new Exception(sprintf('Unable to write dump into: %s', $this->path), 500); |
| 86 | } |
| 87 | |
| 88 | // reset queries list to avoid duplicates |
| 89 | $this->queries = []; |
| 90 | } |
| 91 | } |
| 92 |