File size: 39,732 Bytes
bf47729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efc9636
bf47729
 
efc9636
 
ae25f47
efc9636
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fb22f2
efc9636
 
1fb22f2
 
 
 
 
 
5f45304
 
 
 
 
bf47729
 
efc9636
 
1fb22f2
 
5f45304
efc9636
ae25f47
 
 
 
 
 
 
 
 
bf47729
 
efc9636
 
 
 
 
 
 
 
 
1fb22f2
efc9636
 
1fb22f2
efc9636
 
bf47729
 
5f45304
 
 
bf47729
 
5f45304
 
 
 
 
 
 
 
bf47729
 
ae25f47
5f45304
ae25f47
5f45304
bf47729
 
 
 
efc9636
 
 
 
bf47729
 
 
 
 
 
 
 
efc9636
bf47729
efc9636
 
 
 
 
 
 
 
bf47729
 
 
 
 
ae25f47
5f45304
 
 
 
 
 
 
bf47729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efc9636
 
bf47729
 
efc9636
bf47729
 
 
 
 
 
 
 
 
 
 
efc9636
 
bf47729
efc9636
bf47729
 
 
 
 
 
 
 
 
 
efc9636
 
bf47729
efc9636
bf47729
 
 
 
efc9636
bf47729
 
 
 
 
 
 
 
 
efc9636
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ed2b6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efc9636
5f45304
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efc9636
 
