[xoops-cvslog 5518] CVS update: xoops2jp/html/modules/legacy/admin/class

Back to archive index

Minahito minah****@users*****
2006年 10月 30日 (月) 18:53:15 JST


Index: xoops2jp/html/modules/legacy/admin/class/ModuleUpdater.class.php
diff -u xoops2jp/html/modules/legacy/admin/class/ModuleUpdater.class.php:1.1.2.5 xoops2jp/html/modules/legacy/admin/class/ModuleUpdater.class.php:1.1.2.6
--- xoops2jp/html/modules/legacy/admin/class/ModuleUpdater.class.php:1.1.2.5	Fri Oct 27 20:45:38 2006
+++ xoops2jp/html/modules/legacy/admin/class/ModuleUpdater.class.php	Mon Oct 30 18:53:15 2006
@@ -9,7 +9,27 @@
  */
 class Legacy_ModulePhasedUpgrader
 {
-	var $mLog;
+	/**
+	 * This is an array of milestone version informations. Key is a version
+	 * number. Value is a method name called by execute().
+	 * 
+	 * Format:
+	 * {version} => {methodName}
+	 * 
+	 * Example:
+	 * var $_mMilestone = array('020' => 'update020', '025' => 'update025');
+	 * 
+	 * @access protected
+	 */
+	var $_mMilestone = array();
+	
+	/**
+	 * This instance is prepared automatically in the constructor.
+	 * 
+	 * @public
+	 * @var Legacy_ModuleUtilsSimpleLog
+	 */
+	var $mLog = null;
 	
 	/**
 	 * @var XoopsModule
@@ -31,7 +51,10 @@
 	 */
 	var $_mTargetVersion;
 	
-	var $_mMilestone = array();
+	/**
+	 * @var bool
+	 */
+	var $_mForceMode = false;
 	
 	function Legacy_ModulePhasedUpgrader()
 	{
@@ -39,12 +62,22 @@
 	}
 	
 	/**
+	 * Sets a value indicating whether the force mode is on.
+	 * @param bool $isForceMode
+	 */
+	function setForceMode($isForceMode)
+	{
+		$this->_mForceMode = $isForceMode;
+	}
+	
+	/**
 	 * Sets the current XoopsModule. This method creates the clone of this
 	 * object to prevent cache of the module handler, and then keep it to the
 	 * property. Plus, this method copies the version value of this object to
 	 * the _mCurrentVersion as backup for the case where the value of this
 	 * object is changed for updating.
 	 * 
+	 * @access public
 	 * @param XoopsModule $xoopsModule
 	 */
 	function setCurrentXoopsModule(&$xoopsModule)
@@ -52,6 +85,7 @@
 		$handler =& xoops_gethandler('module');
 		$cloneModule =& $handler->create();
 		
+		$cloneModule->unsetNew();
 		$cloneModule->set('mid', $xoopsModule->get('mid'));
 		$cloneModule->set('name', $xoopsModule->get('name'));
 		$cloneModule->set('version', $xoopsModule->get('version'));
@@ -67,10 +101,14 @@
 		$cloneModule->set('hasnotification', $xoopsModule->get('hasnotification'));
 		
 		$this->_mCurrentXoopsModule =& $cloneModule;
+		$this->_mCurrentVersion = $cloneModule->get('version');
 	}
 	
 	/**
 	 * Sets the target XoopsModule.
+	 * 
+	 * @access public
+	 * @param XoopsModule $xoopsModule
 	 */
 	function setTargetXoopsModule(&$xoopsModule)
 	{
@@ -78,33 +116,31 @@
 		$this->_mTargetVersion = $this->getTargetPhase();
 	}
 	
-	function doUpdate()
+	/**
+	 * Execute upgrade. If the specific method for the milestone, this method
+	 * calls the method. If such milestone doesn't exist, call the automatic
+	 * upgrade method.
+	 * 
+	 * @access public
+	 */
+	function executeUpgrade()
 	{
 		if ($this->hasUpgradeMethod()) {
-			$this->_callUpgradeMethod();
+			return $this->_callUpgradeMethod();
 		}
 		else {
-			$this->executeAutomaticUpgrade();
+			return $this->executeAutomaticUpgrade();
 		}
 	}
 
