风讯4.0数据迁入dede系统的简单脚本

公司老网站改版,新版本用的是dede5.7,老版是风讯4.0 没有现成的工具.自己写了一个简单的.有需要把其它系统迁入到dede的童鞋可以参考下.
思路如下.关键是四个表
dede_arctype 目录表
dede_arctiny 文章添加时间
dede_archives 文章标题索引
dede_addonarticle 文章内容
先把风讯的数据导入到mysql里面.
添加完成后,需要更新下缓存.否则文章分类列表那显示不正常.
强烈建议cli下运行,否则,时间长了.页面会失去响应.
下面直接贴代码了.

<?php

/**
 * @author kingHack
 * @copyright 2013
 * @example 风讯库导入新库
 */

//后台导,不需要uft-8
header( 'Content-Type:text/html; charset=gb2312' );
//定义下数据库参数
function GlobalConnectionOptions_old()
{
    return array(
        'server' => '127.0.0.1',
        'port' => '3306',
        'username' => 'admin',
        'password' => '',
        'database' => 'foosun' );
}
//进行数据库连接
$mysql_result = GlobalConnectionOptions_old();
//获取数据库连接指针
$con = mysql_connect( $mysql_result['server'], $mysql_result['username'], $mysql_result['password'] );
//var_dump($con);
if ( !$con )
{
    die( 'Could not connect: ' . mysql_error() );
}
mysql_query( "SET NAMES 'GBK'" ); //改一下编码否则会乱码
$source_selected = mysql_select_db( $mysql_result['database'], $con ); //打开数据库
if ( !$source_selected )
{
    die( 'Can\'t use base : ' . mysql_error() );
}


//添加一个顶级目录
$var_insert_code_class =
    "INSERT INTO dedecmstest.dede_arctype ( sortrank, typename, typedir, isdefault, issend, tempindex, templist, temparticle, namerule, namerule2, modname,  sitepath, content, crossid, smalltypes ) VALUES (50 ,'zk100', '{cmspath}/a/zk100', - 1, 1, '{style}/index_article.htm', '{style}/list_article.htm', '{style}/article_article.htm', '{typedir}/{Y}/{M}{D}/{aid}.html', '{typedir}/list_{tid}_{page}.html', 'default', '{cmspath}/a/zk100', '&nbsp;', '', '' )";
//var_dump($var_insert_code_class);
//echo "\n";
mysql_query( $var_insert_code_class, $con );
//需要把前面插入的数据的行号返回,作为新参数插入
//新参数是新表的分类代号
$var_sql_code_class =
    "SELECT dedecmstest.dede_arctype.id FROM dedecmstest.dede_arctype WHERE dede_arctype.typename = 'zk100'";
$zk100_result = mysql_query( $var_sql_code_class, $con ); //获取数据集
$zk100_row = mysql_fetch_array( $zk100_result ); //获取数据集
//var_dump( $zk100_row['id'] );

define( 'ZK', '_zk100' );
//注意有三级目录层次
//查询旧表分类
$var_sql_code_class =
    "SELECT fs_ns_newsclass.ID, fs_ns_newsclass.ClassID, fs_ns_newsclass.ParentID, fs_ns_newsclass.ClassName, fs_ns_newsclass.ClassEName FROM foosun.fs_ns_newsclass WHERE fs_ns_newsclass.IsURL = 0 AND fs_ns_newsclass.ParentID = '0' ORDER BY fs_ns_newsclass.ID ASC";
