3 require_once 'Pman/Core/Cli.php';
5 class Pman_Core_Process_FixMysqlCharset extends Pman_Core_Cli {
7 static $cli_desc = "Base class for CLI only commands";
8 static $cli_opts = array(
10 'desc' => 'Database Table',
18 'desc' => 'Table Column Name',
28 // bugs: 'PICUT, MAĆPPE' << strips out chars..
33 * should we do it in PHP - not using the lating stuff?
36 function get($req , $opts=array())
38 // DB_DataObject::debugLevel(1);
40 if (file_exists('/tmp/fix_mysql_charset_'. $opts['table'])) {
41 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";
44 touch('/tmp/fix_mysql_charset_'. $opts['table']);
46 $this->disableTriggers($opts['table']);
48 $t = DB_DataObject::factory($opts['table']);
49 $cols = $t->tableColumns();
55 foreach($cols as $k=>$v) {
59 if (($v & 4)) { // date?
64 $t->selectAdd("COALESCE(convert(cast(convert({$k} using latin1) as binary) using utf8), '') as zz_{$k}");
65 $w[] = " convert(cast(convert({$k} using latin1) as binary) using utf8) != $k";
67 $t->whereAdd(implode(" OR ", $w));
68 //$t->whereAdd('id=4555');
70 $t->orderBy('id ASC');
71 $all = $t->fetchAll();
76 foreach($conv as $k) {
77 if ($t->{$k} != $t->{'zz_'.$k}) {
78 if (strpos($t->{'zz_'. $k}, '?') !== false) {
83 $t->{$k} = $t->{'zz_'.$k};
90 echo "UPDATE $t->id\n";
91 $t->_skip_write_xml= true;
92 //DB_DataObject::debugLevel(1);
94 //DB_DataObject::debugLevel(0);
98 $this->enableTriggers($opts['table']);
105 var $triggers = array();
106 function disabletriggers($tbl)
109 $t = DB_DataObject::factory($tbl);
110 // DB_DataObject::debugLevel(1);
111 $t->query("SHOW TRIGGERS FROM {$t->databaseNickname()} where `table` = '{$tbl}'");
112 $this->triggers = array();
113 while ($t->fetch()) {
114 $this->triggers[] = $t->toArray('%s', true);
115 $d = DB_DataObject::factory($tbl);
116 $d->query("DROP TRIGGER {$t->Trigger}");
122 [Trigger] => account_transaction_before_delete
124 [Table] => account_transaction
127 UPDATE `Error: Not allow to delete transaction` SET x = 1;
132 function enabletriggers($tbl)
136 //DB_DataObject::debugLevel(1);
137 foreach($this->triggers as $tr) {
138 $t = DB_DataObject::factory($tbl);
140 CREATE TRIGGER {$tr['Trigger']}
141 {$tr['Timing']} {$tr['Event']} ON {$tbl}