wordpress / db.php
CatPtain's picture
Upload 9 files
efc9636 verified
raw
history blame
6.47 kB
<?php
/**
* SQLite integration for WordPress
* This file enables WordPress to use SQLite database instead of MySQL
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
// Check if SQLite extension is available
if (!extension_loaded('pdo_sqlite')) {
wp_die('SQLite PDO extension is not available. Please install php-sqlite3.');
}
// Define SQLite database path
if (!defined('DB_FILE')) {
define('DB_FILE', ABSPATH . 'wp-content/database/wordpress.db');
}
// Create database directory if it doesn't exist
$db_dir = dirname(DB_FILE);
if (!file_exists($db_dir)) {
wp_mkdir_p($db_dir);
}
/**
* Custom database class for SQLite - WordPress wpdb compatible
*/
class SQLite_DB {
public $pdo;
public $last_error = '';
public $insert_id = 0;
public $num_rows = 0;
public $last_query = '';
public $last_result = null;
public $prefix = 'wp_';
// WordPress compatibility properties
public $posts;
public $users;
public $options;
public $postmeta;
public $usermeta;
public $terms;
public $term_taxonomy;
public $term_relationships;
public $comments;
public $commentmeta;
public function __construct() {
global $table_prefix;
$this->prefix = isset($table_prefix) ? $table_prefix : 'wp_';
$this->set_table_names();
$this->connect();
}
private function set_table_names() {
$this->posts = $this->prefix . 'posts';
$this->users = $this->prefix . 'users';
$this->options = $this->prefix . 'options';
$this->postmeta = $this->prefix . 'postmeta';
$this->usermeta = $this->prefix . 'usermeta';
$this->terms = $this->prefix . 'terms';
$this->term_taxonomy = $this->prefix . 'term_taxonomy';
$this->term_relationships = $this->prefix . 'term_relationships';
$this->comments = $this->prefix . 'comments';
$this->commentmeta = $this->prefix . 'commentmeta';
}
private function connect() {
try {
$this->pdo = new PDO('sqlite:' . DB_FILE);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->pdo->exec('PRAGMA foreign_keys = ON');
} catch (PDOException $e) {
$this->last_error = $e->getMessage();
wp_die('Database connection failed: ' . $this->last_error);
}
}
public function query($sql) {
$this->last_query = $sql;
$this->last_error = '';
$this->last_result = null;
try {
// Convert MySQL syntax to SQLite
$sql = $this->mysql_to_sqlite($sql);
$stmt = $this->pdo->prepare($sql);
$result = $stmt->execute();
if ($result) {
$this->insert_id = $this->pdo->lastInsertId();
$this->num_rows = $stmt->rowCount();
$this->last_result = $stmt;
// For SELECT queries, return the number of rows
if (stripos(trim($sql), 'SELECT') === 0) {
return $this->num_rows;
}
// For INSERT, UPDATE, DELETE, return true
return true;
}
return false;
} catch (PDOException $e) {
$this->last_error = $e->getMessage();
return false;
}
}
private function mysql_to_sqlite($sql) {
// Basic MySQL to SQLite conversion
$sql = str_replace('`', '"', $sql);
$sql = preg_replace('/AUTO_INCREMENT/i', 'AUTOINCREMENT', $sql);
$sql = preg_replace('/INT\(\d+\)/i', 'INTEGER', $sql);
$sql = preg_replace('/TINYINT\(\d+\)/i', 'INTEGER', $sql);
$sql = preg_replace('/BIGINT\(\d+\)/i', 'INTEGER', $sql);
$sql = preg_replace('/VARCHAR\((\d+)\)/i', 'TEXT', $sql);
$sql = preg_replace('/LONGTEXT/i', 'TEXT', $sql);
$sql = preg_replace('/DATETIME/i', 'TEXT', $sql);
$sql = preg_replace('/TIMESTAMP/i', 'TEXT', $sql);
return $sql;
}
public function get_results($sql, $output = OBJECT) {
$this->query($sql);
if (!$this->last_result) return null;
$results = [];
while ($row = $this->last_result->fetch(PDO::FETCH_ASSOC)) {
if ($output === OBJECT) {
$results[] = (object) $row;
} else {
$results[] = $row;
}
}
return $results;
}
public function get_row($sql, $output = OBJECT) {
$this->query($sql);
if (!$this->last_result) return null;
$row = $this->last_result->fetch(PDO::FETCH_ASSOC);
if (!$row) return null;
if ($output === OBJECT) {
return (object) $row;
}
return $row;
}
public function get_var($sql) {
$this->query($sql);
if (!$this->last_result) return null;
$row = $this->last_result->fetch(PDO::FETCH_NUM);
return $row ? $row[0] : null;
}
public function insert_id() {
return $this->insert_id;
}
public function last_error() {
return $this->last_error;
}
public function num_rows() {
return $this->num_rows;
}
// WordPress compatibility methods
public function prepare($query, ...$args) {
if (empty($args)) {
return $query;
}
$query = str_replace("'%s'", '%s', $query);
$query = str_replace('"%s"', '%s', $query);
$query = str_replace('%s', "'%s'", $query);
$query = str_replace("'%d'", '%d', $query);
$query = str_replace('"%d"', '%d', $query);
return vsprintf($query, $args);
}
public function get_charset_collate() {
return '';
}
public function esc_like($text) {
return addcslashes($text, '\\%_');
}
public function print_error($str = '') {
if ($this->last_error) {
echo '<div id="error"><p class="wpdberror"><strong>WordPress database error:</strong> [' . $this->last_error . ']<br /><code>' . $this->last_query . '</code></p></div>';
}
}
}
// Initialize SQLite database connection
$wpdb = new SQLite_DB();
// WordPress database compatibility
if (!isset($GLOBALS['wpdb'])) {
$GLOBALS['wpdb'] = $wpdb;
}