banner
MoLeft

MoLeft's xLog

Hey! Welcome to my Xlog. The no blackchains on https://www.moleft.cn
github
email

将Typecho文章导入xLog

如何导入#

xLog 可以直接导入 markdown 文件并且支持 front matter 所以,只需要从数据库里面将文章导出,并且设置成 hexo 的格式,就可以了。

导入过程#

直接使用 php 的 Pdo 将数据库中的 typecho_contents 表导出为文件就好了,代码如下,可以根据自己的实际情况微调。

<?php

// 连接到数据库
$dsn = 'mysql:host=localhost;dbname=blog;charset=utf8';
$username = 'root';
$password = '123456';

$conn = new PDO($dsn, $username, $password);

// 设置错误处理
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// 执行查询
$sql = 'SELECT * FROM `typecho_contents` WHERE `type` = "post"';
$stmt = $conn->prepare($sql);
$stmt->execute();

// 遍历结果集
$results = $stmt->fetchAll();
foreach ($results as $result) {
    // 输出数据
    echo $result['title'] . "\n\n";

    // 获取标签与文章关联关系
    $s_sql = 'SELECT * FROM `typecho_relationships` WHERE `cid`=' . $result['cid'];
    $stmt2 = $conn->prepare($s_sql);
    $stmt2->execute();
    $relationships = [];
    foreach($stmt2->fetchAll() as $s){
        $relationships[] = $s['mid'];
    }

    // 获取标签名
    $t_sql = 'SELECT `name` FROM `typecho_metas` WHERE `mid` in (\''. implode("','", $relationships) .'\') and `type`=\'\'';
    $stmt3 = $conn->prepare($t_sql);
    $stmt3->execute();
    $tags = [];
    foreach($stmt3->fetchAll() as $t){
        $tags[] = $t['name'];
    }

    // 生成 Markdown 文件
    $str = '---
tags:
 - '. implode("\n - ", $tags) .'
type: note
title: "' . $result['title'] . '"
date: ' . date('Y-m-d H:i:s', $result['created']) . '
updated: ' . date('Y-m-d H:i:s', $result['modified']) . '
---
';
    file_put_contents(__DIR__ . '/blog/' . str_replace('/', '_', $result['title']) . '.md', str_replace('<!--markdown-->', $str, $result['text']));
}

// 关闭连接
$conn = null;

文件会存在当前目录下的 blog 文件夹 (没有需要自己建,因为我懒得没写 mkdir )

一些问题#

目前存在以下问题还有待解决。

  1. 导出的 md 文件 标签 以及 最后修改时间 不生效
  2. 导入 xLog 后无法修改标签 (应该是 xLog 的 bug)
  3. 图片无法自动上传到 IPFS

最后一点是让我最难受的,所以又把上传的文章删掉了,等我找到解决办法再迁移。

加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。