Easily realize the migration of dream weaving website data to a new site

As we all know, Dream Weaving has begun to charge, which should not be a bad thing for the enhancement of domestic copyright awareness, but it is a little troublesome for webmasters who want to use it for free and do not want to cause trouble.

Many friends have come to ask if our MyCms support does not support data migration of Weaving Dreams. At present, we have realized the one-click import of the original articles and products of Weaving Dreams. Now we will briefly describe the implementation process.

1. Connect to the database

In order to realize the migration and import of data, you must first get the database information, so our first step is to realize the function of filling in the database information.

11.png

You can open the data/common.inc.php file of the Dreamweaving website and fill in it for comparison.

The single import data field is the amount of data to be imported at one time, the default is 100, which can be adjusted according to your own server.

This step is just to save the database information, nothing else.

Attach the code to connect to the database

//$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');

2. Import categories/articles

1. Import article classification, and clarify the relationship between superiors and subordinates.

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. You must first clarify the information to be imported into the article, and then correspond to the fields of your own system and start importing.

The code imported by MyCms is attached for your reference.


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);
}

3. Import goods

The same is true for importing goods, there are not many, and the code is directly attached.

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);
}

Finally, the import was successful and recorded.

222.png

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324109119&siteId=291194637