💦 FULL SET: Changeset/ - High Quality

Changeset 2507607


Ignore:
Timestamp:
04/01/2021 05:31:38 PM (5 years ago)
Author:
davejesch
Message:

update readme with FAQ, WP version and PHP version
add support for FULLTEXT indexes
add handling of 'enum' column types
avoid scheduling cron during WP install
add check for CLI mode in prepend

Location:
database-collation-fix
Files:
5 added
3 edited

Legend:

Unmodified
Added
Removed
  • database-collation-fix/trunk/databasecollationfix.php

    r1642748 r2507607  
    44Plugin URL: https://serverpress.com/plugins/databasecollationfix
    55Description: 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
     6Version: 1.2.7
    77Author: Dave Jesch
    88Author URI: http://serverpress.com
    99Text Domain: dbcollationfix
    1010Domain path: /language
    11  */
     11*/
    1212
    1313class DS_DatabaseCollationFix
     
    8686    private function _update_schedule($interval = NULL)
    8787    {
     88        if ( defined( 'WP_INSTALLING' ) && WP_INSTALLING )
     89            return;             // do nothing if WP is trying to install
     90
    8891        $time_start = strtotime('yesterday');
    8992        if (NULL === $time_start)
     
    113116        global $wpdb;
    114117
     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
    115129        $this->_report = $report;
    116         $table_count = $column_count = 0;
    117         if ($report)
     130        $table_count = $column_count = $index_count = 0;
     131        if ($report) {
    118132            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);
    119144
    120145        // search for this collation method
     
    128153            foreach ($res as $table) {
    129154$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
    130158                $this->_report(sprintf(__('Checking table "%s"...', 'dbcollationfix'), $table));
    131159                // check how the table was created
     
    134162$this->_log(__METHOD__.'() res=' . var_export($create_table_res, TRUE));
    135163                $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//              }
    136212
    137213                // check table collation and modify if it's an undesired algorithm
    138214                $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)) {
    141217$this->_log(__METHOD__.'() checking collation: ' . $collation_term);
    142                     if (FALSE !== stripos($create_table, $collation_term)) {
    143