轻松实现织梦网站数据迁移到新站点

简介: 众所周知,织梦已经开始收费了,这对国内版权意识增强应该不算坏事,但想要免费使用又不想惹麻烦的站长们就有点麻烦了。

众所周知,织梦已经开始收费了,这对国内版权意识增强应该不算坏事,但想要免费使用又不想惹麻烦的站长们就有点麻烦了。

有不少朋友来问,我们 MyCms 支不支持织梦数据迁移,目前我们已经实现一键导入织梦的原文章和商品了,现在简要讲述一下实现过程。

一、连接数据库

要想实现数据的迁移导入,那么先要得到数据库信息,所以我们第一步就要实现填写数据库信息功能。

11.png

可以打开织梦网站的 data/common.inc.php 文件对照填写。

单次导入数据字段为执行一次导入多少量的数据,默认100条,这个可以依照自己的服务器来调整。

这一步仅仅是保存数据库信息,别无他用。

附上连接数据库代码

//$this->config 为保存的数据库信息

$dedeConnection = array_merge([
    'driver' => 'mysql',
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => $this->config['dede_prefix'],
], $this->config);

config(['database.connections.dedecms' => $dedeConnection]);

$this->connection = DB::connection('dedecms');

二、导入分类/文章

1.导入文章分类,并明确上下级关系。

public function articleCategory()
{
    if (!Storage::exists("dede_article_category")) {

        //导入分类
        $categories = $this->connection
            ->table('arctype')->get();

        $catArray = $catParentArray = [];

        foreach ($categories as $category) {

            $cid = ArticleCategory::insert([
                'pid' => 0,
                'name' => $category->typename,
            ]);

            $catArray[$category->id] = $cid;
            $catParentArray[$cid] = $category->topid;
        }

        foreach ($catParentArray as $key => $value) {

            if ($value > 0) {

                $ac = ArticleCategory::find($key);
                $ac->pid = $catArray[$value];
                $ac->save();
            }
        }

        Storage::put("dede_article_category", json_encode($catArray));

    } else {

        $catArray = json_decode(Storage::get("dede_article_category"), true);
    }

    return $catArray;
}

2.要先明确要导入文章的那些信息,然后对应好自身系统的字段,开始导入。

附上 MyCms 导入的代码给大家参考。


public function article(): JsonResponse
{
    $date = date('Y-m-d H:i:s');
       //最后导入ID
    $lastId = Storage::exists("dede_article_last_id") ? Storage::get("dede_article_last_id") : 0;

    $articles = $this->connection
        ->table('archives')
        ->leftJoin('addonarticle', 'aid', '=', 'id')
        ->where([
            ['channel', '=', 1],
            ['id', '>', $lastId],
        ])->limit($this->config['batch_number'])->get();

    $importLog = [];
    $catArray = $this->articleCategory();

    foreach ($articles as $article) {

        $aid = Article::insert([
            'category_id' => $catArray[$article->typeid],
            'title' => $article->title,
            'content' => $article->body,
            'description' => $article->description,
            'img' => $article->litpic,
            'author' => $article->writer,
            'view' => $article->click,
            'created_at' => date('Y-m-d H:i:s', $article->senddate),
            'updated_at' => date('Y-m-d H:i:s', $article->pubdate),
        ]);

        if ($article->shorttitle) {

            $meta = [
                'article_id' => $aid,
                'meta_key' => 'short_title',
                'meta_value' => $article->shorttitle,
            ];

            ArticleMeta::insert($meta);
        }

        $lastId = $article->id;

        $tagIds = (new ArticleTag)->insertTags(explode(",", trim($article->keywords, ",")));
        (new ArticleTagRel)->insertRel($aid, $tagIds);

              //导入记录
              $importLog[] = [
            'type' => '文章',
            'oid' => $article->id,
            'mid' => $aid,
            'title' => $article->title,
            'created_at' => $date,
            'updated_at' => $date,
        ];
    }

    Dedecms::insertAll($importLog);
       //写入导入最后ID
    Storage::put("dede_article_last_id", $lastId);

    return $this->result(true);
}

三、导入商品

