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