source: trunk/install/db/65-database.php @ 2127

Last change on this file since 2127 was 2127, checked in by rvelices, 17 years ago
  • PWG_CHARSET, DB_CHARSET and DB_COLLATE... utf-8 ready
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 10.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | file          : $Id: 65-database.php 2127 2007-10-09 01:43:29Z rvelices $
8// | last update   : $Date: 2007-10-09 01:43:29 +0000 (Tue, 09 Oct 2007) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 2127 $
11// +-----------------------------------------------------------------------+
12// | This program is free software; you can redistribute it and/or modify  |
13// | it under the terms of the GNU General Public License as published by  |
14// | the Free Software Foundation                                          |
15// |                                                                       |
16// | This program is distributed in the hope that it will be useful, but   |
17// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
18// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
19// | General Public License for more details.                              |
20// |                                                                       |
21// | You should have received a copy of the GNU General Public License     |
22// | along with this program; if not, write to the Free Software           |
23// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
24// | USA.                                                                  |
25// +-----------------------------------------------------------------------+
26
27if (!defined('PHPWG_ROOT_PATH'))
28{
29  die('Hacking attempt!');
30}
31
32function upgrade65_change_table_to_blob($table, $field_definitions)
33{
34  $types = array('varchar'  => 'varbinary',
35    'text' => 'blob',
36    'mediumtext' => 'mediumblob',
37    'longtext' => 'longblob');
38
39  $changes=array();
40  foreach( $field_definitions as $row)
41  {
42    if ( !isset($row['Collation']) or $row['Collation']=='NULL' )
43      continue;
44    list ($type) = explode('(', $row['Type']);
45    if (!isset($types[$type]))
46      continue; // no need
47    $binaryType = preg_replace('/'. $type .'/i', $types[$type], $row['Type'] );
48    $changes[] = 'MODIFY COLUMN '.$row['Field'].' '.$binaryType;
49  }
50  if (count($changes))
51  {
52    $query = 'ALTER TABLE '.$table.' '.implode(', ', $changes);
53    pwg_query($query);
54  }
55}
56
57function upgrade65_change_table_to_charset($table, $field_definitions, $db_charset)
58{
59  $changes=array();
60  foreach( $field_definitions as $row)
61  {
62    if ( !isset($row['Collation']) or $row['Collation']=='NULL' )
63      continue;
64    $query = $row['Field'].' '.$row['Type'];
65    $query .= ' CHARACTER SET '.$db_charset;
66    if (strpos($row['Collation'],'_bin')!==false)
67    {
68      $query .= ' BINARY';
69    }
70    if ($row['Null']!='YES')
71    {
72      $query.=' NOT NULL';
73      if (isset($row['Default']))
74        $query.=' DEFAULT "'.addslashes($row['Default']).'"';
75    }
76    else
77    {
78      if (!isset($row['Default']))
79        $query.=' DEFAULT NULL';
80      else
81        $query.=' DEFAULT "'.addslashes($row['Default']).'"';
82    }
83
84    if ($row['Extra']=='auto_increment')
85    {
86      $query.=' auto_increment';
87    }
88    $changes[] = 'MODIFY COLUMN '.$query;
89  }
90
91  if (count($changes))
92  {
93    $query = 'ALTER TABLE `'.$table.'` '.implode(', ', $changes);
94    pwg_query($query);
95  }
96}
97
98
99$upgrade_description = 'PWG charset migration';
100// +-----------------------------------------------------------------------+
101// |                            Upgrade content                            |
102// +-----------------------------------------------------------------------+
103if ( !defined('PWG_CHARSET') )
104{
105  $upgrade_log = '';
106
107// +-----------------------------------------------------------------------+
108// load the config file
109  $config_file = PHPWG_ROOT_PATH.'include/mysql.inc.php';
110  $config_file_contents = @file_get_contents($config_file);
111  if ($config_file_contents === false)
112  {
113    die('CANNOT LOAD '.$config_file);
114  }
115  $php_end_tag = strrpos($config_file_contents, '?'.'>');
116  if ($php_end_tag === false)
117  {
118    die('CANNOT FIND PHP END TAG IN '.$config_file);
119  }
120  if (!is_writable($config_file))
121  {
122    die('FILE NOT WRITABLE '.$config_file);
123  }
124
125
126// +-----------------------------------------------------------------------+
127// load all the user languages
128  $all_langs=array();
129  $query='
130SELECT language, COUNT(user_id) AS count FROM '.USER_INFOS_TABLE.'
131  GROUP BY language';
132  $result = pwg_query($query);
133  while ( $row=mysql_fetch_assoc($result) )
134  {
135    $lang = $row["language"];
136    $lang_def = explode('.', $lang);
137    if ( count($lang_def)==2 )
138    {
139      $new_lang = $lang_def[0];
140      $charset = strtolower($lang_def[1]);
141    }
142    else
143    {
144      $new_lang = 'en_UK';
145      $charset = 'iso-8859-1';
146    }
147    $all_langs[$lang] = array(
148      'count' => $row['count'],
149      'new_lang' => $new_lang,
150      'charset' => $charset,
151      );
152    $upgrade_log .= ">>user_lang\t".$lang."\t".$row['count']."\n";
153  }
154  $upgrade_log .= "\n";
155
156
157// +-----------------------------------------------------------------------+
158// get admin charset
159  include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
160  @include(PHPWG_ROOT_PATH. 'include/config_local.inc.php');
161  $admin_charset='iso-8859-1';
162  $query='
163SELECT language FROM '.USER_INFOS_TABLE.'
164  WHERE user_id='.$conf['webmaster_id'];
165  $result = pwg_query($query);
166  if (mysql_num_rows($result)==0)
167  {
168    $query='
169SELECT language FROM '.USER_INFOS_TABLE.'
170  WHERE status="webmaster" and adviser="false"
171  LIMIT 1';
172    $result = pwg_query($query);
173  }
174
175  if ( $row=mysql_fetch_assoc($result) )
176  {
177    $admin_charset = $all_langs[$row['language']]['charset'];
178  }
179  $upgrade_log .= ">>admin_charset\t".$admin_charset."\n";
180
181
182// +-----------------------------------------------------------------------+
183// get mysql version and structure of tables
184  $mysql_version = mysql_get_server_info();
185  $upgrade_log .= ">>mysql_ver\t".$mysql_version."\n";
186
187  $all_tables = array();
188  $query = 'SHOW TABLES LIKE "'.$prefixeTable.'%"';
189  $result = pwg_query($query);
190  while ( $row=mysql_fetch_array($result) )
191  {
192    array_push($all_tables, $row[0]);
193  }
194
195  $all_tables_definition = array();
196  foreach( $all_tables as $table)
197  {
198    $query = 'SHOW FULL COLUMNS FROM '.$table;
199    $result = pwg_query($query);
200    $field_definitions=array();
201    while ( $row=mysql_fetch_array($result) )
202    {
203      if ( !isset($row['Collation']) or $row['Collation']=='NULL' )
204        continue;
205      array_push($field_definitions, $row);
206    }
207    $all_tables_definition[$table] = $field_definitions;
208  }
209
210// +-----------------------------------------------------------------------+
211// calculate the result and convert the tables
212
213//tables that can be converted without going through binary (they contain only ascii data)
214  $safe_tables=array('history','history_backup','history_summary','old_permalinks','plugins','rate','upgrade','user_cache','user_feed','user_infos','user_mail_notification', 'users', 'waiting','ws_access');
215  $safe_tables=array_flip($safe_tables);
216
217  $pwg_charset = 'iso-8859-1';
218  $db_charset = 'latin1';
219  $db_collate = '';
220  if ( version_compare($mysql_version, '4.1', '<') )
221  { // below 4.1 no charset support
222    $upgrade_log .= "< conversion\tnothing\n";
223  }
224  elseif ($admin_charset=='iso-8859-1')
225  {
226    $pwg_charset = 'utf-8';
227    $db_charset = 'utf8';
228    foreach($all_tables as $table)
229    {
230      upgrade65_change_table_to_charset($table, $all_tables_definition[$table], 'utf8' );
231      $query = 'ALTER TABLE '.$table.' DEFAULT CHARACTER SET utf8';
232      pwg_query($query);
233    }
234    $upgrade_log .= "< conversion\tchange utf8\n";
235  }
236/*ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name; (or change column character set)
237
238Warning: The preceding operation converts column values between the character sets. This is not what you want if you have a column in one character set (like latin1) but the stored values actually use some other, incompatible character set (like utf8). In this case, you have to do the following for each such column:
239
240ALTER TABLE t1 CHANGE c1 c1 BLOB;
241ALTER TABLE t1 CHANGE c1 c1 TEXT CHARACTER SET utf8;
242*/
243  elseif ( $admin_charset=='utf-8')
244  {
245    $pwg_charset = 'utf-8';
246    $db_charset = 'utf8';
247    foreach($all_tables as $table)
248    {
249      if ( !isset($safe_tables[ substr($table, strlen($prefixeTable)) ]) )
250        upgrade65_change_table_to_blob($table, $all_tables_definition[$table] );
251      upgrade65_change_table_to_charset($table, $all_tables_definition[$table], 'utf8' );
252      $query = 'ALTER TABLE '.$table.' DEFAULT CHARACTER SET utf8';
253      pwg_query($query);
254    }
255    $upgrade_log .= "< conversion\tchange binary\n";
256    $upgrade_log .= "< conversion\tchange utf8\n";
257  }
258  elseif ( $admin_charset=='iso-8859-2'/*Central European*/)
259  {
260    $pwg_charset = 'utf-8';
261    $db_charset = 'utf8';
262    foreach($all_tables as $table)
263    {
264      if ( !isset($safe_tables[ substr($table, strlen($prefixeTable)) ]) )
265      {
266        upgrade65_change_table_to_blob($table, $all_tables_definition[$table] );
267        upgrade65_change_table_to_charset($table, $all_tables_definition[$table], 'latin2' );
268      }
269      upgrade65_change_table_to_charset($table, $all_tables_definition[$table], 'utf8' );
270      $query = 'ALTER TABLE '.$table.' DEFAULT CHARACTER SET utf8';
271      pwg_query($query);
272    }
273    $upgrade_log .= "< conversion\tchange binary\n";
274    $upgrade_log .= "< conversion\tchange latin2\n";
275    $upgrade_log .= "< conversion\tchange utf8\n";
276  }
277
278
279// +-----------------------------------------------------------------------+
280// write the result to file and update #user_infos.language
281  $config_file_contents =
282    substr($config_file_contents, 0, $php_end_tag).'
283define(\'PWG_CHARSET\', \''.$pwg_charset.'\');
284define(\'DB_CHARSET\',  \''.$db_charset.'\');
285define(\'DB_COLLATE\',  \'\');
286'.substr($config_file_contents, $php_end_tag);
287
288  $fp = @fopen( $config_file, 'w' );
289  @fputs($fp, $config_file_contents, strlen($config_file_contents));
290  @fclose($fp);
291
292  foreach ($all_langs as $old_lang=>$lang_data)
293  {
294    $query='
295  UPDATE '.USER_INFOS_TABLE.' SET language="'.$lang_data['new_lang'].'"
296    WHERE language="'.$old_lang.'"';
297    pwg_query($query);
298  }
299
300  define('PWG_CHARSET', $pwg_charset);
301  define('DB_CHARSET',  $db_charset);
302  define('DB_COLLATE',  '');
303
304  echo $upgrade_log;
305  $fp = @fopen( PHPWG_ROOT_PATH.'upgrade65.log', 'w' );
306  if ($fp)
307  {
308    @fputs($fp, $upgrade_log, strlen($upgrade_log));
309    @fclose($fp);
310  }
311
312echo
313"\n"
314.'"'.$upgrade_description.'"'.' ended'
315."\n"
316;
317}
318else
319{
320  echo 'PWG_CHARSET already defined - nada';
321}
322?>
Note: See TracBrowser for help on using the repository browser.