reset_image_ids should properly change id for all possible tables

This commit is contained in:
Daku 2014-02-19 05:48:20 +00:00
parent daad8d00d5
commit 5df6c09da6

View File

@ -218,41 +218,38 @@ class AdminPage extends Extension {
private function reset_image_ids() { private function reset_image_ids() {
global $database; global $database;
//This might be a bit laggy on boards with lots of images (?) //TODO: Make work with PostgreSQL + SQLite
//Seems to work fine with 1.2k~ images though. //TODO: Update score_log (Having an optional ID column for score_log would be nice..)
$i = 0; preg_match("#^(?P<proto>\w+)\:(?:user=(?P<user>\w+)(?:;|$)|password=(?P<password>\w*)(?:;|$)|host=(?P<host>[\w\.\-]+)(?:;|$)|dbname=(?P<dbname>[\w_]+)(?:;|$))+#", DATABASE_DSN, $matches);
$image = $database->get_all("SELECT * FROM images ORDER BY images.id ASC");
/*$score_log = $database->get_all("SELECT message FROM score_log");*/
foreach($image as $img){
$xid = $img[0];
$i = $i + 1;
$table = array( //Might be missing some tables?
"image_tags", "tag_histories", "image_reports", "comments", "user_favorites", "tag_histories",
"numeric_score_votes", "pool_images", "slext_progress_cache", "notes");
$sql = if($matches['proto'] == "mysql"){
"SET FOREIGN_KEY_CHECKS=0; $tables = $database->get_col("SELECT TABLE_NAME
UPDATE images FROM information_schema.KEY_COLUMN_USAGE
SET id=".$i. WHERE TABLE_SCHEMA = :db
" WHERE id=".$xid.";"; //id for images AND REFERENCED_COLUMN_NAME = 'id'
AND REFERENCED_TABLE_NAME = 'images'", array("db" => $matches['dbname']));
foreach($table as $tbl){ $i = 1;
$sql .= " $ids = $database->get_col("SELECT id FROM images ORDER BY images.id ASC");
UPDATE ".$tbl." foreach($ids as $id){
SET image_id=".$i." $sql = "SET FOREIGN_KEY_CHECKS=0;
WHERE image_id=".$xid.";"; UPDATE images SET id={$i} WHERE image_id={$id};";
}
/*foreach($score_log as $sl){ foreach($tables as $table){
//This seems like a bad idea. $sql .= "UPDATE {$table} SET image_id={$i} WHERE image_id={$id};";
//TODO: Might be better for log_info to have an $id option (which would then affix the id to the table?) }
preg_replace(".Image \\#[0-9]+.", "Image #".$i, $sl);
}*/ $sql .= " SET FOREIGN_KEY_CHECKS=1;";
$sql .= " SET FOREIGN_KEY_CHECKS=1;"; $database->execute($sql);
$database->execute($sql);
} $i++;
$count = (count($image)) + 1; }
$database->execute("ALTER TABLE images AUTO_INCREMENT=".$count); $database->execute("ALTER TABLE images AUTO_INCREMENT=".(count($ids) + 1));
}elseif($matches['proto'] == "pgsql"){
//TODO: Make this work with PostgreSQL
}elseif($matches['proto'] == "sqlite"){
//TODO: Make this work with SQLite
}
return true; return true;
} }
} }