<?php
include "mainfile.php";
if (isset($_POST['submit'])) {
    echo "<br /><table width=\"80%\" align=\"center\"><tr><td>";
    echo "<br />更新系统参数...<br /><hr /><br />";
    //Check for duplicate configuration items:
    $sql = "SELECT c1.conf_id FROM ".$xoopsDB->prefix('config')." c1, ".$xoopsDB->prefix('config')." c2 WHERE
                c1.conf_modid=c2.conf_modid AND c1.conf_name=c2.conf_name AND c1.conf_id > c2.conf_id";
    $result = $xoopsDB->query($sql);
    while (list($id) = $xoopsDB->fetchRow($result)) {
        $ids[] = $id;
    }
    if (count($ids) > 0) {
        $criteria = new Criteria('conf_id', "(".implode(',', $ids).")", "IN");
        $config_handler = xoops_gethandler('config');
        $configs = $config_handler->getConfigs($criteria);
        foreach (array_keys($configs) as $i) {
            $config_handler->deleteConfig($configs[$i]);
        }
    }
    
    //echo "<br />System update...<br /><hr /><br />";
    //Update system
    include XOOPS_ROOT_PATH."/modules/system/include/update.php";
    $module_handler = xoops_gethandler('module');
    $module = $module_handler->getByDirname("system");

    include XOOPS_ROOT_PATH."/modules/system/language/english/admin/modulesadmin.php";
    $module->update();
    
    echo "<br />Module config update...<br /><hr /><br />";
    $criteria = new CriteriaCompo(new Criteria("hasconfig", 1));
    $criteria->add(new Criteria('dirname', "system", "!="));
    $modules_with_config = $module_handler->getObjects($criteria);
    unset($criteria);
    if (count($modules_with_config) > 0) {
        $msgs = array();
        foreach (array_keys($modules_with_config) as $i) {
            $modules_with_config[$i]->insertConfigCategories();
            $modules_with_config[$i]->insertConfig();
            $msgs = array_merge($msgs, $modules_with_config[$i]->getMessages());
        }
        echo implode('<br />', $msgs);
    }
    /**/
    
    /*
    echo "<br />更新模块...<br /><hr /><br />";
    $modules =& $module_handler->getObjects();
    if (count($modules) > 0) {
        $msgs = array();
        foreach (array_keys($modules) as $i) {
	        ob_start();
            $modules[$i]->update();
            ob_end_clean();
        	echo "<br />模块已更新: ".$modules[$i]->getVar("dirname");
        }
    }
    */
    
    echo "<br /><br />删除无用区块...<br /><hr /><br />";
    $instance_handler =& xoops_gethandler('blockinstance');
    $criteria = new CriteriaCompo(new Criteria("visible", 0));
    $instances = $instance_handler->getObjects($criteria);
    if (count($instances) > 0) {
        $msgs = array();
        foreach (array_keys($instances) as $i) {
    		$instance_handler->delete($instances[$i]);
        	echo "<br />区块已删除:".$instances[$i]->getVar("title");
        }
    }
    
    echo "<center><font size=\"+1\"><p>祝贺你, 系统已成功升级至 ".XOOPS_VERSION." !</p>";
    echo "<p>在网站正式重新开放前, 你需要<a href=\"admin.php\"><strong>进入管理区</strong></a>更新模块，并对对区块或权限等进行核实，必要时重新设置</p></font>";
    echo "<p>或者<a href=\"index.php\">返回首页</a><p></center>";
    
    echo "</td></tr></table>";
}
else {
    $xoopsDB->queryF("UPDATE ".$xoopsDB->prefix('config')." SET conf_modid=1 WHERE conf_modid='0'"); //Shouldn't really be run several times, but it doesn't hurt anything since there will be no configs with conf_modid=0
    include_once XOOPS_ROOT_PATH."/class/xoopsformloader.php";
    $form = new XoopsThemeForm('XOOPS 系统升级', 'form', 'xoopsupdate.php');
    $form->addElement(new XoopsFormLabel('', '升级过程可能需要几分钟到十几分钟。请耐心等待...'));
    $form->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
    $form->display();
}
include "footer.php";
?>