From: Alan Date: Wed, 23 Aug 2023 02:13:41 +0000 (+0800) Subject: Process/FixMysqlCharset.php X-Git-Url: http://git.roojs.org/?a=commitdiff_plain;h=29b93ea0367c4bee10765df6bcd658f5c52358be;p=Pman.Core Process/FixMysqlCharset.php --- diff --git a/Process/FixMysqlCharset.php b/Process/FixMysqlCharset.php new file mode 100644 index 00000000..f105ee76 --- /dev/null +++ b/Process/FixMysqlCharset.php @@ -0,0 +1,101 @@ + array( + 'desc' => 'Database Table', + 'short' => 't', + 'min' => 1, + 'max' => 1, + + ), + /* + 'field' => array( + 'desc' => 'Table Column Name', + 'short' => 'f', + 'min' => 1, + 'max' => 1, + + ), + */ + ); + + + // bugs: 'PICUT, MAËPPE' << strips out chars.. + + + /* + * + * should we do it in PHP - not using the lating stuff? + */ + + function get($req , $opts=array()) + { + // DB_DataObject::debugLevel(1); + + if (file_exists('/tmp/fix_mysql_charset_'. $opts['table'])) { + echo "Conversion for {$opts['table']} has already been done - doing it again will mess things up.. - delete the /tmp/fix_mysql_charset file if you really want to do this\n\n"; + exit; + } + touch('/tmp/fix_mysql_charset_'. $opts['table']); + + $t = DB_DataObject::factory($opts['table']); + $cols = $t->tableColumns(); + $t->selectAdd(); + $t->selectAdd("id"); + + $conv = array(); + $w = array(); + foreach($cols as $k=>$v) { + if (!($v & 2)) { + continue; + } + if (($v & 4)) { // date? + continue; + } + $conv[] = $k; + $t->selectAdd($k); + $t->selectAdd("COALESCE(convert(cast(convert({$k} using latin1) as binary) using utf8), '') as zz_{$k}"); + $w[] = " convert(cast(convert({$k} using latin1) as binary) using utf8) != $k"; + } + $t->whereAdd(implode(" OR ", $w)); + //$t->whereAdd('id=4555'); + // $t->limit(100); + $t->orderBy('id ASC'); + $all = $t->fetchAll(); + foreach($all as $t) { + $up =false; + $tt = clone($t); + //print_r($tt); exit; + foreach($conv as $k) { + if ($t->{$k} != $t->{'zz_'.$k}) { + if (strpos($t->{'zz_'. $k}, '?') !== false) { + $up = false; + continue; + } + + $t->{$k} = $t->{'zz_'.$k}; + $up =true; + } + + + } + if ($up) { + echo "UPDATE $t->id\n"; + $t->_skip_write_xml= true; + DB_DataObject::debugLevel(1); + $t->update($tt); + DB_DataObject::debugLevel(0); + // print_r($t);exit;; + } + } + + + + exit; + } +}