bug 2825: add maintenance action to clean the database and avoid integrity errors

git-svn-id: http://piwigo.org/svn/trunk@20544 68402e56-0260-453c-a942-63ccdbb3a9ee
This commit is contained in:
plegall
2013-02-04 11:28:03 +00:00
parent b39b4e0f39
commit b267aea0e2
5 changed files with 40 additions and 1 deletions
+28
View File
@@ -484,6 +484,34 @@ SELECT DISTINCT id
}
}
/**
* check and repair images integrity
*
* TODO see delete_elements function to check all linked tables
*/
function images_integrity()
{
$query = '
SELECT
image_id
FROM '.IMAGE_CATEGORY_TABLE.'
LEFT JOIN '.IMAGES_TABLE.' ON id = image_id
WHERE id IS NULL
;';
$result = pwg_query($query);
$orphan_image_ids = array_from_query($query, 'image_id');
if (count($orphan_image_ids) > 0)
{
$query = '
DELETE
FROM '.IMAGE_CATEGORY_TABLE.'
WHERE image_id IN ('.implode(',', $orphan_image_ids).')
;';
pwg_query($query);
}
}
/**
* returns an array containing sub-directories which can be a category,
* recursive by default