Skip to content

Commit 137d62a

Browse files
committed
Replace global PDO injection
Expose the active MySQL-on-SQLite driver through WP_SQLite_DB and its underlying SQLite PDO through WP_MySQL_On_SQLite. Stop treating the compatibility global as a connection input. Retain it temporarily as a deprecated output alias while consumers migrate to the explicit accessors.
1 parent eefe318 commit 137d62a

4 files changed

Lines changed: 103 additions & 24 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,21 @@ public function getAttribute( $attribute ) {
11741174
return $this->connection->get_pdo()->getAttribute( $attribute );
11751175
}
11761176

1177+
/**
1178+
* Get the underlying SQLite PDO instance.
1179+
*
1180+
* This is an advanced escape hatch. Queries executed through the returned
1181+
* PDO instance use SQLite semantics and bypass MySQL emulation. Callers
1182+
* should not retain it across reconnections or modify driver-owned state.
1183+
*
1184+
* @since 3.0.0
1185+
*
1186+
* @return PDO The underlying SQLite PDO instance.
1187+
*/
1188+
public function get_sqlite_pdo(): PDO {
1189+
return $this->connection->get_pdo();
1190+
}
1191+
11771192
/**
11781193
* Get the SQLite connection instance.
11791194
*

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_PDO_API_Tests.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ public function test_connection(): void {
3434
$this->assertInstanceOf( PDO::class, $driver );
3535
}
3636

37+
public function test_exposes_underlying_sqlite_pdo(): void {
38+
$pdo_class = PHP_VERSION_ID >= 80400 ? PDO\SQLite::class : PDO::class;
39+
$pdo = new $pdo_class( 'sqlite::memory:' );
40+
$driver = new WP_MySQL_On_SQLite(
41+
'mysql-on-sqlite:dbname=wp',
42+
null,
43+
null,
44+
array( 'pdo' => $pdo )
45+
);
46+
47+
$this->assertSame( $pdo, $driver->get_sqlite_pdo() );
48+
}
49+
3750
public function test_dsn_parsing(): void {
3851
// Standard DSN.
3952
$driver = new WP_MySQL_On_SQLite( 'mysql-on-sqlite:path=:memory:;dbname=wp' );

‎packages/mysql-on-sqlite/tests/WP_SQLite_DB_Tests.php‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,59 @@ public function setUp(): void {
2121
);
2222
}
2323

24+
public function test_exposes_mysql_on_sqlite_driver(): void {
25+
$wpdb = new class( $this->driver ) extends WP_SQLite_DB {
26+
public function __construct( WP_MySQL_On_SQLite $driver ) {
27+
$this->dbh = $driver;
28+
}
29+
};
30+
31+
$this->assertSame( $this->driver, $wpdb->get_driver() );
32+
}
33+
34+
public function test_db_connect_ignores_global_pdo_injection(): void {
35+
$pdo_class = PHP_VERSION_ID >= 80400 ? PDO\SQLite::class : PDO::class;
36+
$injected_pdo = new $pdo_class( 'sqlite::memory:' );
37+
$original_cwd = getcwd();
38+
$temp_dir = sys_get_temp_dir() . '/wp-sqlite-db-' . uniqid( '', true );
39+
40+
mkdir( $temp_dir );
41+
chdir( $temp_dir );
42+
$GLOBALS['@pdo'] = $injected_pdo;
43+
44+
$wpdb = new class() extends WP_SQLite_DB {
45+
public $charset = 'utf8mb4';
46+
public $dbname = 'wp';
47+
public $has_connected = false;
48+
public $last_error = '';
49+
public $ready = false;
50+
public $result;
51+
52+
public function __construct() {
53+
}
54+
55+
public function set_sql_mode( $modes = array() ) {
56+
}
57+
};
58+
59+
try {
60+
$this->assertTrue( $wpdb->db_connect() );
61+
$this->assertNotSame( $injected_pdo, $wpdb->get_driver()->get_sqlite_pdo() );
62+
$this->assertSame( $wpdb->get_driver()->get_sqlite_pdo(), $GLOBALS['@pdo'] );
63+
$this->assertTrue( $wpdb->close() );
64+
$this->assertArrayNotHasKey( '@pdo', $GLOBALS );
65+
} finally {
66+
unset( $GLOBALS['@pdo'] );
67+
chdir( $original_cwd );
68+
foreach ( array( '.htaccess', 'index.php' ) as $filename ) {
69+
if ( file_exists( $temp_dir . '/' . $filename ) ) {
70+
unlink( $temp_dir . '/' . $filename );
71+
}
72+
}
73+
rmdir( $temp_dir );
74+
}
75+
}
76+
2477
/**
2578
* @dataProvider dataMysqlEscaping
2679
*/

