| 1 |
<?php |
| 2 |
/** |
| 3 |
* Main integration file. |
| 4 |
* |
| 5 |
* @package wp-sqlite-integration |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Function to create tables according to the schemas of WordPress. |
| 11 |
* |
| 12 |
* This is executed only once while installation. |
| 13 |
* |
| 14 |
* @since 1.0.0 |
| 15 |
* |
| 16 |
* @return boolean |
| 17 |
* |
| 18 |
* @throws PDOException If the database connection fails. |
| 19 |
*/ |
| 20 |
function sqlite_make_db_sqlite() { |
| 21 |
global $wpdb; |
| 22 |
|
| 23 |
include_once ABSPATH . 'wp-admin/includes/schema.php'; |
| 24 |
|
| 25 |
$table_schemas = wp_get_db_schema(); |
| 26 |
$queries = explode( ';', $table_schemas ); |
| 27 |
try { |
| 28 |
$pdo = new PDO( 'sqlite:' . FQDB, null, null, array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ) ); // phpcs:ignore WordPress.DB.RestrictedClasses |
| 29 |
} catch ( PDOException $err ) { |
| 30 |
$err_data = $err->errorInfo; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 31 |
$message = 'Database connection error!<br />'; |
| 32 |
$message .= sprintf( 'Error message is: %s', $err_data[2] ); |
| 33 |
wp_die( $message, 'Database Error!' ); |
| 34 |
} |
| 35 |
|
| 36 |
if ( defined( 'WP_SQLITE_AST_DRIVER' ) && WP_SQLITE_AST_DRIVER ) { |
| 37 |
$translator = new WP_SQLite_Driver( |
| 38 |
new WP_SQLite_Connection( array( 'pdo' => $pdo ) ), |
| 39 |
$wpdb->dbname |
| 40 |
); |
| 41 |
} else { |
| 42 |
$translator = new WP_SQLite_Translator( $pdo ); |
| 43 |
} |
| 44 |
$query = null; |
| 45 |
|
| 46 |
try { |
| 47 |
$translator->begin_transaction(); |
| 48 |
foreach ( $queries as $query ) { |
| 49 |
$query = trim( $query ); |
| 50 |
if ( empty( $query ) ) { |
| 51 |
continue; |
| 52 |
} |
| 53 |
|
| 54 |
$result = $translator->query( $query ); |
| 55 |
if ( false === $result ) { |
| 56 |
throw new PDOException( $translator->get_error_message() ); |
| 57 |
} |
| 58 |
} |
| 59 |
$translator->commit(); |
| 60 |
} catch ( PDOException $err ) { |
| 61 |
$err_data = $err->errorInfo; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 62 |
$err_code = $err_data[1]; |
| 63 |
$translator->rollback(); |
| 64 |
$message = sprintf( |
| 65 |
'Error occurred while creating tables or indexes...<br />Query was: %s<br />', |
| 66 |
var_export( $query, true ) |
| 67 |
); |
| 68 |
$message .= sprintf( 'Error message is: %s', $err_data[2] ); |
| 69 |
wp_die( $message, 'Database Error!' ); |
| 70 |
} |
| 71 |
|
| 72 |
/* |
| 73 |
* Debug: Cross-check with MySQL. |
| 74 |
* This is for debugging purpose only and requires files |
| 75 |
* that are present in the GitHub repository |
| 76 |
* but not the plugin published on WordPress.org. |
| 77 |
*/ |
| 78 |
if ( defined( 'SQLITE_DEBUG_CROSSCHECK' ) && SQLITE_DEBUG_CROSSCHECK ) { |
| 79 |
$host = DB_HOST; |
| 80 |
$port = 3306; |
| 81 |
if ( str_contains( $host, ':' ) ) { |
| 82 |
$host_parts = explode( ':', $host ); |
| 83 |
$host = $host_parts[0]; |
| 84 |
$port = $host_parts[1]; |
| 85 |
} |
| 86 |
$dsn = 'mysql:host=' . $host . '; port=' . $port . '; dbname=' . DB_NAME; |
| 87 |
$pdo_mysql = new PDO( $dsn, DB_USER, DB_PASSWORD, array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ) ); // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO |
| 88 |
$pdo_mysql->query( 'SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";' ); |
| 89 |
$pdo_mysql->query( 'SET time_zone = "+00:00";' ); |
| 90 |
foreach ( $queries as $query ) { |
| 91 |
$query = trim( $query ); |
| 92 |
if ( empty( $query ) ) { |
| 93 |
continue; |
| 94 |
} |
| 95 |
try { |
| 96 |
$pdo_mysql->beginTransaction(); |
| 97 |
$pdo_mysql->query( $query ); |
| 98 |
} catch ( PDOException $err ) { |
| 99 |
$err_data = $err->errorInfo; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 100 |
$err_code = $err_data[1]; |
| 101 |
// phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual |
| 102 |
if ( 5 == $err_code || 6 == $err_code ) { |
| 103 |
// If the database is locked, commit again. |
| 104 |
$pdo_mysql->commit(); |
| 105 |
} else { |
| 106 |
$pdo_mysql->rollBack(); |
| 107 |
$message = sprintf( |
| 108 |
'Error occurred while creating tables or indexes...<br />Query was: %s<br />', |
| 109 |
var_export( $query, true ) |
| 110 |
); |
| 111 |
$message .= sprintf( 'Error message is: %s', $err_data[2] ); |
| 112 |
wp_die( $message, 'Database Error!' ); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
$pdo = null; |
| 119 |
|
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
if ( ! function_exists( 'wp_install' ) ) { |
| 124 |
/** |
| 125 |
* Installs the site. |
| 126 |
* |
| 127 |
* Runs the required functions to set up and populate the database, |
| 128 |
* including primary admin user and initial options. |
| 129 |
* |
| 130 |
* @since 1.0.0 |
| 131 |
* |
| 132 |
* @param string $blog_title Site title. |
| 133 |
* @param string $user_name User's username. |
| 134 |
* @param string $user_email User's email. |
| 135 |
* @param bool $is_public Whether the site is public. |
| 136 |
* @param string $deprecated Optional. Not used. |
| 137 |
* @param string $user_password Optional. User's chosen password. Default empty (random password). |
| 138 |
* @param string $language Optional. Language chosen. Default empty. |
| 139 |
* @return array { |
| 140 |
* Data for the newly installed site. |
| 141 |
* |
| 142 |
* @type string $url The URL of the site. |
| 143 |
* @type int $user_id The ID of the site owner. |
| 144 |
* @type string $password The password of the site owner, if their user account didn't already exist. |
| 145 |
* @type string $password_message The explanatory message regarding the password. |
| 146 |
* } |
| 147 |
*/ |
| 148 |
function wp_install( $blog_title, $user_name, $user_email, $is_public, $deprecated = '', $user_password = '', $language = '' ) { |
| 149 |
if ( ! empty( $deprecated ) ) { |
| 150 |
_deprecated_argument( __FUNCTION__, '2.6.0' ); |
| 151 |
} |
| 152 |
|
| 153 |
wp_check_mysql_version(); |
| 154 |
wp_cache_flush(); |
| 155 |
/* SQLite changes: Replace the call to make_db_current_silent() with sqlite_make_db_sqlite(). */ |
| 156 |
sqlite_make_db_sqlite(); // phpcs:ignore PHPCompatibility.Extensions.RemovedExtensions.sqliteRemoved |
| 157 |
populate_options(); |
| 158 |
populate_roles(); |
| 159 |
|
| 160 |
update_option( 'blogname', $blog_title ); |
| 161 |
update_option( 'admin_email', $user_email ); |
| 162 |
update_option( 'blog_public', $is_public ); |
| 163 |
|
| 164 |
// Freshness of site - in the future, this could get more specific about actions taken, perhaps. |
| 165 |
update_option( 'fresh_site', 1 ); |
| 166 |
|
| 167 |
if ( $language ) { |
| 168 |
update_option( 'WPLANG', $language ); |
| 169 |
} |
| 170 |
|
| 171 |
$guessurl = wp_guess_url(); |
| 172 |
|
| 173 |
update_option( 'siteurl', $guessurl ); |
| 174 |
|
| 175 |
// If not a public site, don't ping. |
| 176 |
if ( ! $is_public ) { |
| 177 |
update_option( 'default_pingback_flag', 0 ); |
| 178 |
} |
| 179 |
|
| 180 |
/* |
| 181 |
* Create default user. If the user already exists, the user tables are |
| 182 |
* being shared among sites. Just set the role in that case. |
| 183 |
*/ |
| 184 |
$user_id = username_exists( $user_name ); |
| 185 |
$user_password = trim( $user_password ); |
| 186 |
$email_password = false; |
| 187 |
$user_created = false; |
| 188 |
|
| 189 |
if ( ! $user_id && empty( $user_password ) ) { |
| 190 |
$user_password = wp_generate_password( 12, false ); |
| 191 |
$message = __( '<strong><em>Note that password</em></strong> carefully! It is a <em>random</em> password that was generated just for you.', 'sqlite-database-integration' ); |
| 192 |
$user_id = wp_create_user( $user_name, $user_password, $user_email ); |
| 193 |
update_user_meta( $user_id, 'default_password_nag', true ); |
| 194 |
$email_password = true; |
| 195 |
$user_created = true; |
| 196 |
} elseif ( ! $user_id ) { |
| 197 |
// Password has been provided. |
| 198 |
$message = '<em>' . __( 'Your chosen password.', 'sqlite-database-integration' ) . '</em>'; |
| 199 |
$user_id = wp_create_user( $user_name, $user_password, $user_email ); |
| 200 |
$user_created = true; |
| 201 |
} else { |
| 202 |
$message = __( 'User already exists. Password inherited.', 'sqlite-database-integration' ); |
| 203 |
} |
| 204 |
|
| 205 |
$user = new WP_User( $user_id ); |
| 206 |
$user->set_role( 'administrator' ); |
| 207 |
|
| 208 |
if ( $user_created ) { |
| 209 |
$user->user_url = $guessurl; |
| 210 |
wp_update_user( $user ); |
| 211 |
} |
| 212 |
|
| 213 |
wp_install_defaults( $user_id ); |
| 214 |
|
| 215 |
wp_install_maybe_enable_pretty_permalinks(); |
| 216 |
|
| 217 |
flush_rewrite_rules(); |
| 218 |
|
| 219 |
wp_new_blog_notification( $blog_title, $guessurl, $user_id, ( $email_password ? $user_password : __( 'The password you chose during installation.', 'sqlite-database-integration' ) ) ); |
| 220 |
|
| 221 |
wp_cache_flush(); |
| 222 |
|
| 223 |
/** |
| 224 |
* Fires after a site is fully installed. |
| 225 |
* |
| 226 |
* @since 3.9.0 |
| 227 |
* |
| 228 |
* @param WP_User $user The site owner. |
| 229 |
*/ |
| 230 |
do_action( 'wp_install', $user ); |
| 231 |
|
| 232 |
return array( |
| 233 |
'url' => $guessurl, |
| 234 |
'user_id' => $user_id, |
| 235 |
'password' => $user_password, |
| 236 |
'password_message' => $message, |
| 237 |
); |
| 238 |
} |
| 239 |
} |
| 240 |
|