-	function executeAutomaticUpgrade()
-	{
-		die('MADADAYO! --- This is not implemeted yet!');
-	}
-
 	/**
-	 * Saves the current XoopsModule object to DB.
-	 */	
-	function saveXoopsModule()
+	 * Gets the current version.
+	 * 
+	 * @return int
+	 */
+	function getCurrentVersion()
 	{
-		$handler =& xoops_gethandler('module');
-		if ($handler->insert($this->_mCurrentXoopsModule)) {
-			$this->mLog->addReport("XoopsModule is updated.");
-		}
-		else {
-			$this->mLog->addError("*Could not install module information*");
-		}
+		return $this->_mCurrentVersion;
 	}
 	
 	/**
@@ -134,7 +170,7 @@
 		ksort($this->_mMilestone);
 		
 		foreach ($this->_mMilestone as $t_version => $t_value) {
-			if ($t_version > $this->_mCurrentVersion) {
+			if ($t_version >= $this->_mCurrentVersion) {
 				if (is_callable(array($this, $t_value))) {
 					return true;
 				}
@@ -158,192 +194,135 @@
 		foreach ($this->_mMilestone as $t_version => $t_value) {
 			if ($t_version > $this->_mCurrentVersion) {
 				if (is_callable(array($this, $t_value))) {
-					$this->$t_value();
-					return true;
+					return $this->$t_value();
 				}
 			}
 		}
 		
 		return false;
 	}
-
-}
-
-
-class Legacy_ModuleUpdater extends Legacy_AbstractModuleInstaller
-{
-	var $mPreVersion = 0;
-
-	function Legacy_ModuleUpdater($dirname)
-	{
-		parent::Legacy_AbstractModuleInstaller($dirname);
-		
-		$this->mProcessScript->register('Legacy_ModuleUpdater.ProcessScript');
-		$this->mProcessScript->add(array(&$this, '_processScript'));
-		
-		$this->mInstallTable->register('Legacy_ModuleUpdater.InstallTable');
-
-		$this->mInstallTemplate->register('Legacy_ModuleUpdater.InstallTemplate');
-		$this->mInstallTemplate->add('Legacy_ModuleUpdater::_uninstallTemplate');
-	}
-
+	
 	/**
+	 * Gets a valude indicating whether this process is upgrade for the latest
+	 * version.
+	 * 
 	 * @return bool
 	 */
