<?php
/**
 * 生成所有頁面
 *
 * @version        $Id: makehtml_all.php 1 8:48 2010年7月13日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");
require_once(DEDEINC."/channelunit.func.php");
$action = (empty($action) ? '' : $action);

if($action=='')
{
    require_once(DEDEADMIN."/templets/makehtml_all.htm");
    exit();
}
else if($action=='make')
{
    //step = 1 更新主頁、step = 2 更新內容、step = 3 更新欄目
    if(empty($step)) $step = 1;

    //更新文檔前優化數據
    /*-------------------
    function _1_OptimizeData1()
    ---------------------*/
    if($step==1)
    {
        $starttime = GetMkTime($starttime);
        $mkvalue = ($uptype=='time' ? $starttime : $startid);
        OptimizeData($dsql);
        ShowMsg("完成數據優化，現在開始更新文檔！","makehtml_all.php?action=make&step=2&uptype=$uptype&mkvalue=$mkvalue");
        exit();
    }
    //更新文檔
    /*-------------------
    function _2_MakeArchives()
    ---------------------*/
    else if($step==2)
    {
        include_once(DEDEADMIN."/makehtml_archives_action.php");
        exit();
    }
    //更新主頁
    /*-------------------------
    function _3_MakeHomePage()
    -------------------*/
    if($step==3)
    {
        include_once(DEDEINC."/arc.partview.class.php");
        $pv = new PartView();
        $row = $pv->dsql->GetOne("SELECT * FROM `#@__homepageset` ");
        $templet = str_replace("{style}", $cfg_df_style,$row['templet']);
        $homeFile = DEDEADMIN.'/'.$row['position'];
        $homeFile = str_replace("\\", '/', $homeFile);
        $homeFile = preg_replace("#\/{1,}#" ,'/', $homeFile);
        $pv->SetTemplet($cfg_basedir.$cfg_templets_dir.'/'.$templet);
        $pv->SaveToHtml($homeFile);
        $pv->Close();
        ShowMsg("完成更新所有文檔，現在開始更新欄目頁！","makehtml_all.php?action=make&step=4&uptype=$uptype&mkvalue=$mkvalue");
        exit();
    }
    //更新欄目
    /*-------------------
    function _4_MakeCatalog()
    --------------------*/
    else if($step==4)
    {
        $mkvalue = intval($mkvalue);
        $typeidsok = $typeids = array();
        $adminID = $cuserLogin->getUserID();
        $mkcachefile = DEDEDATA."/mkall_cache_{$adminID}.php";
        if($uptype=='all' || empty($mkvalue))
        {
            ShowMsg("不需要進行初處理，現更新所有欄目！", "makehtml_list_action.php?gotype=mkallct");
            exit();
        }
        else
        {
            if($uptype=='time')
            {
                $query = "SELECT  DISTINCT typeid From `#@__arctiny` WHERE senddate >=".GetMkTime($mkvalue)." AND arcrank>-1";
            }
            else
            {
                $query = "SELECT DISTINCT typeid From `#@__arctiny` WHERE id>=$mkvalue AND arcrank>-1";
            }
            $dsql->SetQuery($query);
            $dsql->Execute();
            while($row = $dsql->GetArray())
            {
                $typeids[$row['typeid']] = 1;
            }

            foreach($typeids as $k=>$v)
            {
                $vs = array();
                $vs = GetParentIds($k);
                if( !isset($typeidsok[$k]) )
                {
                    $typeidsok[$k] = 1;
                }
                foreach($vs as $k=>$v)
                {
                    if(!isset($typeidsok[$v]))
                    {
                        $typeidsok[$v] = 1;
                    }
                }
            }
        }
        $fp = fopen($mkcachefile,'w') or die("無法寫入緩存文件：{$mkcachefile} 所以無法更新欄目！");
        if(count($typeidsok)>0)
        {
            fwrite($fp,"<"."?php\r\n");
            $i = -1;
            foreach($typeidsok as $k=>$t)
            {
                if($k!='')
                {
                    $i++;
                    fwrite($fp, "\$idArray[$i]={$k};\r\n");
                }
            }
            fwrite($fp,"?".">");
            fclose($fp);
            ShowMsg("完成欄目緩存處理，現轉向更新欄目！","makehtml_list_action.php?gotype=mkall");
            exit();
        }
        else
        {
            fclose($fp);
            ShowMsg("沒有可更新的欄目，現在作最後數據優化！","makehtml_all.php?action=make&step=10");
            exit();
        }
    }
    //成功狀態
    /*-------------------
    function _10_MakeAllOK()
    --------------------*/
    else if($step==10)
    {
        $adminID = $cuserLogin->getUserID();
        $mkcachefile = DEDEDATA."/mkall_cache_{$adminID}.php";
        @unlink($mkcachefile);
        OptimizeData($dsql);
        ShowMsg("完成所有文件的更新！","javascript:;");
        exit();
    }//make step

} //action=='make'

/**
 *  優化數據
 *
 * @access    public
 * @param     object  $dsql  數據庫對像
 * @return    void
 */
function OptimizeData($dsql)
{
    global $cfg_dbprefix;
    $tptables = array("{$cfg_dbprefix}archives","{$cfg_dbprefix}arctiny");
    $dsql->SetQuery("SELECT maintable,addtable FROM `#@__channeltype` ");
    $dsql->Execute();
    while($row = $dsql->GetObject())
    {
        $addtable = str_replace('#@__',$cfg_dbprefix,$row->addtable);
        if($addtable!='' && !in_array($addtable,$tptables)) $tptables[] = $addtable;
    }
    $tptable = '';
    foreach($tptables as $t) $tptable .= ($tptable=='' ? "`{$t}`" : ",`{$t}`" );
    $dsql->ExecuteNoneQuery(" OPTIMIZE TABLE $tptable; ");
}