Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
],
"wp-test-php": [
"@wp-test-ensure-env @no_additional_args",
"rm -rf wordpress/src/wp-content/database/.ht.sqlite @no_additional_args",
"@wp-test-remove-database @no_additional_args",
"npm --prefix wordpress run test:php -- @additional_args"
],
"wp-test-sqlite-plugin-php": [
Expand All @@ -80,8 +80,11 @@
"npm --prefix wordpress run test:e2e -- @additional_args"
],
"wp-test-clean": [
"npm --prefix wordpress run env:clean",
"rm -rf wordpress/src/wp-content/database/.ht.sqlite"
"@wp-test-remove-database",
"npm --prefix wordpress run env:clean"
],
"wp-test-remove-database": [
"cd wordpress && node tools/local-env/scripts/docker.js run --rm --no-deps --entrypoint rm php -rf /var/www/src/wp-content/database"
]
}
}
15 changes: 7 additions & 8 deletions packages/plugin-sqlite-database-integration/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@
}

/**
* FQDB is a database file name. If DB_FILE is defined, it is used
* as FQDB.
* FQDB is the absolute path to the SQLite database file.
*
* If DB_FILE is defined, FQDB is defined here using FQDBDIR. When SQLite is
* used without DB_FILE, managed storage defines FQDB after resolving the
* randomized database path.
*
* @deprecated 3.0.0 Define DB_DIR and DB_FILE instead of overriding FQDB.
*/
if ( ! defined( 'FQDB' ) ) {
if ( defined( 'DB_FILE' ) ) {
define( 'FQDB', FQDBDIR . DB_FILE );
} else {
define( 'FQDB', FQDBDIR . '.ht.sqlite' );
}
if ( ! defined( 'FQDB' ) && defined( 'DB_FILE' ) ) {
define( 'FQDB', FQDBDIR . DB_FILE );
}
Original file line number Diff line number Diff line change
Expand Up @@ -432,25 +432,6 @@ public function db_connect( $allow_bail = true ) {
$this->init_charset();
}

// Migrate the database file from a legacy path, if it exists.
if ( ! defined( 'DB_FILE' ) && ! file_exists( FQDB ) ) {
$old_db_path = FQDBDIR . '.ht.sqlite.php';

if ( file_exists( $old_db_path ) ) {
if ( ! rename( $old_db_path, FQDB ) ) {
wp_die( 'Failed to rename database file.', 'Error!' );
}

foreach ( array( '-wal', '-shm', '-journal' ) as $suffix ) {
if ( file_exists( $old_db_path . $suffix ) ) {
if ( ! rename( $old_db_path . $suffix, FQDB . $suffix ) ) {
wp_die( 'Failed to rename database file.', 'Error!' );
}
}
}
}
}

if ( null === $this->dbname || '' === $this->dbname ) {
$this->bail(
'The database name was not set. The SQLite driver requires a database name to be set to emulate MySQL information schema tables.',
Expand All @@ -459,8 +440,6 @@ public function db_connect( $allow_bail = true ) {
return false;
}

$this->ensure_database_directory( FQDB );

try {
$options = array(
'sqlite_journal_mode' => defined( 'SQLITE_JOURNAL_MODE' ) ? SQLITE_JOURNAL_MODE : null,
Expand Down Expand Up @@ -775,54 +754,6 @@ public function db_server_info() {
return $this->dbh->getAttribute( PDO::ATTR_SERVER_VERSION ); // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO
}

/**
* Make sure the SQLite database directory exists and is writable.
* Create .htaccess and index.php files to prevent direct access.
*
* @param string $database_path The path to the SQLite database file.
*/
private function ensure_database_directory( string $database_path ) {
$dir = dirname( $database_path );

// Set the umask to 0000 to apply permissions exactly as specified.
// A non-zero umask affects new file and directory permissions.
$umask = umask( 0 );

// Ensure database directory.
if ( ! is_dir( $dir ) ) {
if ( ! @mkdir( $dir, 0700, true ) ) {
wp_die( sprintf( 'Failed to create database directory: %s', $dir ), 'Error!' );
}
}
if ( ! is_writable( $dir ) ) {
wp_die( sprintf( 'Database directory is not writable: %s', $dir ), 'Error!' );
}

// Ensure .htaccess file to prevent direct access.
$path = $dir . DIRECTORY_SEPARATOR . '.htaccess';
if ( ! is_file( $path ) ) {
$result = file_put_contents( $path, 'DENY FROM ALL', LOCK_EX );
if ( false === $result ) {
wp_die( sprintf( 'Failed to create file: %s', $path ), 'Error!' );
}
chmod( $path, 0600 );
}

// Ensure index.php file to prevent direct access.
$path = $dir . DIRECTORY_SEPARATOR . 'index.php';
if ( ! is_file( $path ) ) {
$result = file_put_contents( $path, '<?php // Silence is gold. ?>', LOCK_EX );
if ( false === $result ) {
wp_die( sprintf( 'Failed to create file: %s', $path ), 'Error!' );
}
chmod( $path, 0600 );
}

// Restore the original umask value.
umask( $umask );
}


/**
* Format MySQL-on-SQLite driver error message.
*
Expand Down
Loading
Loading