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

Last change on this file since 2297 was 2297, checked in by plg, 16 years ago

Modification: new header on PHP files, PhpWebGallery renamed Piwigo.

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