| 1 | ################################################################################
|
|---|
| 2 | # _cachemanager.py
|
|---|
| 3 | #
|
|---|
| 4 | # This program is free software; you can redistribute it and/or
|
|---|
| 5 | # modify it under the terms of the GNU General Public License
|
|---|
| 6 | # as published by the Free Software Foundation; either version 2
|
|---|
| 7 | # of the License, or (at your option) any later version.
|
|---|
| 8 | #
|
|---|
| 9 | # This program is distributed in the hope that it will be useful,
|
|---|
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | # GNU General Public License for more details.
|
|---|
| 13 | #
|
|---|
| 14 | # You should have received a copy of the GNU General Public License
|
|---|
| 15 | # along with this program; if not, write to the Free Software
|
|---|
| 16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|---|
| 17 | ################################################################################
|
|---|
| 18 |
|
|---|
| 19 | # Imports.
|
|---|
| 20 | import time
|
|---|
| 21 | import urllib
|
|---|
| 22 | import zExceptions
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 | dct_op = {'index':'','sitemap':'sitemap','index_print':'print'}
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 | # ------------------------------------------------------------------------------
|
|---|
| 29 | # _cachemanager.Empty
|
|---|
| 30 | # ------------------------------------------------------------------------------
|
|---|
| 31 | class Empty: pass
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 | ################################################################################
|
|---|
| 35 | #
|
|---|
| 36 | # STATIC CACHE
|
|---|
| 37 | #
|
|---|
| 38 | ################################################################################
|
|---|
| 39 |
|
|---|
| 40 | # ------------------------------------------------------------------------------
|
|---|
| 41 | # _cachemanager._getIdFromUrl
|
|---|
| 42 | # ------------------------------------------------------------------------------
|
|---|
| 43 | def _getIdFromUrl(REQUEST):
|
|---|
| 44 | id = REQUEST['URL']
|
|---|
| 45 | id = id[:-5]
|
|---|
| 46 | id = id[id.rfind('/')+1:]
|
|---|
| 47 | if id.find('_') < 0 and REQUEST.has_key('lang'):
|
|---|
| 48 | id = '%s_%s'%(id,REQUEST['lang'])
|
|---|
| 49 | return _getCacheId(id)
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 | # ------------------------------------------------------------------------------
|
|---|
| 53 | # _cachemanager._getCacheId
|
|---|
| 54 | # ------------------------------------------------------------------------------
|
|---|
| 55 | def _getCacheId(id):
|
|---|
| 56 | return 'cache_%s_html'%id
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 | # ------------------------------------------------------------------------------
|
|---|
| 60 | # _cachemanager.clearCachePage
|
|---|
| 61 | # ------------------------------------------------------------------------------
|
|---|
| 62 | def _clearCachePage(self, id):
|
|---|
| 63 | count = 0
|
|---|
| 64 | if not self.isPage():
|
|---|
| 65 | return _clearCachePage(self.getParentNode(),id)
|
|---|
| 66 | if id in self.objectIds():
|
|---|
| 67 | self.writeLog( "[_clearCachePage]: Removing ID=%s"%id)
|
|---|
| 68 | self.manage_delObjects(ids=[id])
|
|---|
| 69 | count += 1
|
|---|
| 70 | return count
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 | # ------------------------------------------------------------------------------
|
|---|
| 74 | # _refreshCachePage
|
|---|
| 75 | # ------------------------------------------------------------------------------
|
|---|
| 76 | def _refreshCachePage(self, id, REQUEST):
|
|---|
| 77 | if not self.isPage():
|
|---|
| 78 | return _refreshCachePage(self.getParentNode(),id,REQUEST)
|
|---|
| 79 | _clearCachePage(self,id)
|
|---|
| 80 | self.writeLog( "[_refreshCachePage]: Generating ID=%s"%id)
|
|---|
| 81 | REQUEST.set('ZMS_CACHE',1)
|
|---|
| 82 | title = '*** CACHE [%s] ***'%str(self.getObjProperty( 'attr_cacheable', REQUEST))
|
|---|
| 83 | raw = self.index_html(self,REQUEST)
|
|---|
| 84 | self.manage_addDTMLDocument(id,title,raw)
|
|---|
| 85 | if self.getObjProperty( 'attr_cacheable', REQUEST) in [ 2, '2']:
|
|---|
| 86 | raw = self.dt_html( raw, REQUEST)
|
|---|
| 87 | return raw
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 | ################################################################################
|
|---|
| 91 | #
|
|---|
| 92 | # REQUEST BUFFER
|
|---|
| 93 | #
|
|---|
| 94 | ################################################################################
|
|---|
| 95 |
|
|---|
| 96 | # ------------------------------------------------------------------------------
|
|---|
| 97 | # getReqBuffId:
|
|---|
| 98 | #
|
|---|
| 99 | # Gets buffer-id in Http-Request.
|
|---|
| 100 | #
|
|---|
| 101 | # @throws Exception
|
|---|
| 102 | # ------------------------------------------------------------------------------
|
|---|
| 103 | def getReqBuffId(self, key, REQUEST):
|
|---|
| 104 | id = '%s#%s.%s'%( '/'.join(self.getPhysicalPath()),key,REQUEST.get('lang','*'))
|
|---|
| 105 | return id
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 | # ------------------------------------------------------------------------------
|
|---|
| 109 | # getReqBuff:
|
|---|
| 110 | #
|
|---|
| 111 | # Gets buffer from Http-Request.
|
|---|
| 112 | #
|
|---|
| 113 | # @throws Exception
|
|---|
| 114 | # ------------------------------------------------------------------------------
|
|---|
| 115 | def getReqBuff(self, REQUEST):
|
|---|
| 116 | buff = REQUEST.get('__buff__',None)
|
|---|
| 117 | if buff == None:
|
|---|
| 118 | buff = Empty()
|
|---|
| 119 | return buff
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 | ################################################################################
|
|---|
| 123 | #
|
|---|
| 124 | # ReqBuff
|
|---|
| 125 | #
|
|---|
| 126 | ################################################################################
|
|---|
| 127 | class ReqBuff:
|
|---|
| 128 |
|
|---|
| 129 | # --------------------------------------------------------------------------
|
|---|
| 130 | # fetchReqBuff:
|
|---|
| 131 | #
|
|---|
| 132 | # Fetch buffered value from Http-Request.
|
|---|
| 133 | #
|
|---|
| 134 | # @throws Exception
|
|---|
| 135 | # --------------------------------------------------------------------------
|
|---|
| 136 | def fetchReqBuff(self, key, REQUEST, forced=False):
|
|---|
| 137 | if REQUEST.get('ZMS_FETCH_REQ_BUFF',True):
|
|---|
| 138 | url = REQUEST.get('PSEUDOURL',REQUEST.get('URL','/manage'))
|
|---|
| 139 | url = url[url.rfind('/'):]
|
|---|
| 140 | if forced or not url.find('/manage') >= 0:
|
|---|
| 141 | buff = getReqBuff(self,REQUEST)
|
|---|
| 142 | reqBuffId = getReqBuffId(self,key,REQUEST)
|
|---|
| 143 | try:
|
|---|
| 144 | value = getattr(buff,reqBuffId)
|
|---|
| 145 | return value
|
|---|
| 146 | except:
|
|---|
| 147 | raise zExceptions.InternalError('%s not found in ReqBuff!'%reqBuffId)
|
|---|
| 148 | raise zExceptions.InternalError('ReqBuff is inactive!')
|
|---|
| 149 |
|
|---|
| 150 | # --------------------------------------------------------------------------
|
|---|
| 151 | # storeReqBuff:
|
|---|
| 152 | #
|
|---|
| 153 | # Returns value and stores it in buffer of Http-Request.
|
|---|
| 154 | # --------------------------------------------------------------------------
|
|---|
| 155 | def storeReqBuff(self, key, value, REQUEST):
|
|---|
| 156 | try:
|
|---|
| 157 | buff = getReqBuff(self,REQUEST)
|
|---|
| 158 | reqBuffId = getReqBuffId(self,key,REQUEST)
|
|---|
| 159 | setattr(buff,reqBuffId,value)
|
|---|
| 160 | REQUEST.set('__buff__',buff)
|
|---|
| 161 | except:
|
|---|
| 162 | pass
|
|---|
| 163 | return value
|
|---|
| 164 |
|
|---|
| 165 | # --------------------------------------------------------------------------
|
|---|
| 166 | # clearReqBuff:
|
|---|
| 167 | #
|
|---|
| 168 | # Clears key from buffer of Http-Request.
|
|---|
| 169 | # --------------------------------------------------------------------------
|
|---|
| 170 | def clearReqBuff(self, key, REQUEST):
|
|---|
| 171 | try:
|
|---|
| 172 | buff = getReqBuff(self,REQUEST)
|
|---|
| 173 | reqBuffId = getReqBuffId(self,key,REQUEST)
|
|---|
| 174 | delattr(buff,reqBuffId)
|
|---|
| 175 | REQUEST.set('__buff__',buff)
|
|---|
| 176 | except:
|
|---|
| 177 | pass
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 | ################################################################################
|
|---|
| 181 | ################################################################################
|
|---|
| 182 | ###
|
|---|
| 183 | ### Class CacheableObject:
|
|---|
| 184 | ###
|
|---|
| 185 | ################################################################################
|
|---|
| 186 | ################################################################################
|
|---|
| 187 | class CacheableObject(ReqBuff):
|
|---|
| 188 |
|
|---|
| 189 | # --------------------------------------------------------------------------
|
|---|
| 190 | # CacheableObject.clearCachePage:
|
|---|
| 191 | # --------------------------------------------------------------------------
|
|---|
| 192 | def clearCachePage(self):
|
|---|
| 193 | count = 0
|
|---|
| 194 | for s_lang in self.getLangIds():
|
|---|
| 195 | for s_op in dct_op.keys():
|
|---|
| 196 | id = _getCacheId('%s_%s'%(s_op,s_lang))
|
|---|
| 197 | count += _clearCachePage(self,id)
|
|---|
| 198 | return count
|
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 | # --------------------------------------------------------------------------
|
|---|
| 202 | # CacheableObject.clearCachePages:
|
|---|
| 203 | # --------------------------------------------------------------------------
|
|---|
| 204 | def clearCachePages(self, max_depth=999, cur_depth=0):
|
|---|
| 205 | count = 0
|
|---|
| 206 | if self.isPage():
|
|---|
| 207 | count += self.clearCachePage()
|
|---|
| 208 | if cur_depth < max_depth:
|
|---|
| 209 | for ob in self.getChildNodes():
|
|---|
| 210 | count += ob.clearCachePages( max_depth, cur_depth+1)
|
|---|
| 211 | return count
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 | # --------------------------------------------------------------------------
|
|---|
| 215 | # CacheableObject.synchronizeCachePage:
|
|---|
| 216 | # --------------------------------------------------------------------------
|
|---|
| 217 | def synchronizeCachePage(self, REQUEST):
|
|---|
| 218 | if self.getConfProperty('ZMS.cache.active')==1:
|
|---|
| 219 | s_lang = REQUEST.get('lang',self.getPrimaryLanguage())
|
|---|
| 220 | if self.isPageElement():
|
|---|
| 221 | _clearCachePage(self,_getCacheId('index_%s'%s_lang))
|
|---|
| 222 | _clearCachePage(self,_getCacheId('index_print_%s'%s_lang))
|
|---|
| 223 | elif self.isPage():
|
|---|
| 224 | _clearCachePage(self,_getCacheId('index_%s'%s_lang))
|
|---|
| 225 | _clearCachePage(self,_getCacheId('index_print_%s'%s_lang))
|
|---|
| 226 | _clearCachePage(self,_getCacheId('sitemap_%s'%s_lang))
|
|---|
| 227 | else:
|
|---|
| 228 | self.getParentNode().synchronizeCachePage(REQUEST)
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 | # --------------------------------------------------------------------------
|
|---|
| 232 | # CacheableObject.getCachedPages:
|
|---|
| 233 | # --------------------------------------------------------------------------
|
|---|
| 234 | def getCachedPages(self, REQUEST, max_depth=999, cur_depth=0):
|
|---|
| 235 | count = 0
|
|---|
| 236 | if self.isPage():
|
|---|
| 237 | if self.isCachedPage( REQUEST):
|
|---|
| 238 | self.getCachedPage( REQUEST)
|
|---|
| 239 | count += 1
|
|---|
| 240 | if cur_depth < max_depth:
|
|---|
| 241 | for ob in self.filteredChildNodes( REQUEST, self.PAGES):
|
|---|
| 242 | count += ob.getCachedPages( REQUEST, max_depth, cur_depth+1)
|
|---|
| 243 | return count
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 | # --------------------------------------------------------------------------
|
|---|
| 247 | # CacheableObject.isCachedPage:
|
|---|
| 248 | # --------------------------------------------------------------------------
|
|---|
| 249 | def isCachedPage(self, REQUEST):
|
|---|
| 250 | rtn = True
|
|---|
| 251 | # URL-Params?
|
|---|
| 252 | try: rtn = rtn and not len(filter( lambda x: x != '-C', REQUEST.form.keys())) > 0
|
|---|
| 253 | except: pass
|
|---|
| 254 | if not rtn:
|
|---|
| 255 | return False
|
|---|
| 256 | # Page exists?
|
|---|
| 257 | id = _getIdFromUrl(REQUEST)
|
|---|
| 258 | if id in self.objectIds(['DTML Document']):
|
|---|
| 259 | return True
|
|---|
| 260 | # Page cacheable?
|
|---|
| 261 | if not self.isPage():
|
|---|
| 262 | parent = self.getParentNode()
|
|---|
| 263 | if parent is not None and isinstance( parent, CacheableObject):
|
|---|
| 264 | return parent.isCachedPage(REQUEST)
|
|---|
| 265 | # Page can be cached?
|
|---|
| 266 | found = False
|
|---|
| 267 | for s_lang in self.getLangIds():
|
|---|
| 268 | for s_op in dct_op.keys():
|
|---|
| 269 | if id == _getCacheId('%s_%s'%(s_op,s_lang)):
|
|---|
| 270 | found = True
|
|---|
| 271 | rtn = rtn and found
|
|---|
| 272 | rtn = rtn and self.getConfProperty('ZMS.cache.active')
|
|---|
| 273 | rtn = rtn and self.getObjProperty('attr_cacheable',REQUEST) in [ 1, '1', 2, '2', True]
|
|---|
| 274 | rtn = rtn and not REQUEST.get('preview','')=='preview'
|
|---|
| 275 | rtn = rtn and not REQUEST.get('ZMS_CACHE',0) in [ 1, True]
|
|---|
| 276 | return rtn
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 | # --------------------------------------------------------------------------
|
|---|
| 280 | # CacheableObject.getCachedPage:
|
|---|
| 281 | # --------------------------------------------------------------------------
|
|---|
| 282 | def getCachedPage(self, REQUEST):
|
|---|
| 283 | if not self.isPage():
|
|---|
| 284 | return self.getParentNode().getCachedPage( REQUEST)
|
|---|
| 285 | id = _getIdFromUrl( REQUEST)
|
|---|
| 286 | for ob in self.objectValues( [ 'DTML Document']):
|
|---|
| 287 | if ob.id() == id:
|
|---|
| 288 | raw = ob.raw
|
|---|
| 289 | if self.getObjProperty( 'attr_cacheable', REQUEST) in [ 2, '2']:
|
|---|
| 290 | raw = self.dt_html( raw, REQUEST)
|
|---|
| 291 | return raw
|
|---|
| 292 | return _refreshCachePage( self, id, REQUEST)
|
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 | ############################################################################
|
|---|
| 296 | # CacheableObject.manage_getCachedPages:
|
|---|
| 297 | #
|
|---|
| 298 | # Get cached pages.
|
|---|
| 299 | ############################################################################
|
|---|
| 300 | def manage_getCachedPages(self, lang, REQUEST, RESPONSE):
|
|---|
| 301 | """ CacheableObject.manage_getCachedPages """
|
|---|
| 302 |
|
|---|
| 303 | message = ''
|
|---|
| 304 | t0 = time.time()
|
|---|
| 305 | max_depth = REQUEST.get('attr_cacheable_levels',999)
|
|---|
| 306 |
|
|---|
| 307 | ##### Clear cached pages ####
|
|---|
| 308 | count = self.clearCachePages( max_depth)
|
|---|
| 309 | message += self.getZMILangStr('MSG_DELETED')%count
|
|---|
| 310 |
|
|---|
| 311 | ##### Get cached pages ####
|
|---|
| 312 | REQUEST.set( 'URL', REQUEST['URL1'] + '/index_%s.html'%lang)
|
|---|
| 313 | count = self.getCachedPages( REQUEST, max_depth)
|
|---|
| 314 | message += '<br/>' + self.getZMILangStr('MSG_INSERTED')%(str(count)+' '+self.getZMILangStr('ATTR_OBJECTS'))
|
|---|
| 315 |
|
|---|
| 316 | # Return with message.
|
|---|
| 317 | message += ' (in '+str(int((time.time()-t0)*100.0)/100.0)+' secs.)'
|
|---|
| 318 | target = REQUEST.get('manage_target','manage_properties')
|
|---|
| 319 | return RESPONSE.redirect('%s?preview=preview&lang=%s&manage_tabs_message=%s'%(target,lang,urllib.quote(message)))
|
|---|
| 320 |
|
|---|
| 321 |
|
|---|
| 322 | ############################################################################
|
|---|
| 323 | # CacheableObject.manage_clearCachePages:
|
|---|
| 324 | #
|
|---|
| 325 | # Clear cached pages.
|
|---|
| 326 | ############################################################################
|
|---|
| 327 | def manage_clearCachePages(self, lang, REQUEST, RESPONSE):
|
|---|
| 328 | """ CacheableObject.manage_clearCachePages """
|
|---|
| 329 |
|
|---|
| 330 | message = ''
|
|---|
| 331 | t0 = time.time()
|
|---|
| 332 | max_depth = REQUEST.get('attr_cacheable_levels',999)
|
|---|
| 333 |
|
|---|
| 334 | ##### Clear cached pages ####
|
|---|
| 335 | count = self.clearCachePages( max_depth)
|
|---|
| 336 | message += self.getZMILangStr('MSG_DELETED')%count
|
|---|
| 337 |
|
|---|
| 338 | # Return with message.
|
|---|
| 339 | message += ' (in '+str(int((time.time()-t0)*100.0)/100.0)+' secs.)'
|
|---|
| 340 | target = REQUEST.get('manage_target','manage_properties')
|
|---|
| 341 | return RESPONSE.redirect('%s?preview=preview&lang=%s&manage_tabs_message=%s'%(target,lang,urllib.quote(message)))
|
|---|
| 342 |
|
|---|
| 343 | ################################################################################
|
|---|