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