輕鬆實現織夢網站資料遷移到新站點

苦逼的戰鬥雞發表於2022-01-23

眾所周知,織夢已經開始收費了,這對國內版權意識增強應該不算壞事,但想要免費使用又不想惹麻煩的站長們就有點麻煩了。

有不少朋友來問,我們 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

相關文章