UpdateDatabase/MysqlEngineCharset.php
[Pman.Core] / UpdateDatabase / MysqlEngineCharset.php
1 <?php
2 /**
3   fixes character set and engine=InnoDB.. in Mysql
4   more efficent 
5  */
6
7 class Pman_Core_UpdateDatabase_MysqlEngineCharset {
8     
9     var $dburl;
10     var $schema;
11     var $links;
12     
13     function __construct()
14     {
15           
16         $this->loadIniFiles(); //?? shared???
17         $this->updateEngine(); // update the engine first - get's around 1000 character limit on indexes..
18         
19         $this->updateCharacterSet();
20         
21         
22     }
23     
24     function loadIniFiles()
25     {
26         // will create the combined ini cache file for the running user.
27         
28         $ff = HTML_FlexyFramework::get();
29         $ff->generateDataobjectsCache(true);
30         $this->dburl = parse_url($ff->database);
31         
32         $dbini = 'ini_'. basename($this->dburl['path']);
33         
34         
35         $iniCache = $ff->DB_DataObject[$dbini];
36         
37         $this->schema = parse_ini_file($iniCache, true);
38         $this->links = parse_ini_file(preg_replace('/\.ini$/', '.links.ini', $iniCache), true);
39         
40
41         
42     }
43    
44     function updateCharacterSet()
45     {
46         foreach (array_keys($this->schema) as $tbl){
47             
48             if(strpos($tbl, '__keys') !== false ){
49                 continue;
50             }
51             
52             $ce = DB_DataObject::factory('core_enum');
53             
54             $ce->query("
55                 SELECT
56                         CCSA.character_set_name csname,
57                         CCSA.collation_name collatename
58                 FROM
59                         information_schema.`TABLES` T,
60                         information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA
61                 WHERE
62                         CCSA.collation_name = T.table_collation
63                     AND
64                         T.table_schema = '{$ce->database()}' -- COLLATE utf8_general_ci
65                     AND
66                         T.table_name = '{$tbl}' -- COLLATE utf8_general_ci
67             ");
68                      
69             $ce->fetch();
70             
71             if($ce->csname == 'utf8' && $ce->collatename == 'utf8_general_ci'){
72                 echo "utf8: SKIP $tbl\n";
73                 continue;
74             }
75             // this used to be utf8_unicode_ci
76             //as the default collation for stored procedure parameters is utf8_general_ci and you can't mix collations.
77             
78             $ce = DB_DataObject::factory('core_enum');
79             // not sure why, but convert to does not actually change the 'charset=' bit..
80             $ce->query("ALTER TABLE $tbl CHARSET=utf8");
81             $ce->query("ALTER TABLE {$tbl} CONVERT TO CHARACTER SET  utf8 COLLATE utf8_general_ci");
82             echo "utf8: FIXED {$tbl}\n";
83             
84         }
85     }
86     function updateEngine()
87     {
88         foreach (array_keys($this->schema) as $tbl){
89             
90             if(strpos($tbl, '__keys') !== false ){
91                 continue;
92             }
93             
94             $ce = DB_DataObject::factory('core_enum');
95             
96             $ce->query("select engine from information_schema.tables where table_schema='{$ce->database()}' and table_name = '{$tbl}'");
97
98             $ce->fetch();
99             
100             if($ce->engine == 'InnoDB' ){
101                 echo "InnoDB: SKIP $tbl\n";
102                 continue;
103             }
104             // this used to be utf8_unicode_ci
105             //as the default collation for stored procedure parameters is utf8_general_ci and you can't mix collations.
106             
107             $ce = DB_DataObject::factory('core_enum');
108             $ce->query("ALTER TABLE $tbl ENGINE=InnoDB");
109             echo "InnoDB: FIXED {$tbl}\n";
110             
111         }
112     }
113     
114 }
115