fixes #291, add function categories_integrity

to check and repair integrity on categories. For example,
image_category.category_id should always be linked to a categorie.id,
if not we delete the orphan line.
This commit is contained in:
plegall
2017-03-02 12:27:31 +01:00
parent c594112746
commit ea7e6d76e6
2 changed files with 40 additions and 1 deletions

View File

@@ -536,7 +536,6 @@ SELECT
LEFT JOIN '.IMAGES_TABLE.' ON id = image_id
WHERE id IS NULL
;';
$result = pwg_query($query);
$orphan_image_ids = query2array($query, null, 'image_id');
if (count($orphan_image_ids) > 0)
@@ -550,6 +549,45 @@ DELETE
}
}
/**
* Checks and repairs integrity on categories.
* Removes all entries from related tables which correspond to a deleted category.
*/
function categories_integrity()
{
$related_columns = array(
IMAGE_CATEGORY_TABLE.'.category_id',
USER_ACCESS_TABLE.'.cat_id',
GROUP_ACCESS_TABLE.'.cat_id',
OLD_PERMALINKS_TABLE.'.cat_id',
USER_CACHE_CATEGORIES_TABLE.'.cat_id',
);
foreach ($related_columns as $fullcol)
{
list($table, $column) = explode('.', $fullcol);
$query = '
SELECT
'.$column.'
FROM '.$table.'
LEFT JOIN '.CATEGORIES_TABLE.' ON id = '.$column.'
WHERE id IS NULL
;';
$orphans = array_unique(query2array($query, null, $column));
if (count($orphans) > 0)
{
$query = '
DELETE
FROM '.$table.'
WHERE '.$column.' IN ('.implode(',', $orphans).')
;';
pwg_query($query);
}
}
}
/**
* Returns an array containing sub-directories which are potentially
* a category.