function image_update_4 in Image 5
Same name and namespace in other branches
- 5.2 image.install \image_update_4()
- 6 image.install \image_update_4()
- 7 image.install \image_update_4()
Clean up all the records that aren't in the files directory.
File
- ./
image.install, line 82
Code
function image_update_4() {
$ret = array();
// Locate image files that aren't stored in the files directory.
$files_path = rtrim(file_directory_path(), '\\/');
$result = db_query("SELECT f.nid, f.fid, f.filename, f.filepath FROM {files} f INNER JOIN {node} n ON f.nid = n.nid WHERE n.type = 'image' AND f.filename = '_original' AND NOT f.filepath LIKE '%s/%%'", $files_path);
while ($file = db_fetch_object($result)) {
$file->filepath = file_create_path($file->filepath);
if (file_exists($file->filepath)) {
// File exists, make sure there's not a duplicate record.
if (db_result(db_query("SELECT COUNT(*) FROM {files} f INNER JOIN {node} n ON f.nid = n.nid WHERE n.type = 'image' AND filepath = '%s' AND fid <> %d", $file->filepath, $file->fid))) {
$ret[] = update_sql("DELETE FROM {files} WHERE fid = " . (int) $file->fid);
$ret[] = update_sql("DELETE FROM {file_revisions} WHERE fid = " . (int) $file->fid);
}
else {
$ret[] = update_sql("UPDATE {files} SET filepath = '" . db_escape_string($file->filepath) . "' WHERE fid = " . (int) $file->fid);
}
}
else {
$ret[] = update_sql("DELETE FROM {files} WHERE fid = " . (int) $file->fid);
$ret[] = update_sql("DELETE FROM {file_revisions} WHERE fid = " . (int) $file->fid);
}
}
// Check for and remove {files} with duplicate filenames.
$result = db_query("SELECT f1.fid, f1.nid, f1.filepath FROM {files} f1 INNER JOIN {node} n ON f1.nid = n.nid WHERE n.type = 'image' AND EXISTS ( SELECT * FROM {files} f2 WHERE f2.filepath = f1.filepath AND f1.fid <> f2.fid AND f1.fid < f2.fid )");
while ($file = db_fetch_object($result)) {
$ret[] = update_sql("DELETE FROM {files} WHERE fid = " . (int) $file->fid);
}
// Delete rows from {file_revisions} that don't have matching {files}.
$ret[] = update_sql("DELETE FROM {file_revisions} WHERE NOT EXISTS (SELECT * FROM {files} WHERE {files}.fid = {file_revisions}.fid)");
return $ret;
}