diff --git a/activate.php b/activate.php index e91e1819..2f1bb65f 100644 --- a/activate.php +++ b/activate.php @@ -84,29 +84,7 @@ function sqlite_plugin_copy_db_file() { // Place database drop-in if not present yet, except in case there is // another database drop-in present already. - if ( ! defined( 'SQLITE_DB_DROPIN_VERSION' ) && ! file_exists( $destination ) ) { - // Init the filesystem to allow copying the file. - global $wp_filesystem; - - require_once ABSPATH . '/wp-admin/includes/file.php'; - - // Init the filesystem if needed, then copy the file, replacing contents as needed. - if ( ( $wp_filesystem || WP_Filesystem() ) && $wp_filesystem->touch( $destination ) ) { - - // Get the db.copy.php file contents, replace placeholders and write it to the destination. - $file_contents = str_replace( - array( - '{SQLITE_IMPLEMENTATION_FOLDER_PATH}', - '{SQLITE_PLUGIN}', - ), - array( - __DIR__, - str_replace( WP_PLUGIN_DIR . '/', '', SQLITE_MAIN_FILE ), - ), - file_get_contents( __DIR__ . '/db.copy' ) - ); - - $wp_filesystem->put_contents( $destination, $file_contents ); - } + if ( ! defined( 'SQLITE_DROPIN' ) && ! file_exists( $destination ) ) { + @symlink( __DIR__ . '/wp-content/db.php', $destination ); // phpcs:ignore } } diff --git a/admin-notices.php b/admin-notices.php index 8eaf2531..7f5e273a 100644 --- a/admin-notices.php +++ b/admin-notices.php @@ -29,18 +29,18 @@ function sqlite_plugin_admin_notice() { } /* - * If the SQLITE_DB_DROPIN_VERSION constant is not defined + * If the SQLITE_DROPIN constant is not defined * but there's a db.php file in the wp-content directory, then the module can't be activated. * The module should not have been activated in the first place * (there's a check in the can-load.php file), but this is a fallback check. */ - if ( file_exists( WP_CONTENT_DIR . '/db.php' ) && ! defined( 'SQLITE_DB_DROPIN_VERSION' ) ) { + if ( file_exists( WP_CONTENT_DIR . '/db.php' ) && ! defined( 'SQLITE_DROPIN' ) ) { printf( '

%s

', sprintf( - /* translators: 1: SQLITE_DB_DROPIN_VERSION constant, 2: db.php drop-in path */ + /* translators: 1: SQLITE_DROPIN constant, 2: db.php drop-in path */ __( 'The SQLite Integration module is active, but the %1$s constant is missing. It appears you already have another %2$s file present on your site. ', 'sqlite-database-integration' ), - 'SQLITE_DB_DROPIN_VERSION', + 'SQLITE_DROPIN', '' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php' ) ); diff --git a/admin-page.php b/admin-page.php index 6a44c0e7..c6816498 100644 --- a/admin-page.php +++ b/admin-page.php @@ -32,7 +32,7 @@ function sqlite_integration_admin_screen() {
- +

@@ -55,41 +55,18 @@ function sqlite_integration_admin_screen() {

- - -
-

- ' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php' - ); - ?> -

-
- + +
+

' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php' ); ?> - - -

-

- ' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php' - ); - ?> -

-
- +

+

