Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
55.00% covered (warning)
55.00%
33 / 60
45.45% covered (danger)
45.45%
5 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
Playground_Importer
63.46% covered (warning)
63.46%
33 / 52
45.45% covered (danger)
45.45%
5 / 11
48.81
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 get_sqlite_db_path
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 find_sqlite_db_path
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 get_table_prefix
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
 preprocess
73.33% covered (warning)
73.33%
11 / 15
0.00% covered (danger)
0.00%
0 / 1
4.30
 process_files
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 recreate_database
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 postprocess_database
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 clean_up
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 verify_site_integrity
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 is_valid
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Playground_Importer file.
4 *
5 * @package wpcomsh
6 */
7
8namespace Imports;
9
10require_once __DIR__ . '/../class-backup-importer.php';
11require_once __DIR__ . '/class-playground-db-importer.php';
12require_once __DIR__ . '/class-playground-clean-up.php';
13require_once __DIR__ . '/class-sql-importer.php';
14require_once __DIR__ . '/../utils/class-filerestorer.php';
15require_once __DIR__ . '/../utils/logger/class-filelogger.php';
16require_once __DIR__ . '/class-sql-postprocessor.php';
17require_once __DIR__ . '/class-playground-site-integrity-check.php';
18
19use Imports\Utils\FileRestorer;
20use Imports\Utils\Logger\FileLogger;
21
22/**
23 * Playground backup importer.
24 *
25 * This class provides a common interface for all backup importers.
26 */
27class 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     * Get the path to the SQLite database file.
55     *
56     * @return string|null The path to the SQLite database file, or null if none is found.
57     */
58    public function get_sqlite_db_path(): ?string {
59        return self::find_sqlite_db_path( $this->destination_path );
60    }
61
62    /**
63     * Find the SQLite database file inside an extracted backup.
64     *
65     * The SQLite database file is located at `wp-content/database/.ht.sqlite` or in a folder of the
66     * type `wp-content/database/.ht.<random>/.ht.sqlite`.
67     *
68     * @param string $destination_path The path where the backup was extracted.
69     *
70     * @return string|null The path to the SQLite database file, or null if none is found.
71     */
72    private static function find_sqlite_db_path( string $destination_path ): ?string {
73        $destination_path = trailingslashit( $destination_path );
74        $default_path     = $destination_path . self::SQLITE_DB_PATH;
75        if ( file_exists( $default_path ) ) {
76            return $default_path;
77        }
78
79        $matches = glob( $destination_path . 'wp-content/database/.ht.*/.ht.sqlite' );
80        foreach ( (array) $matches as $path ) {
81            if ( file_exists( $path ) ) {
82                return $path;
83            }
84        }
85
86        return null;
87    }
88
89    /**
90     * Read the table prefix from an extracted backup's `wp-config.php`.
91     *
92     * @param string $destination_path The path where the backup was extracted.
93     *
94     * @return string|null The `$table_prefix`, or null if it can't be read.
95     */
96    public static function get_table_prefix( string $destination_path ): ?string {
97        $wp_config_path = trailingslashit( $destination_path ) . 'wp-config.php';
98
99        if ( ! is_file( $wp_config_path ) || ! is_readable( $wp_config_path ) ) {
100            return null;
101        }
102
103        // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- A local file.
104        $wp_config = file_get_contents( $wp_config_path );
105
106        if ( false === $wp_config || ! preg_match( '/^\s*\$table_prefix\s*=\s*([\'"])(\w+)\1\s*;/m', $wp_config, $matches ) ) {
107            return null;
108        }
109
110        return $matches[2];
111    }
112
113    /**
114     * Preprocess the backup before importing.
115     *
116     * @return bool|\WP_Error True on success, or a WP_Error on failure.
117     */
118    public function preprocess() {
119        $options      = array(
120            'output_mode' => SQL_Generator::OUTPUT_TYPE_FILE,
121            'output_file' => $this->tmp_database,
122            'tmp_tables'  => true,
123            'tmp_prefix'  => $this->tmp_prefix,
124        );
125        $table_prefix = self::get_table_prefix( $this->destination_path );
126        if ( null !== $table_prefix ) {
127            $options['prefix'] = $table_prefix;
128        }
129        $db_path = $this->get_sqlite_db_path();
130        if ( null === $db_path ) {
131            return new \WP_Error( 'database-file-not-exists', 'Database file not exists' );
132        }
133
134        $importer = new Playground_DB_Importer();
135        $results  = $importer->generate_sql( $db_path, $options );
136
137        return is_wp_error( $results ) ? $results : true;
138    }
139
140    /**
141     * Process the files in the backup.
142     *
143     * @return bool|\WP_Error True on success, or a WP_Error on failure.
144     */
145    public function process_files() {
146        $final_path    = '/srv/htdocs/';
147        $file_restorer = new FileRestorer( $this->destination_path, $final_path, $this->logger );
148        $queue_result  = $file_restorer->enqueue_files();
149
150        if ( is_wp_error( $queue_result ) ) {
151            return $queue_result;
152        }
153
154        $restore_result = $file_restorer->restore_files();
155
156        if ( is_wp_error( $restore_result ) ) {
157            return $restore_result;
158        }
159
160        return true;
161    }
162
163    /**
164     * Recreate the database from the backup.
165     *
166     * @return bool|\WP_Error True on success, or a WP_Error on failure.
167     */
168    public function recreate_database() {
169        return SQL_Importer::import( $this->tmp_database );
170    }
171
172    /**
173     * Postprocess the database after importing.
174     *
175     * @return bool|\WP_Error True on success, or a WP_Error on failure.
176     */
177    public function postprocess_database() {
178        $processor = new SQL_Postprocessor( get_home_url(), get_site_url(), $this->tmp_prefix, false, $this->logger );
179
180        return $processor->postprocess();
181    }
182
183    /**
184     * Clean up after the import.
185     *
186     * @return bool|\WP_Error True on success, or a WP_Error on failure.
187     */
188    public function clean_up() {
189        return Playground_Clean_Up::remove_tmp_files( $this->zip_or_tar_file_path, $this->destination_path );
190    }
191
192    /**
193     * Verify the integrity of the site after importing.
194     *
195     * @return bool always true for now
196     */
197    public function verify_site_integrity() {
198        $checker = new Playground_Site_Integrity_Check( $this->logger );
199        return $checker->check();
200    }
201
202    /**
203     * Return whether the specified folder is a valid Playground backup.
204     *
205     * @param string $destination_path The path where the backup will be imported.
206     *
207     * @return bool True if the specified folder is a valid backup, false otherwise.
208     */
209    public static function is_valid( $destination_path ): bool {
210        return self::find_sqlite_db_path( $destination_path ) !== null;
211    }
212}