Minahito
minah****@users*****
2006年 11月 12日 (日) 02:50:38 JST
Index: xoops2jp/html/modules/legacy/admin/class/ModuleInstallUtils.class.php diff -u xoops2jp/html/modules/legacy/admin/class/ModuleInstallUtils.class.php:1.1.2.6 xoops2jp/html/modules/legacy/admin/class/ModuleInstallUtils.class.php:1.1.2.7 --- xoops2jp/html/modules/legacy/admin/class/ModuleInstallUtils.class.php:1.1.2.6 Sat Nov 11 17:37:37 2006 +++ xoops2jp/html/modules/legacy/admin/class/ModuleInstallUtils.class.php Sun Nov 12 02:50:37 2006 @@ -463,9 +463,7 @@ $tplHandler =& xoops_gethandler('tplfile'); - if (!Legacy_ModuleInstallUtils::installAllOfBlockTemplates($module, $blockObj)) { - $log->addError(XCube_Utils::formatMessage(_AD_LEGACY_ERROR_BLOCK_TEMPLATE_INSTALL, $blockObj->getVar('name'))); - } + Legacy_ModuleInstallUtils::installBlockTemplate($blockObj, $module, $log); // // Process of a permission. @@ -542,7 +540,7 @@ * to database. * @return bool */ - function installAllOfBlockTemplates(&$module, &$block) + function installBlockTemplate(&$block, &$module, &$log) { if ($block->get('template') == null) { return true; @@ -553,8 +551,8 @@ $criteria =& new CriteriaCompo(); $criteria->add(new Criteria('tpl_type', 'block')); $criteria->add(new Criteria('tpl_tplset', 'default')); - $criteria->add(new Criteria('tpl_module', $module->getVar('dirname'))); - $criteria->add(new Criteria('tpl_file', $block->getVar('template'))); + $criteria->add(new Criteria('tpl_module', $module->get('dirname'))); + $criteria->add(new Criteria('tpl_file', $block->get('template'))); $tplfiles =& $tplHandler->getObjects($criteria); if (count($tplfiles) > 0) { @@ -562,20 +560,26 @@ } else { $tplfile =& $tplHandler->create(); - $tplfile->setVar('tpl_refid', $block->getVar('bid')); - $tplfile->setVar('tpl_tplset', 'default'); - $tplfile->setVar('tpl_file', $block->getVar('template')); - $tplfile->setVar('tpl_module', $module->getVar('dirname')); - $tplfile->setVar('tpl_type', 'block'); + $tplfile->set('tpl_refid', $block->get('bid')); + $tplfile->set('tpl_tplset', 'default'); + $tplfile->set('tpl_file', $block->get('template')); + $tplfile->set('tpl_module', $module->get('dirname')); + $tplfile->set('tpl_type', 'block'); // $tplfile->setVar('tpl_desc', $tpl_desc); - $tplfile->setVar('tpl_lastimported', 0); + $tplfile->set('tpl_lastimported', 0); } - $tplSource = Legacy_ModuleInstallUtils::readTemplateFile($module->getVar('dirname'), $block->getVar('template'), true); - $tplfile->setVar('tpl_source', $tplSource); - $tplfile->setVar('tpl_lastmodified', time()); + $tplSource = Legacy_ModuleInstallUtils::readTemplateFile($module->get('dirname'), $block->get('template'), true); + $tplfile->set('tpl_source', $tplSource); + $tplfile->set('tpl_lastmodified', time()); - return $tplHandler->insert($tplfile); + if ($tplHandler->insert($tplfile)) { + return true; + } + else { + $log->addError(XCube_Utils::formatMessage(_AD_LEGACY_ERROR_BLOCK_TEMPLATE_INSTALL, $blockObj->get('name'))); + return false; + } } /** @@ -751,6 +755,148 @@ } } + function smartUpdateAllOfBlock(&$module, &$log) + { + $dirname = $module->get('dirname'); + + $fileReader =& new Legacy_ModinfoX2FileReader($dirname); + $latestBlocks =& $fileReader->loadBlockInformations(); + + $dbReader =& new Legacy_ModinfoX2DBReader($dirname); + $currentBlocks =& $dbReader->loadBlockInformations(); + + $currentBlocks->update($latestBlocks); + + foreach (array_keys($currentBlocks->mBlocks) as $idx) { + switch ($currentBlocks->mBlocks[$idx]->mStatus) { + case LEGACY_BLOCKINFOMATION_STATUS_LOADED: + Legacy_ModuleInstallUtils::updateBlockTemplateByInfo($currentBlocks->mBlocks[$idx], $module, $log); + break; + + case LEGACY_BLOCKINFOMATION_STATUS_UPDATED: + Legacy_ModuleInstallUtils::updateBlockByInfo($currentBlocks->mBlocks[$idx], $module, $log); + break; + + case LEGACY_BLOCKINFOMATION_STATUS_NEW: + Legacy_ModuleInstallUtils::installBlockByInfo($currentBlocks->mBlocks[$idx], $module, $log); + break; + + case LEGACY_BLOCKINFOMATION_STATUS_DELETED: + Legacy_ModuleInstallUtils::uninstallBlockByFuncNum($currentBlocks->mBlocks[$idx]->mFuncNum, $module, $log); + break; + } + } + } + + function updateBlockTemplateByInfo(&$info, &$module, &$log) + { + $handler =& xoops_getmodulehandler('newblocks', 'legacy'); + + $criteria =& new CriteriaCompo(); + $criteria->add(new Criteria('dirname', $module->get('dirname'))); + $criteria->add(new Criteria('func_num', $info->mFuncNum)); + + $blockArr =& $handler->getObjects($criteria); + foreach (array_keys($blockArr) as $idx) { + Legacy_ModuleInstallUtils::uninstallBlockTemplate($blockArr[$idx], $module, $log); + Legacy_ModuleInstallUtils::installBlockTemplate($blockArr[$idx], $module, $log); + } + } + + function updateBlockByInfo(&$info, &$module, &$log) + { + $handler =& xoops_getmodulehandler('newblocks', 'legacy'); + + $criteria =& new CriteriaCompo(); + $criteria->add(new Criteria('dirname', $module->get('dirname'))); + $criteria->add(new Criteria('func_num', $info->mFuncNum)); + + $blockArr =& $handler->getObjects($criteria); + foreach (array_keys($blockArr) as $idx) { + $blockArr[$idx]->set('options', $info->mOptions); + $blockArr[$idx]->set('name', $info->mName); + $blockArr[$idx]->set('func_file', $info->mFuncFile); + $blockArr[$idx]->set('show_func', $info->mShowFunc); + $blockArr[$idx]->set('edit_func', $info->mEditFunc); + $blockArr[$idx]->set('template', $info->mTemplate); + + if ($handler->insert($blockArr[$idx])) { + $log->addReport(XCube_Utils::formatMessage('Update {0} block successfully.', $blockArr[$idx]->get('name'))); + } + else { + $log->addError(XCube_Utils::formatMessage('Could not update {0} block.', $blockArr[$idx]->get('name'))); + } + + Legacy_ModuleInstallUtils::uninstallBlockTemplate($blockArr[$idx], $module, $log); + Legacy_ModuleInstallUtils::installBlockTemplate($blockArr[$idx], $module, $log); + } + } + + function installBlockByInfo(&$info, &$module, &$log) + { + $handler =& xoops_gethandler('block'); + $block =& $handler->create(); + + $block->set('mid', $module->get('mid')); + $block->set('func_num', $info->mFuncNum); + $block->set('options', $info->mOptions); + $block->set('name', $info->mName); + $block->set('title', $info->mName); + $block->set('dirname', $module->get('dirname')); + $block->set('func_file', $info->mFuncFile); + $block->set('show_func', $info->mShowFunc); + $block->set('edit_func', $info->mEditFunc); + $block->set('template', $info->mTemplate); + + if (!$handler->insert($block)) { + $log->addError(XCube_Utils::formatMessage(_AD_LEGACY_ERROR_COULD_NOT_INSTALL_BLOCK, $block->get('name'))); + return false; + } + else { + $log->addReport(XCube_Utils::formatMessage(_AD_LEGACY_MESSAGE_BLOCK_INSTALLED, $block->get('name'))); + + Legacy_ModuleInstallUtils::installBlockTemplate($block, $module, $log); + + return true; + } + } + + /** + * @todo Need a message in the fail case. + */ + function uninstallBlockByFuncNum($func_num, &$module, &$log) + { + $handler =& xoops_getmodulehandler('newblocks', 'legacy'); + + $criteria =& new CriteriaCompo(); + $criteria->add(new Criteria('dirname', $module->get('dirname'))); + $criteria->add(new Criteria('func_num', $func_num)); + + $blockArr =& $handler->getObjects($criteria); + foreach (array_keys($blockArr) as $idx) { + if ($handler->delete($blockArr[$idx])) { + $log->addReport(XCube_Utils::formatMessage(_AD_LEGACY_MESSAGE_UNINSTALLATION_BLOCK_SUCCESSFUL, $blockArr[$idx]->get('name'))); + } + else { + // Uninstall fail + } + + Legacy_ModuleInstallUtils::uninstallBlockTemplate($blockArr[$idx], $module, $log); + } + } + + function uninstallBlockTemplate(&$block, &$module, &$log) + { + $handler =& xoops_gethandler('tplfile'); + $criteria =& new CriteriaCompo(); + $criteria->add(new Criteria('tpl_refid', $block->get('bid'))); + $criteria->add(new Criteria('tpl_file', $block->get('template'))); + $criteria->add(new Criteria('tpl_module', $module->get('dirname'))); + $criteria->add(new Criteria('tpl_type', 'block')); + + $handler->deleteAll($criteria); + } + /** * Executes SQL query as cube style. */ @@ -784,4 +930,306 @@ } } +define('LEGACY_BLOCKINFOMATION_STATUS_LOADED', "loaded"); +define('LEGACY_BLOCKINFOMATION_STATUS_UPDATED', "updated"); +define('LEGACY_BLOCKINFOMATION_STATUS_NEW', "new"); +define('LEGACY_BLOCKINFOMATION_STATUS_DELETED', "deleted"); + +/** + * The structure which is able to keep block's informations without DB. THis + * is installer only. + */ +class Legacy_BlockInformation +{ + var $mStatus = LEGACY_BLOCKINFOMATION_STATUS_LOADED; + + var $mFuncNum = 0; + + var $mName = ""; + + var $mOptions = ""; + + var $mFuncFile = ""; + var $mShowFunc = ""; + var $mEditFunc = ""; + var $mTemplate = ""; + + function Legacy_BlockInformation($funcNum, $name, $funcFile, $showFunc, $editFunc, $template, $options = null) + { + $this->mFuncNum = intval($funcNum); + $this->mName = $name; + $this->mFuncFile = $funcFile; + $this->mShowFunc = $showFunc; + $this->mEditFunc = $editFunc; + $this->mTemplate = $template; + $this->mOptions = $options; + } + + /** + * @return bool + */ + function isEqual(&$block) + { + if ($this->mFuncNum != $block->mFuncNum) { + return false; + } + + if ($this->mName != $block->mName) { + return false; + } + + if ($this->mFuncFile != $block->mFuncFile) { + return false; + } + + if ($this->mShowFunc != $block->mShowFunc) { + return false; + } + + if ($this->mEditFunc != $block->mEditFunc) { + return false; + } + + if ($this->mTemplate != $block->mTemplate) { + return false; + } + + return true; + } + + function update(&$block) + { + $this->mStatus = LEGACY_BLOCKINFOMATION_STATUS_UPDATED; + + $this->mName = $block->mName; + $this->mFuncFile = $block->mFuncFile; + $this->mShowFunc = $block->mShowFunc; + $this->mEditFunc = $block->mEditFunc; + $this->mTemplate = $block->mTemplate; + } +} + +class Legacy_BlockInfoCollection +{ + var $mBlocks = array(); + + function add(&$info) + { + if (isset($this->mBlocks[$info->mFuncNum])) { + return false; + } + + $this->mBlocks[$info->mFuncNum] =& $info; + + ksort($this->mBlocks); + + return true; + } + + function &get($funcNum) + { + if (isset($this->mBlocks[$funcNum])) { + return $this->mBlocks[$funcNum]; + } + + $ret = null; + return $ret; + } + + /** + * Updates the list of blocks by comparing with $collection. + */ + function update(&$collection) + { + foreach (array_keys($this->mBlocks) as $idx) { + $t_block =& $collection->get($this->mBlocks[$idx]->mFuncNum); + if ($t_block == null) { + $this->mBlocks[$idx]->mStatus = LEGACY_BLOCKINFOMATION_STATUS_DELETED; + } + elseif (!$this->mBlocks[$idx]->isEqual($t_block)) { + $this->mBlocks[$idx]->update($t_block); + } + } + + foreach (array_keys($collection->mBlocks) as $idx) { + $func_num = $collection->mBlocks[$idx]->mFuncNum; + if (!isset($this->mBlocks[$func_num])) { + $this->add($collection->mBlocks[$idx]); + $this->mBlocks[$func_num]->mStatus = LEGACY_BLOCKINFOMATION_STATUS_NEW; + } + } + } + + function reset() + { + unset($this->mBlocks); + $this->mBlocks = array(); + } +} + +class Legacy_AbstractModinfoReader +{ + function Legacy_AbstractModinfoReader() + { + } + + /** + * @return Legacy_BlockInfoCollection + */ + function &loadBlockInformations() + { + } +} + +/** + * @note final class + */ +class Legacy_ModinfoX2FileReader extends Legacy_AbstractModinfoReader +{ + /** + * @protected + */ + var $_mDirname = null; + + function Legacy_ModinfoX2FileReader($dirname) + { + $this->_mDirname = $dirname; + } + + /** + * @private + */ + function &_createBlockInformation($funcNum, $arr) + { + $showFunc = ""; + if (isset($arr['class'])) { + $showFunc = 'cl::' . $arr['class']; + } + else { + $showFunc = $arr['show_func']; + } + + $editFunc = isset($arr['edit_func']) ? $arr['edit_func'] : null; + $template = isset($arr['template']) ? $arr['template'] : null; + $options = isset($arr['options']) ? $arr['options'] : null; + + $info =& new Legacy_BlockInformation($funcNum, $arr['name'], $arr['file'], $arr['show_func'], $editFunc, $template, $options); + + return $info; + } + + /** + * @todo Need guarantee of global variables. + */ + function &loadBlockInformations() + { + $collection =& new Legacy_BlockInfoCollection(); + + $t_filePath = XOOPS_ROOT_PATH . '/modules/' . $this->_mDirname . '/xoops_version.php'; + if (!file_exists($t_filePath)) { + return $collection; + } + + include $t_filePath; + + if (!isset($modversion['blocks'])) { + return $collection; + } + + $blockArr = $modversion['blocks']; + + // + // Try (1) --- func_num + // + $successFlag = true; + foreach ($blockArr as $idx => $block) { + if (isset($block['func_num'])) { + $info =& $this->_createBlockInformation($block['func_num'], $block); + $successFlag &= $collection->add($info); + unset($info); + } + else { + $successFlag = false; + break; + } + } + + if ($successFlag) { + return $collection; + } + + // + // Try (2) --- index pattern + // + $collection->reset(); + + $successFlag = true; + foreach ($blockArr as $idx => $block) { + if (is_int($idx)) { + $info =& $this->_createBlockInformation($idx, $block); + $successFlag &= $collection->add($info); + unset($info); + } + else { + $successFlag = false; + break; + } + } + + if ($successFlag) { + return $collection; + } + + // + // Try (3) --- automatic + // + $collection->reset(); + + $idx = 1; + foreach ($blockArr as $block) { + $info =& $this->_createBlockInformation($idx++, $block); + $successFlag &= $collection->add($info); + unset($info); + } + + return $collection; + } +} + +class Legacy_ModinfoX2DBReader extends Legacy_AbstractModinfoReader +{ + /** + * @protected + */ + var $_mDirname = null; + + function Legacy_ModinfoX2DBReader($dirname) + { + $this->_mDirname = $dirname; + } + + function &_createBlockInformation(&$block) + { + $info =& new Legacy_BlockInformation($block->get('func_num'), $block->get('name'), $block->get('func_file'), $block->get('show_func'), $block->get('edit_func'), $block->get('template'), $block->get('options')); + return $info; + } + + function &loadBlockInformations() + { + $collection =& new Legacy_BlockInfoCollection(); + + $handler =& xoops_getmodulehandler('newblocks', 'legacy'); + $criteria =& new Criteria('dirname', $this->_mDirname); + $blockArr =& $handler->getObjects($criteria); + + foreach (array_keys($blockArr) as $idx) { + $info =& $this->_createBlockInformation($blockArr[$idx]); + $collection->add($info); + } + + return $collection; + } +} + + ?> \ No newline at end of file