forked from science-ation/science-ation
113 lines
3.2 KiB
PHP
113 lines
3.2 KiB
PHP
|
<?php
|
||
|
function db_update_173_pre(){
|
||
|
}
|
||
|
|
||
|
/****
|
||
|
This script takes all Latin1 character encoding in the database and converts it to UTF-8
|
||
|
****/
|
||
|
function db_update_173_post(){
|
||
|
|
||
|
// patch the committees_link table
|
||
|
mysql_query("ALTER TABLE `committees_link` ADD `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY");
|
||
|
|
||
|
// loop through every table in the database
|
||
|
$data = mysql_query("SHOW TABLES");
|
||
|
$fields = array();
|
||
|
$keys = array();
|
||
|
$allTables = array();
|
||
|
while($tableInfo = mysql_fetch_array($data)){
|
||
|
$tableName = $tableInfo[0];
|
||
|
$allTables[] = $tableName;
|
||
|
|
||
|
// loop through every field in the table
|
||
|
$query = mysql_query("DESCRIBE " . $tableName);
|
||
|
$keys[$tableName] = array();
|
||
|
while($rowInfo = mysql_fetch_array($query)){
|
||
|
|
||
|
// find out if this field is a varchar, char, text, or tinytext field
|
||
|
if(preg_match('/.*char.*|.*text.*/', $rowInfo['Type'])){
|
||
|
// it does, so this field needs to be converted
|
||
|
if(!array_key_exists($tableName, $fields)){
|
||
|
$fields[$tableName] = array();
|
||
|
}
|
||
|
$fields[$tableName][] = $rowInfo['Field'];
|
||
|
}
|
||
|
if($rowInfo['Key'] == 'PRI'){
|
||
|
$keys[$tableName][] = $rowInfo['Field'];
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* The array "fields" now contains the names of all fields in the database that
|
||
|
need to be converted (and only those that need to be converted), stored in this format:
|
||
|
|
||
|
[table1] => Array
|
||
|
(
|
||
|
[0] => fieldname1
|
||
|
[1] => fieldname2
|
||
|
...
|
||
|
)
|
||
|
|
||
|
[table2] => Array
|
||
|
(
|
||
|
[0] => fieldname3
|
||
|
[1] => fieldname4
|
||
|
[2] => fieldname5
|
||
|
...
|
||
|
)
|
||
|
...
|
||
|
|
||
|
Now we need to run through those tables one at a time and convert them
|
||
|
*/
|
||
|
$errorTally = 0;
|
||
|
echo "Updating records:\n";
|
||
|
foreach($fields as $tableName => $fieldSet){
|
||
|
// build the query that gives us the field values we need to update in this table, as well as the primary keys
|
||
|
$query = "SELECT `" . implode('`, `', $fieldSet) . '`';
|
||
|
for($n = 0; $n < count($keys[$tableName]); $n++){
|
||
|
$query .= ", `" . $keys[$tableName][$n] . "` AS __KEYFIELD_" . ($n + 1) . "__";
|
||
|
}
|
||
|
$query .= " FROM $tableName";
|
||
|
|
||
|
// fetch all of those values
|
||
|
$updates = array();
|
||
|
$data = mysql_query($query);
|
||
|
while($record = mysql_fetch_array($data)){
|
||
|
$updates[] = $record;
|
||
|
}
|
||
|
|
||
|
// now re-insert those values into the table
|
||
|
foreach($updates as $update){
|
||
|
$query = "UPDATE $tableName SET";
|
||
|
$useComma = false;
|
||
|
foreach($fieldSet as $fieldName){
|
||
|
$fieldValue = $update[$fieldName];
|
||
|
if($useComma){
|
||
|
$query .= ",";
|
||
|
}else{
|
||
|
$useComma = true;
|
||
|
}
|
||
|
$newValue = mb_convert_encoding($fieldValue, "UTF-8", "iso-8859-1");
|
||
|
$query .= sprintf(" `%s` = '%s'", $fieldName, mysql_real_escape_string($newValue));
|
||
|
}
|
||
|
$query .= " WHERE ";
|
||
|
for($n = 0; $n < count($keys[$tableName]); $n++){
|
||
|
if($n > 0) $query .= " AND ";
|
||
|
$query .= "`" . $keys[$tableName][$n] . "` = '" . mysql_real_escape_string($update["__KEYFIELD_" . ($n + 1) . "__"]) . "'";
|
||
|
}
|
||
|
$success = mysql_query($query);
|
||
|
if($success) echo '.';
|
||
|
else{
|
||
|
echo "\nFailed to execute query: $query\n";
|
||
|
$errorTally ++;
|
||
|
}
|
||
|
}
|
||
|
unset($updates);
|
||
|
}
|
||
|
echo "\nComplete with $errorTally failed queries.\n";
|
||
|
|
||
|
// now drop the id column that we added to the committees_link table
|
||
|
$query = "ALTER TABLE `committees_link` DROP `id`";
|
||
|
mysql_query($query);
|
||
|
}
|