Skip to content

Commit

Permalink
bug 2097: wrong number of sub-albums
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rvelices committed May 26, 2013
1 parent d3ebc32 commit c121640
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 88 deletions.
17 changes: 7 additions & 10 deletions include/category_cats.inc.php
Expand Up @@ -37,6 +37,7 @@
date_last,
max_date_last,
count_images,
nb_categories,
count_categories
FROM '.CATEGORIES_TABLE.' c
INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' ucc
Expand Down Expand Up @@ -119,7 +120,7 @@
{
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;
Expand Down Expand Up @@ -211,7 +212,7 @@

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;
Expand Down Expand Up @@ -246,18 +247,14 @@
{
$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(
Expand Down
125 changes: 47 additions & 78 deletions include/functions_user.inc.php
Expand Up @@ -410,7 +410,7 @@ function getuserdata($user_id, $use_cache)
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) )
Expand Down Expand Up @@ -441,7 +441,7 @@ function getuserdata($user_id, $use_cache)
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)
Expand Down Expand Up @@ -602,75 +602,29 @@ function calculate_permissions($user_id, $user_status)
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']]);
}

/**
Expand All @@ -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
Expand Down Expand Up @@ -713,6 +667,7 @@ function get_computed_categories(&$userdata, $filter_days=null)
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'];
Expand All @@ -721,30 +676,44 @@ function get_computed_categories(&$userdata, $filter_days=null)
$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;
}
Expand Down
37 changes: 37 additions & 0 deletions install/db/136-database.php
@@ -0,0 +1,37 @@
<?php
// +-----------------------------------------------------------------------+
// | Piwigo - a PHP based photo gallery |
// +-----------------------------------------------------------------------+
// | Copyright(C) 2008-2013 Piwigo Team http://piwigo.org |
// | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net |
// | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick |
// +-----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify |
// | it under the terms of the GNU General Public License as published by |
// | the Free Software Foundation |
// | |
// | This program is distributed in the hope that it will be useful, but |
// | WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
// | General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program; if not, write to the Free Software |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA. |
// +-----------------------------------------------------------------------+

defined('PHPWG_ROOT_PATH') or die('Hacking attempt!');

$upgrade_description = 'add nb direct child categories';

$query = '
ALTER TABLE '.USER_CACHE_CATEGORIES_TABLE.'
ADD COLUMN nb_categories mediumint(8) unsigned NOT NULL default 0 AFTER count_images';
pwg_query($query);

invalidate_user_cache();


echo "\n".$upgrade_description."\n";
?>
1 change: 1 addition & 0 deletions install/piwigo_structure-mysql.sql
Expand Up @@ -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`)
Expand Down

0 comments on commit c121640

Please sign in to comment.