aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/category_cats.inc.php17
-rw-r--r--include/functions_user.inc.php125
-rw-r--r--install/db/136-database.php37
-rw-r--r--install/piwigo_structure-mysql.sql1
4 files changed, 92 insertions, 88 deletions
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 @@
+<?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";
+?> \ 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`)