<?php
class UpAction extends Action{	
    public function _initialize() {
	    header("Content-Type:text/html; charset=utf-8");
		if(is_file(RUNTIME_PATH.'temp/Install/up13.lock')){
			$this->error('GXCMS-1.3版已经升级过了，不需要再次升级!');
		}
    }
	
    public function index(){
		$db_config = array('dbms'=>'mysql','username'=>C('db_user'),'password'=>C('db_pwd'),'hostname'=>C('db_host'),'hostport'=>C('db_port'),'database'=>C('db_name'));	
		$sql = read_file('./views/Install/up13.sql');
		$sql = str_replace('gx_',c('db_prefix'),$sql);
		$this->batQuery($sql,$db_config);
		touch(RUNTIME_PATH.'temp/Install/up13.lock');
		@unlink('./temp/~app.php');
		@unlink('./temp/~runtime.php');
		$this->assign("jumpUrl",'index.php');
		$this->success('恭喜您！GXCMS-1.3升级完成！');
    }
	
	public function batQuery($sql,$db_config){
	    // 连接数据库
		//import('Think.Db.Driver.Dbmysql');
		require THINK_PATH.'/Lib/Think/Db/Driver/DbMysql.class.php';
		$db = new Dbmysql($db_config);
		$sql = str_replace("\r\n", "\n", $sql); 
		$ret = array(); 
		$num = 0; 
		foreach(explode(";\n", trim($sql)) as $query){
			$queries = explode("\n", trim($query)); 
			foreach($queries as $query) { 
				$ret[$num] .= $query[0] == '#' || $query[0].$query[1] == '--' ? '' : $query; 
			} $num++; 
		} 
		unset($sql); 
		foreach($ret as $query) {
			if(trim($query)) { 
			    $db->query($query); 
			} 
		} 
		return true; 
	}								
}
?>