-	function execute()
+	function isLatestUpgrade()
 	{
-		$this->mLog->addReport(_AD_LEGACY_MESSAGE_UPDATE_STARTED);
-		
-		parent::execute();
+		return ($this->_mTargetXoopsModule->get('version') == $this->getTargetPhase());
 	}
 	
 	/**
-	 * Load xoops module object installed and return it.
-	 * @static
-	 * @access public
-	 * @param $dirname string
-	 * @return XoopsModule
-	 */
-	function &loadModuleObject($dirname)
+	 * Saves XoopsModule object to DB.
+	 * 
+	 * @access protected
+	 */	
+	function saveXoopsModule(&$module)
 	{
-		$ret = null;
-
-		$moduleHandler =& xoops_gethandler('module');
-		$module =& $moduleHandler->getByDirname($dirname);
-
-		if (!is_object($module)) {
-			return $ret;
+		$handler =& xoops_gethandler('module');
+		if ($handler->insert($module)) {
+			$this->mLog->addReport("XoopsModule is updated.");
+		}
+		else {
+			$this->mLog->addError("*Could not install module information*");
 		}
-		
-		$this->mPreVersion = $module->get('version');
-        $name = $module->getVar('name','n');
-		$module->loadInfoAsVar($dirname);
-		$module->set('name', $name, true);
-		
-		return $module;
 	}
-
-	/**
-	 * @static
-	 */
-	function _uninstallTemplate(&$module, &$log)
+	
+	function _processScript(&$module, &$log)
 	{
-		$tplHandler =& xoops_gethandler('tplfile');
-
-		//
-		// The following processing depends on the structure of Legacy_RenderSystem.
-		//
-		$tplHandler =& xoops_gethandler('tplfile');
-		$delTemplates =& $tplHandler->find('default', 'module', $module->getVar('mid'));
-		if(is_array($delTemplates) && count($delTemplates) > 0) {
-			//
-			// clear cache
-			//
-			$xoopsTpl=new XoopsTpl();
-			$xoopsTpl->clear_cache(null, "mod_" . $module->getVar('dirname'));
-			
-			foreach ($delTemplates as $tpl) {
-				if (!$tplHandler->delete($tpl)) {
-					$log->addError(XCube_Utils::formatMessage(_AD_LEGACY_ERROR_TEMPLATE_UNINSTALLED, $tpl->getVar('tpl_file')));
+		$installScript = trim($module->getInfo('onUpdate'));
+		if ($installScript != false) {
+			require_once XOOPS_MODULE_PATH . "/" . $module->getVar('dirname') . "/" . $installScript;
+			$funcName = 'xoops_module_update_' . $module->getVar('dirname');
+			if (function_exists($funcName)) {
+				if (!call_user_func($funcName, $module, $this->mPreVersion)) {
+					$log->addError("Failed to execute " . $funcName);
 				}
 			}
 		}
-
-		//
-		// install Template All
-		//
-		$templates = $module->getInfo('templates');
-		if ($templates != false) {
-			foreach ($templates as $template) {
-				Legacy_ModuleUtils::installTemplate($module, $template, $log);
-			}
+	}
+	
+	function _processReport()
+	{
+		if (!$this->mLog->hasError()) {
+			$this->mLog->add(XCube_Utils::formatMessage(_AD_LEGACY_MESSAGE_UPDATING_MODULE_SUCCESSFUL, $this->_mCurrentXoopsModule->get('name')));
+		}
+		else {
+			$this->mLog->addError(XCube_Utils::formatMessage(_AD_LEGACY_ERROR_UPDATING_MODULE_FAILURE, $this->_mCurrentXoopsModule->get('name')));
 		}
 	}
-
+	
 	/**
+	 * This method executes upgrading automatically by the diff of
+	 * xoops_version.
+	 * 
+	 * 1) Uninstall all of module templates
+	 * 2) Install all of module templates
+	 * 
 	 * @return bool
 	 */
-	function _installBlock()
+	function executeAutomaticUpgrade()
 	{
-		// XoopsBlock[]
-		$installBlocks = array();
-
-		// XoopsBlock[]
-		$unInstallBlocks = array();
-
-		$definedBlocks = $this->mModule->getInfo('blocks');
-		if($definedBlocks == false)
+		$this->mLog->addReport(_AD_LEGACY_MESSAGE_UPDATE_STARTED);
+		
+		//
+		// まず全てのテンプレートを削除します
+		//
+		Legacy_ModuleUtils::uninstallAllModuleTemplates($this->_mTargetXoopsModule, $this->mLog);
+		if (!$this->_mForceMode && $this->mLog->hasError()) {
+			$this->_processReport();
 			return false;
-
-		$blockHandler =& xoops_gethandler('block');
-
-		$func_num=0;
-		$showfuncs = array();
-        $funcfiles = array();
-		for ($i=1; $i<= count($definedBlocks); $i++) {
-		    $block =&  $definedBlocks[$i];
-			$func_num = $i;
-		    $showfuncs[] = isset($block['class']) ? ('cl::' . $block['class']) : $block['show_func'];
-		    $funcfiles[]=$block['file'];
-			$newBlock =& Legacy_ModuleUtils::createBlockByInfo($this->mModule, $block);
-			//
-			// Get a installed block by mid and name.
-			//
-			$criteria = new CriteriaCompo();
-			$criteria->add(new Criteria('mid', $this->mModule->getVar('mid')));
-			$criteria->add(new Criteria('func_num', $func_num));
-			$criteria->add(new Criteria('show_func', isset($block['class']) ? ('cl::' . $block['class']) : $block['show_func']));
-			$criteria->add(new Criteria('func_file', $block['file']));
-			$installedBlocks =& $blockHandler->getObjectsDirectly($criteria);
-			//
-			// If a installed block is not found, push it to new install block list.
-			//
-			if (count($installedBlocks) == 0) {
-				$newBlock->setVar('func_num', $func_num);
-				$installBlocks[] = array($newBlock, $block);
-				unset($newBlock);
-
-				continue;
-			}
-
-			$changedFlag = false;
-
-			$oldBlock =& $installedBlocks[0];
-            $newBlock =& Legacy_ModuleUtils::mergeBlockObject($oldBlock, $newBlock, $changedFlag);
-
-			if ($changedFlag) {
-				$installBlocks[] = array($newBlock, $block);
-			}
-			else {
-				if (!Legacy_ModuleUtils::installBlockTemplate($this->mModule, $oldBlock)) {
-					$this->mLog->addError("ERROR : Could not update block template ".$oldBlock->getVar('name'));
-				}
-			}
-
-			unset($newBlock);
-			unset($oldBlock);
 		}
-		$criteria = new CriteriaCompo();
-		$criteria->add(new Criteria('mid', $this->mModule->getVar('mid')));
-		$installedBlocks =& $blockHandler->getObjectsDirectly($criteria);
-		foreach ($installedBlocks as $blockObj) {
-            if (!in_array($blockObj->getVar('show_func'), $showfuncs) || !in_array($blockObj->getVar('func_file'), $funcfiles)) {
-				$unInstallBlocks[] =& $blockObj;
-				unset($blockObj);
-            }
-        }
-
-		foreach ($installBlocks as $block) {
-			Legacy_ModuleUtils::installBlock($this->mModule, $block[0], $block[1], $this->mLog);
+		
+		//
+		// 全てのテンプレートをインストールします
+		//
+		Legacy_ModuleUtils::installAllModuleTemplates($this->_mTargetXoopsModule, $this->mLog);
+		if (!$this->_mForceMode && $this->mLog->hasError()) {
+			$this->_processReport();
+			return false;
+		}
+		
+		//
+		// ブロックの更新を行います
+		//
+		Legacy_ModuleUtils::uninstallAllBlocks($this->_mTargetXoopsModule, $this->mLog);
+		Legacy_ModuleUtils::installAllBlocks($this->_mTargetXoopsModule, $this->mLog);
+		if (!$this->_mForceMode && $this->mLog->hasError()) {
+			$this->_processReport();
+			return false;
 		}
-        
-		foreach ($unInstallBlocks as $block) {
-			Legacy_ModuleUtils::unInstallBlock($block, $this->mLog);
+		
+		//
+		// Preference & Notify の更新を行います
+		//
+		$this->_upgradePreference();
+		if (!$this->_mForceMode && $this->mLog->hasError()) {
+			$this->_processReport();
+			return false;
+		}
+		
+		//
+		// モジュールオブジェクトの更新を行います
+		//
+		$this->saveXoopsModule($this->_mTargetXoopsModule);
+		if (!$this->_mForceMode && $this->mLog->hasError()) {
+			$this->_processReport();
+			return false;
 		}
 
+		//
+		// アップデートスクリプトの呼び出し
+		//
+		$this->_processScript($this->_mTargetXoopsModule, $this->mLog);
+		
+		$this->_processReport();
+		
+		return true;
 	}
-	
-	function _installPreference()
+
+	function _upgradePreference()
 	{
 		$configHandler =& xoops_gethandler('config');
 		
@@ -351,7 +330,7 @@
 		// At the start, load config items of this time.
 		//
 		$criteria =& new CriteriaCompo();
-		$criteria->add(new Criteria('conf_modid', $this->mModule->get('mid')));
+		$criteria->add(new Criteria('conf_modid', $this->_mTargetXoopsModule->get('mid')));
 		$criteria->add(new Criteria('conf_catid', 0));
 		
 		$activeConfigArr =& $configHandler->getConfigs($criteria);
@@ -360,13 +339,13 @@
 		// Next, load config item from xoops_version.php.
 		//
 		$newConfigArr = array();
-		$t_configInfoArr = Legacy_ModuleUtils::getConfigInfosFromManifesto($this->mModule);
+		$t_configInfoArr = Legacy_ModuleUtils::getConfigInfosFromManifesto($this->_mTargetXoopsModule);
 		
 		$count = 0;
 		if (is_array($t_configInfoArr)) {
 			foreach ($t_configInfoArr as $t_configInfo) {
 				$config =& $configHandler->createConfig();
-				$config->loadFromConfigInfo($this->mModule->get('mid'), $t_configInfo, $count++);
+				$config->loadFromConfigInfo($this->_mTargetXoopsModule->get('mid'), $t_configInfo, $count++);
 				$newConfigArr[] =& $config;
 				unset($config);
 			}
@@ -444,35 +423,6 @@
 			}
 		}
 	}
-
-	function _processScript(&$module, &$log)
-	{
-		$installScript = trim($module->getInfo('onUpdate'));
-		if ($installScript != false) {
-			require_once XOOPS_MODULE_PATH . "/" . $module->getVar('dirname') . "/" . $installScript;
-			$funcName = 'xoops_module_update_' . $module->getVar('dirname');
-			if (function_exists($funcName)) {
-				if (!call_user_func($funcName, $module, $this->mPreVersion)) {
-					$log->addError("Failed to execute " . $funcName);
-				}
-			}
-		}
-	}
-	
-	function _processReport()
-	{
-		if (!$this->mLog->hasError()) {
-			$this->mLog->add(XCube_Utils::formatMessage(_AD_LEGACY_MESSAGE_UPDATING_MODULE_SUCCESSFUL, $this->mModule->get('name')));
-		}
-		else {
-			if (is_object($this->mModule)) {
-				$this->mLog->addError(XCube_Utils::formatMessage(_AD_LEGACY_ERROR_UPDATING_MODULE_FAILURE, $this->mModule->get('name')));
-			}
-			else {
-				$this->mLog->addError(XCube_Utils::formatMessage(_AD_LEGACY_ERROR_UPDATING_MODULE_FAILURE, $this->mDirname));
-			}
-		}
-	}
 }
 
 ?>
\ No newline at end of file


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