<?php
/**
 * 系統修復工具
 *
 * @version        $Id: sys_repair.php 1 22:28 2010年7月20日Z tianya $
 * @package        DedeCMS.Administrator
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.dedecms.com/usersguide/license.html
 * @link           http://www.dedecms.com
 */
require_once(dirname(__FILE__).'/config.php');
CheckPurview('sys_ArcBatch');
require_once(dirname(__FILE__).'/../include/oxwindow.class.php');
//ShowMsg("目前暫不需要此工具，以後有需要系統會進行自動升級這個程序！<br /><a href='index_body.php'>&lt;&lt;點擊此返回&gt;&gt;</a>", "javascript:;");
//exit();
if(empty($dopost))
{
    $win = new OxWindow();
    $win->Init("sys_repair.php", "js/blank.js", "POST' enctype='multipart/form-data' ");
    $win->mainTitle = "系統修復工具";
    $wecome_info = "<a href='index_body.php'>系統主頁</a> &gt;&gt; 系統錯誤修復工具";
    $win->AddTitle('本工具用於檢測和修復你的系統可能存在的錯誤');
    $msg = "
    <table width='98%' border='0' cellspacing='0' cellpadding='0' align='center'>
  <tr>
    <td height='250' valign='top'>
    <br />
    由於手動升級時用戶沒運行指定的SQL語句，或自動升級的遺漏處理或處理出錯，可能會導致一些錯誤，使用本工具會自動檢測並處理。<br /><br />
    <b>本工具目前主要執行下面動作：</b><br />
    1、修復/優化數據表；<br />
    2、更新系統緩存；<br />
    3、檢測系統變量一致性。<br />
    4、檢測微表與主表數據一致性。<br />
    <br />
    <br />
    <a href='sys_repair.php?dopost=1' style='font-size:18px;color:red'><b>點擊此開始進行常規檢測&gt;&gt;</b></a>
    <br /><br /><br />
    </td>
  </tr>
 </table>
    ";
    $win->AddMsgItem("<div style='padding-left:20px;line-height:150%'>$msg</div>");
    $winform = $win->GetWindow('hand','');
    $win->Display();
    exit();
}
/*-------------------
數據結構常規檢測
function 1_test_db() {  }
--------------------*/
else if($dopost==1)
{
    $win = new OxWindow();
    $win->Init("sys_repair.php", "js/blank.js", "POST' enctype='multipart/form-data' ");
    $win->mainTitle = "系統修復工具";
    $wecome_info = "<a href='sys_repair.php'>系統錯誤修復工具</a> &gt;&gt; 檢測數據結構";
    $win->AddTitle('本工具用於檢測和修復你的系統可能存在的錯誤');
    $msg = "
    <table width='98%' border='0' cellspacing='0' cellpadding='0' align='center'>
  <tr>
    <td height='250' valign='top'>
    <b><font color='green'>已完成數據結構完整性檢測！</font></b>
    <hr size='1'/>
    <br />
    <b>如果你系統有下面幾種問題之一，請檢測微表正確性：</b><br />
    1、無法獲得主鍵，因此無法進行後續操作<br />
    2、更新數據庫archives表時出錯<br />
    3、列表顯示數據目與實際文檔數不一致<br />
    <br />
    <a href='sys_repair.php?dopost=2' style='font-size:18px;'><b>點擊此檢測微表正確性&gt;&gt;</b></a>
    <br /><br /><br />
    </td>
  </tr>
 </table>
    ";
    $win->AddMsgItem("<div style='padding-left:20px;line-height:150%'>$msg</div>");
    $winform = $win->GetWindow('hand','');
    $win->Display();
    exit();
}
/*-------------------
檢測微表正確性並嘗試修復
function 2_test_arctiny() {  }
--------------------*/
else if($dopost==2)
{
    $msg = '';
  
    $allarcnum = 0;
    $row = $dsql->GetOne("SELECT COUNT(*) AS dd FROM `#@__archives` ");
    $allarcnum = $arcnum = $row['dd'];
    $msg .= "·#@__archives 表總記錄數： {$arcnum} <br />";
  
    $shtables = array();
    $dsql->Execute('me', " SELECT addtable FROM `#@__channeltype` WHERE id < -1 ");
    while($row = $dsql->GetArray('me') )
    {
        $addtable = strtolower(trim(str_replace('#@__', $cfg_dbprefix, $row['addtable'])));
        if(empty($addtable)) 
        {
            continue;
        }
        else
        {
            if( !isset($shtables[$addtable]) )
            {
                $shtables[$addtable] = 1;
                $row = $dsql->GetOne("SELECT COUNT(aid) AS dd FROM `$addtable` ");
                $msg .= "·{$addtable} 表總記錄數： {$row['dd']} <br />";
                $allarcnum += $row['dd'];
            }
        }
    }
    $msg .= "※總有效記錄數： {$allarcnum} <br /> ";
    $errall = "<a href='index_body.php' style='font-size:14px;'><b>完成修正或無錯誤返回&gt;&gt;</b></a>";
    $row = $dsql->GetOne("SELECT COUNT(*) AS dd FROM `#@__arctiny` ");
    $msg .= "※微統計表記錄數： {$row['dd']}<br />";
    if($row['dd']==$allarcnum)
    {
        $msg .= "<p style='color:green;font-size:16px'><b>兩者記錄一致，無需修正！</b></p><br />";
    }
    else
    {
        $sql = " TRUNCATE TABLE `#@__arctiny`";
        $dsql->ExecuteNoneQuery($sql);
        $msg .= "<font color='red'>兩者記錄不一致，嘗試進行簡單修正...</font><br />";
        //導入普通模型微數據
        $sql = "INSERT INTO `#@__arctiny`(id, typeid, typeid2, arcrank, channel, senddate, sortrank, mid)  
            SELECT id, typeid, typeid2, arcrank, channel, senddate, sortrank, mid FROM `#@__archives` ";
        $dsql->ExecuteNoneQuery($sql);
        //導入單表模型微數據
        foreach($shtables as $tb=>$v)
        {
            $sql = "INSERT INTO `#@__arctiny`(id, typeid, typeid2, arcrank, channel, senddate, sortrank, mid)  
                    SELECT aid, typeid, 0, arcrank, channel, senddate, 0, mid FROM `$tb` ";
            $rs = $dsql->ExecuteNoneQuery($sql); 
            $doarray[$tb]  = 1;
        }
        $row = $dsql->GetOne("SELECT COUNT(*) AS dd FROM `#@__arctiny` ");
        if($row['dd']==$allarcnum)
        {
            $msg .= "<p style='color:green;font-size:16px'><b>修正記錄成功！</b></p><br />";
        }
        else
        {
            $msg .= "<p style='color:red;font-size:16px'><b>修正記錄失敗，建議進行高級綜合檢測！</b></p><br />";
            $errall = " <a href='sys_repair.php?dopost=3' style='font-size:14px;'><b>進行高級結合性檢測&gt;&gt;</b></a> ";
        }
    }
    UpDateCatCache();
    $win = new OxWindow();
    $win->Init("sys_repair.php", "js/blank.js", "POST' enctype='multipart/form-data' ");
    $win->mainTitle = "系統修復工具";
    $wecome_info = "<a href='sys_repair.php'>系統錯誤修復工具</a> &gt;&gt; 檢測微表正確性";
    $win->AddTitle('本工具用於檢測和修復你的系統可能存在的錯誤');
    $msg = "
    <table width='98%' border='0' cellspacing='0' cellpadding='0' align='center'>
  <tr>
    <td height='250' valign='top'>
    {$msg}
    <hr />
    <br />
    {$errall}
    </td>
  </tr>
 </table>
    ";
    $win->AddMsgItem("<div style='padding-left:20px;line-height:150%'>$msg</div>");
    $winform = $win->GetWindow('hand','');
    $win->Display();
    exit();
}
/*-------------------
高級方式修復微表(會刪除不合法主鍵的內容)
function 3_re_arctiny() {  }
--------------------*/
else if($dopost==3)
{
    $errnum = 0;
    $sql = " TRUNCATE TABLE `#@__arctiny`";
    $dsql->ExecuteNoneQuery($sql);
    
    $sql = "SELECT arc.id, arc.typeid, arc.typeid2, arc.arcrank, arc.channel, arc.senddate, arc.sortrank,
            arc.mid, ch.addtable FROM `#@__archives` arc LEFT JOIN `#@__channeltype` ch ON ch.id=arc.channel ";
    $dsql->Execute('me', $sql);
    while($row = $dsql->GetArray('me') )
    {
        $sql = "INSERT INTO `#@__arctiny`(id, typeid, typeid2, arcrank, channel, senddate, sortrank, mid)
              VALUES('{$row['id']}','{$row['typeid']}','{$row['typeid2']}','{$row['arcrank']}',
             '{$row['channel']}','{$row['senddate']}','{$row['sortrank']}','{$row['mid']}');  ";
        $rs = $dsql->ExecuteNoneQuery($sql);
        if(!$rs)
        {
            $addtable = trim($addtable);
            $errnum ++;
            $dsql->ExecuteNoneQuery("DELETE FROM `#@__archives` WHERE id='{$row['id']}' ");
            if(!empty($addtable)) $dsql->ExecuteNoneQuery("DELETE FROM `$addtable` WHERE id='{$row['id']}' ");
        }
    }
    //導入單表模型微數據
    $dsql->SetQuery("SELECT id,addtable FROM `#@__channeltype` WHERE id < -1 ");
    $dsql->Execute();
    $doarray = array();
    while($row = $dsql->GetArray())
    {
        $tb = str_replace('#@__', $cfg_dbprefix, $row['addtable']);
        if(empty($tb) || isset($doarray[$tb]) )
        {
            continue;
        }
        else
        {
            $sql = "INSERT INTO `#@__arctiny`(id, typeid, typeid2, arcrank, channel, senddate, sortrank, mid)  
                    SELECT aid, typeid, 0, arcrank, channel, senddate, 0, mid FROM `$tb` ";
            $rs = $dsql->ExecuteNoneQuery($sql); 
            $doarray[$tb]  = 1;
        }
    }
    $win = new OxWindow();
    $win->Init("sys_repair.php","js/blank.js","POST' enctype='multipart/form-data' ");
    $win->mainTitle = "系統修復工具";
    $wecome_info = "<a href='sys_repair.php'>系統錯誤修復工具</a> &gt;&gt; 高級綜合檢測修復";
    $win->AddTitle('本工具用於檢測和修復你的系統可能存在的錯誤');
    $msg = "
    <table width='98%' border='0' cellspacing='0' cellpadding='0' align='center'>
  <tr>
    <td height='250' valign='top'>
    完成所有修復操作，移除錯誤記錄 {$errnum} 條！
    <hr />
    <br />
    <a href='index_body.php' style='font-size:14px;'><b>完成修正或無錯誤返回&gt;&gt;</b></a>
    </td>
  </tr>
 </table>
    ";
    $win->AddMsgItem("<div style='padding-left:20px;line-height:150%'>$msg</div>");
    $winform = $win->GetWindow('hand','');
    $win->Display();
    exit();
}