PluginProbe
SQLite Database Integration / 2.1.15
SQLite Database Integration v2.1.15
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.1.15, at wp-includes/sqlite/install-functions.php

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