导入商品也是一样的道理,就不多少,直接附上代码。

public function goods()
{
    $date = date('Y-m-d H:i:s');

    $lastId = Storage::exists("dede_goods_last_id") ? Storage::get("dede_goods_last_id") : 0;

    $articles = $this->connection
        ->table('archives')
        ->leftJoin('addonshop', 'aid', '=', 'id')
        ->where([
            ['channel', '=', 6],
            ['id', '>', $lastId],
        ])->limit($this->config['batch_number'])->get();

    $importLog = [];
    $catArray = $this->goodsCategory();

    foreach ($articles as $article) {

        $aid = Goods::insert([
            'category_id' => $catArray[$article->typeid],
            'goods_name' => $article->title,
            'content' => $article->body,
            'description' => $article->description,
            'goods_image' => $article->litpic,
            'view' => $article->click,
            'shop_price' => $article->trueprice ?: $article->price,
            'market_price' => $article->price,
            'created_at' => date('Y-m-d H:i:s', $article->senddate),
            'updated_at' => date('Y-m-d H:i:s', $article->pubdate),
        ]);

        if ($article->shorttitle) {

            $meta = [
                'goods_id' => $aid,
                'meta_key' => 'short_title',
                'meta_value' => $article->shorttitle,
            ];

            GoodsMeta::insert($meta);
        }

        $lastId = $article->id;

        $importLog[] = [
            'type' => '商品',
            'oid' => $article->id,
            'mid' => $aid,
            'title' => $article->title,
            'created_at' => $date,
            'updated_at' => $date,
        ];
    }

    Dedecms::insertAll($importLog);

    Storage::put("dede_goods_last_id", $lastId);

    return $this->result(true);
}

最后导入成功,并记录下来。

222.png

目录
相关文章
|
关系型数据库 MySQL 数据库
WordPress网站迁移麻烦吗?用WordPress建站靠谱吗?
因某些原因将WordPress网站迁移到新域,下面北京六翼信息技术有限公司的开发工程师通过分析您可能的迁移原因来给出做这些操作前的准备工作。将 WordPress 网站移至新域的原因 以下是您可能需要将网站移至新域的几个示例。 您购买了一个更好的域名——也许您一直想要的域名终于可用了,因此您准备好更改您的网站或博客的 URL。
WordPress网站迁移麻烦吗?用WordPress建站靠谱吗?
|
数据库
ecshop网站搬家过程中数据库太大不好备份解决方案
ecshop网站搬家过程中数据库太大不好备份解决方案
|
Web App开发 关系型数据库 测试技术
|
数据库 数据安全/隐私保护 Apache
如何将网站程序迁移至URLOS
准备工作: 网站程序文件 网站程序数据库文件(xxx.sql) 操作流程: 安装网站程序需要的环境。(本次案例使用php5.6+apache) 安装phpMyadmin数据库管理工具。 使用phpMyadmin导入网站程序的数据库文件。
1107 0
|
数据库
外贸狗教你WordPress从本地环境迁移到网站主机
我们把外贸网站建设的流程都用文字记录了一遍,但是最重要的一步还没有讲,那就是wordpress网站从本地环境迁移到网站主机,也就是“网站上线”。 网站上线前需要准备: 域名(如果是给歪果仁看的,不需要备案) 虚拟主机 这两个都容易,去阿里云,Godaddy或者Namesilo注册就好了。
2581 0
|
监控 安全 搜索推荐
网站百度快照被劫持怎么办
网站安全服务 网站安全是重中之重对此我们Sinesafe对于网站被挂马被黑的防范意识,如何判断网站被黑,网站被挂马,网站快照被劫持,网站快照被篡改,导致被百度网址安全中心提醒您:该页面可能存在违法信息。
3048 0
|
安全 PHP
站长dedecms网站被挂马清理过程与分析解决
最近收到一位客户的反馈,告知网站又被挂马,(织梦程序真让人头疼总是被挂马,dedecms经常是被挂马真晕了是的~)相信站长们都有遇到过网站被挂马或代码恶意植入的问题。下面把处理流程写下来,帮助大家了解并简单处理的过程。
1542 0