1fb22f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae25f47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fb22f2
 
 
5f45304
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fb22f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f45304
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fb22f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d13adb8
1fb22f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d13adb8
1fb22f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d13adb8
1fb22f2
5f45304
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf47729
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
<?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 $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 $termmeta;
    public $comments;
    public $commentmeta;
    public $links;
    public $field_types = array();
    public $charset;
    public $collate;
    public $dbname;
    public $ready = false;
    public $suppress_errors = false;
    public $show_errors = true;
    public $time_start = 0;
    public $blogid = 1;
    public $base_prefix = 'wp_';
    
    public function __construct() {
        global $table_prefix;
        $this->prefix = isset($table_prefix) ? $table_prefix : 'wp_';
        $this->charset = 'utf8';
        $this->collate = '';
        $this->dbname = '/var/www/html/wp-content/database';
        $this->set_table_names();
        
        if ($this->connect()) {
            $this->ready = true;
            $this->base_prefix = $this->prefix;
            $this->timer_start();
        } else {
            $this->ready = false;
            // Don't bail here, let WordPress handle the connection error
        }
    }
    
    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->termmeta = $this->prefix . 'termmeta';
        $this->comments = $this->prefix . 'comments';
        $this->commentmeta = $this->prefix . 'commentmeta';
        $this->links = $this->prefix . 'links';
    }
    
    private function connect() {
        try {
            // Use in-memory SQLite database for Hugging Face Spaces
            // This avoids file permission issues and improves performance
            $this->pdo = new PDO('sqlite::memory:');
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->pdo->exec('PRAGMA foreign_keys = ON');
            $this->pdo->exec('PRAGMA journal_mode = MEMORY');
            $this->pdo->exec('PRAGMA synchronous = OFF');
            $this->pdo->exec('PRAGMA cache_size = 10000');
            
            // Initialize WordPress tables in memory
            $this->create_wordpress_tables();
            
            return true;
        } catch (PDOException $e) {
            $this->last_error = $e->getMessage();
            $this->error = $e->getMessage();
            error_log("SQLite connection error: " . $e->getMessage());
            $this->ready = false;
            return false;
        }
    }
    
    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();
            $this->error = $e->getMessage();
            
            if (!$this->suppress_errors) {
                if ($this->show_errors) {
                    error_log("SQLite 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 _escape($data) {
        if (is_array($data)) {
            return array_map(array($this, '_escape'), $data);
        }
        
        if (is_string($data)) {
            return addslashes($data);
        }
        
        return $data;
    }
    
    public function _real_escape($data) {
        if (!is_scalar($data)) {
            return '';
        }
        
        // For SQLite, we use addslashes as there's no equivalent to mysqli_real_escape_string
        $escaped = addslashes($data);
        return $this->add_placeholder_escape($escaped);
    }
    
    public function _weak_escape($data) {
        if (func_num_args() === 1 && function_exists('_deprecated_function')) {
            _deprecated_function(__METHOD__, '3.6.0', 'wpdb::prepare() or esc_sql()');
        }
        return addslashes($data);
    }
    
    public function escape($data) {
        if (func_num_args() === 1 && function_exists('_deprecated_function')) {
            _deprecated_function(__METHOD__, '3.6.0', 'wpdb::prepare() or esc_sql()');
        }
        if (is_array($data)) {
            foreach ($data as $k => $v) {
                if (is_array($v)) {
                    $data[$k] = $this->escape($v);
                } else {
                    $data[$k] = $this->_weak_escape($v);
                }
            }
        } else {
            $data = $this->_weak_escape($data);
        }
        return $data;
    }
    
    public function add_placeholder_escape($query) {
        // Replace % with placeholder to prevent SQLi attacks
        return str_replace('%', $this->placeholder_escape(), $query);
    }
    
    public function placeholder_escape() {
        static $placeholder;
        if (!$placeholder) {
            // Generate a unique placeholder
            $placeholder = '{' . wp_generate_password(20, false) . '}';
        }
        return $placeholder;
    }
    
    public function print_error($str = '') {
        global $EZSQL_ERROR;
        
        if (!$str) {
            $str = $this->last_error;
        }
        
        $EZSQL_ERROR[] = array(
            'query' => $this->last_query,
            'error_str' => $str
        );
        
        if ($this->suppress_errors) {
            return false;
        }
        
        if ($this->show_errors) {
            if (function_exists('wp_die')) {
                wp_die($str);
            } else {
                die($str);
            }
        }
        
        return false;
    }
    
    public function bail($message, $error_code = '500') {
        if (!$this->show_errors) {
            if (class_exists('WP_Error')) {
                return new WP_Error($error_code, $message);
            } else {
                return false;
            }
        }
        
        if (function_exists('wp_die')) {
            wp_die($message);
        } else {
            die($message);
        }
    }
    
    // WordPress required methods
    public function set_prefix($prefix, $set_table_names = true) {
        if (preg_match('|[^a-z0-9_]|i', $prefix)) {
            // Return false instead of WP_Error since WP_Error might not be available yet
            return false;
        }
        
        $old_prefix = $this->prefix;
        $this->prefix = $prefix;
        
        if ($set_table_names) {
            $this->set_table_names();
        }
        
        return $old_prefix;
    }
    
    public function set_blog_id($blog_id, $network_id = 0) {
        // For single site, just return
        return $this->prefix;
    }
    
    public function tables($scope = 'all', $prefix = true, $blog_id = 0) {
        $tables = array(
            'posts', 'comments', 'links', 'options', 'postmeta',
            'terms', 'term_taxonomy', 'term_relationships', 'termmeta',
            'commentmeta', 'users', 'usermeta'
        );
        
        if ($prefix) {
            $prefixed_tables = array();
            foreach ($tables as $table) {
                $prefixed_tables[] = $this->prefix . $table;
            }
            return $prefixed_tables;
        }
        
        return $tables;
    }
    
    public function get_table_charset($table) {
        return 'utf8';
    }
    
    public function get_col_charset($table, $column) {
        return 'utf8';
    }
    
    public function check_connection($allow_bail = true) {
        if (!$this->pdo) {
            if ($allow_bail) {
                $this->bail('Error establishing a database connection');
            }
            return false;
        }
        
        try {
            // Test the connection with a simple query
            $this->pdo->query('SELECT 1');
            return true;
        } catch (PDOException $e) {
            $this->last_error = $e->getMessage();
            $this->error = $e->getMessage();
            
            if ($allow_bail) {
                $this->bail('Error establishing a database connection: ' . $e->getMessage());
            }
            return false;
        }
    }
    
    public function bail($message, $error_code = '500') {
        if (!$this->show_errors) {
            if (class_exists('WP_Error')) {
                $this->error = new WP_Error('db_connect_fail', $message);
            } else {
                $this->error = $message;
            }
            return false;
        }
        
        if (function_exists('wp_die')) {
            wp_die($message);
        } else {
            die($message);
        }
    }
    
    public function db_connect($allow_bail = true) {
        $this->ready = false;
        
        if ($this->connect()) {
            $this->ready = true;
            return true;
        }
        
        if ($allow_bail) {
            $this->bail('Error establishing a database connection');
        }
        
        return false;
    }
    
    public function db_version() {
        try {
            $result = $this->pdo->query('SELECT sqlite_version()');
            return $result->fetchColumn();
        } catch (PDOException $e) {
            return '0';
        }
    }
    
    // Additional WordPress methods
    public function get_col($query = null, $x = 0) {
        if ($query) {
            $this->query($query);
        }
        
        $new_array = array();
        if ($this->last_result) {
            for ($i = 0; $i < count($this->last_result); $i++) {
                $row = (array) $this->last_result[$i];
                $values = array_values($row);
                if (isset($values[$x]) && $values[$x] !== '') {
                    $new_array[] = $values[$x];
                }
            }
        }
        return $new_array;
    }
    
    public function timer_start() {
        $this->time_start = microtime(true);
        return true;
    }
    
    public function timer_stop() {
        return (microtime(true) - $this->time_start);
    }
    
    public function get_blog_prefix($blog_id = null) {
        if (defined('MULTISITE') && MULTISITE) {
            if (null === $blog_id) {
                $blog_id = $this->blogid;
            }
            $blog_id = (int) $blog_id;
            if (0 == $blog_id || 1 == $blog_id) {
                return $this->base_prefix;
            } else {
                return $this->base_prefix . $blog_id . '_';
            }
        } else {
            return $this->prefix;
        }
    }
    
    public function flush() {
        $this->last_result = null;
        $this->last_query = null;
        $this->last_error = '';
    }
    
    public function close() {
        $this->pdo = null;
        return true;
    }
    
    public function has_cap($db_cap) {
        switch (strtolower($db_cap)) {
            case 'collation':
            case 'group_concat':
            case 'subqueries':
                return true;
            default:
                return false;
        }
    }
    
    public function get_caller() {
        if (function_exists('wp_debug_backtrace_summary')) {
            return wp_debug_backtrace_summary(__CLASS__);
        }
        return '';
    }
    
    public function init_charset() {
        // SQLite uses UTF-8 by default
        return true;
    }
    
    public function set_charset($dbh, $charset = null, $collate = null) {
        // SQLite uses UTF-8 by default
        return true;
    }
    
    // Error handling methods
    public function suppress_errors($suppress = true) {
        $errors_before = $this->suppress_errors;
        $this->suppress_errors = $suppress;
        return $errors_before;
    }
    
    public function hide_errors() {
        $show = $this->show_errors;
        $this->show_errors = false;
        return $show;
    }
    
    public function show_errors($show = true) {
        $errors_before = $this->show_errors;
        $this->show_errors = $show;
        return $errors_before;
    }
    
    // WordPress data manipulation methods
    public function insert($table, $data, $format = null) {
        return $this->_insert_replace_helper($table, $data, $format, 'INSERT');
    }
    
    public function replace($table, $data, $format = null) {
        return $this->_insert_replace_helper($table, $data, $format, 'REPLACE');
    }
    
    public function update($table, $data, $where, $format = null, $where_format = null) {
        if (!is_array($data) || !is_array($where)) {
            return false;
        }
        
        $formats = $format = (array) $format;
        $bits = $wheres = array();
        foreach ((array) array_keys($data) as $field) {
            if (!empty($formats)) {
                $form = ($form = array_shift($formats)) ? $form : $formats[0];
            } elseif (isset($this->field_types[$field])) {
                $form = $this->field_types[$field];
            } else {
                $form = '%s';
            }
            $bits[] = "`$field` = {$form}";
        }
        
        $where_formats = $where_format = (array) $where_format;
        foreach ((array) array_keys($where) as $field) {
            if (!empty($where_formats)) {
                $form = ($form = array_shift($where_formats)) ? $form : $where_formats[0];
            } elseif (isset($this->field_types[$field])) {
                $form = $this->field_types[$field];
            } else {
                $form = '%s';
            }
            $wheres[] = "`$field` = {$form}";
        }
        
        $sql = "UPDATE `$table` SET " . implode(', ', $bits) . ' WHERE ' . implode(' AND ', $wheres);
        return $this->query($this->prepare($sql, ...array_merge(array_values($data), array_values($where))));
    }
    
    public function delete($table, $where, $where_format = null) {
        if (!is_array($where)) {
            return false;
        }
        
        $where_formats = $where_format = (array) $where_format;
        $wheres = array();
        foreach (array_keys($where) as $field) {
            if (!empty($where_formats)) {
                $form = ($form = array_shift($where_formats)) ? $form : $where_formats[0];
            } elseif (isset($this->field_types[$field])) {
                $form = $this->field_types[$field];
            } else {
                $form = '%s';
            }
            $wheres[] = "`$field` = {$form}";
        }
        
        $sql = "DELETE FROM `$table` WHERE " . implode(' AND ', $wheres);
        return $this->query($this->prepare($sql, ...array_values($where)));
    }
    
    private function _insert_replace_helper($table, $data, $format = null, $type = 'INSERT') {
        if (!in_array(strtoupper($type), array('INSERT', 'REPLACE'))) {
            return false;
        }
        
        if (!is_array($data)) {
            return false;
        }
        
        $formats = $format = (array) $format;
        $fields = array_keys($data);
        $formatted_fields = array();
        
        foreach ($fields as $field) {
            if (!empty($formats)) {
                $form = ($form = array_shift($formats)) ? $form : $formats[0];
            } elseif (isset($this->field_types[$field])) {
                $form = $this->field_types[$field];
            } else {
                $form = '%s';
            }
            $formatted_fields[] = $form;
        }
        
        $sql = "$type INTO `$table` (`" . implode('`,`', $fields) . '`) VALUES (' . implode(',', $formatted_fields) . ')';
        return $this->query($this->prepare($sql, ...array_values($data)));
    }
    
    // Create WordPress tables in memory
    private function create_wordpress_tables() {
        $tables = array(
            // Posts table
            "CREATE TABLE IF NOT EXISTS {$this->prefix}posts (
                ID bigint(20) unsigned NOT NULL PRIMARY KEY,
                post_author bigint(20) unsigned NOT NULL DEFAULT 0,
                post_date datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
                post_date_gmt datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
                post_content longtext NOT NULL,
                post_title text NOT NULL,
                post_excerpt text NOT NULL,
                post_status varchar(20) NOT NULL DEFAULT 'publish',
                comment_status varchar(20) NOT NULL DEFAULT 'open',
                ping_status varchar(20) NOT NULL DEFAULT 'open',
                post_password varchar(255) NOT NULL DEFAULT '',
                post_name varchar(200) NOT NULL DEFAULT '',
                to_ping text NOT NULL,
                pinged text NOT NULL,
                post_modified datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
                post_modified_gmt datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
                post_content_filtered longtext NOT NULL,
                post_parent bigint(20) unsigned NOT NULL DEFAULT 0,
                guid varchar(255) NOT NULL DEFAULT '',
                menu_order int(11) NOT NULL DEFAULT 0,
                post_type varchar(20) NOT NULL DEFAULT 'post',
                post_mime_type varchar(100) NOT NULL DEFAULT '',
                comment_count bigint(20) NOT NULL DEFAULT 0
            )",
            
            // Users table
            "CREATE TABLE IF NOT EXISTS {$this->prefix}users (
                ID bigint(20) unsigned NOT NULL PRIMARY KEY,
                user_login varchar(60) NOT NULL DEFAULT '',
                user_pass varchar(255) NOT NULL DEFAULT '',
                user_nicename varchar(50) NOT NULL DEFAULT '',
                user_email varchar(100) NOT NULL DEFAULT '',
                user_url varchar(100) NOT NULL DEFAULT '',
                user_registered datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
                user_activation_key varchar(255) NOT NULL DEFAULT '',
                user_status int(11) NOT NULL DEFAULT 0,
                display_name varchar(250) NOT NULL DEFAULT ''
            )",
            
            // Options table
            "CREATE TABLE IF NOT EXISTS {$this->prefix}options (
                option_id bigint(20) unsigned NOT NULL PRIMARY KEY,
                option_name varchar(191) NOT NULL DEFAULT '',
                option_value longtext NOT NULL,
                autoload varchar(20) NOT NULL DEFAULT 'yes'
            )",
            
            // Comments table
            "CREATE TABLE IF NOT EXISTS {$this->prefix}comments (
                comment_ID bigint(20) unsigned NOT NULL PRIMARY KEY,
                comment_post_ID bigint(20) unsigned NOT NULL DEFAULT 0,
                comment_author tinytext NOT NULL,
                comment_author_email varchar(100) NOT NULL DEFAULT '',
                comment_author_url varchar(200) NOT NULL DEFAULT '',
                comment_author_IP varchar(100) NOT NULL DEFAULT '',
                comment_date datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
                comment_date_gmt datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
                comment_content text NOT NULL,
                comment_karma int(11) NOT NULL DEFAULT 0,
                comment_approved varchar(20) NOT NULL DEFAULT '1',
                comment_agent varchar(255) NOT NULL DEFAULT '',
                comment_type varchar(20) NOT NULL DEFAULT 'comment',
                comment_parent bigint(20) unsigned NOT NULL DEFAULT 0,
                user_id bigint(20) unsigned NOT NULL DEFAULT 0
            )",
            
            // Terms table
            "CREATE TABLE IF NOT EXISTS {$this->prefix}terms (
                term_id bigint(20) unsigned NOT NULL PRIMARY KEY,
                name varchar(200) NOT NULL DEFAULT '',
                slug varchar(200) NOT NULL DEFAULT '',
                term_group bigint(10) NOT NULL DEFAULT 0
            )",
            
            // Term taxonomy table
            "CREATE TABLE IF NOT EXISTS {$this->prefix}term_taxonomy (
                term_taxonomy_id bigint(20) unsigned NOT NULL PRIMARY KEY,
                term_id bigint(20) unsigned NOT NULL DEFAULT 0,
                taxonomy varchar(32) NOT NULL DEFAULT '',
                description longtext NOT NULL,
                parent bigint(20) unsigned NOT NULL DEFAULT 0,
                count bigint(20) NOT NULL DEFAULT 0
            )",
            
            // Term relationships table
            "CREATE TABLE IF NOT EXISTS {$this->prefix}term_relationships (
                object_id bigint(20) unsigned NOT NULL DEFAULT 0,
                term_taxonomy_id bigint(20) unsigned NOT NULL DEFAULT 0,
                term_order int(11) NOT NULL DEFAULT 0,
                PRIMARY KEY (object_id, term_taxonomy_id)
            )"
        );
        
        foreach ($tables as $sql) {
            try {
                $this->pdo->exec($sql);
            } catch (PDOException $e) {
                error_log("Error creating table: " . $e->getMessage());
            }
        }
        
        // Insert default options
        $this->insert_default_options();
    }
    
    // Insert default WordPress options
    private function insert_default_options() {
        $default_options = array(
            array('option_name' => 'siteurl', 'option_value' => 'http://localhost', 'autoload' => 'yes'),
            array('option_name' => 'home', 'option_value' => 'http://localhost', 'autoload' => 'yes'),
            array('option_name' => 'blogname', 'option_value' => 'WordPress on Hugging Face', 'autoload' => 'yes'),
            array('option_name' => 'blogdescription', 'option_value' => 'Just another WordPress site', 'autoload' => 'yes'),
            array('option_name' => 'users_can_register', 'option_value' => '0', 'autoload' => 'yes'),
            array('option_name' => 'admin_email', 'option_value' => '[email protected]', 'autoload' => 'yes'),
            array('option_name' => 'start_of_week', 'option_value' => '1', 'autoload' => 'yes'),
            array('option_name' => 'use_balanceTags', 'option_value' => '0', 'autoload' => 'yes'),
            array('option_name' => 'use_smilies', 'option_value' => '1', 'autoload' => 'yes'),
            array('option_name' => 'require_name_email', 'option_value' => '1', 'autoload' => 'yes'),
            array('option_name' => 'comments_notify', 'option_value' => '1', 'autoload' => 'yes'),
            array('option_name' => 'posts_per_rss', 'option_value' => '10', 'autoload' => 'yes'),
            array('option_name' => 'rss_use_excerpt', 'option_value' => '0', 'autoload' => 'yes'),
            array('option_name' => 'mailserver_url', 'option_value' => 'mail.example.com', 'autoload' => 'yes'),
            array('option_name' => 'mailserver_login', 'option_value' => '[email protected]', 'autoload' => 'yes'),
            array('option_name' => 'mailserver_pass', 'option_value' => 'password', 'autoload' => 'yes'),
            array('option_name' => 'mailserver_port', 'option_value' => '110', 'autoload' => 'yes'),
            array('option_name' => 'default_category', 'option_value' => '1', 'autoload' => 'yes'),
            array('option_name' => 'default_comment_status', 'option_value' => 'open', 'autoload' => 'yes'),
            array('option_name' => 'default_ping_status', 'option_value' => 'open', 'autoload' => 'yes'),
            array('option_name' => 'default_pingback_flag', 'option_value' => '1', 'autoload' => 'yes'),
            array('option_name' => 'posts_per_page', 'option_value' => '10', 'autoload' => 'yes'),
            array('option_name' => 'date_format', 'option_value' => 'F j, Y', 'autoload' => 'yes'),
            array('option_name' => 'time_format', 'option_value' => 'g:i a', 'autoload' => 'yes'),
            array('option_name' => 'links_updated_date_format', 'option_value' => 'F j, Y g:i a', 'autoload' => 'yes'),
            array('option_name' => 'comment_moderation', 'option_value' => '0', 'autoload' => 'yes'),
            array('option_name' => 'moderation_notify', 'option_value' => '1', 'autoload' => 'yes'),
            array('option_name' => 'permalink_structure', 'option_value' => '/%year%/%monthnum%/%day%/%postname%/', 'autoload' => 'yes'),
            array('option_name' => 'rewrite_rules', 'option_value' => '', 'autoload' => 'yes'),
            array('option_name' => 'hack_file', 'option_value' => '0', 'autoload' => 'yes'),
            array('option_name' => 'blog_charset', 'option_value' => 'UTF-8', 'autoload' => 'yes'),
            array('option_name' => 'moderation_keys', 'option_value' => '', 'autoload' => 'no'),
            array('option_name' => 'active_plugins', 'option_value' => 'a:0:{}', 'autoload' => 'yes'),
            array('option_name' => 'category_base', 'option_value' => '', 'autoload' => 'yes'),
            array('option_name' => 'ping_sites', 'option_value' => 'http://rpc.pingomatic.com/', 'autoload' => 'yes'),
            array('option_name' => 'comment_max_links', 'option_value' => '2', 'autoload' => 'yes'),
            array('option_name' => 'gmt_offset', 'option_value' => '0', 'autoload' => 'yes'),
            array('option_name' => 'default_email_category', 'option_value' => '1', 'autoload' => 'yes'),
            array('option_name' => 'recently_edited', 'option_value' => '', 'autoload' => 'no'),
            array('option_name' => 'template', 'option_value' => 'twentytwentyfour', 'autoload' => 'yes'),
            array('option_name' => 'stylesheet', 'option_value' => 'twentytwentyfour', 'autoload' => 'yes'),
            array('option_name' => 'comment_registration', 'option_value' => '0', 'autoload' => 'yes'),
            array('option_name' => 'html_type', 'option_value' => 'text/html', 'autoload' => 'yes'),
            array('option_name' => 'use_trackback', 'option_value' => '0', 'autoload' => 'yes'),
            array('option_name' => 'default_role', 'option_value' => 'subscriber', 'autoload' => 'yes'),
            array('option_name' => 'db_version', 'option_value' => '57155', 'autoload' => 'yes'),
            array('option_name' => 'uploads_use_yearmonth_folders', 'option_value' => '1', 'autoload' => 'yes'),
            array('option_name' => 'upload_path', 'option_value' => '', 'autoload' => 'yes'),
            array('option_name' => 'blog_public', 'option_value' => '1', 'autoload' => 'yes'),
            array('option_name' => 'default_link_category', 'option_value' => '2', 'autoload' => 'yes'),
            array('option_name' => 'show_on_front', 'option_value' => 'posts', 'autoload' => 'yes'),
            array('option_name' => 'tag_base', 'option_value' => '', 'autoload' => 'yes'),
            array('option_name' => 'show_avatars', 'option_value' => '1', 'autoload' => 'yes'),
            array('option_name' => 'avatar_rating', 'option_value' => 'G', 'autoload' => 'yes'),
            array('option_name' => 'upload_url_path', 'option_value' => '', 'autoload' => 'yes'),
            array('option_name' => 'thumbnail_size_w', 'option_value' => '150', 'autoload' => 'yes'),
            array('option_name' => 'thumbnail_size_h', 'option_value' => '150', 'autoload' => 'yes'),
            array('option_name' => 'thumbnail_crop', 'option_value' => '1', 'autoload' => 'yes'),
            array('option_name' => 'medium_size_w', 'option_value' => '300', 'autoload' => 'yes'),
            array('option_name' => 'medium_size_h', 'option_value' => '300', 'autoload' => 'yes'),
            array('option_name' => 'avatar_default', 'option_value' => 'mystery', 'autoload' => 'yes'),
            array('option_name' => 'large_size_w', 'option_value' => '1024', 'autoload' => 'yes'),
            array('option_name' => 'large_size_h', 'option_value' => '1024', 'autoload' => 'yes'),
            array('option_name' => 'image_default_link_type', 'option_value' => 'none', 'autoload' => 'yes'),
            array('option_name' => 'image_default_size', 'option_value' => '', 'autoload' => 'yes'),
            array('option_name' => 'image_default_align', 'option_value' => '', 'autoload' => 'yes'),
            array('option_name' => 'close_comments_for_old_posts', 'option_value' => '0', 'autoload' => 'yes'),
            array('option_name' => 'close_comments_days_old', 'option_value' => '14', 'autoload' => 'yes'),
            array('option_name' => 'thread_comments', 'option_value' => '1', 'autoload' => 'yes'),
            array('option_name' => 'thread_comments_depth', 'option_value' => '5', 'autoload' => 'yes'),
            array('option_name' => 'page_comments', 'option_value' => '0', 'autoload' => 'yes'),
            array('option_name' => 'comments_per_page', 'option_value' => '50', 'autoload' => 'yes'),
            array('option_name' => 'default_comments_page', 'option_value' => 'newest', 'autoload' => 'yes'),
            array('option_name' => 'comment_order', 'option_value' => 'asc', 'autoload' => 'yes'),
            array('option_name' => 'sticky_posts', 'option_value' => 'a:0:{}', 'autoload' => 'yes'),
            array('option_name' => 'widget_categories', 'option_value' => 'a:0:{}', 'autoload' => 'yes'),
            array('option_name' => 'widget_text', 'option_value' => 'a:0:{}', 'autoload' => 'yes'),
            array('option_name' => 'widget_rss', 'option_value' => 'a:0:{}', 'autoload' => 'yes'),
            array('option_name' => 'uninstall_plugins', 'option_value' => 'a:0:{}', 'autoload' => 'no'),
            array('option_name' => 'timezone_string', 'option_value' => '', 'autoload' => 'yes'),
            array('option_name' => 'page_for_posts', 'option_value' => '0', 'autoload' => 'yes'),
            array('option_name' => 'page_on_front', 'option_value' => '0', 'autoload' => 'yes'),
            array('option_name' => 'default_post_format', 'option_value' => '0', 'autoload' => 'yes'),
            array('option_name' => 'link_manager_enabled', 'option_value' => '0', 'autoload' => 'yes'),
            array('option_name' => 'finished_splitting_shared_terms', 'option_value' => '1', 'autoload' => 'yes'),
            array('option_name' => 'site_icon', 'option_value' => '0', 'autoload' => 'yes'),
            array('option_name' => 'medium_large_size_w', 'option_value' => '768', 'autoload' => 'yes'),
            array('option_name' => 'medium_large_size_h', 'option_value' => '0', 'autoload' => 'yes'),
            array('option_name' => 'wp_page_for_privacy_policy', 'option_value' => '3', 'autoload' => 'yes'),
            array('option_name' => 'show_comments_cookies_opt_in', 'option_value' => '1', 'autoload' => 'yes'),
            array('option_name' => 'admin_email_lifespan', 'option_value' => '1735689600', 'autoload' => 'yes'),
            array('option_name' => 'disallowed_keys', 'option_value' => '', 'autoload' => 'no'),
            array('option_name' => 'comment_previously_approved', 'option_value' => '1', 'autoload' => 'yes'),
            array('option_name' => 'auto_plugin_theme_update_emails', 'option_value' => 'a:0:{}', 'autoload' => 'no'),
            array('option_name' => 'auto_update_core_dev', 'option_value' => 'enabled', 'autoload' => 'yes'),
            array('option_name' => 'auto_update_core_minor', 'option_value' => 'enabled', 'autoload' => 'yes'),
            array('option_name' => 'auto_update_core_major', 'option_value' => 'enabled', 'autoload' => 'yes'),
            array('option_name' => 'wp_force_deactivated_plugins', 'option_value' => 'a:0:{}', 'autoload' => 'yes'),
            array('option_name' => 'initial_db_version', 'option_value' => '57155', 'autoload' => 'yes'),
            array('option_name' => 'wp_user_roles', 'option_value' => 'a:5:{s:13:"administrator";a:2:{s:4:"name";s:13:"Administrator";s:12:"capabilities";a:61:{s:13:"switch_themes";b:1;s:11:"edit_themes";b:1;s:16:"activate_plugins";b:1;s:12:"edit_plugins";b:1;s:10:"edit_users";b:1;s:10:"edit_files";b:1;s:14:"manage_options";b:1;s:17:"moderate_comments";b:1;s:17:"manage_categories";b:1;s:12:"manage_links";b:1;s:12:"upload_files";b:1;s:6:"import";b:1;s:15:"unfiltered_html";b:1;s:10:"edit_posts";b:1;s:17:"edit_others_posts";b:1;s:20:"edit_published_posts";b:1;s:13:"publish_posts";b:1;s:10:"edit_pages";b:1;s:4:"read";b:1;s:8:"level_10";b:1;s:7:"level_9";b:1;s:7:"level_8";b:1;s:7:"level_7";b:1;s:7:"level_6";b:1;s:7:"level_5";b:1;s:7:"level_4";b:1;s:7:"level_3";b:1;s:7:"level_2";b:1;s:7:"level_1";b:1;s:7:"level_0";b:1;s:17:"edit_others_pages";b:1;s:20:"edit_published_pages";b:1;s:13:"publish_pages";b:1;s:12:"delete_pages";b:1;s:19:"delete_others_pages";b:1;s:22:"delete_published_pages";b:1;s:12:"delete_posts";b:1;s:19:"delete_others_posts";b:1;s:22:"delete_published_posts";b:1;s:20:"delete_private_posts";b:1;s:18:"edit_private_posts";b:1;s:18:"read_private_posts";b:1;s:20:"delete_private_pages";b:1;s:18:"edit_private_pages";b:1;s:18:"read_private_pages";b:1;s:12:"delete_users";b:1;s:12:"create_users";b:1;s:17:"unfiltered_upload";b:1;s:14:"edit_dashboard";b:1;s:14:"update_plugins";b:1;s:14:"delete_plugins";b:1;s:15:"install_plugins";b:1;s:13:"update_themes";b:1;s:14:"install_themes";b:1;s:11:"update_core";b:1;s:10:"list_users";b:1;s:12:"remove_users";b:1;s:13:"promote_users";b:1;s:18:"edit_theme_options";b:1;s:13:"delete_themes";b:1;s:6:"export";b:1;}}s:6:"editor";a:2:{s:4:"name";s:6:"Editor";s:12:"capabilities";a:34:{s:17:"moderate_comments";b:1;s:17:"manage_categories";b:1;s:12:"manage_links";b:1;s:12:"upload_files";b:1;s:15:"unfiltered_html";b:1;s:10:"edit_posts";b:1;s:17:"edit_others_posts";b:1;s:20:"edit_published_posts";b:1;s:13:"publish_posts";b:1;s:10:"edit_pages";b:1;s:4:"read";b:1;s:7:"level_7";b:1;s:7:"level_6";b:1;s:7:"level_5";b:1;s:7:"level_4";b:1;s:7:"level_3";b:1;s:7:"level_2";b:1;s:7:"level_1";b:1;s:7:"level_0";b:1;s:17:"edit_others_pages";b:1;s:20:"edit_published_pages";b:1;s:13:"publish_pages";b:1;s:12:"delete_pages";b:1;s:19:"delete_others_pages";b:1;s:22:"delete_published_pages";b:1;s:12:"delete_posts";b:1;s:19:"delete_others_posts";b:1;s:22:"delete_published_posts";b:1;s:20:"delete_private_posts";b:1;s:18:"edit_private_posts";b:1;s:18:"read_private_posts";b:1;s:20:"delete_private_pages";b:1;s:18:"edit_private_pages";b:1;s:18:"read_private_pages";b:1;}}s:6:"author";a:2:{s:4:"name";s:6:"Author";s:12:"capabilities";a:10:{s:12:"upload_files";b:1;s:10:"edit_posts";b:1;s:20:"edit_published_posts";b:1;s:13:"publish_posts";b:1;s:4:"read";b:1;s:7:"level_2";b:1;s:7:"level_1";b:1;s:7:"level_0";b:1;s:12:"delete_posts";b:1;s:22:"delete_published_posts";b:1;}}s:11:"contributor";a:2:{s:4:"name";s:11:"Contributor";s:12:"capabilities";a:5:{s:10:"edit_posts";b:1;s:4:"read";b:1;s:7:"level_1";b:1;s:7:"level_0";b:1;s:12:"delete_posts";b:1;}}s:10:"subscriber";a:2:{s:4:"name";s:10:"Subscriber";s:12:"capabilities";a:2:{s:4:"read";b:1;s:7:"level_0";b:1;}}}', 'autoload' => 'yes'),
            array('option_name' => 'fresh_site', 'option_value' => '1', 'autoload' => 'yes')
        );
        
        foreach ($default_options as $option) {
            try {
                $this->insert($this->prefix . 'options', $option);
            } catch (Exception $e) {
                // Ignore duplicate key errors
            }
        }
    }
}

// Initialize SQLite database connection
$wpdb = new SQLite_DB();

// WordPress database compatibility
if (!isset($GLOBALS['wpdb'])) {
    $GLOBALS['wpdb'] = $wpdb;
}