[xoops-cvslog 4610] CVS update: xoops2jp/html/modules/base/kernel

Back to archive index

Minahito minah****@users*****
2006年 9月 25日 (月) 12:22:52 JST


Index: xoops2jp/html/modules/base/kernel/Legacy_Controller.class.php
diff -u xoops2jp/html/modules/base/kernel/Legacy_Controller.class.php:1.1.2.78 xoops2jp/html/modules/base/kernel/Legacy_Controller.class.php:1.1.2.79
--- xoops2jp/html/modules/base/kernel/Legacy_Controller.class.php:1.1.2.78	Sun Sep 24 23:20:38 2006
+++ xoops2jp/html/modules/base/kernel/Legacy_Controller.class.php	Mon Sep 25 12:22:52 2006
@@ -1,7 +1,7 @@
 <?php
 /**
  * @package Legacy
- * @version $Id: Legacy_Controller.class.php,v 1.1.2.78 2006/09/24 14:20:38 minahito Exp $
+ * @version $Id: Legacy_Controller.class.php,v 1.1.2.79 2006/09/25 03:22:52 minahito Exp $
  */
 
 if (!defined('XOOPS_ROOT_PATH')) exit();
@@ -80,6 +80,11 @@
 	/**
 	 * @var XCube_Delegate
 	 */
+	var $mGetBlockCacheFilePath = null;
+	
+	/**
+	 * @var XCube_Delegate
+	 */
 	var $mGetLanguageName = null;
 	
 	var $mBlockShowFlags = array();
@@ -115,6 +120,8 @@
 		
 		$this->mCheckEnableBlockCache =& new XCube_Delegate();
 		$this->mCheckEnableModuleCache =& new XCube_Delegate();
+		$this->mGetModuleCacheFilePath =& new XCube_Delegate();
+		$this->mGetBlockCacheFilePath =& new XCube_Delegate();
 
 		set_magic_quotes_runtime(0);	// ^^;
 		
@@ -260,32 +267,47 @@
 		$renderTarget =& new XCube_RenderTarget();
 		$renderTarget->setType(XCUBE_RENDER_TARGET_TYPE_BLOCK);
 		
