DISCUZ版本过低腰升级的话,官方好像也没有升级程序,让DISCUZ提供技术支持,费用太贵,自己处理,以下是核心代码.
注意:cdb_templates 这个表不能导,否则一清空缓存就乱了,切记,花了一天时间才找到.
主程序:
- set_time_limit(9999999);
- include "inc/init.php
- cls::getDatabase()->start();
类程序:
- class common_database{
- //所有有数据的表
- private $_needtable = null;
- //表对应关系 - 优先这样导入
- private $_distablearr = array(
- 'cdb_members'=>array('yzlt_common_member','yzlt_ucenter_members'),
- 'cdb_settings'=>'yzlt_ucenter_settings',
- ''=>''
- );
- //明确禁止导入的表
- private $_disable_table = array('cdb_templates','cdb_buddys','cdb_caches','cdb_favorites','cdb_memberspaces','cdb_mythreads','cdb_myposts','cdb_projects','cdb_stats');
- //构造函数
- public function __construct()
- {
- }
- public function start(){
- //
- $rst = $this->getNeedTable();
- foreach($rst as $tb){
- $this->importTable($tb);//逐个导入表
- //break;
- }
- //收尾工作
- $this->endDoing();
- }
- private function endDoing(){
- //给会员再加密
- $rs = cls::getDB()->getRowsNew("select uid,password from yzlt_ucenter_members");$num = count($rs);
- for($i=0;$i<$num;$i++){
- cls::getDB()->Query("update yzlt_ucenter_members set password='".md5($rs[$i]["password"])."' where uid='".$rs[$i]["uid"]."'");
- }
- //其他操作
- }
- private function getShoulTable($tb){
- $DB1 = cls::getDB(1);
- $sqla = "describe ".$tb."";
- $rsa = $DB1->getRowsNew($sqla);//原来表字段
- $numa = count($rsa);
- $DB = cls::getDB();
- $sql = "show tables";
- $rs = $DB->getRowsNew($sql);$num = count($rs);
- $samenumtj = 0;$mytb='';
- for($i=0;$i<$num;$i++){
- $rscc = $DB->getRowsNew("describe ".$rs[$i][0]);$numcc = count($rscc);
- $samezd = 0;
- for($k1=0;$k1<$numa;$k1++){
- for($k=0;$k<$numcc;$k++){
- if($rsa[$k1][0]==$rscc[$k][0]){
- $samezd++;
- }
- }
- }
- //echo "
- 源表:".$tb.",目标表:".$rs[$i][0].",相同字段数:".$samezd."";
- if((int)$samezd>(int)$samenumtj){
- //echo "----------------".$samenumtj;
- $samenumtj = $samezd;$mytb = $rs[$i][0];
- }
- //$samefield = array_intersect($rsa,$rscc);$samenum = count($samefield);
- //if($samenum>=$samenumtj){
- // $samenumtj = $samenum;$mytb = $rs[$i][0];
- // echo "表名:".$mytb.",相同字段数:".$samenumtj."
- ";
- //}
- }
- //return $mytb."-".$samenumtj;
- return $mytb;
- }
- private function getNeedTable(){
- if(!isset($this->_needtable)){
- $DB1 = cls::getDB(1);
- $sql = "show tables";$table = array();
- $rs = $DB1->getRowsNew($sql);$num = count($rs);
- for($i=0;$i<$num;$i++){
- $sqla = "select count(*) from ".$rs[$i][0]."";$rsa = $DB1->getRowsNew($sqla);
- if($rsa[0][0]>0){
- $table[] = $rs[$i][0];
- }
- }
- $this->_needtable = $table;
- }
- return $this->_needtable;
- }
- private function getDistTable($tb){
- if(isset($this->_distablearr[$tb])){
- return $this->_distablearr[$tb];
- }else{
- //如果是明确禁止的,不要再找了
- if(in_array($tb,$this->_disable_table)){return "";}
- return $this->getShoulTable($tb);
- }
- }
- private function getFieldSource($tb){
- $DB1 = cls::getDB(1);
- $sql = "describe ".$tb;
- return $DB1->getRowsNew($sql);
- }
- private function getFieldDist($tb){
- $DB = cls::getDB(0);
- $sql = "describe ".$tb;
- return $DB->getRowsNew($sql);
- }
- private function getImportField($fieldsource,$fielddist){
- $backzd = array();
- for($i=0;$i
- $zd = $fieldsource[$i][0];
- for($k=0;$k
- if($zd == $fielddist[$k][0]){
- $backzd[] = $zd;
- }
- }
- }
- return $backzd;
- }
- private function getFieldValueStr($zd,$rssource,$i){
- $str="";
- foreach($zd as $field){
- $rssource[$i][$field] = str_replace("'","/'",$rssource[$i][$field]);$rssource[$i][$field] = str_replace("///'","/'",$rssource[$i][$field]);
- $rssource[$i][$field] = str_replace("(","/(",$rssource[$i][$field]);$rssource[$i][$field] = str_replace("///(","/(",$rssource[$i][$field]);
- $rssource[$i][$field] = str_replace(")","/)",$rssource[$i][$field]);$rssource[$i][$field] = str_replace("///)","/)",$rssource[$i][$field]);
- if($str==""){
- $str="'".$rssource[$i][$field]."'";
- }else{
- $str.=",'".$rssource[$i][$field]."'";
- }
- }
- return $str;
- }
- private function importTable($tb){
- $disTable = $this->getDistTable($tb); //获取目标表
- if(!is_array($disTable) && $disTable==''){echo "".$tb." not find distTable
- ";return false;}
- if(!is_array($disTable)){$disTable = array($disTable);}
- foreach($disTable as $mytable){
- echo "源表:".$tb.",导入到目的表:".$mytable."
- ";
- //删除目标表所有数据
- cls::getDB(0)->Query("delete from ".$mytable." where 1");
- //开始导入 - 先获取可导入的字段(目标和源头一样的字段)
- $fieldsource = $this->getFieldSource($tb); $fielddist = $this->getFieldDist($mytable);
- $rsf = $this->getImportField($fieldsource,$fielddist);
- //读取源表数据
- for($p=0;$p<1000;$p++){
- $limit_begin = $p*1000;
- $rssource = cls::getDB(1)->getRowsNew("select * from ".$tb." limit ".$limit_begin.",1000 ");$numsource = count($rssource);
- if($numsource == 0){break;}
- for($i=0;$i<$numsource;$i++){
- $sqla = "insert into ".$mytable."(".implode(',',$rsf).")values(".$this->getFieldValueStr($rsf,$rssource,$i).") ";
- //echo $sqla." //开源软件:Vevb.com
- ";
- cls::getDB(0)->Query($sqla);
- }
- }
- }
- }
- }
新闻热点
疑难解答