- Update the copyright notices to use the correct year and not a non-ASCII symbol
[bugdar.git] / install / convert_database_charset.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar
5 || # Copyright (c)2004-2008 Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version 2 of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22
23 chdir('../admin/');
24 require_once('./global.php');
25
26 define('ISSO_PRINTER_NO_NAVIGATION', 1);
27
28 // you can change what you want to conver to here
29 define('TARGET', 'utf8_general_ci');
30
31 $input->inputClean('step', TYPE_UINT);
32
33 // columns to convert per table
34 $columnConversions = array();
35
36 // primary keys that are text
37 $primaryKeys = array();
38
39 // fulltext
40 $fulltext = array();
41
42 // ###################################################################
43
44 function PrintContinue($step)
45 {
46 global $admin;
47
48 $admin->form_start('convert_database_charset.php', 'convert');
49 $admin->form_hidden_field('step', $step);
50 echo "\n<input type=\"submit\" value=\" Continue &#8594; \" name=\"__submit__\" />";
51 $admin->form_end();
52 }
53
54 $admin->page_start('Convert Database Character Set');
55
56 // ###################################################################
57
58 if ($input->in['step'] == 0)
59 {
60 $collation = $db->queryFirst("SHOW VARIABLES LIKE 'collation_database'");
61
62 echo '<h1>Convert Database Character Set</h1>';
63 echo '<p>Earlier versions of Bugdar did not check or enforce the MySQL database character set or collation. It is recommended that the database be set at <strong>' . TARGET . '</strong>. This script can migrate all of data from your current character set/collation of <strong>' . $collation['Value'] . '</strong>. If you would like to convert your database to utf8, please click the link below.</p>';
64
65 echo '<h3>Please back up your database before continuing! While this script is tested and should be safe, it is better to be cautious.</h3>';
66
67 PrintContinue(1);
68 }
69
70 // ###################################################################
71
72 else if ($input->in['step'] == 1)
73 {
74 echo '<h1>Preserve Existing Data</h1>';
75 echo '<p>This converts all the table columns that are stored with a character set to a BLOB type to ensure there are no problems with the target character set.</p>';
76
77 $admin->table_start();
78 $admin->table_head('Column Preservation');
79 $admin->table_column_head(array('Table', 'Column'));
80
81 // get rid of indices that are strings
82 $db->showerrors = false;
83 $db->query("ALTER TABLE " . TABLE_PREFIX . "bug DROP INDEX summary");
84 $db->query("ALTER TABLE " . TABLE_PREFIX . "comment DROP INDEX comment");
85 $db->query("ALTER TABLE " . TABLE_PREFIX . "language DROP INDEX languagecode");
86 $db->query("ALTER TABLE " . TABLE_PREFIX . "language DROP INDEX langcode");
87 $db->showerrors = true;
88
89 $tables = $db->query("SHOW TABLES");
90 while ($table = $db->fetch_array($tables, false))
91 {
92 $columns = $db->query("SHOW FULL COLUMNS FROM $table[0]");
93 foreach ($columns as $col)
94 {
95 if (!is_null($col['Collation']) AND (strpos($col['Type'], 'char') !== false OR strpos($col['Type'], 'text') !== false))
96 {
97 $columnConversions[$table[0]][$col['Field']] = $col['Type'];
98 if ($col['Key'] == 'PRI')
99 {
100 $db->query("ALTER TABLE $table[0] DROP PRIMARY KEY");
101 $primaryKeys[$table[0]] = $col['Field'];
102 }
103 $db->query("ALTER TABLE $table[0] MODIFY $col[Field] BLOB");
104 $admin->row_text($table[0], $col['Field']);
105 }
106 }
107 }
108
109 $admin->table_end();
110
111 echo '<h1>Convert Database Character Set</h1>';
112 echo '<p>This step changes the database\'s character set.</p>';
113
114 require './includes/config.php';
115 $db->query("ALTER DATABASE $database COLLATE " . TARGET);
116
117 echo '<h1>Convert Table Character Set</h1>';
118 echo '<p>This converts each table\'s character set and collation.</p>';
119
120 $admin->table_start();
121 $admin->table_head('Table Conversion');
122 $admin->table_column_head(array('Table Name', 'Result'));
123
124 $tables = $db->query("SHOW TABLES");
125 while ($table = $db->fetch_array($tables, false))
126 {
127 $db->query("ALTER TABLE $table[0] COLLATE " . TARGET);
128 $admin->row_text($table[0], 'Good');
129 }
130
131 $admin->table_end();
132
133 echo '<h1>Convert Table Columns</h1>';
134 echo '<p>This converts all the table columns that are stored with a character set.</p>';
135
136 $admin->table_start();
137 $admin->table_head('Column Conversion');
138 $admin->table_column_head(array('Table', 'Column'));
139
140 foreach ($columnConversions AS $table => $columns)
141 {
142 foreach ($columns AS $column => $type)
143 {
144 $db->query("ALTER TABLE $table MODIFY $column $type COLLATE " . TARGET);
145 $admin->row_text($table, $column);
146 }
147 }
148
149 foreach ($primaryKeys AS $table => $field)
150 {
151 $db->query("ALTER TABLE $table ADD PRIMARY KEY ($field)");
152 }
153
154 $db->query("ALTER TABLE " . TABLE_PREFIX . "bug ADD FULLTEXT(summary)");
155 $db->query("ALTER TABLE " . TABLE_PREFIX . "comment ADD FULLTEXT(comment)");
156 $db->query("ALTER TABLE " . TABLE_PREFIX . "language ADD UNIQUE(langcode)");
157
158 $admin->table_end();
159
160 PrintContinue(2);
161 }
162
163 // ###################################################################
164
165 else if ($input->in['step'] == 2)
166 {
167 echo '<h1>Conversion Complete</h1>';
168 echo '<p>Your database has now been converted to the <strong>' . TARGET . '</strong> collation. Congratulations. Please check that everything is operating correctly.</p>';
169 }
170
171 // ###################################################################
172
173 $admin->page_end();
174
175 ?>