‎packages/plugin-sqlite-database-integration/wp-includes/sqlite/class-wp-sqlite-db.php‎

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ class WP_SQLite_DB extends wpdb {
2020
*/
2121
protected $dbh;
2222

23-
/**
24-
* Whether the PDO instance was provided externally through $GLOBALS['@pdo'].
25-
*
26-
* @var bool
27-
*/
28-
private $is_pdo_external;
29-
3023
/**
3124
* Backward compatibility, see wpdb::$allow_unsafe_unquoted_parameters.
3225
*
@@ -63,6 +56,17 @@ public function __construct( $dbname ) {
6356
$this->charset = 'utf8mb4';
6457
}
6558

59+
/**
60+
* Returns the active MySQL-on-SQLite driver.
61+
*
62+
* @since 3.0.0
63+
*
64+
* @return WP_MySQL_On_SQLite|null The active driver, or null while disconnected.
65+
*/
66+
public function get_driver(): ?WP_MySQL_On_SQLite {
67+
return $this->dbh;
68+
}
69+
6670
/**
6771
* Method to set character set for the database.
6872
*
@@ -212,14 +216,8 @@ public function close() {
212216
return false;
213217
}
214218

215-
/*
216-
* @TODO: Replace and deprecate the $GLOBALS['@pdo'] injection mechanism.
217-
* PDO has no close method and is released only when all references are unset.
218-
* Until then, retain external PDOs so reconnects reuse the same database.
219-
*/
220219
if (
221-
! $this->is_pdo_external
222-
&& isset( $GLOBALS['@pdo'] )
220+
isset( $GLOBALS['@pdo'] )
223221
&& $GLOBALS['@pdo'] === $pdo
224222
) {
225223
unset( $GLOBALS['@pdo'] );
@@ -427,9 +425,6 @@ public function db_connect( $allow_bail = true ) {
427425
$this->init_charset();
428426
}
429427

430-
$this->is_pdo_external = isset( $GLOBALS['@pdo'] );
431-
$pdo = $this->is_pdo_external ? $GLOBALS['@pdo'] : null;
432-
433428
// Migrate the database file from a legacy path, if it exists.
434429
if ( ! defined( 'DB_FILE' ) && ! file_exists( FQDB ) ) {
435430
$old_db_path = FQDBDIR . '.ht.sqlite.php';
@@ -463,10 +458,7 @@ public function db_connect( $allow_bail = true ) {
463458
$options = array(
464459
'journal_mode' => defined( 'SQLITE_JOURNAL_MODE' ) ? SQLITE_JOURNAL_MODE : null,
465460
);
466-
if ( null !== $pdo ) {
467-
$options['pdo'] = $pdo;
468-
}
469-
$dbh = new WP_MySQL_On_SQLite(
461+
$dbh = new WP_MySQL_On_SQLite(
470462
sprintf(
471463
'mysql-on-sqlite:path=%s;dbname=%s',
472464
str_replace( ';', ';;', FQDB ),
@@ -477,9 +469,15 @@ public function db_connect( $allow_bail = true ) {
477469
$options
478470
);
479471
$dbh->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true ); // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO
480-
$pdo = $dbh->get_connection()->get_pdo();
481-
$this->dbh = $dbh;
482-
$GLOBALS['@pdo'] = $pdo;
472+
$this->dbh = $dbh;
473+
474+
/**
475+
* Exposes the SQLite PDO instance for backward compatibility.
476+
*
477+
* @deprecated 3.0.0 Use WP_SQLite_DB::get_driver() with
478+
* WP_MySQL_On_SQLite::get_sqlite_pdo() instead.
479+
*/
480+
$GLOBALS['@pdo'] = $dbh->get_sqlite_pdo();
483481
} catch ( Throwable $e ) {
484482
$this->last_error = $this->format_error_message( $e );
485483
}

0 commit comments

Comments
 (0)