|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DeliciousBrains\WPMDB\Common; |
| 4 | + |
| 5 | +use DeliciousBrains\WPMDB\Common\Filesystem\Filesystem; |
| 6 | +use DeliciousBrains\WPMDB\Common\FormData\FormData; |
| 7 | +use DeliciousBrains\WPMDB\Common\Http\Http; |
| 8 | +use DeliciousBrains\WPMDB\Common\MigrationState\StateDataContainer; |
| 9 | +use DeliciousBrains\WPMDB\Common\Properties\Properties; |
| 10 | +use DeliciousBrains\WPMDB\Common\Settings\Settings; |
| 11 | +use DeliciousBrains\WPMDB\Common\Sql\Table; |
| 12 | +use DeliciousBrains\WPMDB\Common\Sql\TableHelper; |
| 13 | + |
| 14 | +class BackupExport { |
| 15 | + /** |
| 16 | + * @var Filesystem |
| 17 | + */ |
| 18 | + private $filesystem; |
| 19 | + /** |
| 20 | + * @var TableHelper |
| 21 | + */ |
| 22 | + private $table_helper; |
| 23 | + /** |
| 24 | + * @var Http |
| 25 | + */ |
| 26 | + private $http; |
| 27 | + |
| 28 | + public $state_data; |
| 29 | + /** |
| 30 | + * @var Table |
| 31 | + */ |
| 32 | + private $table; |
| 33 | + /** |
| 34 | + * @var FormData |
| 35 | + */ |
| 36 | + private $form_data; |
| 37 | + /** |
| 38 | + * @var Properties |
| 39 | + */ |
| 40 | + private $props; |
| 41 | + /** |
| 42 | + * @var Settings |
| 43 | + */ |
| 44 | + private $settings; |
| 45 | + |
| 46 | + public function __construct( |
| 47 | + Settings $settings, |
| 48 | + Filesystem $filesystem, |
| 49 | + TableHelper $table_helper, |
| 50 | + Http $http, |
| 51 | + FormData $form_data, |
| 52 | + Table $table, |
| 53 | + Properties $properties, |
| 54 | + StateDataContainer $state_data_container |
| 55 | + ) { |
| 56 | + $this->props = $properties; |
| 57 | + $this->settings = $settings->get_settings(); |
| 58 | + $this->filesystem = $filesystem; |
| 59 | + $this->table_helper = $table_helper; |
| 60 | + $this->http = $http; |
| 61 | + $this->state_data = $state_data_container; |
| 62 | + $this->form_data = $form_data; |
| 63 | + $this->table = $table; |
| 64 | + } |
| 65 | + |
| 66 | + function register() { |
| 67 | + add_filter( 'wpmdb_backup_header_included_tables', array( $this, 'backup_header_included_tables' ) ); |
| 68 | + } |
| 69 | + |
| 70 | + function delete_export_file( $filename, $is_backup ) { |
| 71 | + $dump_file = $this->table_helper->format_dump_name( $filename ); |
| 72 | + |
| 73 | + if ( true == $is_backup ) { |
| 74 | + $dump_file = preg_replace( '/.gz$/', '', $dump_file ); |
| 75 | + } |
| 76 | + |
| 77 | + $dump_file = $this->filesystem->get_upload_info( 'path' ) . DIRECTORY_SEPARATOR . $dump_file; |
| 78 | + |
| 79 | + if ( empty( $dump_file ) || false === $this->filesystem->file_exists( $dump_file ) ) { |
| 80 | + $return = array( 'wpmdb_error' => 1, 'body' => __( 'MySQL export file not found.', 'wp-migrate-db' ) ); |
| 81 | + |
| 82 | + return $this->http->end_ajax( json_encode( $return ) ); |
| 83 | + } |
| 84 | + |
| 85 | + if ( false === $this->filesystem->unlink( $dump_file ) ) { |
| 86 | + $return = array( 'wpmdb_error' => 1, 'body' => __( 'Could not delete the MySQL export file.', 'wp-migrate-db' ) ); |
| 87 | + |
| 88 | + return $this->http->end_ajax( json_encode( $return ) ); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Determine which tables to backup (if required). |
| 94 | + * |
| 95 | + * @param $profile |
| 96 | + * @param $prefixed_tables |
| 97 | + * @param $all_tables |
| 98 | + * |
| 99 | + * @return mixed|void |
| 100 | + */ |
| 101 | + public function get_tables_to_backup( $profile, $prefixed_tables, $all_tables ) { |
| 102 | + $tables_to_backup = array(); |
| 103 | + |
| 104 | + switch ( $profile['backup_option'] ) { |
| 105 | + case 'backup_only_with_prefix': |
| 106 | + $tables_to_backup = $prefixed_tables; |
| 107 | + break; |
| 108 | + case 'backup_selected': |
| 109 | + /** |
| 110 | + * When tables to migrate is tables with prefix, select_tables |
| 111 | + * might be empty. Intersecting it with remote/local tables |
| 112 | + * throws notice/warning and won't backup the file either. |
| 113 | + */ |
| 114 | + if ( 'migrate_only_with_prefix' === $profile['table_migrate_option'] ) { |
| 115 | + $tables_to_backup = $prefixed_tables; |
| 116 | + } else { |
| 117 | + $tables_to_backup = array_intersect( $profile['select_tables'], $all_tables ); |
| 118 | + } |
| 119 | + break; |
| 120 | + case 'backup_manual_select': |
| 121 | + $tables_to_backup = array_intersect( $profile['select_backup'], $all_tables ); |
| 122 | + break; |
| 123 | + } |
| 124 | + |
| 125 | + return apply_filters( 'wpmdb_tables_to_backup', $tables_to_backup, $profile ); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Updates the database backup header with the tables that were backed up. |
| 130 | + * |
| 131 | + * @param $included_tables |
| 132 | + * |
| 133 | + * @return mixed|void |
| 134 | + */ |
| 135 | + public function backup_header_included_tables( $included_tables ) { |
| 136 | + $state_data = $this->state_data->getData(); |
| 137 | + $form_data = $this->form_data->getFormData(); |
| 138 | + if ( 'backup' === $state_data['stage'] ) { |
| 139 | + $included_tables = $this->get_tables_to_backup( $form_data, $this->table->get_tables( 'prefix' ), $this->table->get_tables() ); |
| 140 | + } |
| 141 | + |
| 142 | + return $included_tables; |
| 143 | + } |
| 144 | +} |
0 commit comments