Przeglądaj źródła

Merge branch 'master' of https://github.com/unacms/una

AntonLV 1 rok temu
rodzic
commit
bca503c1c5

+ 3 - 3
inc/utils.inc.php

@@ -2824,7 +2824,7 @@ function bx_content_cache_get($sKey, $iTTL = false)
         return null;
 
     $oCache = bx_content_cache_obj();
-    return $oCache->getData('content_' . $sKey . bx_site_hash() . '.php', $iTTL);
+    return $oCache->getData('content_' . $sKey . '_' . bx_site_hash() . '.php', $iTTL);
 }
 
 function bx_content_cache_set($sKey, $mixedData, $iTTL = false)
@@ -2833,13 +2833,13 @@ function bx_content_cache_set($sKey, $mixedData, $iTTL = false)
         return false;
 
     $oCache = bx_content_cache_obj();
-    return $oCache->setData('content_' . $sKey . bx_site_hash() . '.php', $mixedData, $iTTL);
+    return $oCache->setData('content_' . $sKey . '_' . bx_site_hash() . '.php', $mixedData, $iTTL);
 }
 
 function bx_content_cache_del($sKey)
 {
     $oCache = bx_content_cache_obj();
-    return $oCache->delData('content_' . $sKey . bx_site_hash() . '.php');
+    return $oCache->delData('content_' . $sKey . '_' . bx_site_hash() . '.php');
 }
 
 function bx_content_cache_obj()

+ 3 - 0
install/sql/system.sql

