From fea2a4efd1ad085def7cf84cc444325d0815b62e Mon Sep 17 00:00:00 2001 From: mistic100 Date: Sat, 24 May 2014 14:18:04 +0000 Subject: feature 3077 : improve cache invalidation - add "lastmodified" automatic field for categories, groups, users, tags and images tables - provide a "server key" to the client cache manager git-svn-id: http://piwigo.org/svn/trunk@28532 68402e56-0260-453c-a942-63ccdbb3a9ee --- admin/batch_manager_global.php | 11 ++- admin/batch_manager_unit.php | 5 +- admin/cat_perm.php | 6 +- admin/include/functions.php | 61 +++++++++++++--- admin/picture_modify.php | 6 +- admin/themes/default/js/LocalStorageCache.js | 22 +++--- .../default/template/batch_manager_global.tpl | 35 +++++---- .../themes/default/template/batch_manager_unit.tpl | 35 +++++---- admin/themes/default/template/cat_perm.tpl | 84 ++++++++++++++++------ admin/themes/default/template/picture_modify.tpl | 49 +++++++------ install/db/141-database.php | 45 ++++++++++++ install/piwigo_structure-mysql.sql | 20 ++++-- 12 files changed, 263 insertions(+), 116 deletions(-) create mode 100644 install/db/141-database.php diff --git a/admin/batch_manager_global.php b/admin/batch_manager_global.php index 57cb2acdf..4bec501db 100644 --- a/admin/batch_manager_global.php +++ b/admin/batch_manager_global.php @@ -701,12 +701,11 @@ SELECT id,path,representative_ext,file,filesize,level,name,width,height,rotation $template->assign('thumb_params', $thumb_params); } -$template->assign( - array( - 'nb_thumbs_page' => $nb_thumbs_page, - 'nb_thumbs_set' => count($page['cat_elements_id']), - ) - ); +$template->assign(array( + 'nb_thumbs_page' => $nb_thumbs_page, + 'nb_thumbs_set' => count($page['cat_elements_id']), + 'CACHE_KEYS' => get_admin_client_cache_keys(array('tags')) + )); trigger_action('loc_end_element_set_global'); diff --git a/admin/batch_manager_unit.php b/admin/batch_manager_unit.php index bbbe4810c..05424dccf 100644 --- a/admin/batch_manager_unit.php +++ b/admin/batch_manager_unit.php @@ -248,7 +248,10 @@ SELECT )); } - $template->assign('ELEMENT_IDS', implode(',', $element_ids)); + $template->assign(array( + 'ELEMENT_IDS' => implode(',', $element_ids), + 'CACHE_KEYS' => get_admin_client_cache_keys(array('tags')), + )); } trigger_action('loc_end_element_set_unit'); diff --git a/admin/cat_perm.php b/admin/cat_perm.php index 71653d9ec..c42b3eaa2 100644 --- a/admin/cat_perm.php +++ b/admin/cat_perm.php @@ -298,7 +298,11 @@ SELECT user_id, group_id // +-----------------------------------------------------------------------+ // | sending html code | // +-----------------------------------------------------------------------+ -$template->assign(array('PWG_TOKEN' => get_pwg_token(), 'INHERIT' => $conf['inheritance_by_default'])); +$template->assign(array( + 'PWG_TOKEN' => get_pwg_token(), + 'INHERIT' => $conf['inheritance_by_default'], + 'CACHE_KEYS' => get_admin_client_cache_keys(array('groups', 'users')), + )); $template->assign_var_from_handle('ADMIN_CONTENT', 'cat_perm'); ?> diff --git a/admin/include/functions.php b/admin/include/functions.php index 384189408..a3778f595 100644 --- a/admin/include/functions.php +++ b/admin/include/functions.php @@ -400,13 +400,8 @@ function delete_orphan_tags() { $orphan_tag_ids[] = $tag['id']; } - - $query = ' -DELETE - FROM '.TAGS_TABLE.' - WHERE id IN ('.implode(',', $orphan_tag_ids).') -;'; - pwg_query($query); + + delete_tags($orphan_tag_ids); } } @@ -2733,4 +2728,54 @@ function deltree($path, $trash_path=null) } } -?> \ No newline at end of file +/** + * Returns keys to identify the state of main tables. A key consists of the + * last modification timestamp and the total of items (separated by a _). + * Additionally returns the hash of root path. + * Used to invalidate LocalStorage cache on admin pages. + * + * @param string|string[] list of keys to retrieve (categories,groups,images,tags,users) + * @return string[] + */ +function get_admin_client_cache_keys($requested=array()) +{ + $tables = array( + 'categories' => CATEGORIES_TABLE, + 'groups' => GROUPS_TABLE, + 'images' => IMAGES_TABLE, + 'tags' => TAGS_TABLE, + 'users' => USER_INFOS_TABLE + ); + + if (!is_array($requested)) + { + $requested = array($requested); + } + if (empty($requested)) + { + $requested = array_keys($tables); + } + else + { + $requested = array_intersect($requested, array_keys($tables)); + } + + $keys = array( + '_hash' => md5(get_absolute_root_url()), + ); + + foreach ($requested as $item) + { + $query = ' +SELECT CONCAT( + UNIX_TIMESTAMP(MAX(lastmodified)), + "_", + COUNT(*) + ) + FROM '. $tables[$item] .' +;'; + list($keys[$item]) = pwg_db_fetch_row(pwg_query($query)); + } + + return $keys; +} \ No newline at end of file diff --git a/admin/picture_modify.php b/admin/picture_modify.php index 1b99e5f55..370d64caf 100644 --- a/admin/picture_modify.php +++ b/admin/picture_modify.php @@ -408,7 +408,11 @@ SELECT id ;'; $associate_options_selected = query2array($query, null, 'id'); -$template->assign(compact('associate_options_selected', 'represent_options_selected')); +$template->assign(array( + 'associate_options_selected' => $associate_options_selected, + 'represent_options_selected' => $represent_options_selected, + 'CACHE_KEYS' => get_admin_client_cache_keys(array('tags', 'categories')), + )); trigger_action('loc_end_picture_modify'); diff --git a/admin/themes/default/js/LocalStorageCache.js b/admin/themes/default/js/LocalStorageCache.js index 49a4fa98d..c18171efc 100644 --- a/admin/themes/default/js/LocalStorageCache.js +++ b/admin/themes/default/js/LocalStorageCache.js @@ -1,7 +1,8 @@ -var LocalStorageCache = function(key, lifetime, loader) { - this.key = key; - this.lifetime = lifetime*1000; - this.loader = loader; +var LocalStorageCache = function(options) { + this.key = options.key + '-' + options.serverId; + this.serverKey = options.serverKey; + this.lifetime = options.lifetime ? options.lifetime*1000 : 3600*1000; + this.loader = options.loader; this.storage = window.localStorage; this.ready = !!this.storage; @@ -14,28 +15,23 @@ LocalStorageCache.prototype.get = function(callback) { if (this.ready && this.storage[this.key] != undefined) { var cache = JSON.parse(this.storage[this.key]); - if (now - cache.timestamp <= this.lifetime) { + if (now - cache.timestamp <= this.lifetime && cache.key == this.serverKey) { callback(cache.data); return; } } this.loader(function(data) { - if (that.ready) { - that.storage[that.key] = JSON.stringify({ - timestamp: now, - data: data - }); - } - + that.set.call(that, data); callback(data); }); }; LocalStorageCache.prototype.set = function(data) { if (this.ready) { - that.storage[that.key] = JSON.stringify({ + this.storage[this.key] = JSON.stringify({ timestamp: new Date().getTime(), + key: this.serverKey, data: data }); } diff --git a/admin/themes/default/template/batch_manager_global.tpl b/admin/themes/default/template/batch_manager_global.tpl index 3e22edb02..087f9916a 100644 --- a/admin/themes/default/template/batch_manager_global.tpl +++ b/admin/themes/default/template/batch_manager_global.tpl @@ -68,16 +68,22 @@ jQuery(document).ready(function() {ldelim} jQuery("a.preview-box").colorbox(); {* *} - var tagsCache = new LocalStorageCache('tagsAdminList', 5*60, function(callback) { - jQuery.getJSON('{$ROOT_URL}ws.php?format=json&method=pwg.tags.getAdminList', function(data) { - var tags = data.result.tags; - - for (var i=0, l=tags.length; i *} -var tagsCache = new LocalStorageCache('tagsAdminList', 5*60, function(callback) { - jQuery.getJSON('{$ROOT_URL}ws.php?format=json&method=pwg.tags.getAdminList', function(data) { - var tags = data.result.tags; - - for (var i=0, l=tags.length; i *} -var groupsCache = new LocalStorageCache('groupsAdminList', 5*60, function(callback) { - jQuery.getJSON('{$ROOT_URL}ws.php?format=json&method=pwg.groups.getList&per_page=99999', function(data) { - callback(data.result.groups); - }); +var groupsCache = new LocalStorageCache({ + key: 'groupsAdminList', + serverKey: '{$CACHE_KEYS.groups}', + serverId: '{$CACHE_KEYS._hash}', + + loader: function(callback) { + jQuery.getJSON('{$ROOT_URL}ws.php?format=json&method=pwg.groups.getList&per_page=99999', function(data) { + callback(data.result.groups); + }); + } }); jQuery('[data-selectize=groups]').selectize({ @@ -32,22 +38,28 @@ groupsCache.get(function(groups) { }); {* *} -var usersCache = new LocalStorageCache('usersAdminList', 5*60, function(callback) { - var page = 0, - users = []; - - (function load(page){ - jQuery.getJSON('{$ROOT_URL}ws.php?format=json&method=pwg.users.getList&display=username&per_page=99999&page='+ page, function(data) { - users = users.concat(data.result.users); - - if (data.result.paging.count == data.result.paging.per_page) { - load(++page); - } - else { - callback(users); - } - }); - }(page)); +var usersCache = new LocalStorageCache({ + key: 'usersAdminList', + serverKey: '{$CACHE_KEYS.users}', + serverId: '{$CACHE_KEYS._hash}', + + loader: function(callback) { + var users = []; + + // recursive loader + (function load(page){ + jQuery.getJSON('{$ROOT_URL}ws.php?format=json&method=pwg.users.getList&display=username&per_page=99999&page='+ page, function(data) { + users = users.concat(data.result.users); + + if (data.result.paging.count == data.result.paging.per_page) { + load(++page); + } + else { + callback(users); + } + }); + }(0)); + } }); jQuery('[data-selectize=users]').selectize({ @@ -68,6 +80,29 @@ usersCache.get(function(users) { }, this)); }); }); + +{* *} +function checkStatusOptions() { + if (jQuery("input[name=status]:checked").val() == "private") { + jQuery("#privateOptions, #applytoSubAction").show(); + } + else { + jQuery("#privateOptions, #applytoSubAction").hide(); + } +} + +checkStatusOptions(); +jQuery("#selectStatus").change(function() { + checkStatusOptions(); +}); + +{if isset($nb_users_granted_indirect) && $nb_users_granted_indirect>0} + jQuery(".toggle-indirectPermissions").click(function(e){ + jQuery(".toggle-indirectPermissions").toggle(); + jQuery("#indirectPermissionsDetails").toggle(); + e.preventDefault(); + }); +{/if} }()); {/footer_script} @@ -111,8 +146,8 @@ usersCache.get(function(users) { {if isset($nb_users_granted_indirect) && $nb_users_granted_indirect>0}

{'%u users have automatic permission because they belong to a granted group.'|@translate:$nb_users_granted_indirect} - - {'show details'|@translate} + + {'show details'|@translate}