@@ -111,9 +88,13 @@ function sqlite_integration_admin_screen() {

+
+ + + ' . __( 'Database: SQLite', 'sqlite-database-integration' ) . ''; } elseif ( stripos( $wpdb->db_server_info(), 'maria' ) !== false ) { $title = '' . __( 'Database: MariaDB', 'sqlite-database-integration' ) . ''; diff --git a/constants.php b/constants.php index 15e6772a..ce88d557 100644 --- a/constants.php +++ b/constants.php @@ -8,14 +8,7 @@ // Temporary - This will be in wp-config.php once SQLite is merged in Core. if ( ! defined( 'DB_ENGINE' ) ) { - if ( defined( 'SQLITE_DB_DROPIN_VERSION' ) ) { - define( 'DB_ENGINE', 'sqlite' ); - } elseif ( defined( 'DATABASE_ENGINE' ) ) { - // backwards compatibility with previous versions of the plugin. - define( 'DB_ENGINE', DATABASE_ENGINE ); - } else { - define( 'DB_ENGINE', 'mysql' ); - } + define( 'DB_ENGINE', 'mysql' ); } /** diff --git a/deactivate.php b/deactivate.php index 2b79d9a3..d9284bee 100644 --- a/deactivate.php +++ b/deactivate.php @@ -12,7 +12,7 @@ * When the plugin gets merged in wp-core, this is not to be ported. */ function sqlite_plugin_remove_db_file() { - if ( ! defined( 'SQLITE_DB_DROPIN_VERSION' ) || ! file_exists( WP_CONTENT_DIR . '/db.php' ) ) { + if ( ! defined( 'SQLITE_DROPIN' ) || ! SQLITE_DROPIN || ! file_exists( WP_CONTENT_DIR . '/db.php' ) ) { return; } diff --git a/load.php b/load.php index 2a3764f9..94536961 100644 --- a/load.php +++ b/load.php @@ -20,3 +20,4 @@ require_once __DIR__ . '/deactivate.php'; require_once __DIR__ . '/admin-notices.php'; require_once __DIR__ . '/health-check.php'; +require_once __DIR__ . '/mysql-to-sqlite-migration.php'; diff --git a/mysql-to-sqlite-migration.php b/mysql-to-sqlite-migration.php new file mode 100644 index 00000000..4f2cce59 --- /dev/null +++ b/mysql-to-sqlite-migration.php @@ -0,0 +1,142 @@ +get_results( 'SHOW TABLES' ) ); +} + +/** + * Create a table in the SQLite database. + * + * @param string $table_name The name of the table to create. + * @param string $create_table_query The CREATE TABLE query to execute. + */ +function sqlite_integration_create_table( $table_name ) { + global $wpdb, $sqlite_db; + // Get the table structure. + $table_structure = $wpdb->get_results( "SHOW CREATE TABLE $table_name" ); + // Execute the CREATE TABLE query. + $sqlite_db->query( $table_structure[0]->{'Create Table'} ); +} + +/** + * Migrate data for a single table. + * + * @param string $table_name The name of the table to migrate data for. + */ +function sqlite_integration_migrate_table( $table_name ) { + global $wpdb, $sqlite_db; + // Get the data from the MySQL table, and insert it into the SQLite table. + $data = $wpdb->get_results( "SELECT * FROM $table_name" ); + foreach ( $data as $row ) { + $sqlite_db->insert( $table_name, (array) $row ); + } +} + +/** + * Run AJAX action to migrate a table. + */ +function sqlite_integration_migrate_table_ajax() { + if ( ! isset( $_POST['table_name'] ) ) { + wp_send_json_error( 'Table name is required' ); + } + $table_name = sanitize_text_field( $_POST['table_name'] ); + sqlite_integration_create_table( $table_name ); + sqlite_integration_migrate_table( $table_name ); + wp_send_json_success( 'Table migrated' ); +} +add_action( 'wp_ajax_sqlite_integration_migrate_table', 'sqlite_integration_migrate_table_ajax' ); + +/** + * Run AJAX action to add the db.php file. + */ +function sqlite_integration_add_db_php_file_ajax() { + // Check if the sqlite_plugin_copy_db_file function exists. + if ( ! function_exists( 'sqlite_plugin_copy_db_file' ) ) { + require_once __DIR__ . '/activate.php'; + } + sqlite_plugin_copy_db_file(); + wp_send_json_success( 'db.php file added' ); +} +add_action( 'wp_ajax_sqlite_integration_add_db_php_file', 'sqlite_integration_add_db_php_file_ajax' ); + +/** + * Add a script to the admin page to handle the migration process. + */ +function sqlite_integration_add_admin_script() { + ?> + +