Process/FixMysqlCharset.php
[Pman.Core] / Process / FixMysqlCharset.php
1 <?php
2
3 require_once 'Pman/Core/Cli.php';
4
5 class Pman_Core_Process_FixMysqlCharset extends Pman_Core_Cli {
6     
7     static $cli_desc = "Base class for CLI only commands";
8     static $cli_opts = array(
9         'table' => array(
10             'desc' => 'Database Table',
11             'short' => 't',
12             'min' => 1,
13             'max' => 1,
14             
15         ),
16         /*
17         'field' => array(
18             'desc' => 'Table Column Name',
19             'short' => 'f',
20             'min' => 1,
21             'max' => 1,
22             
23         ),
24         */
25     );
26     
27     
28     // bugs: 'PICUT, MAƋPPE' << strips out chars..
29     
30     
31     /*
32      *
33      * should we do it in PHP - not using the lating stuff?
34      */
35     
36     function get($req , $opts=array())
37     {
38        // DB_DataObject::debugLevel(1);
39        
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";
42             exit;
43         }
44         touch('/tmp/fix_mysql_charset_'. $opts['table']);
45         
46         $t = DB_DataObject::factory($opts['table']);
47         $cols = $t->tableColumns();
48         $t->selectAdd();
49         $t->selectAdd("id");
50         
51         $conv = array();
52         $w = array();
53         foreach($cols as $k=>$v) {
54             if (!($v & 2)) {
55                 continue;
56             }
57             if (($v & 4)) { // date?
58                 continue;
59             }
60             $conv[] = $k;
61             $t->selectAdd($k);
62             $t->selectAdd("COALESCE(convert(cast(convert({$k} using  latin1) as binary) using utf8), '') as zz_{$k}");
63             $w[] = " convert(cast(convert({$k} using  latin1) as binary) using utf8) != $k";
64         }
65         $t->whereAdd(implode(" OR ", $w));
66         //$t->whereAdd('id=4555');
67        // $t->limit(100);
68         $t->orderBy('id ASC');
69         $all = $t->fetchAll();
70         foreach($all as $t) {
71             $up =false;
72             $tt = clone($t);
73             //print_r($tt); exit;
74             foreach($conv as $k) {
75                if ($t->{$k} != $t->{'zz_'.$k}) {
76                     if (strpos($t->{'zz_'. $k}, '?') !== false) {
77                         $up = false;
78                         continue;
79                     }
80                 
81                     $t->{$k} = $t->{'zz_'.$k};
82                     $up =true;
83                }
84                
85                
86             }
87             if ($up) {
88                 echo "UPDATE $t->id\n";
89                 $t->_skip_write_xml= true;
90                 DB_DataObject::debugLevel(1);
91                 $t->update($tt);
92                 DB_DataObject::debugLevel(0);
93                //  print_r($t);exit;; 
94             }
95         }
96         
97         
98          
99         exit;
100     }
101 }