| @@ -1,10 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Main integration file. |
| 4 | - * | |
| 5 | - * @package wp-sqlite-integration | |
| 6 | - * @since 1.0.0 | |
| 7 | 4 | */ |
| 8 | 5 | |
| 9 | 6 | /** |
| 10 | 7 | * Function to create tables according to the schemas of WordPress. |
| @@ -10,10 +7,8 @@ | ||
| 10 | 7 | * Function to create tables according to the schemas of WordPress. |
| 11 | 8 | * |
| 12 | 9 | * This is executed only once while installation. |
| 13 | 10 | * |
| 14 | - * @since 1.0.0 | |
| 15 | - * | |
| 16 | 11 | * @return boolean |
| 17 | 12 | * |
| 18 | 13 | * @throws PDOException If the database connection fails. |
| 19 | 14 | */ |
| @@ -24,29 +19,32 @@ | ||
| 24 | 19 | |
| 25 | 20 | $table_schemas = wp_get_db_schema(); |
| 26 | 21 | $queries = explode( ';', $table_schemas ); |
| 27 | 22 | try { |
| 28 | - $pdo_class = PHP_VERSION_ID >= 80400 ? PDO\SQLite::class : PDO::class; // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO | |
| 29 | - $pdo = new $pdo_class( 'sqlite:' . FQDB, null, null, array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ) ); // phpcs:ignore WordPress.DB.RestrictedClasses | |
| 23 | + $translator = new WP_MySQL_On_SQLite( | |
| 24 | + sprintf( | |
| 25 | + 'mysql-on-sqlite:path=%s;dbname=%s', | |
| 26 | + str_replace( ';', ';;', FQDB ), | |
| 27 | + str_replace( ';', ';;', $wpdb->dbname ) | |
| 28 | + ), | |
| 29 | + null, | |
| 30 | + null, | |
| 31 | + array( | |
| 32 | + 'sqlite_journal_mode' => defined( 'SQLITE_JOURNAL_MODE' ) ? SQLITE_JOURNAL_MODE : null, | |
| 33 | + ) | |
| 34 | + ); | |
| 35 | + $translator->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true ); // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO | |
| 30 | 36 | } catch ( PDOException $err ) { |
| 31 | 37 | $err_data = $err->errorInfo; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 32 | 38 | $message = 'Database connection error!<br />'; |
| 33 | - $message .= sprintf( 'Error message is: %s', $err_data[2] ); | |
| 39 | + $message .= sprintf( 'Error message is: %s', $err_data[2] ?? $err->getMessage() ); | |
| 34 | 40 | wp_die( $message, 'Database Error!' ); |
| 35 | 41 | } |
| 36 | 42 | |
| 37 | - if ( defined( 'WP_SQLITE_AST_DRIVER' ) && WP_SQLITE_AST_DRIVER ) { | |
| 38 | - $translator = new WP_SQLite_Driver( | |
| 39 | - new WP_SQLite_Connection( array( 'pdo' => $pdo ) ), | |
| 40 | - $wpdb->dbname | |
| 41 | - ); | |
| 42 | - } else { | |
| 43 | - $translator = new WP_SQLite_Translator( $pdo ); | |
| 44 | - } | |
| 45 | 43 | $query = null; |
| 46 | 44 | |
| 47 | 45 | try { |
| 48 | - $translator->begin_transaction(); | |
| 46 | + $translator->beginTransaction(); | |
| 49 | 47 | foreach ( $queries as $query ) { |
| 50 | 48 | $query = trim( $query ); |
| 51 | 49 | if ( empty( $query ) ) { |
| 52 | 50 | continue; |
| @@ -51,75 +49,21 @@ | ||
| 51 | 49 | if ( empty( $query ) ) { |
| 52 | 50 | continue; |
| 53 | 51 | } |
| 54 | 52 | |
| 55 | - $result = $translator->query( $query ); | |
| 56 | - if ( false === $result ) { | |
| 57 | - throw new PDOException( $translator->get_error_message() ); | |
| 58 | - } | |
| 53 | + $translator->query( $query ); | |
| 59 | 54 | } |
| 60 | 55 | $translator->commit(); |
| 61 | 56 | } catch ( PDOException $err ) { |
| 62 | - $err_data = $err->errorInfo; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase | |
| 63 | - $err_code = $err_data[1]; | |
| 64 | - $translator->rollback(); | |
| 57 | + $translator->rollBack(); | |
| 65 | 58 | $message = sprintf( |
| 66 | 59 | 'Error occurred while creating tables or indexes...<br />Query was: %s<br />', |
| 67 | 60 | var_export( $query, true ) |
| 68 | 61 | ); |
| 69 | - $message .= sprintf( 'Error message is: %s', $err_data[2] ); | |
| 62 | + $message .= sprintf( 'Error message is: %s', $err->getMessage() ); | |
| 70 | 63 | wp_die( $message, 'Database Error!' ); |
| 71 | 64 | } |
| 72 | 65 | |
| 73 | - /* | |
| 74 | - * Debug: Cross-check with MySQL. | |
| 75 | - * This is for debugging purpose only and requires files | |
| 76 | - * that are present in the GitHub repository | |
| 77 | - * but not the plugin published on WordPress.org. | |
| 78 | - */ | |
| 79 | - if ( defined( 'SQLITE_DEBUG_CROSSCHECK' ) && SQLITE_DEBUG_CROSSCHECK ) { | |
| 80 | - $host = DB_HOST; | |
| 81 | - $port = 3306; | |
| 82 | - if ( str_contains( $host, ':' ) ) { | |
| 83 | - $host_parts = explode( ':', $host ); | |
| 84 | - $host = $host_parts[0]; | |
| 85 | - $port = $host_parts[1]; | |
| 86 | - } | |
| 87 | - $dsn = 'mysql:host=' . $host . '; port=' . $port . '; dbname=' . DB_NAME; | |
| 88 | - $pdo_class = PHP_VERSION_ID >= 80400 ? PDO\MySQL::class : PDO::class; // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO | |
| 89 | - $pdo_mysql = new $pdo_class( $dsn, DB_USER, DB_PASSWORD, array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ) ); // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO | |
| 90 | - $pdo_mysql->query( 'SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";' ); | |
| 91 | - $pdo_mysql->query( 'SET time_zone = "+00:00";' ); | |
| 92 | - foreach ( $queries as $query ) { | |
| 93 | - $query = trim( $query ); | |
| 94 | - if ( empty( $query ) ) { | |
| 95 | - continue; | |
| 96 | - } | |
| 97 | - try { | |
| 98 | - $pdo_mysql->beginTransaction(); | |
| 99 | - $pdo_mysql->query( $query ); | |
| 100 | - } catch ( PDOException $err ) { | |
| 101 | - $err_data = $err->errorInfo; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase | |
| 102 | - $err_code = $err_data[1]; | |
| 103 | - // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual | |
| 104 | - if ( 5 == $err_code || 6 == $err_code ) { | |
| 105 | - // If the database is locked, commit again. | |
| 106 | - $pdo_mysql->commit(); | |
| 107 | - } else { | |
| 108 | - $pdo_mysql->rollBack(); | |
| 109 | - $message = sprintf( | |
| 110 | - 'Error occurred while creating tables or indexes...<br />Query was: %s<br />', | |
| 111 | - var_export( $query, true ) | |
| 112 | - ); | |
| 113 | - $message .= sprintf( 'Error message is: %s', $err_data[2] ); | |
| 114 | - wp_die( $message, 'Database Error!' ); | |
| 115 | - } | |
| 116 | - } | |
| 117 | - } | |
| 118 | - } | |
| 119 | - | |
| 120 | - $pdo = null; | |
| 121 | - | |
| 122 | 66 | return true; |
| 123 | 67 | } |
| 124 | 68 | |
| 125 | 69 | if ( ! function_exists( 'wp_install' ) ) { |
| @@ -127,10 +71,8 @@ | ||
| 127 | 71 | * Installs the site. |
| 128 | 72 | * |
| 129 | 73 | * Runs the required functions to set up and populate the database, |
| 130 | 74 | * including primary admin user and initial options. |
| 131 | - * | |
| 132 | - * @since 1.0.0 | |
| 133 | 75 | * |
| 134 | 76 | * @param string $blog_title Site title. |
| 135 | 77 | * @param string $user_name User's username. |
| 136 | 78 | * @param string $user_email User's email. |