254 lines
6.6 KiB
PHP
254 lines
6.6 KiB
PHP
<?php
|
|
/**
|
|
* Database handler.
|
|
*
|
|
* @package RobotstxtTelemetry
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
if ( ! class_exists( 'Robotstxt_Telemetry_DB' ) ) {
|
|
/**
|
|
* Class Robotstxt_Telemetry_DB
|
|
*/
|
|
class Robotstxt_Telemetry_DB {
|
|
|
|
/**
|
|
* Maximum number of log entries kept, regardless of retention period.
|
|
*
|
|
* @var int
|
|
*/
|
|
const MAX_LOG_ROWS = 1000;
|
|
|
|
/**
|
|
* Whether the schema was checked in this request.
|
|
*
|
|
* @var bool
|
|
*/
|
|
private static $did_check = false;
|
|
|
|
/**
|
|
* Get the table name.
|
|
*
|
|
* In global mode all sites share the main site table.
|
|
*
|
|
* @return string
|
|
*/
|
|
public static function get_table_name() {
|
|
global $wpdb;
|
|
|
|
if ( Robotstxt_Telemetry_Network::is_global() ) {
|
|
return $wpdb->get_blog_prefix( Robotstxt_Telemetry_Network::main_site_id() ) . 'robotstxt_telemetry_logs';
|
|
}
|
|
|
|
return $wpdb->prefix . 'robotstxt_telemetry_logs';
|
|
}
|
|
|
|
/**
|
|
* Activation hook.
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function activate() {
|
|
self::create_table();
|
|
update_option( 'robotstxt_telemetry_db_version', ROBOTSTXT_TELEMETRY_DB_VERSION );
|
|
}
|
|
|
|
/**
|
|
* Ensure the database schema is up to date.
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function maybe_upgrade() {
|
|
if ( self::$did_check ) {
|
|
return;
|
|
}
|
|
|
|
self::$did_check = true;
|
|
|
|
// In global mode the shared table belongs to the main site only.
|
|
if ( Robotstxt_Telemetry_Network::is_global() && ! is_main_site() ) {
|
|
return;
|
|
}
|
|
|
|
$installed_version = get_option( 'robotstxt_telemetry_db_version' );
|
|
if ( ROBOTSTXT_TELEMETRY_DB_VERSION !== $installed_version ) {
|
|
self::create_table();
|
|
update_option( 'robotstxt_telemetry_db_version', ROBOTSTXT_TELEMETRY_DB_VERSION );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create or update the logs table.
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function create_table() {
|
|
global $wpdb;
|
|
|
|
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
|
|
|
$table_name = self::get_table_name();
|
|
$charset_collate = $wpdb->get_charset_collate();
|
|
|
|
$sql = "CREATE TABLE {$table_name} (
|
|
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
created_at DATETIME NOT NULL,
|
|
method VARCHAR(10) NOT NULL,
|
|
url TEXT NOT NULL,
|
|
host VARCHAR(255) NULL,
|
|
path TEXT NULL,
|
|
query_json LONGTEXT NULL,
|
|
body_json LONGTEXT NULL,
|
|
headers_json LONGTEXT NULL,
|
|
user_agent TEXT NULL,
|
|
raw_body LONGTEXT NULL,
|
|
caller TEXT NULL,
|
|
redacted TINYINT(1) NOT NULL DEFAULT 1,
|
|
response_code SMALLINT UNSIGNED NULL,
|
|
response_time_ms FLOAT NULL,
|
|
PRIMARY KEY (id),
|
|
KEY created_at (created_at),
|
|
KEY host (host),
|
|
KEY method_host (method, host)
|
|
) {$charset_collate};";
|
|
|
|
dbDelta( $sql );
|
|
}
|
|
|
|
/**
|
|
* Insert a log row.
|
|
*
|
|
* @param array<string,mixed> $data Log data.
|
|
* @return void
|
|
*/
|
|
public static function insert_log( array $data ) {
|
|
global $wpdb;
|
|
|
|
$wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Insert into the plugin's custom table via the wpdb API.
|
|
self::get_table_name(),
|
|
$data,
|
|
array(
|
|
'%s',
|
|
'%s',
|
|
'%s',
|
|
'%s',
|
|
'%s',
|
|
'%s',
|
|
'%s',
|
|
'%s',
|
|
'%s',
|
|
'%s',
|
|
'%s',
|
|
'%d',
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Fetch a single log row.
|
|
*
|
|
* @param int $log_id Log ID.
|
|
* @return array<string,mixed>|null
|
|
*/
|
|
public static function get_log( $log_id ) {
|
|
global $wpdb;
|
|
|
|
$table_name = self::get_table_name();
|
|
|
|
$log = $wpdb->get_row( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Custom table read via the wpdb API; single-row view is never cached.
|
|
$wpdb->prepare(
|
|
"SELECT * FROM {$table_name} WHERE id = %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is internal; the %i placeholder requires WordPress 6.2.
|
|
$log_id
|
|
),
|
|
ARRAY_A
|
|
);
|
|
|
|
if ( empty( $log ) ) {
|
|
return null;
|
|
}
|
|
|
|
return $log;
|
|
}
|
|
|
|
/**
|
|
* Delete a log row.
|
|
*
|
|
* @param int $log_id Log ID.
|
|
* @return bool
|
|
*/
|
|
public static function delete_log( $log_id ) {
|
|
global $wpdb;
|
|
|
|
$deleted = $wpdb->delete( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Delete from the plugin's custom table via the wpdb API.
|
|
self::get_table_name(),
|
|
array( 'id' => $log_id ),
|
|
array( '%d' )
|
|
);
|
|
|
|
return false !== $deleted;
|
|
}
|
|
|
|
/**
|
|
* Delete all log rows.
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function delete_all_logs() {
|
|
global $wpdb;
|
|
|
|
$table_name = self::get_table_name();
|
|
|
|
$wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Delete from the plugin's custom table via the wpdb API.
|
|
"DELETE FROM {$table_name}" // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is internal; the %i placeholder requires WordPress 6.2.
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Delete logs according to the retention settings.
|
|
*
|
|
* Removes entries older than the configured retention period and
|
|
* keeps at most MAX_LOG_ROWS entries (newest first). Runs when the
|
|
* logs list is visited and twice daily via cron.
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function cleanup() {
|
|
global $wpdb;
|
|
|
|
$table_name = self::get_table_name();
|
|
|
|
$retention = Robotstxt_Telemetry_Network::get_setting( 'robotstxt_telemetry_retention_period', '12hours' );
|
|
|
|
$periods = array(
|
|
'12hours' => 12 * HOUR_IN_SECONDS,
|
|
'1day' => DAY_IN_SECONDS,
|
|
'3days' => 3 * DAY_IN_SECONDS,
|
|
);
|
|
|
|
if ( is_string( $retention ) && isset( $periods[ $retention ] ) ) {
|
|
$cutoff = gmdate( 'Y-m-d H:i:s', time() - $periods[ $retention ] );
|
|
|
|
$wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Delete from the plugin's custom table via the wpdb API.
|
|
$wpdb->prepare( "DELETE FROM {$table_name} WHERE created_at < %s", $cutoff ) // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is internal; the %i placeholder requires WordPress 6.2.
|
|
);
|
|
}
|
|
|
|
// Keep only the newest MAX_LOG_ROWS entries.
|
|
$threshold_id = (int) $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Custom table read via the wpdb API; cleanup threshold lookup.
|
|
$wpdb->prepare(
|
|
"SELECT id FROM {$table_name} ORDER BY id DESC LIMIT 1 OFFSET %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is internal; the %i placeholder requires WordPress 6.2.
|
|
self::MAX_LOG_ROWS - 1
|
|
)
|
|
);
|
|
|
|
if ( $threshold_id > 0 ) {
|
|
$wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Delete from the plugin's custom table via the wpdb API.
|
|
$wpdb->prepare( "DELETE FROM {$table_name} WHERE id < %d", $threshold_id ) // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is internal; the %i placeholder requires WordPress 6.2.
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|