PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.5.2
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.5.2
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Core / Migration.php

Migration.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.5.2, at includes/Core/Migration.php

154 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDeveloper\BetterDocs\Core;
4
5 use WP_Query;
6 use WPDeveloper\BetterDocs\Utils\Base;
7 use WPDeveloper\BetterDocs\Utils\Database;
8 use WPDeveloper\BetterDocs\Dependencies\DI\Container;
9
10 class Migration extends Base {
11 /**
12 * Database
13 * @var Database
14 */
15 private $database;
16 private $settings;
17 private $container;
18
19 public function __construct( Container $container ) {
20 $this->container = $container;
21 $this->database = $container->get( Database::class );
22 $this->settings = $container->get( Settings::class );
23 }
24
25 public function init( $version ) {
26 if ( $version > 250 ) {
27 for ( $_version = 250; $_version <= $version; $_version++ ) {
28 if ( method_exists( $this, "v$_version" ) ) {
29 call_user_func( [ $this, "v$_version" ] );
30 }
31 }
32 }
33
34 $this->search_migration();
35 $this->fix_search_table_collation();
36
37 /**
38 * Settings Migration
39 */
40 $this->settings->migration( $version );
41 }
42
43 public function search_migration() {
44 global $wpdb;
45 if ( ! $this->database->get( 'betterdocs_search_data_migration', false ) ) {
46 $search_data = $this->database->get( 'betterdocs_search_data' );
47 if ( ! empty( $search_data ) ) {
48 $search_data_arr = unserialize( $search_data );
49 foreach ( $search_data_arr as $key => $value ) {
50 $args = [
51 'post_type' => 'docs',
52 'post_status' => 'publish',
53 'posts_per_page' => -1,
54 'suppress_filters' => true,
55 's' => $key
56 ];
57
58 $loop = new WP_Query( $args );
59 if ( $loop->have_posts() ) {
60 $count = $value;
61 $not_found_count = 0;
62 } else {
63 $count = 0;
64 $not_found_count = $value;
65 }
66
67 // Use BINARY comparison to avoid collation mismatch errors
68 $keyword = $wpdb->get_var(
69 $wpdb->prepare(
70 "
71 SELECT keyword
72 FROM {$wpdb->prefix}betterdocs_search_keyword
73 WHERE BINARY keyword = %s",
74 $key
75 )
76 );
77
78 if ( $keyword == null ) {
79 $insert = $wpdb->query(
80 $wpdb->prepare(
81 "INSERT INTO {$wpdb->prefix}betterdocs_search_keyword
82 ( keyword )
83 VALUES ( %s )",
84 [
85 $key
86 ]
87 )
88 );
89
90 if ( $insert ) {
91 $wpdb->query(
92 $wpdb->prepare(
93 "INSERT INTO {$wpdb->prefix}betterdocs_search_log
94 (keyword_id, count, not_found_count, created_at)
95 VALUES (%d, %d, %d, %s)",
96 [
97 $wpdb->insert_id,
98 $count,
99 $not_found_count,
100 date( 'Y-m-d' )
101 ]
102 )
103 );
104 }
105 }
106 }
107
108 $this->database->save( 'betterdocs_search_data_migration', '1.0' );
109 }
110 }
111 }
112
113 /**
114 * Fix collation issues in search tables
115 * Converts tables to proper UTF-8 charset and collation to prevent
116 * "Illegal mix of collations" errors when searching with non-Latin characters
117 *
118 * @since 1.0.2
119 * @return void
120 */
121 public function fix_search_table_collation() {
122 global $wpdb;
123
124 // Check if migration already ran
125 if ( $this->database->get( 'betterdocs_search_collation_fixed', false ) ) {
126 return;
127 }
128
129 // Get the WordPress default charset and collation
130 $charset = $wpdb->charset ? $wpdb->charset : 'utf8mb4';
131 $collate = $wpdb->collate ? $wpdb->collate : 'utf8mb4_unicode_520_ci';
132
133 // Fix search_keyword table
134 $search_keyword_table = $wpdb->prefix . 'betterdocs_search_keyword';
135 if ( $wpdb->get_var( "SHOW TABLES LIKE '$search_keyword_table'" ) == $search_keyword_table ) {
136 // Convert table charset and collation
137 $wpdb->query( "ALTER TABLE {$search_keyword_table} CONVERT TO CHARACTER SET {$charset} COLLATE {$collate}" );
138
139 // Explicitly set keyword column collation
140 $wpdb->query( "ALTER TABLE {$search_keyword_table} MODIFY keyword TEXT CHARACTER SET {$charset} COLLATE {$collate} NOT NULL" );
141 }
142
143 // Fix search_log table
144 $search_log_table = $wpdb->prefix . 'betterdocs_search_log';
145 if ( $wpdb->get_var( "SHOW TABLES LIKE '$search_log_table'" ) == $search_log_table ) {
146 // Convert table charset and collation
147 $wpdb->query( "ALTER TABLE {$search_log_table} CONVERT TO CHARACTER SET {$charset} COLLATE {$collate}" );
148 }
149
150 // Mark migration as complete
151 $this->database->save( 'betterdocs_search_collation_fixed', '1.0' );
152 }
153 }
154