@@ -4770,6 +4770,7 @@ CREATE TABLE IF NOT EXISTS `sys_objects_menu` (
   `template_id` int(11) NOT NULL,
   `config_api` text NOT NULL,
   `persistent` tinyint(4) NOT NULL DEFAULT '0',
+  `cache` enum('off','guest','global','per_user','per_user_auto') NOT NULL DEFAULT 'off',
   `deletable` tinyint(4) NOT NULL DEFAULT '1',
   `active` tinyint(4) NOT NULL DEFAULT '0',
   `override_class_name` varchar(255) NOT NULL,
@@ -4824,6 +4825,8 @@ INSERT INTO `sys_objects_menu` (`object`, `title`, `set_name`, `module`, `templa
 
 ('sys_studio_account_popup', '_sys_menu_title_studio_account_popup', 'sys_studio_account_popup', 'system', 4, 0, 1, 'BxTemplStudioMenuAccountPopup', '');
 
+UPDATE `sys_objects_menu` SET `cache` = 'per_acl' WHERE `object` IN('sys_site', 'sys_account_notifications', 'sys_add_content', 'sys_toolbar_site', 'sys_footer', 'sys_site_submenu', 'sys_homepage_submenu', 'sys_homepage', 'sys_application', 'sys_site_in_panel');
+UPDATE `sys_objects_menu` SET `cache` = 'sys_site' WHERE `object` IN('sys_toolbar_member');
 
 CREATE TABLE IF NOT EXISTS `sys_menu_sets` (
   `set_name` varchar(64) NOT NULL,

+ 2 - 3
modules/boonex/artificer/install/sql/enable.sql

@@ -13,9 +13,8 @@ INSERT INTO `sys_menu_templates` (`id`, `template`, `title`, `visible`) VALUES
 (ROUND(RAND()*(9999 - 1000) + 1000), 'menu_sidebar_site.html', '_bx_artificer_menu_template_title_sidebar_site', 1);
 SET @iTemplId = (SELECT `id` FROM `sys_menu_templates` WHERE `template`='menu_sidebar_site.html' AND `title`='_bx_artificer_menu_template_title_sidebar_site' LIMIT 1);
 
-INSERT INTO `sys_objects_menu`(`object`, `title`, `set_name`, `module`, `template_id`, `deletable`, `active`, `override_class_name`, `override_class_file`) VALUES 
-('bx_artificer_sidebar_site', '_bx_artificer_menu_title_sidebar_site', 'sys_site', @sName, @iTemplId, 0, 1, 'BxTemplMenuSidebarSite', '');
-
+INSERT INTO `sys_objects_menu`(`object`, `title`, `set_name`, `module`, `template_id`, `cache`, `deletable`, `active`, `override_class_name`, `override_class_file`) VALUES 
+('bx_artificer_sidebar_site', '_bx_artificer_menu_title_sidebar_site', 'sys_site', @sName, @iTemplId, 'per_acl', 0, 1, 'BxTemplMenuSidebarSite', '');
 
 -- ALERTS
 INSERT INTO `sys_alerts_handlers` (`name`, `class`, `file`, `service_call`) VALUES 

+ 4 - 2
modules/boonex/profiler/classes/BxProfiler.php

@@ -341,14 +341,16 @@ class BxProfiler extends BxDol
         $this->_aMenus[$sName]['begin'] = microtime ();
     }
 
-    function endMenu ($sName)
+    function endMenu ($sName, $sCacheType = '', $bCacheUsed = false)
     {
         if (!isset($this->_aMenus[$sName]))
             return;
         $iTime = $this->_calcTime ($this->_aMenus[$sName]['begin']);
         unset ($this->_aMenus[$sName]['begin']);
         $this->_aMenus[$sName]['time'] = $this->_formatTime($iTime);
-        $this->_aMenus[$sName]['raw_time'] = $iTime;
+        $this->_aMenus[$sName]['raw_time'] = $iTime;        
+        $this->_aMenus[$sName]['cache type'] = $sCacheType;
+        $this->_aMenus[$sName]['cache used'] = $bCacheUsed ? 'yes' : 'no';
     }
 
     function _getCurrentDelay ()

+ 97 - 57
template/scripts/BxBaseMenu.php

@@ -53,71 +53,111 @@ class BxBaseMenu extends BxDolMenu
      */
     public function getCode ()
     {
+        $bCacheUsed = false;
+        $sRet = '';
         $sMenuTitle = isset($this->_aObject['title']) ? _t($this->_aObject['title']) : 'Menu-' . rand(0, PHP_INT_MAX);
         if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginMenu($sMenuTitle);
 
-        /**
-         * @hooks
-         * @hookdef hook-menu-get_code_before 'menu', 'get_code_before' - hook before menu output
-         * - $unit_name - equals `menu`
-         * - $action - equals `get_code_before` 
-         * - $object_id - not used 
-         * - $sender_id - not used 
-         * - $extra_params - array of additional params with the following array keys:
-         *      - `object_name` - menu object name
-         *      - `object_array` - menu object array
-         *      - `object` - menu object
-         *      - `override_result` - menu code
-         * @hook @ref hook-menu-get_code_before
-         */
-        $mixedRes = null;
-        bx_alert('menu', 'get_code_before', 0, 0, [
-            'object_name' => $this->_sObject, 
-            'object_array' => $this->_aObject, 
-            'object' => $this, 
-            'override_result' => &$mixedRes,
-        ]);
-        if ($mixedRes !== null) {
-            if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endMenu($sMenuTitle);
-            return $mixedRes;
+        $sCacheKey = '';
+        $mixedCache = null;
+        // Use cache only if:
+        //  - cache mode is set, AND
+        //  - it's not disabled (`off`), AND
+        //  - it's not "guest cache" while a user is logged in
+        if (isset($this->_aObject['cache']) && !($this->_aObject['cache'] == 'off' || ($this->_aObject['cache'] == 'guest' && isLogged()))) {
+            switch ($this->_aObject['cache']) {
+                case 'global': // one cache for everyone
+                case 'guest': // one cache for guests only
+                    $sCacheKey = "menu_{$this->_sObject}";
+                    break;                
+                case 'per_user': // separate cache for each user
+                    $sCacheKey = "menu_{$this->_sObject}_p" . bx_get_logged_profile_id();
+                    break;                
+                case 'per_acl': // separate cache for each ACL
+                    $aAcl = BxDolAcl::getInstance()->getMemberMembershipInfo(bx_get_logged_profile_id());
+                    $sCacheKey = "menu_{$this->_sObject}" . "_acl" . $aAcl['id'];
+                    break;
+                default:
+                    trigger_error("Unknown cache mode for menu ({$this->_sObject}): {$this->_aObject['cache']}", E_USER_ERROR);
+                    break;
+            }            
+            $mixedCache = bx_content_cache_get($sCacheKey);
         }
+        if ($mixedCache !== null) {
+            if ($mixedCache)
+                $this->_addJsCss();
+            $bCacheUsed = true;
+            $sRet = $mixedCache;
+        }
+        else {
 
-        if(!$this->_iPageType)
-            $this->_iPageType = BxDolTemplate::getInstance()->getPageType();
+            /**
+             * @hooks
+             * @hookdef hook-menu-get_code_before 'menu', 'get_code_before' - hook before menu output
+             * - $unit_name - equals `menu`
+             * - $action - equals `get_code_before` 
+             * - $object_id - not used 
+             * - $sender_id - not used 
+             * - $extra_params - array of additional params with the following array keys:
+             *      - `object_name` - menu object name
+             *      - `object_array` - menu object array
+             *      - `object` - menu object
+             *      - `override_result` - menu code
+             * @hook @ref hook-menu-get_code_before
+             */
+            $mixedRes = null;
+            bx_alert('menu', 'get_code_before', 0, 0, [
+                'object_name' => $this->_sObject, 
+                'object_array' => $this->_aObject, 
+                'object' => $this, 
+                'override_result' => &$mixedRes,
+            ]);
+            if ($mixedRes !== null) {            
+                $sRet = $mixedRes;
+            }
+            else {
+                if(!$this->_iPageType)
+                    $this->_iPageType = BxDolTemplate::getInstance()->getPageType();
+
+                $s = '';
+                $aVars = $this->_getTemplateVars ();
+                if (!empty($aVars['bx_repeat:menu_items'])) {
+                    $this->_addJsCss();
+                    $s = $this->_getCode($this->_aObject['template'], $aVars);
+                }
+
+                /**
+                 * @hooks
+                 * @hookdef hook-menu-get_code_after 'menu', 'get_code_after' - hook after menu output
+                 * - $unit_name - equals `menu`
+                 * - $action - equals `get_code_after` 
+                 * - $object_id - not used 
+                 * - $sender_id - not used 
+                 * - $extra_params - array of additional params with the following array keys:
+                 *      - `object_name` - menu object name
+                 *      - `object_array` - menu object array
+                 *      - `object` - menu object
+                 *      - `override_result` - menu code
+                 * @hook @ref hook-menu-get_code_after
+                 */
+                $mixedRes = null;
+                bx_alert('menu', 'get_code_after', 0, 0, [
+                    'object_name' => $this->_sObject, 
+                    'object_array' => $this->_aObject, 
+                    'object' => $this, 
+                    'vars' => $aVars,
+                    'override_result' => &$s,
+                ]);
+                $sRet = $s;
+            }
 
-        $s = '';
-        $aVars = $this->_getTemplateVars ();
-        if (!empty($aVars['bx_repeat:menu_items'])) {
-            $this->_addJsCss();
-            $s = $this->_getCode($this->_aObject['template'], $aVars);
+            if ($sCacheKey)
+                bx_content_cache_set($sCacheKey, $sRet);
         }
 
-        /**
-         * @hooks
-         * @hookdef hook-menu-get_code_after 'menu', 'get_code_after' - hook after menu output
-         * - $unit_name - equals `menu`
-         * - $action - equals `get_code_after` 
-         * - $object_id - not used 
-         * - $sender_id - not used 
-         * - $extra_params - array of additional params with the following array keys:
-         *      - `object_name` - menu object name
-         *      - `object_array` - menu object array
-         *      - `object` - menu object
-         *      - `override_result` - menu code
-         * @hook @ref hook-menu-get_code_after
-         */
-        $mixedRes = null;
-        bx_alert('menu', 'get_code_after', 0, 0, [
-            'object_name' => $this->_sObject, 
-            'object_array' => $this->_aObject, 
-            'object' => $this, 
-            'vars' => $aVars,
-            'override_result' => &$s,
-        ]);
-
-        if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endMenu($sMenuTitle);
-
-        return $s;
+        if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endMenu($sMenuTitle, $this->_aObject['cache'] ?? 'undefined', $bCacheUsed);
+        
+        return $sRet;
     }
 
     /**

+ 0 - 1
template/scripts/BxBaseServiceProfiles.php

@@ -1271,7 +1271,6 @@ class BxBaseServiceProfiles extends BxDol
             'content' => $oTemplate->parseHtmlByName('profile_switch_row.html', $aVars),
         );
         if ($sUrlProfileAction == '' && $bShowAll == 0 && $sButtonTitle == '' && $sProfileTemplate == '') {
-            echoDbgLog('set: ' . $sCacheKey);
             bx_content_cache_set($sCacheKey, $a);
         }
         return $a;