r1546: Adding convert_database_charset.php
[bugdar.git] / install / convert_database_charset.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright ©2002-[#]year[#] 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 [#]gpl[#] 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 define('SVN', '$Id$');
23
24 chdir('../admin/');
25 require_once('./global.php');
26
27 define('ISSO_PRINTER_NO_NAVIGATION', 1);
28
29 // you can change what you want to conver to here
30 define('TARGET', 'utf8_general_ci');
31
32 $bugsys->input_clean('step', TYPE_UINT);
33
34 // ###################################################################
35
36 function PrintContinue($step)
37 {
38 global $admin;
39
40 $admin->form_start('convert_database_charset.php', 'convert');
41 $admin->form_hidden_field('step', $step);
42 echo "\n<input type=\"submit\" value=\" Continue &#8594; \" name=\"__submit__\" />";
43 $admin->form_end();
44 }
45
46 $admin->page_start('Convert Database Character Set');
47
48 // ###################################################################
49
50 if ($bugsys->in['step'] == 0)
51 {
52 $collation = $db->query_first("SHOW VARIABLES LIKE 'collation_database'");
53
54 echo '<h1>Convert Database Character Set</h1>';
55 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>';
56
57 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>';
58
59 PrintContinue(1);
60 }
61
62 // ###################################################################
63
64 else if ($bugsys->in['step'] == 1)
65 {
66 echo '<h1>Convert Database Character Set</h1>';
67 echo '<p>This step changes the database\'s character set.</p>';
68
69 require './includes/config.php';
70 $db->query("ALTER DATABASE $database COLLATE " . TARGET);
71
72 PrintContinue(2);
73 }
74
75 // ###################################################################
76
77 else if ($bugsys->in['step'] == 2)
78 {
79 echo '<h1>Convert Table Character Set</h1>';
80 echo '<p>This converts each table\'s character set and collation.</p>';
81
82 $admin->table_start();
83 $admin->table_head('Table Conversion');
84 $admin->table_column_head(array('Table Name', 'Result'));
85
86 $tables = $db->query("SHOW TABLES");
87 while ($table = $db->fetch_array($tables, false))
88 {
89 $db->query("ALTER TABLE $table[0] COLLATE " . TARGET);
90 $admin->row_text($table[0], 'Good');
91 }
92
93 $admin->table_end();
94
95 PrintContinue(3);
96 }
97
98 // ###################################################################
99
100 else if ($bugsys->in['step'] == 3)
101 {
102 echo '<h1>Convert Table Columns</h1>';
103 echo '<p>This converts all the table columns that are stored with a character set.</p>';
104
105 $admin->table_start();
106 $admin->table_head('Column Conversion');
107 $admin->table_column_head(array('Table', 'Column'));
108
109 $tables = $db->query("SHOW TABLES");
110 while ($table = $db->fetch_array($tables, false))
111 {
112 $columns = $db->query("SHOW FULL COLUMNS FROM $table[0]");
113 while ($col = $db->fetch_array($columns))
114 {
115 if (!is_null($col['Collation']))
116 {
117 $db->query("ALTER TABLE $table[0] MODIFY $col[Field] $col[Type] COLLATE " . TARGET);
118 $admin->row_text($table[0], $col['Field']);
119 }
120 }
121 }
122
123 $admin->table_end();
124
125 PrintContinue(4);
126 }
127
128 // ###################################################################
129
130 else if ($bugsys->in['step'] == 4)
131 {
132 echo '<h1>Conversion Complete</h1>';
133 echo '<p>Your database has now been converted to the <strong>' . TARGET . '</strong> collation. Congratulations. Please check that everything is operating correctly.</p>';
134 }
135
136 // ###################################################################
137
138 $admin->page_end();
139
140 /*=====================================================================*\
141 || ###################################################################
142 || # $HeadURL$
143 || # $Id$
144 || ###################################################################
145 \*=====================================================================*/
146 ?>