-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.php
More file actions
384 lines (344 loc) · 17.2 KB
/
Copy pathdb.php
File metadata and controls
384 lines (344 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
/**
* WordPress db.php drop-in that replaces the SQLite database file with
* markdown files as the sole source of truth. In-memory SQLite is used
* as the runtime query engine.
*
* Supports two modes (set MARKDOWN_DB_MODE in wp-config.php):
* 'mirror' — Phase 1: SQLite on disk, markdown mirrored on writes
* 'primary' — Phase 2: In-memory SQLite, markdown files are the database
*
* This file goes in wp-content/db.php (replacing the SQLite drop-in's version).
*
* @studio-keep
* @package Markdown_Database_Integration
*/
if ( ! function_exists( 'markdown_db_default_content_dir' ) ) {
function markdown_db_default_content_dir(): string {
if ( ! defined( 'WP_CONTENT_DIR' ) ) {
return '';
}
$db = WP_CONTENT_DIR . '/db';
$legacy = WP_CONTENT_DIR . '/markdown';
if ( ! is_dir( $db ) && is_dir( $legacy ) ) {
return $legacy;
}
return $db;
}
}
if ( ! function_exists( 'markdown_database_integration_enable_native_shadow' ) ) {
function markdown_database_integration_enable_native_shadow( object $database, string $plugin_dir ): void {
if ( ! defined( 'MARKDOWN_DB_NATIVE_SHADOW' ) || true !== MARKDOWN_DB_NATIVE_SHADOW || ! method_exists( $database, 'set_native_shadow_verifier' ) ) {
return;
}
try {
require_once $plugin_dir . '/inc/native/class-wp-markdown-native-shadow-verifier.php';
$verifier = WP_Markdown_Native_Shadow_Factory::from_globals( $database );
$database->set_native_shadow_verifier( $verifier );
$GLOBALS['markdown_db_native_shadow_verifier'] = $verifier;
} catch ( Throwable $error ) {
$GLOBALS['markdown_db_native_shadow_diagnostic'] = array(
'code' => 'markdown_db_native_shadow_bootstrap_failed',
'class' => get_class( $error ),
);
}
}
}
// mysql-content is a plugin-level MySQL runtime. Leave normal wpdb bootstrap
// intact so the plugin can report the explicit db.php migration diagnostic.
if ( defined( 'MARKDOWN_DB_BACKEND' ) && 'mysql-content' === MARKDOWN_DB_BACKEND ) {
define( 'MARKDOWN_DB_RETAINED_DROPIN', true );
return;
}
// mysql-full must own this boundary because plugins load after wpdb bootstrap.
if ( defined( 'MARKDOWN_DB_BACKEND' ) && 'mysql-full' === MARKDOWN_DB_BACKEND ) {
$markdown_db_mysql_plugin_dir = null;
foreach ( array( __DIR__ . '/mu-plugins/markdown-database-integration', __DIR__ . '/plugins/markdown-database-integration' ) as $path ) {
if ( is_file( $path . '/inc/class-wp-markdown-backend-capabilities.php' ) && is_file( $path . '/inc/class-wp-markdown-sql-classifier.php' ) && is_file( $path . '/inc/mysql/class-wp-markdown-mysql-wpdb.php' ) && is_file( $path . '/inc/mysql/class-wp-markdown-mysql-outbox.php' ) ) {
$markdown_db_mysql_plugin_dir = $path;
break;
}
}
if ( null === $markdown_db_mysql_plugin_dir || ! class_exists( 'wpdb' ) ) {
$GLOBALS['markdown_db_mysql_full_diagnostic'] = array( 'code' => 'markdown_db_mysql_full_bootstrap_unavailable', 'message' => 'mysql-full requires a compatible MDI db.php bootstrap and stock wpdb.' );
return;
}
try {
require_once $markdown_db_mysql_plugin_dir . '/inc/class-wp-markdown-backend-capabilities.php';
$markdown_db_mysql_backend = WP_Markdown_Backend_Resolver::configure_from_globals();
$markdown_db_mysql_backend->require( 'table_mutation_capture' );
require_once $markdown_db_mysql_plugin_dir . '/inc/mysql/class-wp-markdown-mysql-outbox.php';
require_once $markdown_db_mysql_plugin_dir . '/inc/mysql/class-wp-markdown-mysql-impact-adapter.php';
require_once $markdown_db_mysql_plugin_dir . '/inc/mysql/class-wp-markdown-mysql-semantic-drain.php';
require_once $markdown_db_mysql_plugin_dir . '/inc/mysql/class-wp-markdown-mysql-wpdb.php';
if ( 2 !== WP_Markdown_MySQL_WPDB::BOOTSTRAP_ABI ) {
throw new RuntimeException( 'Incompatible mysql-full bootstrap ABI.' );
}
$markdown_db_mysql_wpdb = new WP_Markdown_MySQL_WPDB( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST );
$markdown_db_base_prefix = (string) ( $GLOBALS['table_prefix'] ?? 'wp_' );
$markdown_db_mysql_impact_adapter = new WP_Markdown_MySQL_Impact_Adapter( $markdown_db_mysql_wpdb->markdown_db_mysql_connection() );
$markdown_db_mysql_outbox = new WP_Markdown_MySQL_Outbox( $markdown_db_mysql_wpdb->markdown_db_mysql_connection(), $markdown_db_base_prefix . 'mdi_mysql_outbox', array( $markdown_db_mysql_impact_adapter, 'intents' ) );
$markdown_db_mysql_wpdb->set_mutation_sink( $markdown_db_mysql_outbox );
markdown_database_integration_enable_native_shadow( $markdown_db_mysql_wpdb, $markdown_db_mysql_plugin_dir );
$GLOBALS['markdown_db_mysql_outbox'] = $markdown_db_mysql_outbox;
$GLOBALS['markdown_db_mysql_semantic_drain'] = new WP_Markdown_MySQL_Semantic_Drain( $markdown_db_mysql_outbox, $markdown_db_mysql_impact_adapter );
$GLOBALS['wpdb'] = $markdown_db_mysql_wpdb;
define( 'MARKDOWN_DB_DROPIN', true );
} catch ( Throwable $error ) {
unset( $GLOBALS['wpdb'] );
$GLOBALS['markdown_db_mysql_full_diagnostic'] = array( 'code' => 'markdown_db_mysql_full_bootstrap_incompatible', 'message' => $error->getMessage() );
}
return;
}
// mdi-native serves bounded reads directly from canonical Markdown and JSON.
// It must replace wpdb here because regular plugins load after database boot.
if ( defined( 'MARKDOWN_DB_BACKEND' ) && 'mdi-native' === MARKDOWN_DB_BACKEND ) {
$markdown_db_native_plugin_dir = null;
foreach ( array( __DIR__ . '/mu-plugins/markdown-database-integration', __DIR__ . '/plugins/markdown-database-integration' ) as $path ) {
if ( is_file( $path . '/inc/class-wp-markdown-backend-capabilities.php' ) && is_file( $path . '/inc/native/class-wp-markdown-native-query-runtime.php' ) && is_file( $path . '/inc/native/class-wp-markdown-native-wpdb.php' ) ) {
$markdown_db_native_plugin_dir = $path;
break;
}
}
if ( null === $markdown_db_native_plugin_dir || ! class_exists( 'wpdb' ) ) {
throw new RuntimeException( 'mdi-native requires the MDI plugin and stock wpdb during db.php bootstrap.' );
}
require_once $markdown_db_native_plugin_dir . '/inc/class-wp-markdown-backend-capabilities.php';
$markdown_db_native_backend = WP_Markdown_Backend_Resolver::configure_from_globals();
$markdown_db_native_backend->require( 'canonical_option_select' );
require_once $markdown_db_native_plugin_dir . '/inc/native/class-wp-markdown-native-query-runtime.php';
require_once $markdown_db_native_plugin_dir . '/inc/native/class-wp-markdown-native-wpdb.php';
$markdown_db_native_content_dir = defined( 'MARKDOWN_DB_CONTENT_DIR' ) ? MARKDOWN_DB_CONTENT_DIR : markdown_db_default_content_dir();
$markdown_db_native_state_dir = defined( 'MARKDOWN_DB_STATE_DIR' ) ? MARKDOWN_DB_STATE_DIR : $markdown_db_native_content_dir;
$markdown_db_native_prefix = (string) ( $GLOBALS['table_prefix'] ?? 'wp_' );
$markdown_db_native_runtime = WP_Markdown_Native_Runtime_Factory::runtime(
$markdown_db_native_state_dir,
$markdown_db_native_prefix,
$markdown_db_native_prefix,
function_exists( 'is_multisite' ) && is_multisite(),
$markdown_db_native_content_dir
);
$GLOBALS['wpdb'] = new WP_Markdown_Native_WPDB( $markdown_db_native_runtime, $markdown_db_native_prefix );
define( 'MARKDOWN_DB_DROPIN', true );
return;
}
define( 'SQLITE_DB_DROPIN_VERSION', '1.8.0' );
define( 'MARKDOWN_DB_DROPIN', true );
if ( ! function_exists( 'markdown_database_integration_store_has_siteurl' ) ) {
/**
* Whether the state store already contains an installed-site siteurl.
*
* @param string $state_dir Runtime state directory.
* @return bool True when siteurl is persisted in per-option or legacy form.
*/
function markdown_database_integration_store_has_siteurl( string $state_dir ): bool {
$siteurl_file = rtrim( $state_dir, '/\\' ) . '/_options/siteurl.json';
if ( file_exists( $siteurl_file ) ) {
return true;
}
$legacy_file = rtrim( $state_dir, '/\\' ) . '/options.json';
if ( ! file_exists( $legacy_file ) ) {
return false;
}
$decoded = json_decode( (string) file_get_contents( $legacy_file ), true );
if ( ! is_array( $decoded ) ) {
return false;
}
foreach ( $decoded as $row ) {
if ( is_array( $row ) && isset( $row['option_name'] ) && 'siteurl' === $row['option_name'] ) {
return true;
}
}
return false;
}
}
if ( ! function_exists( 'markdown_database_integration_primary_index_path' ) ) {
/**
* Resolve the default primary index path for the configured roots.
*
* @param string $content_dir Markdown post content directory.
* @param string $state_dir Runtime state directory.
* @return string Primary SQLite index path.
*/
function markdown_database_integration_primary_index_path( string $content_dir, string $state_dir ): string {
if ( rtrim( $state_dir, '/\\' ) !== rtrim( $content_dir, '/\\' ) ) {
return rtrim( $state_dir, '/\\' ) . '/markdown-index.sqlite';
}
return dirname( rtrim( $content_dir, '/\\' ) ) . '/markdown-index.sqlite';
}
}
// Find the SQLite integration plugin. Probe order:
// 1. wp-content/mu-plugins/sqlite-database-integration (typical install)
// 2. wp-content/plugins/sqlite-database-integration (regular plugin install)
// 3. /internal/shared/sqlite-database-integration (WordPress Playground)
//
// The Playground location is the bundled SDI install used by
// @wp-playground/cli — required so MDI works under the homeboy-extensions
// wordpress backend (test runner, bench dispatcher) without a separate
// drop-in. Playground exposes SDI via VFS at this absolute path; the host
// filesystem doesn't have it, so the realpath() probe is unsafe — file_exists
// suffices because the file is either there or it's not.
$sqlite_plugin_implementation_folder_path = realpath( __DIR__ . '/mu-plugins/sqlite-database-integration' );
if ( ! $sqlite_plugin_implementation_folder_path || ! file_exists( $sqlite_plugin_implementation_folder_path ) ) {
$sqlite_plugin_implementation_folder_path = realpath( __DIR__ . '/plugins/sqlite-database-integration' );
}
if ( ! $sqlite_plugin_implementation_folder_path || ! file_exists( $sqlite_plugin_implementation_folder_path . '/wp-includes/sqlite/db.php' ) ) {
$playground_sqlite = '/internal/shared/sqlite-database-integration';
if ( @file_exists( $playground_sqlite . '/wp-includes/sqlite/db.php' ) ) {
$sqlite_plugin_implementation_folder_path = $playground_sqlite;
}
}
if ( ! defined( 'MARKDOWN_DB_PLAYGROUND_RUNTIME' ) ) {
define( 'MARKDOWN_DB_PLAYGROUND_RUNTIME', isset( $playground_sqlite ) && $playground_sqlite === $sqlite_plugin_implementation_folder_path );
}
// Bail if SQLite integration is not installed.
if ( ! $sqlite_plugin_implementation_folder_path || ! file_exists( $sqlite_plugin_implementation_folder_path . '/wp-includes/sqlite/db.php' ) ) {
return;
}
// Standard SQLite constants.
if ( ! defined( 'DATABASE_TYPE' ) ) {
define( 'DATABASE_TYPE', 'sqlite' );
}
if ( ! defined( 'DB_ENGINE' ) ) {
define( 'DB_ENGINE', 'sqlite' );
}
// Force the v2 AST driver — required for our integration.
if ( ! defined( 'WP_SQLITE_AST_DRIVER' ) ) {
define( 'WP_SQLITE_AST_DRIVER', true );
}
// Primary mode uses markdown-index.sqlite as the active query engine. The
// SQLite Integration install shim opens FQDB directly during wp_install(). Only
// point FQDB at the markdown index when the markdown store already represents an
// installed site. Partial seed stores fall back to the existing SQLite database
// so already-installed Playground sites do not reset to the install screen.
if ( defined( 'MARKDOWN_DB_MODE' ) && 'primary' === MARKDOWN_DB_MODE ) {
$markdown_db_content_dir = defined( 'MARKDOWN_DB_CONTENT_DIR' )
? MARKDOWN_DB_CONTENT_DIR
: markdown_db_default_content_dir();
$markdown_db_state_dir = defined( 'MARKDOWN_DB_STATE_DIR' )
? MARKDOWN_DB_STATE_DIR
: $markdown_db_content_dir;
if ( markdown_database_integration_store_has_siteurl( $markdown_db_state_dir ) ) {
$markdown_db_index_path = markdown_database_integration_primary_index_path( $markdown_db_content_dir, $markdown_db_state_dir );
if ( MARKDOWN_DB_PLAYGROUND_RUNTIME ) {
$markdown_db_index_path = rtrim( sys_get_temp_dir(), '/\\' ) . '/markdown-index-' . substr( md5( $markdown_db_index_path ), 0, 12 ) . '.sqlite';
}
if ( ! defined( 'MARKDOWN_DB_INDEX_PATH' ) ) {
define( 'MARKDOWN_DB_INDEX_PATH', $markdown_db_index_path );
}
if ( ! defined( 'FQDBDIR' ) ) {
define( 'FQDBDIR', rtrim( dirname( MARKDOWN_DB_INDEX_PATH ), '/\\' ) . '/' );
}
if ( ! defined( 'FQDB' ) ) {
define( 'FQDB', MARKDOWN_DB_INDEX_PATH );
}
}
}
// Load the SQLite integration's version and constants.
require_once $sqlite_plugin_implementation_folder_path . '/wp-includes/database/version.php';
require_once $sqlite_plugin_implementation_folder_path . '/constants.php';
// Check PDO extensions.
if ( ! extension_loaded( 'pdo' ) || ! extension_loaded( 'pdo_sqlite' ) ) {
return;
}
// Load the SQLite v2 driver stack (parser, lexer, connection, driver).
require_once $sqlite_plugin_implementation_folder_path . '/wp-includes/database/load.php';
// SQLite Integration renamed the canonical PDO driver after its latest release.
if ( ! class_exists( 'WP_MySQL_On_SQLite', false ) ) {
if ( class_exists( 'WP_PDO_MySQL_On_SQLite', false ) ) {
if ( ! defined( 'MARKDOWN_DB_SQLITE_LEGACY_RESULT_API' ) ) {
define( 'MARKDOWN_DB_SQLITE_LEGACY_RESULT_API', true );
}
class_alias( 'WP_PDO_MySQL_On_SQLite', 'WP_MySQL_On_SQLite' );
} else {
throw new RuntimeException(
'Markdown Database Integration requires SQLite Integration with the PDO-compatible WP_MySQL_On_SQLite or WP_PDO_MySQL_On_SQLite driver.'
);
}
}
// Load the SQLite DB class.
require_once $sqlite_plugin_implementation_folder_path . '/wp-includes/sqlite/class-wp-sqlite-db.php';
require_once $sqlite_plugin_implementation_folder_path . '/wp-includes/sqlite/install-functions.php';
if ( defined( 'MARKDOWN_DB_MODE' ) && 'primary' === MARKDOWN_DB_MODE ) {
$markdown_db_content_dir = defined( 'MARKDOWN_DB_CONTENT_DIR' )
? MARKDOWN_DB_CONTENT_DIR
: markdown_db_default_content_dir();
$markdown_db_state_dir = defined( 'MARKDOWN_DB_STATE_DIR' )
? MARKDOWN_DB_STATE_DIR
: $markdown_db_content_dir;
if ( ! markdown_database_integration_store_has_siteurl( $markdown_db_state_dir ) ) {
if ( ! defined( 'MARKDOWN_DB_INSTALL_FALLBACK' ) ) {
define( 'MARKDOWN_DB_INSTALL_FALLBACK', true );
}
$db_name = defined( 'DB_NAME' ) && '' !== DB_NAME ? DB_NAME : 'database_name_here';
$GLOBALS['wpdb'] = new WP_SQLite_DB( $db_name );
return;
}
}
// Load our markdown classes.
$markdown_plugin_dir = null;
// Look in mu-plugins first, then plugins.
$possible_paths = array(
__DIR__ . '/mu-plugins/markdown-database-integration',
__DIR__ . '/plugins/markdown-database-integration',
);
foreach ( $possible_paths as $path ) {
if ( is_dir( $path ) && file_exists( $path . '/inc/class-wp-markdown-storage.php' ) ) {
$markdown_plugin_dir = $path;
break;
}
}
// If the markdown plugin isn't installed yet, fall back to standard SQLite.
if ( ! $markdown_plugin_dir ) {
// Fallback: standard SQLite behavior.
if ( defined( 'DB_NAME' ) && '' !== DB_NAME ) {
$db_name = DB_NAME;
} else {
$db_name = 'database_name_here';
}
$GLOBALS['wpdb'] = new WP_SQLite_DB( $db_name );
return;
}
// Load composer autoloader if present. MDI is storage-only; content-format
// dependencies belong to the application layer above this drop-in.
$composer_autoload = $markdown_plugin_dir . '/vendor/autoload.php';
if ( file_exists( $composer_autoload ) ) {
require_once $composer_autoload;
}
// Load markdown integration classes.
require_once $markdown_plugin_dir . '/inc/class-wp-markdown-backend-capabilities.php';
$markdown_db_backend = WP_Markdown_Backend_Resolver::configure_from_globals();
WP_Markdown_Backend_Resolver::require_runtime_capabilities(
$markdown_db_backend,
defined( 'MARKDOWN_DB_MODE' ) ? (string) MARKDOWN_DB_MODE : 'mirror'
);
require_once $markdown_plugin_dir . '/inc/class-wp-markdown-frontmatter-profiles.php';
require_once $markdown_plugin_dir . '/inc/class-wp-markdown-content-layout-profiles.php';
if ( defined( 'MARKDOWN_DB_CONTENT_LAYOUT_PROFILE_BOOTSTRAP' ) && is_file( MARKDOWN_DB_CONTENT_LAYOUT_PROFILE_BOOTSTRAP ) ) {
require_once MARKDOWN_DB_CONTENT_LAYOUT_PROFILE_BOOTSTRAP;
}
require_once $markdown_plugin_dir . '/inc/class-wp-markdown-storage.php';
require_once $markdown_plugin_dir . '/inc/class-wp-markdown-driver.php';
require_once $markdown_plugin_dir . '/inc/class-wp-markdown-search.php';
require_once $markdown_plugin_dir . '/inc/class-wp-markdown-write-engine.php';
require_once $markdown_plugin_dir . '/inc/class-wp-markdown-reconciliation-adapters.php';
require_once $markdown_plugin_dir . '/inc/class-wp-markdown-loader.php';
require_once $markdown_plugin_dir . '/inc/class-wp-markdown-primary-index-health.php';
require_once $markdown_plugin_dir . '/inc/class-wp-markdown-db.php';
// Load plugin constants (if not already loaded via the plugin file).
if ( ! defined( 'MARKDOWN_DB_VERSION' ) ) {
require_once $markdown_plugin_dir . '/markdown-database-integration.php';
}
// Create the database connection — our WP_Markdown_DB extends WP_SQLite_DB.
if ( defined( 'DB_NAME' ) && '' !== DB_NAME ) {
$db_name = DB_NAME;
} else {
$db_name = 'database_name_here';
}
$GLOBALS['wpdb'] = new WP_Markdown_DB( $db_name );
markdown_database_integration_enable_native_shadow( $GLOBALS['wpdb'], $markdown_plugin_dir );
// Boot Query Monitor integration if present.
$qm_boot = $sqlite_plugin_implementation_folder_path . '/integrations/query-monitor/boot.php';
if ( file_exists( $qm_boot ) ) {
require_once $qm_boot;
}