Changeset 2507607
- Timestamp:
- 04/01/2021 05:31:38 PM (5 years ago)
- Location:
- database-collation-fix
- Files:
-
- 5 added
- 3 edited
-
tags/1.2 (added)
-
tags/1.2/README.md (added)
-
tags/1.2/databasecollationfix.php (added)
-
tags/1.2/prepend.php (added)
-
tags/1.2/readme.txt (added)
-
trunk/databasecollationfix.php (modified) (9 diffs)
-
trunk/prepend.php (modified) (1 diff)
-
trunk/readme.txt (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
database-collation-fix/trunk/databasecollationfix.php
r1642748 r2507607 4 4 Plugin URL: https://serverpress.com/plugins/databasecollationfix 5 5 Description: Convert tables using utf8mb4_unicode_520_ci or utf8_unicode_520_ci collation to standard collation on a cron interval, plus on DesktopServer Create, Copy, Move, Import and Export operations. 6 Version: 1.2 6 Version: 1.2.7 7 7 Author: Dave Jesch 8 8 Author URI: http://serverpress.com 9 9 Text Domain: dbcollationfix 10 10 Domain path: /language 11 */11 */ 12 12 13 13 class DS_DatabaseCollationFix … … 86 86 private function _update_schedule($interval = NULL) 87 87 { 88 if ( defined( 'WP_INSTALLING' ) && WP_INSTALLING ) 89 return; // do nothing if WP is trying to install 90 88 91 $time_start = strtotime('yesterday'); 89 92 if (NULL === $time_start) … … 113 116 global $wpdb; 114 117 118 $force = FALSE; 119 if (isset($_SERVER['REQUEST_METHOD']) && 'POST' === $_SERVER['REQUEST_METHOD']) { 120 if (isset($_POST['force-collation']) && '1' === $_POST['force-collation']) { 121 $force = TRUE; 122 $force_algorithm = 'utf8mb4_unicode_ci'; 123 if (isset($_POST['force-collation-algorithm'])) 124 $force_algorithm = $_POST['force-collation-algorithm']; 125 $this->_collation = $force_algorithm; 126 } 127 } 128 115 129 $this->_report = $report; 116 $table_count = $column_count = 0;117 if ($report) 130 $table_count = $column_count = $index_count = 0; 131 if ($report) { 118 132 echo '<div style="width:100%; margin-top:15px">'; 133 if ($force) { 134 echo '<p>', sprintf(__('Forcing Collation Algorithm to: <b>%s</b>.', 'dbcollationfix'), 135 $force_algorithm), '</p>'; 136 } 137 } 138 139 $this->_report(sprintf(__('Changing database Collation Algorithm to: %s', 'dbcollationfix'), 140 ($force ? $force_algorithm : $this->_collation))); 141 $sql = 'ALTER DATABASE `' . DB_NAME . '` COLLATE=' . ($force ? $force_algorithm : $this->_collation); 142 $res = $wpdb->query($sql); 143 $this->_report($sql, TRUE); 119 144 120 145 // search for this collation method … … 128 153 foreach ($res as $table) { 129 154 $this->_log(__METHOD__.'() checking table ' . $table); 155 // create a list of any indexes that need to be recreated after column alterations 156 $indexes = array(); 157 130 158 $this->_report(sprintf(__('Checking table "%s"...', 'dbcollationfix'), $table)); 131 159 // check how the table was created … … 134 162 $this->_log(__METHOD__.'() res=' . var_export($create_table_res, TRUE)); 135 163 $create_table = $create_table_res['Create Table']; 164 //$this->_report('create table=' . $create_table); 165 166 // drop any FULLTEXT indexes #8 167 // ALTER TABLE `tablename` DROP INDEX `idxname` 168 $offset = 0; 169 $this->_report('create table: ' . var_export($create_table, TRUE)); 170 while ($offset < strlen($create_table)) { 171 $pos = stripos($create_table, 'FULLTEXT KEY', $offset); 172 $this->_report('searching FULLTEXT INDEX: ' . var_export($pos, TRUE)); 173 if (FALSE === $pos) 174 break; 175 176 // found a fulltext index 177 $idx_name = substr($create_table, $pos + 14); 178 $end_quote = strpos($idx_name, '`'); 179 $idx_name = substr($idx_name, 0, $end_quote); 180 $col_names = substr($create_table, $pos + 15 + strlen($idx_name) + 1); 181 $close_paren = strpos($col_names, ')'); 182 $col_names = substr($col_names, 0, $close_paren + 1); 183 184 // move offset pointer to end of FULLTEXT INDEX so we can look for any remaining indexes 185 $offset += $pos + 13 + strlen($idx_name) + strlen($col_names); 186 $this->_report('found index [' . $idx_name . '] of [' . $col_names . ']'); 187 $sql = "CREATE FULLTEXT INDEX `{$idx_name}` ON `{$table}` {$col_names}"; 188 //$this->_report("creating index update: {$sql}"); 189 $indexes[] = $sql; // add to list of indexes to recreate after column alterations 190 ++$index_count; 191 $sql = "ALTER TABLE `{$table}` DROP INDEX `{$idx_name}`"; 192 $this->_report('removing index: ' . $sql); 193 $wpdb->query($sql); 194 } 195 196 // determine current collation value 197 $old_coll = ''; 198 $pos = strpos($create_table, ' COLLATE='); 199 if (FALSE !== $pos) { 200 $old_coll = substr($create_table, $pos + 9); 201 $pos = strpos($old_coll, ' '); 202 if (FALSE !== $pos) 203 $old_coll = substr($old_coll, 0, $pos); 204 //$this->_report('current table collation="' . $old_coll . "'"); 205 } 206 //$this->_report('- current collation: ' . $old_coll); 207 208 // if (!empty($old_coll) && $old_coll !== $force_algorithm && !in_array($old_coll, $this->_change_collation)) { 209 // $this->_change_collation[] = $old_coll; 210 //$this->_report('++ adding collation algorithm to change list: ' . implode(', ', $this->_change_collation)); 211 // } 136 212 137 213 // check table collation and modify if it's an undesired algorithm 138 214 $mod = FALSE; 139 foreach ($this->_change_collation as $coll) {140 $collation_term =" COLLATE={$coll}";215 if (in_array($old_coll, $this->_change_collation) || 216 ($force && $old_coll !== $force_algorithm)) { 141 217 $this->_log(__METHOD__.'() checking collation: ' . $collation_term); 142 if (FALSE !== stripos($create_table, $collation_term)) {143