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 = array();
11     var $links = array();
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         if (!file_exists($iniCache)) {
40             return;
41         }
42         
43         $this->schema = parse_ini_file($iniCache, true);
44         $this->links = parse_ini_file(preg_replace('/\.ini$/', '.links.ini', $iniCache), true);
45         
46
47         
48     }
49    
50     function updateCharacterSet()
51     {
52         $db = DB_DataObject::factory('core_enum')->getDatabaseConnection();
53         $views = $db->getListOf(  'views');
54         
55         
56         foreach (array_keys($this->schema) as $tbl){
57             
58             if(strpos($tbl, '__keys') !== false ){
59                 continue;
60             }
61             
62             if(in_array($tbl , $views)) {
63                 continue;
64             }
65             
66             $ce = DB_DataObject::factory('core_enum');
67             
68             $ce->query("
69                 SELECT
70                         CCSA.character_set_name csname,
71                         CCSA.collation_name collatename
72                 FROM
73                         information_schema.`TABLES` T,
74                         information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA
75                 WHERE
76                         CCSA.collation_name = T.table_collation
77                     AND
78                         T.table_schema = '{$ce->database()}' -- COLLATE utf8_general_ci
79                     AND
80                         T.table_name = '{$tbl}' -- COLLATE utf8_general_ci
81             ");
82                      
83             if (!$ce->fetch()) {
84                 continue;
85             }
86             
87             if($ce->csname == 'utf8' && $ce->collatename == 'utf8_general_ci'){
88                 echo "utf8: SKIP $tbl\n";
89                 continue;
90             }
91             // this used to be utf8_unicode_ci
92             //as the default collation for stored procedure parameters is utf8_general_ci and you can't mix collations.
93             
94             $ce = DB_DataObject::factory('core_enum');
95             // not sure why, but convert to does not actually change the 'charset=' bit..
96             $ce->query("ALTER TABLE $tbl CHARSET=utf8");
97             $ce->query("ALTER TABLE {$tbl} CONVERT TO CHARACTER SET  utf8 COLLATE utf8_general_ci");
98             echo "utf8: FIXED {$tbl}\n";
99             
100         }
101     }
102     function updateEngine()
103     {
104         $db = DB_DataObject::factory('core_enum');
105         $db->query("show variables like 'innodb_file_per_table'");
106         $db->fetch();
107         if ($db->Value == 'OFF') {
108             die("Error: set innodb_file_per_table = 1 in my.cnf\n\n");
109         }
110         
111         // get a list of table views...
112         // innodb in single files is far more efficient that MYD or one big innodb file.
113         // first check if database is using this format.
114         
115         
116         
117         $db = DB_DataObject::factory('core_enum')->getDatabaseConnection();
118         $views = $db->getListOf( 'views');
119         
120         
121         
122         
123         foreach (array_keys($this->schema) as $tbl){
124             
125             if(strpos($tbl, '__keys') !== false ){
126                 continue;
127             }
128             if(in_array($tbl , $views)) {
129                 continue;
130             }
131             
132             $ce = DB_DataObject::factory('core_enum');
133             
134             $ce->query("
135                 select
136                     engine
137                 from
138                     information_schema.tables
139                 where
140                     table_schema='{$ce->database()}'
141                     and
142                     table_name = '{$tbl}'
143             ");
144
145             if (!$ce->fetch()) {
146                 continue;
147             }
148             
149             if($ce->engine == 'InnoDB' ){
150                 echo "InnoDB: SKIP $tbl\n";
151                 continue;
152             }
153             // this used to be utf8_unicode_ci
154             //as the default collation for stored procedure parameters is utf8_general_ci and you can't mix collations.
155             
156             $ce = DB_DataObject::factory('core_enum');
157             $ce->query("ALTER TABLE $tbl ENGINE=InnoDB");
158             echo "InnoDB: FIXED {$tbl}\n";
159             
160         }
161     }
162     
163 }
164