From c121640b6d64bb4f1bb4b7ca2b96c70635db1c21 Mon Sep 17 00:00:00 2001 From: rvelices Date: Sun, 26 May 2013 19:56:31 +0000 Subject: bug 2097: wrong number of sub-albums bug 2098: make number of direct sub-albums available for each user git-svn-id: http://piwigo.org/svn/trunk@22879 68402e56-0260-453c-a942-63ccdbb3a9ee --- include/category_cats.inc.php | 17 +++-- include/functions_user.inc.php | 125 ++++++++++++++----------------------- install/db/136-database.php | 37 +++++++++++ install/piwigo_structure-mysql.sql | 1 + 4 files changed, 92 insertions(+), 88 deletions(-) create mode 100644 install/db/136-database.php diff --git a/include/category_cats.inc.php b/include/category_cats.inc.php index cbdbe47b7..d96903700 100644 --- a/include/category_cats.inc.php +++ b/include/category_cats.inc.php @@ -37,6 +37,7 @@ SELECT date_last, max_date_last, count_images, + nb_categories, count_categories FROM '.CATEGORIES_TABLE.' c INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' ucc @@ -119,7 +120,7 @@ SELECT representative_picture_id { if ($conf['representative_cache_on_subcats'] and $row['user_representative_picture_id'] != $image_id) { - $user_representative_updates_for[ $user['id'].'#'.$row['id'] ] = $image_id; + $user_representative_updates_for[ $row['id'] ] = $image_id; } $row['representative_picture_id'] = $image_id; @@ -211,7 +212,7 @@ SELECT * if ($conf['representative_cache_on_level']) { - $user_representative_updates_for[ $user['id'].'#'.$category['id'] ] = $image_id; + $user_representative_updates_for[ $category['id'] ] = $image_id; } $category['representative_picture_id'] = $image_id; @@ -246,18 +247,14 @@ if (count($user_representative_updates_for)) { $updates = array(); - foreach ($user_representative_updates_for as $user_cat => $image_id) + foreach ($user_representative_updates_for as $cat_id => $image_id) { - list($user_id, $cat_id) = explode('#', $user_cat); - - array_push( - $updates, + $updates[] = array( - 'user_id' => $user_id, + 'user_id' => $user['id'], 'cat_id' => $cat_id, 'user_representative_picture_id' => $image_id, - ) - ); + ); } mass_updates( diff --git a/include/functions_user.inc.php b/include/functions_user.inc.php index 145d00954..a5d57daeb 100644 --- a/include/functions_user.inc.php +++ b/include/functions_user.inc.php @@ -410,7 +410,7 @@ SELECT COUNT(DISTINCT(image_id)) as total if ($cat['count_images']==0) { $forbidden_ids[] = $cat['cat_id']; - unset( $user_cache_cats[$cat['cat_id']] ); + remove_computed_category($user_cache_cats, $cat); } } if ( !empty($forbidden_ids) ) @@ -441,7 +441,7 @@ DELETE FROM '.USER_CACHE_CATEGORIES_TABLE.' array ( 'user_id', 'cat_id', - 'date_last', 'max_date_last', 'nb_images', 'count_images', 'count_categories' + 'date_last', 'max_date_last', 'nb_images', 'count_images', 'nb_categories', 'count_categories' ), $user_cache_cats, array('ignore' => true) @@ -602,75 +602,29 @@ SELECT id return implode(',', $forbidden_array); } -/** - * compute data of categories branches (one branch only) - */ -function compute_branch_cat_data(&$cats, &$list_cat_id, &$level, &$ref_level) + +/*update counters with a category removal*/ +function remove_computed_category(&$cats, $cat) { - $date = ''; - $count_images = 0; - $count_categories = 0; - do + if ( isset( $cats[$cat['id_uppercat']] ) ) { - $cat_id = array_pop($list_cat_id); - if (!is_null($cat_id)) - { - // Count images and categories - $cats[$cat_id]['count_images'] += $count_images; - $cats[$cat_id]['count_categories'] += $count_categories; - $count_images = $cats[$cat_id]['count_images']; - $count_categories = $cats[$cat_id]['count_categories'] + 1; + $parent = & $cats[ $cat['id_uppercat'] ]; + $parent['nb_categories']--; - if ((empty($cats[$cat_id]['max_date_last'])) or ($cats[$cat_id]['max_date_last'] < $date)) - { - $cats[$cat_id]['max_date_last'] = $date; - } - else - { - $date = $cats[$cat_id]['max_date_last']; - } - $ref_level = substr_count($cats[$cat_id]['global_rank'], '.') + 1; - } - else + do { - $ref_level = 0; - } - } while ($level <= $ref_level); - // Last cat updating must be added to list for next branch - if ($ref_level <> 0) - { - $list_cat_id[] = $cat_id; - } -} - -/** - * compute data of categories branches - */ -function compute_categories_data(&$cats) -{ - $ref_level = 0; - $level = 0; - $list_cat_id = array(); + $parent['count_images'] -= $cat['nb_images']; + $parent['count_categories'] -= 1+$cat['count_categories']; - foreach ($cats as $id => $category) - { - // Compute - $level = substr_count($category['global_rank'], '.') + 1; - if ($level > $ref_level) - { - $list_cat_id[] = $id; - } - else - { - compute_branch_cat_data($cats, $list_cat_id, $level, $ref_level); - $list_cat_id[] = $id; + if ( !isset( $cats[$parent['id_uppercat']] ) ) + break; + $parent = & $cats[$parent['id_uppercat']]; } - $ref_level = $level; + while (true); } - $level = 1; - compute_branch_cat_data($cats, $list_cat_id, $level, $ref_level); + unset($cats[$cat['cat_id']]); } /** @@ -682,7 +636,7 @@ function compute_categories_data(&$cats) */ function get_computed_categories(&$userdata, $filter_days=null) { - $query = 'SELECT c.id AS cat_id, global_rank'; + $query = 'SELECT c.id AS cat_id, id_uppercat'; // Count by date_available to avoid count null $query .= ', MAX(date_available) AS date_last, COUNT(date_available) AS nb_images @@ -713,6 +667,7 @@ FROM '.CATEGORIES_TABLE.' as c while ($row = pwg_db_fetch_assoc($result)) { $row['user_id'] = $userdata['id']; + $row['nb_categories'] = 0; $row['count_categories'] = 0; $row['count_images'] = (int)$row['nb_images']; $row['max_date_last'] = $row['date_last']; @@ -721,30 +676,44 @@ FROM '.CATEGORIES_TABLE.' as c $userdata['last_photo_date'] = $row['date_last']; } - $cats += array($row['cat_id'] => $row); + $cats[$row['cat_id']] = $row; } - uasort($cats, 'global_rank_compare'); - compute_categories_data($cats); + foreach ($cats as $cat) + { + if ( !isset( $cat['id_uppercat'] ) ) + continue; + + $parent = & $cats[ $cat['id_uppercat'] ]; + $parent['nb_categories']++; + + do + { + $parent['count_images'] += $cat['nb_images']; + $parent['count_categories']++; + + if ((empty($parent['max_date_last'])) or ($parent['max_date_last'] < $cat['date_last'])) + { + $parent['max_date_last'] = $cat['date_last']; + } + + if ( !isset( $parent['id_uppercat'] ) ) + break; + $parent = & $cats[$parent['id_uppercat']]; + } + while (true); + unset($parent); + } if ( isset($filter_days) ) { - $cat_tmp = $cats; - $cats = array(); - - foreach ($cat_tmp as $category) + foreach ($cats as $category) { - if (!empty($category['max_date_last'])) + if (empty($category['max_date_last'])) { - // Re-init counters - $category['count_categories'] = 0; - $category['count_images'] = (int)$category['nb_images']; - // Keep category - $cats[$category['cat_id']] = $category; + remove_computed_category($cats, $category); } } - // Compute a second time - compute_categories_data($cats); } return $cats; } diff --git a/install/db/136-database.php b/install/db/136-database.php new file mode 100644 index 000000000..739639d7d --- /dev/null +++ b/install/db/136-database.php @@ -0,0 +1,37 @@ + \ No newline at end of file diff --git a/install/piwigo_structure-mysql.sql b/install/piwigo_structure-mysql.sql index 3de3004ac..d161d40e3 100644 --- a/install/piwigo_structure-mysql.sql +++ b/install/piwigo_structure-mysql.sql @@ -372,6 +372,7 @@ CREATE TABLE `piwigo_user_cache_categories` ( `max_date_last` datetime default NULL, `nb_images` mediumint(8) unsigned NOT NULL default '0', `count_images` mediumint(8) unsigned default '0', + `nb_categories` mediumint(8) unsigned default '0', `count_categories` mediumint(8) unsigned default '0', `user_representative_picture_id` mediumint(8) unsigned default NULL, PRIMARY KEY (`user_id`,`cat_id`) -- cgit v1.2.3