Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
39.47% |
15 / 38 |
|
37.50% |
3 / 8 |
CRAP | |
0.00% |
0 / 1 |
| Playground_Importer | |
50.00% |
15 / 30 |
|
37.50% |
3 / 8 |
26.12 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| preprocess | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| process_files | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| recreate_database | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| postprocess_database | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| clean_up | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| verify_site_integrity | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| is_valid | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Playground_Importer file. |
| 4 | * |
| 5 | * @package wpcomsh |
| 6 | */ |
| 7 | |
| 8 | namespace Imports; |
| 9 | |
| 10 | require_once __DIR__ . '/../class-backup-importer.php'; |
| 11 | require_once __DIR__ . '/class-playground-db-importer.php'; |
| 12 | require_once __DIR__ . '/class-playground-clean-up.php'; |
| 13 | require_once __DIR__ . '/class-sql-importer.php'; |
| 14 | require_once __DIR__ . '/../utils/class-filerestorer.php'; |
| 15 | require_once __DIR__ . '/../utils/logger/class-filelogger.php'; |
| 16 | require_once __DIR__ . '/class-sql-postprocessor.php'; |
| 17 | require_once __DIR__ . '/class-playground-site-integrity-check.php'; |
| 18 | |
| 19 | use Imports\Utils\FileRestorer; |
| 20 | use Imports\Utils\Logger\FileLogger; |
| 21 | |
| 22 | /** |
| 23 | * Playground backup importer. |
| 24 | * |
| 25 | * This class provides a common interface for all backup importers. |
| 26 | */ |
| 27 | class Playground_Importer extends \Imports\Backup_Importer { |
| 28 | const SQLITE_DB_PATH = 'wp-content/database/.ht.sqlite'; |
| 29 | |
| 30 | /** |
| 31 | * File logger |
| 32 | * |
| 33 | * @var FileLogger |
| 34 | */ |
| 35 | private FileLogger $logger; |
| 36 | |
| 37 | /** |
| 38 | * Constructor. |
| 39 | * |
| 40 | * @param string $zip_or_tar_file_path The path to the ZIP or TAR file to be imported. |
| 41 | * @param string $destination_path The path where the backup will be imported. |
| 42 | * @param string $tmp_prefix The table prefix to use when importing the database. |
| 43 | */ |
| 44 | public function __construct( string $zip_or_tar_file_path, string $destination_path, string $tmp_prefix ) { |
| 45 | parent::__construct( $zip_or_tar_file_path, $destination_path, $tmp_prefix ); |
| 46 | |
| 47 | $this->logger = new FileLogger(); |
| 48 | $this->logger->check_and_clear_file(); |
| 49 | |
| 50 | $this->tmp_database = $this->destination_path . 'database.sql'; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Preprocess the backup before importing. |
| 55 | * |
| 56 | * @return bool|\WP_Error True on success, or a WP_Error on failure. |
| 57 | */ |
| 58 | public function preprocess() { |
| 59 | $options = array( |
| 60 | 'output_mode' => SQL_Generator::OUTPUT_TYPE_FILE, |
| 61 | 'output_file' => $this->tmp_database, |
| 62 | 'tmp_tables' => true, |
| 63 | 'tmp_prefix' => $this->tmp_prefix, |
| 64 | ); |
| 65 | $db_path = $this->destination_path . self::SQLITE_DB_PATH; |
| 66 | $importer = new Playground_DB_Importer(); |
| 67 | $results = $importer->generate_sql( $db_path, $options ); |
| 68 | |
| 69 | return is_wp_error( $results ) ? $results : true; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Process the files in the backup. |
| 74 | * |
| 75 | * @return bool|\WP_Error True on success, or a WP_Error on failure. |
| 76 | */ |
| 77 | public function process_files() { |
| 78 | $final_path = '/srv/htdocs/'; |
| 79 | $file_restorer = new FileRestorer( $this->destination_path, $final_path, $this->logger ); |
| 80 | $queue_result = $file_restorer->enqueue_files(); |
| 81 | |
| 82 | if ( is_wp_error( $queue_result ) ) { |
| 83 | return $queue_result; |
| 84 | } |
| 85 | |
| 86 | $restore_result = $file_restorer->restore_files(); |
| 87 | |
| 88 | if ( is_wp_error( $restore_result ) ) { |
| 89 | return $restore_result; |
| 90 | } |
| 91 | |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Recreate the database from the backup. |
| 97 | * |
| 98 | * @return bool|\WP_Error True on success, or a WP_Error on failure. |
| 99 | */ |
| 100 | public function recreate_database() { |
| 101 | return SQL_Importer::import( $this->tmp_database ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Postprocess the database after importing. |
| 106 | * |
| 107 | * @return bool|\WP_Error True on success, or a WP_Error on failure. |
| 108 | */ |
| 109 | public function postprocess_database() { |
| 110 | $processor = new SQL_Postprocessor( get_home_url(), get_site_url(), $this->tmp_prefix, false, $this->logger ); |
| 111 | |
| 112 | return $processor->postprocess(); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Clean up after the import. |
| 117 | * |
| 118 | * @return bool|\WP_Error True on success, or a WP_Error on failure. |
| 119 | */ |
| 120 | public function clean_up() { |
| 121 | return Playground_Clean_Up::remove_tmp_files( $this->zip_or_tar_file_path, $this->destination_path ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Verify the integrity of the site after importing. |
| 126 | * |
| 127 | * @return bool always true for now |
| 128 | */ |
| 129 | public function verify_site_integrity() { |
| 130 | $checker = new Playground_Site_Integrity_Check( $this->logger ); |
| 131 | return $checker->check(); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Return whether the specified folder is a valid Playground backup. |
| 136 | * |
| 137 | * @param string $destination_path The path where the backup will be imported. |
| 138 | * |
| 139 | * @return bool True if the specified folder is a valid backup, false otherwise. |
| 140 | */ |
| 141 | public static function is_valid( $destination_path ): bool { |
| 142 | return file_exists( trailingslashit( $destination_path ) . self::SQLITE_DB_PATH ); |
| 143 | } |
| 144 | } |