PluginProbe
SQLite Database Integration / 2.2.17
SQLite Database Integration v2.2.17
3.0.2 3.0.1 trunk 2.1.13 2.1.14 2.1.15 2.1.16 2.2.0 2.2.1 2.2.10 2.2.11 2.2.12 2.2.13 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.2.2 2.2.20 2.2.21 2.2.22 2.2.23 2.2.3 All 32 releases
sqlite-database-integration / wp-includes / sqlite / install-functions.php

install-functions.php in SQLite Database Integration 2.2.17, at wp-includes/sqlite/install-functions.php

242 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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_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
30 } catch ( PDOException $err ) {
31 $err_data = $err->errorInfo; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
32 $message = 'Database connection error!<br />';
33 $message .= sprintf( 'Error message is: %s', $err_data[2] );
34 wp_die( $message, 'Database Error!' );
35 }
36
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 $query = null;
46
47 try {
48 $translator->begin_transaction();
49 foreach ( $queries as $query ) {
50 $query = trim( $query );
51 if ( empty( $query ) ) {
52 continue;
53 }
54
55 $result = $translator->query( $query );
56 if ( false === $result ) {
57 throw new PDOException( $translator->get_error_message() );
58 }
59 }
60 $translator->commit();
61 } catch ( PDOException $err ) {
62 $err_data = $err->errorInfo; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
63 $err_code = $err_data[1];
64 $translator->rollback();
65 $message = sprintf(
66 'Error occurred while creating tables or indexes...<br />Query was: %s<br />',
67 var_export( $query, true )
68 );
69 $message .= sprintf( 'Error message is: %s', $err_data[2] );
70 wp_die( $message, 'Database Error!' );
71 }
72
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 return true;
123 }
124
125 if ( ! function_exists( 'wp_install' ) ) {
126 /**
127 * Installs the site.
128 *
129 * Runs the required functions to set up and populate the database,
130 * including primary admin user and initial options.
131 *
132 * @since 1.0.0
133 *
134 * @param string $blog_title Site title.
135 * @param string $user_name User's username.
136 * @param string $user_email User's email.
137 * @param bool $is_public Whether the site is public.
138 * @param string $deprecated Optional. Not used.
139 * @param string $user_password Optional. User's chosen password. Default empty (random password).
140 * @param string $language Optional. Language chosen. Default empty.
141 * @return array {
142 * Data for the newly installed site.
143 *
144 * @type string $url The URL of the site.
145 * @type int $user_id The ID of the site owner.
146 * @type string $password The password of the site owner, if their user account didn't already exist.
147 * @type string $password_message The explanatory message regarding the password.
148 * }
149 */
150 function wp_install( $blog_title, $user_name, $user_email, $is_public, $deprecated = '', $user_password = '', $language = '' ) {
151 if ( ! empty( $deprecated ) ) {
152 _deprecated_argument( __FUNCTION__, '2.6.0' );
153 }
154
155 wp_check_mysql_version();
156 wp_cache_flush();
157 /* SQLite changes: Replace the call to make_db_current_silent() with sqlite_make_db_sqlite(). */
158 sqlite_make_db_sqlite(); // phpcs:ignore PHPCompatibility.Extensions.RemovedExtensions.sqliteRemoved
159 populate_options();
160 populate_roles();
161
162 update_option( 'blogname', $blog_title );
163 update_option( 'admin_email', $user_email );
164 update_option( 'blog_public', $is_public );
165
166 // Freshness of site - in the future, this could get more specific about actions taken, perhaps.
167 update_option( 'fresh_site', 1 );
168
169 if ( $language ) {
170 update_option( 'WPLANG', $language );
171 }
172
173 $guessurl = wp_guess_url();
174
175 update_option( 'siteurl', $guessurl );
176
177 // If not a public site, don't ping.
178 if ( ! $is_public ) {
179 update_option( 'default_pingback_flag', 0 );
180 }
181
182 /*
183 * Create default user. If the user already exists, the user tables are
184 * being shared among sites. Just set the role in that case.
185 */
186 $user_id = username_exists( $user_name );
187 $user_password = trim( $user_password );
188 $email_password = false;
189 $user_created = false;
190
191 if ( ! $user_id && empty( $user_password ) ) {
192 $user_password = wp_generate_password( 12, false );
193 $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' );
194 $user_id = wp_create_user( $user_name, $user_password, $user_email );
195 update_user_meta( $user_id, 'default_password_nag', true );
196 $email_password = true;
197 $user_created = true;
198 } elseif ( ! $user_id ) {
199 // Password has been provided.
200 $message = '<em>' . __( 'Your chosen password.', 'sqlite-database-integration' ) . '</em>';
201 $user_id = wp_create_user( $user_name, $user_password, $user_email );
202 $user_created = true;
203 } else {
204 $message = __( 'User already exists. Password inherited.', 'sqlite-database-integration' );
205 }
206
207 $user = new WP_User( $user_id );
208 $user->set_role( 'administrator' );
209
210 if ( $user_created ) {
211 $user->user_url = $guessurl;
212 wp_update_user( $user );
213 }
214
215 wp_install_defaults( $user_id );
216
217 wp_install_maybe_enable_pretty_permalinks();
218
219 flush_rewrite_rules();
220
221 wp_new_blog_notification( $blog_title, $guessurl, $user_id, ( $email_password ? $user_password : __( 'The password you chose during installation.', 'sqlite-database-integration' ) ) );
222
223 wp_cache_flush();
224
225 /**
226 * Fires after a site is fully installed.
227 *
228 * @since 3.9.0
229 *
230 * @param WP_User $user The site owner.
231 */
232 do_action( 'wp_install', $user );
233
234 return array(
235 'url' => $guessurl,
236 'user_id' => $user_id,
237 'password' => $user_password,
238 'password_message' => $message,
239 );
240 }
241 }
242