From f9f077bb15a6789946af6806f04f0db63da692b3 Mon Sep 17 00:00:00 2001 From: plegall Date: Thu, 23 Jun 2016 12:37:43 +0200 Subject: fixes #452, update images.lastmodified when changing tag list --- admin/batch_manager_global.php | 8 ++- admin/include/functions.php | 119 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 1 deletion(-) diff --git a/admin/batch_manager_global.php b/admin/batch_manager_global.php index 1d5cb747d..332f3f7a7 100644 --- a/admin/batch_manager_global.php +++ b/admin/batch_manager_global.php @@ -123,6 +123,8 @@ DELETE { if (isset($_POST['del_tags']) and count($_POST['del_tags']) > 0) { + $taglist_before = get_image_tag_ids($collection); + $query = ' DELETE FROM '.IMAGE_TAG_TABLE.' @@ -130,7 +132,11 @@ DELETE AND tag_id IN ('.implode(',', $_POST['del_tags']).') ;'; pwg_query($query); - + + $taglist_after = get_image_tag_ids($collection); + $images_to_update = compare_image_tag_lists($taglist_before, $taglist_after); + update_images_lastmodified($images_to_update); + if (isset($_SESSION['bulk_manager_filter']['tags']) && count(array_intersect($_SESSION['bulk_manager_filter']['tags'], $_POST['del_tags']))) { diff --git a/admin/include/functions.php b/admin/include/functions.php index 9a827d1d6..030e7fa31 100644 --- a/admin/include/functions.php +++ b/admin/include/functions.php @@ -1519,6 +1519,8 @@ function add_tags($tags, $images) return; } + $taglist_before = get_image_tag_ids($images); + // we can't insert twice the same {image_id,tag_id} so we must first // delete lines we'll insert later $query = ' @@ -1545,6 +1547,11 @@ DELETE array_keys($inserts[0]), $inserts ); + + $taglist_after = get_image_tag_ids($images); + $images_to_update = compare_image_tag_lists($taglist_before, $taglist_after); + update_images_lastmodified($images_to_update); + invalidate_user_cache_nb_tags(); } @@ -1565,6 +1572,15 @@ function delete_tags($tag_ids) return false; } + // we need the list of impacted images, to update their lastmodified + $query = ' +SELECT + image_id + FROM '.IMAGE_TAG_TABLE.' + WHERE tag_id IN ('.implode(',', $tag_ids).') +;'; + $image_ids = query2array($query, null, 'image_id'); + $query = ' DELETE FROM '.IMAGE_TAG_TABLE.' @@ -1579,6 +1595,7 @@ DELETE ;'; pwg_query($query); + update_images_lastmodified($image_ids); invalidate_user_cache_nb_tags(); } @@ -1662,6 +1679,9 @@ function set_tags_of($tags_of) { if (count($tags_of) > 0) { + $taglist_before = get_image_tag_ids(array_keys($tags_of)); + global $logger; $logger->debug('taglist_before', $taglist_before); + $query = ' DELETE FROM '.IMAGE_TAG_TABLE.' @@ -1691,10 +1711,82 @@ DELETE ); } + $taglist_after = get_image_tag_ids(array_keys($tags_of)); + global $logger; $logger->debug('taglist_after', $taglist_after); + $images_to_update = compare_image_tag_lists($taglist_before, $taglist_after); + global $logger; $logger->debug('$images_to_update', $images_to_update); + + update_images_lastmodified($images_to_update); invalidate_user_cache_nb_tags(); } } +/** + * Get list of tag ids for each image. Returns an empty list if the image has + * no tags. + * + * @since 2.9 + * @param array $image_ids + * @return associative array, image_id => list of tag ids + */ +function get_image_tag_ids($image_ids) +{ + if (!is_array($image_ids) and is_int($image_ids)) + { + $images_ids = array($image_ids); + } + + if (count($image_ids) == 0) + { + return array(); + } + + $query = ' +SELECT + image_id, + tag_id + FROM '.IMAGE_TAG_TABLE.' + WHERE image_id IN ('.implode(',', $image_ids).') +;'; + + $tags_of = array_fill_keys($image_ids, array()); + $image_tags = query2array($query); + foreach ($image_tags as $image_tag) + { + $tags_of[ $image_tag['image_id'] ][] = $image_tag['tag_id']; + } + + return $tags_of; +} + +/** + * Compare the list of tags, for each image. Returns image_ids where tag list has changed. + * + * @since 2.9 + * @param array $taglist_before - for each image_id (key), list of tag ids + * @param array $taglist_after - for each image_id (key), list of tag ids + * @return array - image_ids where the list has changed + */ +function compare_image_tag_lists($taglist_before, $taglist_after) +{ + $images_to_update = array(); + + foreach ($taglist_after as $image_id => $list_after) + { + sort($list_after); + + $list_before = isset($taglist_before[$image_id]) ? $taglist_before[$image_id] : array(); + sort($list_before); + + if ($list_after != $list_before) + { + $images_to_update[] = $image_id; + } + } + + return $images_to_update; +} + /** * Associate a list of images to a list of categories. * The function will not duplicate links and will preserve ranks. @@ -2887,3 +2979,30 @@ function save_images_order($category_id, $images) ); mass_updates(IMAGE_CATEGORY_TABLE, $fields, $datas); } + +/** + * Force update on images.lastmodified column. Useful when modifying the tag + * list. + * + * @since 2.9 + * @param array $image_ids + */ +function update_images_lastmodified($image_ids) +{ + if (!is_array($image_ids) and is_int($image_ids)) + { + $images_ids = array($image_ids); + } + + if (count($image_ids) == 0) + { + return; + } + + $query = ' +UPDATE '.IMAGES_TABLE.' + SET lastmodified = NOW() + WHERE id IN ('.implode(',', $image_ids).') +;'; + pwg_query($query); +} -- cgit v1.2.3