1 | <?php |
---|
2 | /**************************************************************************\ |
---|
3 | * eGroupWare - Setup - change / convert system-charset * |
---|
4 | * http://www.eGroupWare.org * |
---|
5 | * Written by RalfBecker@outdoor-training.de * |
---|
6 | * -------------------------------------------- * |
---|
7 | * This program is free software; you can redistribute it and/or modify it * |
---|
8 | * under the terms of the GNU General Public License as published by the * |
---|
9 | * Free Software Foundation; either version 2 of the License, or (at your * |
---|
10 | * option) any later version. * |
---|
11 | \**************************************************************************/ |
---|
12 | |
---|
13 | |
---|
14 | $diagnostics = 1; // can be set to 0=non, 1=some (default for now), 2=all |
---|
15 | |
---|
16 | $phpgw_info = array(); |
---|
17 | $GLOBALS['phpgw_info']['flags'] = array( |
---|
18 | 'noheader' => True, |
---|
19 | 'nonavbar' => True, |
---|
20 | 'currentapp' => 'home', |
---|
21 | 'noapi' => True |
---|
22 | ); |
---|
23 | include('./inc/functions.inc.php'); |
---|
24 | // Authorize the user to use setup app and load the database |
---|
25 | // Does not return unless user is authorized |
---|
26 | if (!$GLOBALS['phpgw_setup']->auth('Config') || @$_POST['cancel']) |
---|
27 | { |
---|
28 | Header('Location: index.php'); |
---|
29 | exit; |
---|
30 | } |
---|
31 | $GLOBALS['phpgw_setup']->loaddb(); |
---|
32 | |
---|
33 | $GLOBALS['phpgw_setup']->translation->setup_translation_sql(); |
---|
34 | $translation = &$GLOBALS['phpgw_setup']->translation->sql; |
---|
35 | $translation->translation(True); // to get the mbstring warnings |
---|
36 | |
---|
37 | $tpl_root = $GLOBALS['phpgw_setup']->html->setup_tpl_dir('setup'); |
---|
38 | $setup_tpl = CreateObject('phpgwapi.Template',$tpl_root); |
---|
39 | $setup_tpl->set_file(array( |
---|
40 | 'T_head' => 'head.tpl', |
---|
41 | 'T_footer' => 'footer.tpl', |
---|
42 | 'T_system_charset' => 'system_charset.tpl', |
---|
43 | )); |
---|
44 | |
---|
45 | $stage_title = lang('Change system-charset'); |
---|
46 | $stage_desc = lang('This program will convert your database to a new system-charset.'); |
---|
47 | |
---|
48 | if ($diagnostics || !@$_POST['convert']) |
---|
49 | { |
---|
50 | $GLOBALS['phpgw_setup']->html->show_header($stage_title,False,'config',$GLOBALS['phpgw_setup']->ConfigDomain . '(' . $phpgw_domain[$GLOBALS['phpgw_setup']->ConfigDomain]['db_type'] . ')'); |
---|
51 | } |
---|
52 | if (@$_POST['convert']) |
---|
53 | { |
---|
54 | if (empty($_POST['current_charset'])) |
---|
55 | { |
---|
56 | $errors[] = lang('You need to select your current charset!'); |
---|
57 | $GLOBALS['phpgw_setup']->html->show_header($stage_title,False,'config',$GLOBALS['phpgw_setup']->ConfigDomain . '(' . $phpgw_domain[$GLOBALS['phpgw_setup']->ConfigDomain]['db_type'] . ')'); |
---|
58 | } |
---|
59 | else |
---|
60 | { |
---|
61 | convert_db($_POST['current_charset'],$_POST['new_charset'],$diagnostics); |
---|
62 | |
---|
63 | if (!$diagnostics) |
---|
64 | { |
---|
65 | Header('Location: index.php'); |
---|
66 | } |
---|
67 | echo '<h3>'.lang("Database successfully converted from '%1' to '%2'",$_POST['current_charset'],$_POST['new_charset'])."</h3>\n"; |
---|
68 | echo '<p>'.lang('Click <a href="index.php">here</a> to return to setup.')."</p>\n"; |
---|
69 | exit; |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | function convert_db($from,$to,$diagnostics=1) |
---|
74 | { |
---|
75 | if ($diagnostics) echo "<h3>Converting database from '$from' to '$to'</h3>\n"; |
---|
76 | |
---|
77 | @set_time_limit(0); // this might take a while |
---|
78 | |
---|
79 | $db2 = $GLOBALS['phpgw_setup']->db; |
---|
80 | $setup_info = $GLOBALS['phpgw_setup']->detection->get_versions(); |
---|
81 | $setup_info = $GLOBALS['phpgw_setup']->detection->get_db_versions($setup_info); |
---|
82 | // Visit each app/setup dir, look for a phpgw_lang file |
---|
83 | |
---|
84 | foreach($setup_info as $app => $data) |
---|
85 | { |
---|
86 | if ($diagnostics) echo "<p><b>$app</b>: "; |
---|
87 | |
---|
88 | if (!isset($data['tables']) || !count($data['tables']) || |
---|
89 | !$GLOBALS['phpgw_setup']->app_registered($app) || |
---|
90 | !($table_definitions = $db2->get_table_definitions($app))) |
---|
91 | { |
---|
92 | if ($diagnostics) echo "skipping (no tables or not installed)</p>\n"; |
---|
93 | continue; |
---|
94 | } |
---|
95 | foreach($table_definitions as $table => $definition) |
---|
96 | { |
---|
97 | if ($diagnostics) { echo "<br>start converting table '$table' ... "; } |
---|
98 | $db2->set_column_definitions($definitions['fd']); |
---|
99 | $updates = 0; |
---|
100 | $GLOBALS['phpgw_setup']->db->query("SELECT * FROM $table",__LINE__,__FILE__); |
---|
101 | while($columns = $GLOBALS['phpgw_setup']->db->row(True)) |
---|
102 | { |
---|
103 | $update = array(); |
---|
104 | foreach($columns as $name => $data) |
---|
105 | { |
---|
106 | switch($definition['fd'][$name]['type']) |
---|
107 | { |
---|
108 | case 'char': |
---|
109 | case 'varchar': |
---|
110 | case 'text': |
---|
111 | case 'longtext': |
---|
112 | $converted = $GLOBALS['translation']->convert($data,$from,$to); |
---|
113 | if ($converted != $data) |
---|
114 | { |
---|
115 | $update[$name] = $converted; |
---|
116 | } |
---|
117 | break; |
---|
118 | } |
---|
119 | } |
---|
120 | if (count($update)) |
---|
121 | { |
---|
122 | if (count($definition['pk'])) |
---|
123 | { |
---|
124 | $db2->query($query="UPDATE $table SET ".$db2->column_data_implode(',',$update)." WHERE ".$db2->column_data_implode(' AND ',$columns,True,$definition['pk']),__LINE__,__FILE__); |
---|
125 | } |
---|
126 | else |
---|
127 | { |
---|
128 | // if we have no primary key, we need to delete and re-write the row |
---|
129 | $db2->query($query="DELETE FROM $table WHERE ".$db2->column_data_implode(' AND ',$columns),__LINE__,__FILE__); |
---|
130 | if ($diagnostics > 1) echo " $query<br>\n"; |
---|
131 | $db2->query($query="INSERT INTO $table (".implode(',',array_keys($columns)).") VALUES (".$db2->column_data_implode(',',array_merge($columns,$update),False).")",__LINE__,__FILE__); |
---|
132 | } |
---|
133 | if ($diagnostics > 1) echo " $query<p>\n"; |
---|
134 | ++$updates; |
---|
135 | } |
---|
136 | } |
---|
137 | if ($diagnostics) |
---|
138 | { |
---|
139 | $GLOBALS['phpgw_setup']->db->query("SELECT count(*) FROM $table",__LINE__,__FILE__); |
---|
140 | $GLOBALS['phpgw_setup']->db->next_record(); |
---|
141 | $total = $GLOBALS['phpgw_setup']->db->f(0); |
---|
142 | echo " done, $updates/$total rows updated"; |
---|
143 | } |
---|
144 | } |
---|
145 | } |
---|
146 | @$GLOBALS['phpgw_setup']->db->query("DELETE FROM phpgw_config WHERE config_app='phpgwapi' AND config_name='system_charset'",__LINE__,__FILE__); |
---|
147 | $GLOBALS['phpgw_setup']->db->query("INSERT INTO phpgw_config (config_app,config_name,config_value) VALUES ('phpgwapi','system_charset','$to')",__LINE__,__FILE__); |
---|
148 | } |
---|
149 | |
---|
150 | $setup_tpl->set_var('stage_title',$stage_title); |
---|
151 | $setup_tpl->set_var('stage_desc',$stage_desc); |
---|
152 | $setup_tpl->set_var('error_msg',is_array($errors) ? implode('<br>',$errors) : ' '); |
---|
153 | |
---|
154 | $setup_tpl->set_var('lang_convert',lang('Convert')); |
---|
155 | $setup_tpl->set_var('lang_cancel',lang('Cancel')); |
---|
156 | $setup_tpl->set_var('lang_current',lang('Current system-charset')); |
---|
157 | $setup_tpl->set_var('lang_convert_to',lang('Charset to convert to')); |
---|
158 | $setup_tpl->set_var('lang_warning','<b>'.lang('Setting the system-charset to UTF-8 (unicode) allows the coexistens of data from languages of different charsets.').'</b><br>'. |
---|
159 | lang('If you use only languages of the same charset (eg. western european ones) you dont need to set a system-charset!')); |
---|
160 | |
---|
161 | $installed_charsets = $translation->get_installed_charsets(); |
---|
162 | if ($translation->system_charset || count($installed_charsets) == 1) |
---|
163 | { |
---|
164 | reset($installed_charsets); |
---|
165 | list($current_charset) = each($installed_charsets); |
---|
166 | if ($translation->system_charset) |
---|
167 | { |
---|
168 | $current_charset = $translation->system_charset; |
---|
169 | } |
---|
170 | $setup_tpl->set_var('current_charset',"<b>$current_charset</b>". |
---|
171 | "<input type=\"hidden\" name=\"current_charset\" value=\"$current_charset\">\n"); |
---|
172 | } |
---|
173 | else |
---|
174 | { |
---|
175 | $options = '<option value="">'.lang('select one...')."</option>\n"; |
---|
176 | foreach($installed_charsets as $charset => $description) |
---|
177 | { |
---|
178 | $options .= "<option value=\"$charset\">$description</option>\n"; |
---|
179 | } |
---|
180 | $setup_tpl->set_var('current_charset',"<select name=\"current_charset\">\n$options</select>\n"); |
---|
181 | } |
---|
182 | if ($translation->system_charset == 'utf-8' || count($installed_charsets) == 1) |
---|
183 | { |
---|
184 | reset($installed_charsets); |
---|
185 | list($other_charset) = each($installed_charsets); |
---|
186 | if (!$translation->system_charset || $other_charset == $translation->system_charset) |
---|
187 | { |
---|
188 | $other_charset = 'utf-8'; |
---|
189 | } |
---|
190 | $setup_tpl->set_var('new_charset',"<b>$other_charset</b><input type=\"hidden\" name=\"new_charset\" value=\"$other_charset\">\n"); |
---|
191 | } |
---|
192 | else |
---|
193 | { |
---|
194 | if ($translation->system_charset != 'utf-8') |
---|
195 | { |
---|
196 | $options = '<option value="utf-8">'.lang('utf-8 (Unicode)')."</option>\n"; |
---|
197 | } |
---|
198 | foreach($installed_charsets as $charset => $description) |
---|
199 | { |
---|
200 | if ($charset != $translation->system_charset) |
---|
201 | { |
---|
202 | $options .= "<option value=\"$charset\">$description</option>\n"; |
---|
203 | } |
---|
204 | } |
---|
205 | $setup_tpl->set_var('new_charset',"<select name=\"new_charset\">\n$options</select>\n"); |
---|
206 | } |
---|
207 | $setup_tpl->pparse('out','T_system_charset'); |
---|
208 | $GLOBALS['phpgw_setup']->html->show_footer(); |
---|
209 | ?> |
---|