Skip to content

Query Monitor support #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions activate.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,28 @@ function sqlite_plugin_copy_db_file() {

$destination = WP_CONTENT_DIR . '/db.php';

/*
* When an existing "db.php" drop-in is detected, let's check if it's a known
* plugin that we can continue supporting even when we override the drop-in.
*/
$override_db_dropin = false;
if ( file_exists( $destination ) ) {
// Check for the Query Monitor plugin.
// When "QM_DB" exists, it must have been loaded via the "db.php" file.
if ( class_exists( 'QM_DB', false ) ) {
$override_db_dropin = true;
}

if ( $override_db_dropin ) {
require_once ABSPATH . '/wp-admin/includes/file.php';
global $wp_filesystem;
if ( ! $wp_filesystem ) {
WP_Filesystem();
}
$wp_filesystem->delete( $destination );
}
}

// Place database drop-in if not present yet, except in case there is
// another database drop-in present already.
if ( ! defined( 'SQLITE_DB_DROPIN_VERSION' ) && ! file_exists( $destination ) ) {
Expand Down
119 changes: 74 additions & 45 deletions admin-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ function sqlite_add_admin_menu() {
* The admin page contents.
*/
function sqlite_integration_admin_screen() {
$db_dropin_path = WP_CONTENT_DIR . '/db.php';

/*
* When an existing "db.php" drop-in is detected, let's check if it's a known
* plugin that we can continue supporting even when we override the drop-in.
*/
$override_db_dropin = false;
if ( file_exists( $db_dropin_path ) && ! defined( 'SQLITE_DB_DROPIN_VERSION' ) ) {
// Check for the Query Monitor plugin.
// When "QM_DB" exists, it must have been loaded via the "db.php" file.
if ( class_exists( 'QM_DB', false ) ) {
$override_db_dropin = true;
}
}

?>
<div class="wrap">
<h1><?php esc_html_e( 'SQLite integration.', 'sqlite-database-integration' ); ?></h1>
Expand All @@ -46,68 +61,82 @@ function sqlite_integration_admin_screen() {
);
?>
</p>
<?php else : ?>
<?php if ( ! extension_loaded( 'pdo_sqlite' ) ) : ?>
<div class="notice notice-error">
<p><?php esc_html_e( 'We detected that the PDO SQLite driver is missing from your server (the pdo_sqlite extension is not loaded). Please make sure that SQLite is enabled in your PHP installation before proceeding.', 'sqlite-database-integration' ); ?></p>
</div>
<?php elseif ( file_exists( WP_CONTENT_DIR . '/db.php' ) && ! defined( 'SQLITE_DB_DROPIN_VERSION' ) ) : ?>
<?php if ( defined( 'PERFLAB_SQLITE_DB_DROPIN_VERSION' ) ) : ?>
<div class="notice notice-warning">
<p>
<?php
printf(
/* translators: %s: db.php drop-in path */
esc_html__( 'An older %s file was detected. Please click the button below to update the file.', 'sqlite-database-integration' ),
'<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>'
);
?>
</p>
</div>
<a class="button button-primary" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=sqlite-integration&confirm-install&upgrade-from-pl' ), 'sqlite-install' ) ); ?>">
<?php elseif ( ! extension_loaded( 'pdo_sqlite' ) ) : ?>
<div class="notice notice-error">
<p><?php esc_html_e( 'We detected that the PDO SQLite driver is missing from your server (the pdo_sqlite extension is not loaded). Please make sure that SQLite is enabled in your PHP installation before proceeding.', 'sqlite-database-integration' ); ?></p>
</div>
<?php elseif ( file_exists( $db_dropin_path ) && ! defined( 'SQLITE_DB_DROPIN_VERSION' ) && ! $override_db_dropin ) : ?>
<?php if ( defined( 'PERFLAB_SQLITE_DB_DROPIN_VERSION' ) ) : ?>
<div class="notice notice-warning">
<p>
<?php
printf(
/* translators: %s: db.php drop-in path */
esc_html__( 'Update %s file', 'sqlite-database-integration' ),
esc_html__( 'An older %s file was detected. Please click the button below to update the file.', 'sqlite-database-integration' ),
'<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>'
);
?>
</a>
<?php else : ?>
<div class="notice notice-error">
<p>
<?php
printf(
/* translators: %s: db.php drop-in path */
esc_html__( 'The SQLite plugin cannot be activated because a different %s drop-in already exists.', 'sqlite-database-integration' ),
'<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>'
);
?>
</p>
</div>
<?php endif; ?>
<?php elseif ( ! is_writable( WP_CONTENT_DIR ) ) : ?>
</p>
</div>
<a class="button button-primary" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=sqlite-integration&confirm-install&upgrade-from-pl' ), 'sqlite-install' ) ); ?>">
<?php
printf(
/* translators: %s: db.php drop-in path */
esc_html__( 'Update %s file', 'sqlite-database-integration' ),
'<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>'
);
?>
</a>
<?php else : ?>
<div class="notice notice-error">
<p>
<?php
printf(
/* translators: %s: db.php drop-in path */
esc_html__( 'The SQLite plugin cannot be activated because the %s directory is not writable.', 'sqlite-database-integration' ),
'<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '</code>'
esc_html__( 'The SQLite plugin cannot be activated because a different %s drop-in already exists.', 'sqlite-database-integration' ),
'<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>'
);
?>
</p>
</div>
<?php else : ?>
<div class="notice notice-success">
<p><?php esc_html_e( 'All checks completed successfully, your site can use an SQLite database. You can proceed with the installation.', 'sqlite-database-integration' ); ?></p>
</div>
<h2><?php esc_html_e( 'Important note', 'sqlite-database-integration' ); ?></h2>
<p><?php esc_html_e( 'This plugin will switch to a separate database and install WordPress in it. You will need to reconfigure your site, and start with a fresh site. Disabling the plugin you will get back to your previous MySQL database, with all your previous data intact.', 'sqlite-database-integration' ); ?></p>
<p><?php esc_html_e( 'By clicking the button below, you will be redirected to the WordPress installation screen to setup your new database', 'sqlite-database-integration' ); ?></p>
<?php endif; ?>
<?php elseif ( ! is_writable( WP_CONTENT_DIR ) ) : ?>
<div class="notice notice-error">
<p>
<?php
printf(
/* translators: %s: db.php drop-in path */
esc_html__( 'The SQLite plugin cannot be activated because the %s directory is not writable.', 'sqlite-database-integration' ),
'<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '</code>'
);
?>
</p>
</div>
<?php else : ?>
<div class="notice notice-success">
<p><?php esc_html_e( 'All checks completed successfully, your site can use an SQLite database. You can proceed with the installation.', 'sqlite-database-integration' ); ?></p>
</div>
<h2><?php esc_html_e( 'Important note', 'sqlite-database-integration' ); ?></h2>
<p><?php esc_html_e( 'This plugin will switch to a separate database and install WordPress in it. You will need to reconfigure your site, and start with a fresh site. Disabling the plugin you will get back to your previous MySQL database, with all your previous data intact.', 'sqlite-database-integration' ); ?></p>

<a class="button button-primary" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=sqlite-integration&confirm-install' ), 'sqlite-install' ) ); ?>"><?php esc_html_e( 'Install SQLite database', 'sqlite-database-integration' ); ?></a>
<?php if ( $override_db_dropin ) : ?>
<p>
<strong>NOTE:</strong>
<?php
printf(
/* translators: %s: db.php drop-in path */
esc_html__( 'We’ve detected an existing database drop-in file at %s, created by the Query Monitor plugin.', 'sqlite-database-integration' ),
'<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>'
);
?>
<?php esc_html_e( 'To enable SQLite support, this file will need to be replaced.', 'sqlite-database-integration' ); ?>
<?php esc_html_e( 'The Query Monitor plugin will continue to function correctly after the change. You can safely proceed with the installation.', 'sqlite-database-integration' ); ?>
</p>
<?php endif; ?>

<p><?php esc_html_e( 'By clicking the button below, you will be redirected to the WordPress installation screen to setup your new database', 'sqlite-database-integration' ); ?></p>

<a class="button button-primary" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=sqlite-integration&confirm-install' ), 'sqlite-install' ) ); ?>"><?php esc_html_e( 'Install SQLite database', 'sqlite-database-integration' ); ?></a>
<?php endif; ?>
</div>
<?php
Expand Down
122 changes: 122 additions & 0 deletions integrations/query-monitor/boot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

/**
* Boot Query Monitor from the SQLite Database Integration plugin.
*
* When the Query Monitor plugin exists in its standard location, let's check
* if it is active, so we can boot it eagerly. This is a workaround to avoid
* SQLite and Query Monitor competing for the "wp-content/db.php" file.
*
* This file is a modified version of the original Query Monitor "db.php" file.
*
* See: https://github.com/johnbillion/query-monitor/blob/develop/wp-content/db.php
*/

/*
* In Playground, the SQLite plugin is preloaded without using the "db.php" file.
* To prevent Query Monitor from injecting its own "db.php" file, we need to set
* the "QM_DB_SYMLINK" constant to "false".
*/
if ( ! defined( 'QM_DB_SYMLINK' ) ) {
define( 'QM_DB_SYMLINK', false );
}

// 1. Check if we should load Query Monitor (as per the original "db.php" file).
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

if ( ! defined( 'DB_USER' ) ) {
return;
}

if ( defined( 'QM_DISABLED' ) && QM_DISABLED ) {
return;
}

if ( defined( 'WP_INSTALLING' ) && WP_INSTALLING ) {
return;
}

if ( 'cli' === php_sapi_name() && ! defined( 'QM_TESTS' ) ) {
return;
}

if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
return;
}

if ( is_admin() ) {
if ( isset( $_GET['action'] ) && 'upgrade-plugin' === $_GET['action'] ) {
return;
}

if ( isset( $_POST['action'] ) && 'update-plugin' === $_POST['action'] ) {
return;
}
}

// 2. Check if Query Monitor is active.
if ( null === $wpdb->options ) {
global $table_prefix;
$wpdb->set_prefix( $table_prefix ?? '' );
}

$query_monitor_active = false;
try {
$value = $wpdb->get_row(
$wpdb->prepare(
"SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1",
'active_plugins'
)
);
$query_monitor_active = in_array(
'query-monitor/query-monitor.php',
unserialize( $value->option_value ),
true
);
} catch ( Throwable $e ) {
return;
}

if ( ! $query_monitor_active ) {
return;
}

// 3. Determine the plugins directory.
if ( defined( 'WP_PLUGIN_DIR' ) ) {
$plugins_dir = WP_PLUGIN_DIR;
} else {
$plugins_dir = WP_CONTENT_DIR . '/plugins';
}

// 4. Load Query Monitor (as per the original "db.php" file).
$qm_dir = "{$plugins_dir}/query-monitor";
$qm_php = "{$qm_dir}/classes/PHP.php";

if ( ! is_readable( $qm_php ) ) {
return;
}
require_once $qm_php;

if ( ! QM_PHP::version_met() ) {
return;
}

if ( ! file_exists( "{$qm_dir}/vendor/autoload.php" ) ) {
add_action( 'all_admin_notices', 'QM_PHP::vendor_nope' );
return;
}

require_once "{$qm_dir}/vendor/autoload.php";

if ( ! class_exists( 'QM_Backtrace' ) ) {
return;
}

if ( ! defined( 'SAVEQUERIES' ) ) {
define( 'SAVEQUERIES', true );
}

// 5. Mark the Query Monitor integration as loaded.
define( 'SQLITE_QUERY_MONITOR_LOADED', true );
89 changes: 89 additions & 0 deletions integrations/query-monitor/plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php // phpcs:disable WordPress.Files.FileName.InvalidClassFileName

if ( ! class_exists( 'QM_Output_Html_DB_Queries' ) || ! class_exists( 'QM_Collectors' ) ) {
return;
}

/**
* Override Query Monitor's "QM_Output_Html_DB_Queries" to inject SQLite info.
*/
class SQLite_QM_Output_Html_DB_Queries extends QM_Output_Html_DB_Queries {
/**
* Override the parent method to inject SQLite info.
*
* Currently, Query Monitor doesn't provide a way to customize the rendered
* query HTML. To overcome this limitation, we capture the HTML generated by
* the parent method and modify it to inject the SQLite query data.
*
* @param array<string, mixed> $row The row data.
* @param array<int, string> $cols The column names.
* @return void
*/
protected function output_query_row( array $row, array $cols ) {
// Capture the query row HTML.
ob_start();
parent::output_query_row( $row, $cols );
$data = ob_get_length() > 0 ? ob_get_clean() : '';

// Get the corresponding SQLite queries.
global $wpdb;
static $query_index = 0;
$sqlite_queries = $wpdb->queries[ $query_index ]['sqlite_queries'] ?? array();
$sqlite_query_count = count( $sqlite_queries );
$query_index += 1;

// Build the SQLite info HTML.
$sqlite_info = sprintf(
'<div class="qm-info" style="margin: 15px 0 8px;">Executed %d SQLite %s:</div>',
$sqlite_query_count,
1 === $sqlite_query_count ? 'Query' : 'Queries'
);
$sqlite_info .= '<ol>';
foreach ( $sqlite_queries as $query ) {
$sqlite_info .= '<li class="qm-sqlite-query" style="list-style: decimal !important; margin-left: 20px !important;">';
$sqlite_info .= '<code>' . str_replace( '<br>', '', self::format_sql( $query['sql'] ) ) . '</code>';
$sqlite_info .= '</li>';
}
$sqlite_info .= '</ol>';

// Inject toggle button and SQLite info into the query row HTML.
$toggle_button = '<button class="qm-toggle sqlite-toggle" data-on="+" data-off="-" aria-expanded="false" aria-label="Toggle SQLite queries" title="Toggle SQLite queries"><span aria-hidden="true">+</span></button>';
$toggle_content = sprintf( '<div class="qm-toggled" style="display: none;">%s</div>', $sqlite_info );

$data = str_replace( 'qm-row-sql', 'qm-row-sql qm-has-toggle', $data );
$data = preg_replace(
'/(<td class="qm-row-sql.*?">)(.*?)(<\/td>)/s',
implode(
array(
'$1',
str_replace( '$', '\\$', $toggle_button ),
'$2',
str_replace( '$', '\\$', $toggle_content ),
'$3',
)
),
$data
);
echo $data;
}
}

// Remove the default Query Monitor class and replace it with the custom one.
remove_filter( 'qm/outputter/html', 'register_qm_output_html_db_queries', 20 );

/**
* Register the custom HTML output class.
*
* @param array<string, QM_Output> $output
* @param QM_Collectors $collectors
* @return array<string, QM_Output>
*/
function register_sqlite_qm_output_html_db_queries( array $output, $collectors ) {
$collector = QM_Collectors::get( 'db_queries' );
if ( $collector ) {
$output['db_queries'] = new SQLite_QM_Output_Html_DB_Queries( $collector );
}
return $output;
}

add_filter( 'qm/outputter/html', 'register_sqlite_qm_output_html_db_queries', 20, 2 );
Loading