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             $ce->query("ALTER TABLE {$tbl} CONVERT TO CHARACTER SET  utf8 COLLATE utf8_general_ci");
79             echo "utf8: FIXED {$tbl}\n";
80             
81         }
82     }
83     function updateEngine()
84     {
85         foreach (array_keys($this->schema) as $tbl){
86             
87             if(strpos($tbl, '__keys') !== false ){
88                 continue;
89             }
90             
91             $ce = DB_DataObject::factory('core_enum');
92             
93             $ce->query("select engine from information_schema.tables where table_schema='hydra' and table_name = 'core_enum'");
94
95             $ce->fetch();
96             
97             if($ce->engine == 'InnoDB' ){
98                 echo "InnoDB: SKIP $tbl\n";
99                 continue;
100             }
101             // this used to be utf8_unicode_ci
102             //as the default collation for stored procedure parameters is utf8_general_ci and you can't mix collations.
103             
104             $ce = DB_DataObject::factory('core_enum');
105             $ce->query("ALTER TABLE $tbl ENGINE=InnoDB");
106             echo "InnoDB: FIXED {$tbl}\n";
107             
108         }
109     }
110     
111 }
112