$source_result_class = mysql_query( $var_sql_code_class, $con ); //获取数据集
//因为要循环读取数据,所以得先判断下有多少列
if ( mysql_num_rows( $source_result_class ) > 0 )
{
    while ( $source_row_class = mysql_fetch_array( $source_result_class ) )
    {
        //var_dump( $source_row_class['ID'],$source_row_class['ClassID'],$source_row_class['ParentID'],$source_row_class['ClassName'],$source_row_class['ClassEName']);
        //echo "\n";
        //提取大类,名字全部添加个zk100的后缀
        $var_insert_code_class =
            "INSERT INTO dedecmstest.dede_arctype ( sortrank, typename, typedir, isdefault, issend, tempindex, templist, temparticle, namerule, namerule2, modname,  sitepath, content, crossid, smalltypes,reid,topid ) VALUES ( 50, '" .
            $source_row_class['ClassName'] . ZK . "', '{cmspath}/a/" . $source_row_class['ClassEName'] .
            "', - 1, 1, '{style}/index_article.htm', '{style}/list_article.htm', '{style}/article_article.htm', '{typedir}/{Y}/{M}{D}/{aid}.html', '{typedir}/list_{tid}_{page}.html', 'default', '{cmspath}/a/" .
            $source_row_class['ClassEName'] . "', '', '', ''," . $zk100_row['id'] . "," . $zk100_row['id'] .
            " )";
        //var_dump($var_insert_code_class);
        //echo "\n";
        mysql_query( $var_insert_code_class, $con );
        //exit();
        //需要把前面插入的数据的行号返回,作为新参数插入
        //新参数是新表的分类代号
        $var_sql_code_class =
            "SELECT dedecmstest.dede_arctype.id FROM dedecmstest.dede_arctype WHERE dede_arctype.typename = '" .
            $source_row_class['ClassName'] . ZK . "'";
        //var_dump( $var_sql_code_class );
        //echo "\n";
        $a_result = mysql_query( $var_sql_code_class, $con ); //获取数据集
        $a_row = mysql_fetch_array( $a_result ); //获取数据集

        $var_int_a = 1; //运行标识1运行 0放弃

        //查询是否有一级子类
        $var_sql_code_tree =
            "SELECT fs_ns_newsclass.ID, fs_ns_newsclass.ClassID, fs_ns_newsclass.ParentID, fs_ns_newsclass.ClassName, fs_ns_newsclass.ClassEName FROM foosun.fs_ns_newsclass WHERE fs_ns_newsclass.IsURL = 0 AND fs_ns_newsclass.ParentID = '" .
            $source_row_class['ClassID'] . "' ORDER BY fs_ns_newsclass.ID ASC";
        $source_tree_result = mysql_query( $var_sql_code_tree, $con ); //获取数据集
        if ( mysql_num_rows( $source_tree_result ) > 0 )
        {
            //var_dump( $var_sql_code_tree );
            //echo "\n";
            while ( $source_tree_row = mysql_fetch_array( $source_tree_result ) )
            {
                //var_dump( $source_tree_row );
                //echo "\n";
                //添加二级目录
                $var_insert_code = "INSERT INTO dedecmstest.dede_arctype ( sortrank, typename, typedir, isdefault, issend, tempindex, templist, temparticle, namerule, namerule2, modname,  sitepath, content, crossid, smalltypes,reid,topid ) VALUES ( 50, '" .
                    $source_tree_row['ClassName'] . ZK . "', '{cmspath}/a/" . $source_tree_row['ClassEName'] .
                    "', - 1, 1, '{style}/index_article.htm', '{style}/list_article.htm', '{style}/article_article.htm', '{typedir}/{Y}/{M}{D}/{aid}.html', '{typedir}/list_{tid}_{page}.html', 'default', '{cmspath}/a/" .
                    $source_tree_row['ClassEName'] . "', '', '', ''," . $a_row['id'] . "," . $zk100_row['id'] .
                    " )";
                mysql_query( $var_insert_code, $con );
                //var_dump( $var_insert_code );
                //echo "\n";
                //查询新表中的分类编码
                $var_sql_code = "SELECT dedecmstest.dede_arctype.id FROM dedecmstest.dede_arctype WHERE dede_arctype.typename = '" .
                    $source_tree_row['ClassName'] . ZK . "'";
                $b_result = mysql_query( $var_sql_code, $con ); //获取数据集
                $b_row = mysql_fetch_array( $b_result ); //获取数据集


                //查询是否有叶三级分类
                //查询是否有下级子类
                $var_sql_code = "SELECT fs_ns_newsclass.ID, fs_ns_newsclass.ClassID, fs_ns_newsclass.ParentID, fs_ns_newsclass.ClassName, fs_ns_newsclass.ClassEName FROM foosun.fs_ns_newsclass WHERE fs_ns_newsclass.IsURL = 0 AND fs_ns_newsclass.ParentID = '" .
                    $source_tree_row['ClassID'] . "' ORDER BY fs_ns_newsclass.ID ASC";
                $source_leaf_result = mysql_query( $var_sql_code, $con ); //获取数据集
                if ( mysql_num_rows( $source_leaf_result ) > 0 )
                {
                    while ( $source_leaf_row = mysql_fetch_array( $source_leaf_result ) )
                    {
                        $var_insert_code = "INSERT INTO dedecmstest.dede_arctype ( sortrank, typename, typedir, isdefault, issend, tempindex, templist, temparticle, namerule, namerule2, modname,  sitepath, content, crossid, smalltypes,reid,topid ) VALUES ( 50, '" .
                            $source_leaf_row['ClassName'] . ZK . "', '{cmspath}/a/" . $source_leaf_row['ClassEName'] .
                            "', - 1, 1, '{style}/index_article.htm', '{style}/list_article.htm', '{style}/article_article.htm', '{typedir}/{Y}/{M}{D}/{aid}.html', '{typedir}/list_{tid}_{page}.html', 'default', '{cmspath}/a/" .
                            $source_leaf_row['ClassEName'] . "', '', '', ''," . $b_row['id'] . "," . $a_row['id'] .
                            " )";
                        mysql_query( $var_insert_code, $con );
                        //var_dump( $var_insert_code );
                        //echo "\n";
                        //查询新表中的分类编码
                        $var_sql_code = "SELECT dedecmstest.dede_arctype.id FROM dedecmstest.dede_arctype WHERE dede_arctype.typename = '" .
                            $source_leaf_row['ClassName'] . ZK . "'";
                        $c_result = mysql_query( $var_sql_code, $con ); //获取数据集
                        $c_row = mysql_fetch_array( $c_result ); //获取数据集

                        if ( $var_int_a == 1 )
                        {
                            //$var_int_a = 0;
                            //先提取旧数据库表记录,然后在插入到新表里

                            $var_sql_code = "SELECT fs_ns_news.ID, fs_ns_news.NewsID, fs_ns_news.ClassID, fs_ns_news.Hits, fs_ns_news.NewsTitle, fs_ns_news.Content,  unix_timestamp(fs_ns_news.addtime) as addtimes FROM fs_ns_news WHERE fs_ns_news.ClassID = '" .
                                $source_leaf_row['ClassID'] . "' ORDER BY fs_ns_news.ID ASC";

                            $article_source_result = mysql_query( $var_sql_code, $con ); //获取数据集
                            //var_dump( $var_sql_code, $article_source_result );
                            //因为要循环读取数据,所以得先判断下有多少列
                            if ( mysql_num_rows( $article_source_result ) > 0 )
                            {
                                while ( $article_source_row = mysql_fetch_array( $article_source_result ) )
                                {
                                    //var_dump( $article_source_row );
                                    //echo "\n";
                                    //执行顺序
                                    //dede_arctiny.执行获取对应的ID。然后dede_addonarticle->dede_archives
                                    //添加更新时间
                                    $var_insert_code = "INSERT INTO dedecmstest.dede_arctiny ( typeid, senddate, sortrank, MID) VALUES ( " .
                                        $c_row['id'] . ", " . $article_source_row['addtimes'] . ", " . $article_source_row['addtimes'] .
                                        ", 1 )";
                                    //var_dump( $var_insert_code );
                                    //echo "\n";
                                    //exit();
                                    mysql_query( $var_insert_code, $con );
                                    $var_sql_code = "SELECT dede_arctiny.id FROM dedecmstest.dede_arctiny ORDER BY dede_arctiny.id DESC LIMIT 1";
                                    //var_dump($var_insert_code);
                                    $article_a_result = mysql_query( $var_sql_code, $con ); //获取数据集
                                    $article_a_row = mysql_fetch_array( $article_a_result );
                                    $var_int_a_id = $article_a_row['id'];

                                    //添加文章内容
                                    $var_insert_code = "INSERT INTO dedecmstest.dede_addonarticle ( aid, typeid, body, redirecturl, templet, userip ) VALUES (" .
                                        $var_int_a_id . ", " . $c_row['id'] . ", '" . $article_source_row['Content'] .
                                        "', '', '', '127.0.0.1' )";
                                    mysql_query( $var_insert_code, $con );

                                    //添加文章标题属性等
                                    $var_insert_code = "INSERT INTO dedecmstest.dede_archives ( id, typeid, typeid2, sortrank, flag, ismake, channel, arcrank, click, money, title, shorttitle, color, writer, source, litpic, pubdate, senddate, MID, keywords, lastpost, scores, goodpost, badpost, voteid, notpost, description, filename, dutyadmin, tackid, mtype, weight ) VALUES ( " .
                                        $var_int_a_id . ", " . $c_row['id'] . ", 0, " . $article_source_row['addtimes'] .
                                        ", '', - 1, 1, 0, " . $article_source_row['Hits'] . ", 0, '" . $article_source_row['NewsTitle'] .
                                        "', '', '', 'admin', '未知', '', '" . $article_source_row['addtimes'] . "', '" . $article_source_row['addtimes'] .
                                        "', 1, '', 0, 0, 0, 0, 0, 0, '', '', 1, 0, 0, 1 )";
                                    mysql_query( $var_insert_code, $con );
                                }
                            }


                        }
                    }
                }

                //针对二级分类的
                if ( $var_int_a == 1 )
                {

                    //$var_int_a = 0;
                    //先提取旧数据库表记录,然后在插入到新表里

                    $var_sql_code = "SELECT fs_ns_news.ID, fs_ns_news.NewsID, fs_ns_news.ClassID, fs_ns_news.Hits, fs_ns_news.NewsTitle, fs_ns_news.Content,  unix_timestamp(fs_ns_news.addtime) as addtimes FROM fs_ns_news WHERE fs_ns_news.ClassID = '" .
                        $source_tree_row['ClassID'] . "' ORDER BY fs_ns_news.ID ASC ";

                    $article_source_result = mysql_query( $var_sql_code, $con ); //获取数据集
                    //var_dump( $var_sql_code, $article_source_result );
                    //因为要循环读取数据,所以得先判断下有多少列
                    if ( mysql_num_rows( $article_source_result ) > 0 )
                    {
                        while ( $article_source_row = mysql_fetch_array( $article_source_result ) )
                        {
                            //var_dump( $article_source_row );
                            //echo "\n";
                            //执行顺序
                            //dede_arctiny.执行获取对应的ID。然后dede_addonarticle->dede_archives
                            //添加更新时间
                            $var_insert_code = "INSERT INTO dedecmstest.dede_arctiny ( typeid, senddate, sortrank, MID) VALUES ( " .
                                $b_row['id'] . ", " . $article_source_row['addtimes'] . ", " . $article_source_row['addtimes'] .
                                ", 1 )";
                            //var_dump( $var_insert_code );
                            //echo "\n";
                            //exit();
                            mysql_query( $var_insert_code, $con );
                            $var_sql_code = "SELECT dede_arctiny.id FROM dedecmstest.dede_arctiny ORDER BY dede_arctiny.id DESC LIMIT 1";
                            //var_dump($var_insert_code);
                            $article_a_result = mysql_query( $var_sql_code, $con ); //获取数据集
                            $article_a_row = mysql_fetch_array( $article_a_result );
                            $var_int_a_id = $article_a_row['id'];

                            //添加文章内容
                            $var_insert_code = "INSERT INTO dedecmstest.dede_addonarticle ( aid, typeid, body, redirecturl, templet, userip ) VALUES (" .
                                $var_int_a_id . ", " . $b_row['id'] . ", '" . $article_source_row['Content'] .
                                "', '', '', '127.0.0.1' )";
                            mysql_query( $var_insert_code, $con );

                            //添加文章标题属性等
                            $var_insert_code = "INSERT INTO dedecmstest.dede_archives ( id, typeid, typeid2, sortrank, flag, ismake, channel, arcrank, click, money, title, shorttitle, color, writer, source, litpic, pubdate, senddate, MID, keywords, lastpost, scores, goodpost, badpost, voteid, notpost, description, filename, dutyadmin, tackid, mtype, weight ) VALUES ( " .
                                $var_int_a_id . ", " . $b_row['id'] . ", 0, " . $article_source_row['addtimes'] .
                                ", '', - 1, 1, 0, " . $article_source_row['Hits'] . ", 0, '" . $article_source_row['NewsTitle'] .
                                "', '', '', 'admin', '未知', '', '" . $article_source_row['addtimes'] . "', '" . $article_source_row['addtimes'] .
                                "', 1, '', 0, 0, 0, 0, 0, 0, '', '', 1, 0, 0, 1 )";
                            mysql_query( $var_insert_code, $con );
                        }
                    }


                }
            }

        }


        //针对顶级分类的
        if ( $var_int_a == 1 )
        {
            //先提取旧数据库表记录,然后在插入到新表里

            $var_sql_code = "SELECT fs_ns_news.ID, fs_ns_news.NewsID, fs_ns_news.ClassID, fs_ns_news.Hits, fs_ns_news.NewsTitle, fs_ns_news.Content,  unix_timestamp(fs_ns_news.addtime) as addtimes FROM fs_ns_news WHERE fs_ns_news.ClassID = '" .
                $source_row_class['ClassID'] . "' ORDER BY fs_ns_news.ID ASC ";
            //var_dump( $var_sql_code );
            //echo "\n";
            //exit();
            $article_source_result = mysql_query( $var_sql_code, $con ); //获取数据集
            //var_dump( $var_sql_code, $article_source_result );
            //因为要循环读取数据,所以得先判断下有多少列
            if ( mysql_num_rows( $article_source_result ) > 0 )
            {
                while ( $article_source_row = mysql_fetch_array( $article_source_result ) )
                {
                    //执行顺序
                    //dede_arctiny.执行获取对应的ID。然后dede_addonarticle->dede_archives
                    //添加更新时间
                    $var_insert_code = "INSERT INTO dedecmstest.dede_arctiny ( typeid, senddate, sortrank, MID) VALUES ( " .
                        $a_row['id'] . ", " . $article_source_row['addtimes'] . ", " . $article_source_row['addtimes'] .
                        ", 1 )";
                    //var_dump( $var_insert_code );
                    //echo "\n";
                    //exit();
                    mysql_query( $var_insert_code, $con );
                    $var_sql_code = "SELECT dede_arctiny.id FROM dedecmstest.dede_arctiny ORDER BY dede_arctiny.id DESC LIMIT 1";
                    //var_dump($var_insert_code);
                    $article_a_result = mysql_query( $var_sql_code, $con ); //获取数据集
                    $article_a_row = mysql_fetch_array( $article_a_result );
                    $var_int_a_id = $article_a_row['id'];

                    //添加文章内容
                    $var_insert_code = "INSERT INTO dedecmstest.dede_addonarticle ( aid, typeid, body, redirecturl, templet, userip ) VALUES (" .
                        $var_int_a_id . ", " . $a_row['id'] . ", '" . $article_source_row['Content'] .
                        "', '', '', '127.0.0.1' )";
                    mysql_query( $var_insert_code, $con );

                    //添加文章标题属性等
                    $var_insert_code = "INSERT INTO dedecmstest.dede_archives ( id, typeid, typeid2, sortrank, flag, ismake, channel, arcrank, click, money, title, shorttitle, color, writer, source, litpic, pubdate, senddate, MID, keywords, lastpost, scores, goodpost, badpost, voteid, notpost, description, filename, dutyadmin, tackid, mtype, weight ) VALUES ( " .
                        $var_int_a_id . ", " . $a_row['id'] . ", 0, " . $article_source_row['addtimes'] .
                        ", '', - 1, 1, 0, " . $article_source_row['Hits'] . ", 0, '" . $article_source_row['NewsTitle'] .
                        "', '', '', 'admin', '未知', '', '" . $article_source_row['addtimes'] . "', '" . $article_source_row['addtimes'] .
                        "', 1, '', 0, 0, 0, 0, 0, 0, '', '', 1, 0, 0, 1 )";
                    mysql_query( $var_insert_code, $con );
                }
            }
        }
    }
}


mysql_close( $con ); //关闭数据库连接
//看下运行情况
echo "内存:" . ( xdebug_memory_usage() / 1024 ) . " KB  峰值:" . ( xdebug_peak_memory_usage
    () / 1024 ) . ' KB';
echo "\nxdebug计时(毫秒):" . ( xdebug_time_index() * 1000 );

?>

你可能感兴趣的:(风讯4.0数据迁入dede系统的简单脚本)