aboutsummaryrefslogtreecommitdiffstats
path: root/install/db/65-database.php
blob: cf892343b82b9c00157b94a2f7528f1326ea7ee3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
// +-----------------------------------------------------------------------+
// | Piwigo - a PHP based photo gallery                                    |
// +-----------------------------------------------------------------------+
// | Copyright(C) 2008-2013 Piwigo Team                  http://piwigo.org |
// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
// +-----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify  |
// | it under the terms of the GNU General Public License as published by  |
// | the Free Software Foundation                                          |
// |                                                                       |
// | This program is distributed in the hope that it will be useful, but   |
// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
// | General Public License for more details.                              |
// |                                                                       |
// | You should have received a copy of the GNU General Public License     |
// | along with this program; if not, write to the Free Software           |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA.                                                                  |
// +-----------------------------------------------------------------------+

if (!defined('PHPWG_ROOT_PATH'))
{
  die('Hacking attempt!');
}

function upgrade65_change_table_to_blob($table, $field_definitions)
{
  $types = array('varchar'  => 'varbinary',
    'text' => 'blob',
    'mediumtext' => 'mediumblob',
    'longtext' => 'longblob');

  $changes=array();
  foreach( $field_definitions as $row)
  {
    if ( !isset($row['Collation']) or $row['Collation']=='NULL' )
      continue;
    list ($type) = explode('(', $row['Type']);
    if (!isset($types[$type]))
      continue; // no need
    $binaryType = preg_replace('/'. $type .'/i', $types[$type], $row['Type'] );
    $changes[] = 'MODIFY COLUMN '.$row['Field'].' '.$binaryType;
  }
  if (count($changes))
  {
    $query = 'ALTER TABLE '.$table.' '.implode(', ', $changes);
    pwg_query($query);
  }
}

function upgrade65_change_table_to_charset($table, $field_definitions, $db_charset)
{
  $changes=array();
  foreach( $field_definitions as $row)
  {
    if ( !isset($row['Collation']) or $row['Collation']=='NULL' )
      continue;
    $query = $row['Field'].' '.$row['Type'];
    $query .= ' CHARACTER SET '.$db_charset;
    if (strpos($row['Collation'],'_bin')!==false)
    {
      $query .= ' BINARY';
    }
    if ($row['Null']!='YES')
    {
      $query.=' NOT NULL';
      if (isset($row['Default']))
        $query.=' DEFAULT "'.addslashes($row['Default']).'"';
    }
    else
    {
      if (!isset($row['Default']))
        $query.=' DEFAULT NULL';
      else
        $query.=' DEFAULT "'.addslashes($row['Default']).'"';
    }

    if ($row['Extra']=='auto_increment')
    {
      $query.=' auto_increment';
    }
    $changes[] = 'MODIFY COLUMN '.$query;
  }

  if (count($changes))
  {
    $query = 'ALTER TABLE `'.$table.'` '.implode(', ', $changes);
    pwg_query($query);
  }
}