-		$blockCache =& new Legacy_BlockCacheInformation();
+		$cacheInfo =& new Legacy_BlockCacheInformation();
 
 		foreach ($this->mBlockChain as $blockProcedure) {
 			$renderTarget->reset();
-			$blockCache->reset();
-			
-			$blockCache->setBlock($blockProcedure->mAdapteeBlockObject);	//< FIXME
 			
 			//
-			// Checks whether the controller should do cache.
+			// This is a flag indicating whether the controller needs to call
+			// the logic of the block.
 			//
-			$this->mCheckEnableBlockCache->call(new XCube_Ref($blockCache));
+			$usedCacheFlag = false;
+			
+			if ($this->isEnableCacheFeature()) {
+				//
+				// Reset the block cache information structure, and initialize.
+				//
+				$cacheInfo->reset();
+				$cacheInfo->setBlock($blockProcedure->mAdapteeBlockObject);	//< FIXME
+				$this->mCheckEnableBlockCache->call(new XCube_Ref($cacheInfo));
 
-			if ($blockCache->isEnableCache() && $blockCache->isExpireCache($blockProcedure->getCacheTime())) {
-				$content = $blockCache->load();
-				if ($content) {
-					$this->mBlockShowFlags[$blockProcedure->getEntryIndex()] = true;
-					$this->mBlockContents[$blockProcedure->getEntryIndex()][] = array(
+				$filepath = $this->getBlockCacheFilePath($cacheInfo);
+				
+				//
+				// If caching is enable and the cache file exists, load and use.
+				//
+				if ($cacheInfo->isEnableCache() && $this->existActiveCacheFile($filepath, $blockProcedure->getCacheTime())) {
+					$content = $this->loadCache($filepath);
+					if ($content != null) {
+						$this->mBlockShowFlags[$blockProcedure->getEntryIndex()] = true;
+						$this->mBlockContents[$blockProcedure->getEntryIndex()][] = array(
 							'name' => $blockProcedure->getName(),
 							'title'   => $blockProcedure->getTitle(),
 							'content' => $content,
 							'weight'  => $blockProcedure->getWeight()
-					);
+						);
+						
+						$usedCacheFlag = true;
+					}
 				}
 			}
-			else {
+			
+			if (!$usedCacheFlag) {
 				$renderSystem =& $this->mRoot->getRenderSystem($blockProcedure->getRenderSystemName());
 				
 				$blockProcedure->execute($this, $this->getXoopsUser(), $renderTarget);
@@ -304,8 +326,8 @@
 						'weight'=>$blockProcedure->getWeight()
 				);
 
-				if ($blockProcedure->getCacheTime() > 0 && $blockCache->isEnableCache()) {
-					$blockCache->save($renderTarget);
+				if ($this->isEnableCacheFeature() && $blockProcedure->getCacheTime() > 0 && $cacheInfo->isEnableCache()) {
+					$this->cacheRenderTarget($this->getBlockCacheFilePath($cacheInfo), $renderTarget);
 				}
 			}
 
@@ -637,26 +659,27 @@
 		//
 		// cache check
 		//
-		$this->mModuleController->setupCacheInformation();
-		$moduleCache =& $this->mModuleController->mCacheInformation;
+		$cacheInfo =& $this->mModuleController->createCacheInfo();
 		
-		$this->mCheckEnableModuleCache->call(new XCube_Ref($moduleCache));
+		$this->mCheckEnableModuleCache->call(new XCube_Ref($cacheInfo));
 		
-		if ($this->mModuleController->hasModuleCacheConfig()) {
+		if ($this->isEnableCacheFeature() && $this->mModuleController->hasCacheConfig()) {
 			//
 			// Checks whether the cache file exists.
 			//
 			$xoopsModule =& $this->mModuleController->getXoopsModule();
+
 			$cachetime = $this->mConfig['module_cache'][$xoopsModule->get('mid')];
+			$filepath = $this->getModuleCacheFilePath($cacheInfo);
 			
 			//
 			// Checks whether the active cache file exists. If it's OK, load
 			// cache and do display.
 			//			
-			if ($moduleCache->isEnableCache() && $moduleCache->isExpireCache($cachetime)) {
+			if ($cacheInfo->isEnableCache() && $this->existActiveCacheFile($filepath, $cachetime)) {
 				$renderSystem =& $this->mRoot->getRenderSystem($this->mModuleController->getDependRenderSystem());
 				$renderTarget =& $renderSystem->createRenderTarget(XCUBE_RENDER_TARGET_TYPE_MAIN);
-				$renderTarget->setResult($moduleCache->load());
+				$renderTarget->setResult($this->loadCache($filepath));
 				
 				$this->_executeViewTheme($renderTarget);
 				
@@ -710,13 +733,14 @@
 			//
 			// Cache Control
 			//
-			if ($this->mModuleController->hasModuleCacheConfig()) {
-				if ($this->mModuleController->mCacheInformation->isEnableCache()) {
-					$this->mModuleController->mCacheInformation->save($renderTarget);
+			if ($this->isEnableCacheFeature() && $this->mModuleController->hasCacheConfig()) {
+				if ($this->mModuleController->mCacheInfo->isEnableCache()) {
+					$this->cacheRenderTarget($this->getModuleCacheFilePath($this->mModuleController->mCacheInfo), $renderTarget);
 				}
 			}
+			else {
+			}
 		}
-
 		
 		$this->_executeViewTheme($renderTarget);
 	}
@@ -1065,9 +1089,101 @@
 		exit();
 	}
 	
+	/**
+	 * Gets a value indicating whether the controller can use a cache mechanism.
+	 * @return bool
+	 */	
 	function isEnableCacheFeature()
 	{
-		return $this->mStrategy->isEnableCacheFeature();
+		return $this->_mStrategy->isEnableCacheFeature();
+	}
+
+	/**
+	 * Gets a value indicating wheter a cache file keeps life time. If
+	 * $cachetime is 0 or the specific cache file doesn't exist, gets false.
+	 * 
+	 * @param string $filepath  a file path of the specific cache file.
+	 * @param int    $cachetime cache active duration. (Sec)
+	 * @return bool
+	 */
+	function existActiveCacheFile($filepath, $cachetime)
+	{
+		if ($cachetime == 0) {
+			return false;
+		}
+		
+		if (!file_exists($filepath)) {
+			return false;
+		}
+		
+		return ((time() - filemtime($filepath)) <= $cachetime);
+	}
+	
+	/**
+	 * Gets a file path of a cache file for module contents.
+	 * @param Legacy_ModuleCacheInformation $cacheInfo
+	 * @return string
+	 */
+	function getModuleCacheFilePath(&$cacheInfo)
+	{
+		$filepath = null;
+		$this->mGetModuleCacheFilePath->call(new XCube_Ref($filepath), $cacheInfo);
+		
+		if (!$filepath) {
+			$id = md5(XOOPS_SALT . $cacheInfo->mURL . "(" . implode("_", $cacheInfo->mIdentityArr) . ")" . implode("_", $cacheInfo->mGroupArr));
+			$filepath = XOOPS_CACHE_PATH . "/" . $id . ".cache.html";
+		}
+		
+		return $filepath;
+	}
+
+	/**
+	 * Gets a file path of a cache file for module contents.
+	 * @param Legacy_BlockCacheInformation $cacheInfo
+	 * @return string
+	 */
+	function getBlockCacheFilePath(&$cacheInfo)
+	{
+		$filepath = null;
+		$this->mGetBlockCacheFilePath->call(new XCube_Ref($filepath), $cacheInfo);
+		
+		if (!$filepath) {
+			$id = md5(XOOPS_SALT . $cacheInfo->mBlock->get('bid') . "(" . implode("_", $cacheInfo->mIdentityArr) . ")" . implode("_", $cacheInfo->mGroupArr));
+			$filepath = XOOPS_CACHE_PATH . "/" . $id . ".cache.html";
+		}
+		
+		return $filepath;
+	}
+
+	/**
+	 * Save the content of $renderTarget to $filepath.
+	 * @param string $filepath a file path of the cache file.
+	 * @param XCube_RenderTarget $renderBuffer
+	 * @return bool success or failure.
+	 */	
+	function cacheRenderTarget($filepath, &$renderTarget)
+	{
+		$fp = fopen($filepath, "wb");
+		if ($fp) {
+			fwrite($fp, $renderTarget->getResult());
+			fclose($fp);
+			return true;
+		}
+		
+		return false;
+	}
+	
+	/**
+	 * Loads $filepath and gets the content of the file.
+	 * @return string the content or null.
+	 */
+	function loadCache($filepath)
+	{
+		if (file_exists($filepath)) {
+			return file_get_contents($filepath);
+		}
+		
+		return null;
 	}
 }
 
@@ -1141,6 +1257,13 @@
 	function &getMainThemeObject()
 	{
 	}
+
+	/**
+	 * Gets a value indicating whether the controller can use a cache mechanism.
+	 */	
+	function isEnableCacheFeature()
+	{
+	}
 }
 
 class Legacy_PublicControllerStrategy extends Legacy_AbstractControllerStrategy
@@ -1323,7 +1446,7 @@
 	
 	function isEnableCacheFeature()
 	{
-		return true;
+		return false;
 	}
 }
 


xoops-cvslog メーリングリストの案内
Back to archive index