$upgrade_description = 'PWG charset migration';
// +-----------------------------------------------------------------------+
// |                            Upgrade content                            |
// +-----------------------------------------------------------------------+
if ( !defined('PWG_CHARSET') )
{
  $upgrade_log = '';

// +-----------------------------------------------------------------------+
// load all the user languages
  $all_langs=array();
  $query='
SELECT language, COUNT(user_id) AS count FROM '.USER_INFOS_TABLE.'
  GROUP BY language';
  $result = pwg_query($query);
  while ( $row=pwg_db_fetch_assoc($result) )
  {
    $language = $row["language"];
    $lang_def = explode('.', $language);
    if ( count($lang_def)==2 )
    {
      $new_lang = $lang_def[0];
      $charset = strtolower($lang_def[1]);
    }
    else
    {
      $new_lang = 'en_UK';
      $charset = 'iso-8859-1';
    }
    $all_langs[$language] = array(
      'count' => $row['count'],
      'new_lang' => $new_lang,
      'charset' => $charset,
      );
    $upgrade_log .= ">>user_lang\t".$language."\t".$row['count']."\n";
  }
  $upgrade_log .= "\n";


// +-----------------------------------------------------------------------+
// get admin charset
  include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
  @include(PHPWG_ROOT_PATH. 'local/config/config.inc.php');
  $admin_charset='iso-8859-1';
  $query='
SELECT language FROM '.USER_INFOS_TABLE.'
  WHERE user_id='.$conf['webmaster_id'];
  $result = pwg_query($query);
  if (pwg_db_num_rows($result)==0)
  {
    $query='
SELECT language FROM '.USER_INFOS_TABLE.'
  WHERE status="webmaster" and adviser="false"
  LIMIT 1';
    $result = pwg_query($query);
  }

  if ( $row=pwg_db_fetch_assoc($result) )
  {
    $admin_charset = $all_langs[$row['language']]['charset'];
  }
  $upgrade_log .= ">>admin_charset\t".$admin_charset."\n";


// +-----------------------------------------------------------------------+
// get mysql version and structure of tables
  $mysql_version = mysql_get_server_info();
  $upgrade_log .= ">>mysql_ver\t".$mysql_version."\n";

  $all_tables = array();
  $query = 'SHOW TABLES LIKE "'.$prefixeTable.'%"';
  $result = pwg_query($query);
  while ( $row=pwg_db_fetch_row($result) )
  {
    array_push($all_tables, $row[0]);
  }

  $all_tables_definition = array();
  foreach( $all_tables as $table)
  {
    $query = 'SHOW FULL COLUMNS FROM '.$table;
    $result = pwg_query($query);
    $field_definitions=array();
    while ( $row=pwg_db_fetch_assoc($result) )
    {
      if ( !isset($row['Collation']) or $row['Collation']=='NULL' )
        continue;
      array_push($field_definitions, $row);
    }
    $all_tables_definition[$table] = $field_definitions;
  }

// +-----------------------------------------------------------------------+
// calculate the result and convert the tables

//tables that can be converted without going through binary (they contain only ascii data)
  $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');
  $safe_tables=array_flip($safe_tables);

  $pwg_charset = 'iso-8859-1';
  $db_charset = 'latin1';
  $db_collate = '';
  if ( version_compare($mysql_version, '4.1', '<') )
  { // below 4.1 no charset support
    $upgrade_log .= "< conversion\tnothing\n";
  }
  elseif ($admin_charset=='iso-8859-1')
  {
    $pwg_charset = 'utf-8';
    $db_charset = 'utf8';
    foreach($all_tables as $table)
    {
      upgrade65_change_table_to_charset($table, $all_tables_definition[$table], 'utf8' );
      $query = 'ALTER TABLE '.$table.' DEFAULT CHARACTER SET utf8';
      pwg_query($query);
    }
    $upgrade_log .= "< conversion\tchange utf8\n";
  }
/*ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name; (or change column character set)

Warning: 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:

ALTER TABLE t1 CHANGE c1 c1 BLOB;
ALTER TABLE t1 CHANGE c1 c1 TEXT CHARACTER SET utf8;
*/
  elseif ( $admin_charset=='utf-8')
  {
    $pwg_charset = 'utf-8';
    $db_charset = 'utf8';
    foreach($all_tables as $table)
    {
      if ( !isset($safe_tables[ substr($table, strlen($prefixeTable)) ]) )
        upgrade65_change_table_to_blob($table, $all_tables_definition[$table] );
      upgrade65_change_table_to_charset($table, $all_tables_definition[$table], 'utf8' );
      $query = 'ALTER TABLE '.$table.' DEFAULT CHARACTER SET utf8';
      pwg_query($query);
    }
    $upgrade_log .= "< conversion\tchange binary\n";
    $upgrade_log .= "< conversion\tchange utf8\n";
  }
  elseif ( $admin_charset=='iso-8859-2'/*Central European*/)
  {
    $pwg_charset = 'utf-8';
    $db_charset = 'utf8';
    foreach($all_tables as $table)
    {
      if ( !isset($safe_tables[ substr($table, strlen($prefixeTable)) ]) )
      {
        upgrade65_change_table_to_blob($table, $all_tables_definition[$table] );
        upgrade65_change_table_to_charset($table, $all_tables_definition[$table], 'latin2' );
      }
      upgrade65_change_table_to_charset($table, $all_tables_definition[$table], 'utf8' );
      $query = 'ALTER TABLE '.$table.' DEFAULT CHARACTER SET utf8';
      pwg_query($query);
    }
    $upgrade_log .= "< conversion\tchange binary\n";
    $upgrade_log .= "< conversion\tchange latin2\n";
    $upgrade_log .= "< conversion\tchange utf8\n";
  }


// +-----------------------------------------------------------------------+
// changes to write in database.inc.php
  array_push($mysql_changes,
'define(\'PWG_CHARSET\', \''.$pwg_charset.'\');
define(\'DB_CHARSET\',  \''.$db_charset.'\');
define(\'DB_COLLATE\',  \'\');'
  );

  foreach ($all_langs as $old_lang=>$lang_data)
  {
    $query='
  UPDATE '.USER_INFOS_TABLE.' SET language="'.$lang_data['new_lang'].'"
    WHERE language="'.$old_lang.'"';
    pwg_query($query);
  }

  define('PWG_CHARSET', $pwg_charset);
  define('DB_CHARSET',  $db_charset);
  define('DB_COLLATE',  '');

  if ( version_compare(mysql_get_server_info(), '4.1.0', '>=') and DB_CHARSET!='' )
  {
    pwg_query('SET NAMES "'.DB_CHARSET.'"');
  }

  echo $upgrade_log;
  $fp = @fopen( PHPWG_ROOT_PATH.'upgrade65.log', 'w' );
  if ($fp)
  {
    @fputs($fp, $upgrade_log, strlen($upgrade_log));
    @fclose($fp);
  }

echo
"\n"
.'"'.$upgrade_description.'"'.' ended'
."\n"
;
}
else
{
  echo 'PWG_CHARSET already defined - nada';
}
?>