getPrefix();
// ==================== 导出处理 ====================
if (isset($_POST['do_export'])) {
$exportType = $_POST['export_type'];
$exportValue = $_POST['export_value'];
$batch = isset($_POST['batch']) ? intval($_POST['batch']) : 0;
$batchSize = 300; // 每批导出300篇
// 构建查询条件
$whereClause = "";
if ($exportType == 'year' && !empty($exportValue)) {
$year = intval($exportValue);
$start = strtotime($year . '-01-01');
$end = strtotime($year . '-12-31');
$whereClause = " AND created >= " . intval($start) . " AND created <= " . intval($end);
} elseif ($exportType == 'category' && !empty($exportValue)) {
$mid = intval($exportValue);
$whereClause = " AND cid IN (SELECT cid FROM " . $prefix . "relationships WHERE mid = " . $mid . ")";
} elseif ($exportType == 'tag' && !empty($exportValue)) {
$mid = intval($exportValue);
$whereClause = " AND cid IN (SELECT cid FROM " . $prefix . "relationships WHERE mid = " . $mid . ")";
}
// 获取总文章数
$totalSql = "SELECT COUNT(*) as total FROM " . $prefix . "contents WHERE type = 'post' AND status = 'publish'" . $whereClause;
$totalResult = $db->fetchRow($db->query($totalSql));
$totalPosts = $totalResult['total'];
// 如果是第一次请求(非分批),检查是否需要分批
if ($batch === 0 && $totalPosts > $batchSize) {
// 需要分批导出,返回提示页面
$totalBatches = ceil($totalPosts / $batchSize);
/* 这一页是 target="_blank" 打开的独立页面(没有 header.php /
menu.php,不在 #newadmin 框架里),拿不到 NewAdmin 的深浅色,
直接读同一个 cookie 跟随后台主题 —— 用户在后台选了深色,
这里就跟着是深色,两边观感一致。 */
$isDarkPage = (isset($_COOKIE['dark']) && $_COOKIE['dark'] === 'dark');
?>
>
分批导出
📦 文章数量较多,建议分批导出
📊 共找到 篇文章
⚡ 每批导出 篇,共 批
💡 建议分批导出,避免超时或内存不足
fetchAll($db->query($sql));
if (empty($posts)) {
die('没有找到符合条件的文章');
}
$zip = new ZipArchive();
$batchSuffix = ($batch !== 0 && $batch !== 'all') ? "_batch{$batch}" : "";
$tmpFile = sys_get_temp_dir() . '/export_' . uniqid() . $batchSuffix . '.zip';
$zip->open($tmpFile, ZipArchive::CREATE);
$exportCount = 0;
foreach ($posts as $post) {
$cid = $post['cid'];
$cat = $db->fetchRow($db->query("SELECT m.name FROM " . $prefix . "metas m INNER JOIN " . $prefix . "relationships r ON m.mid=r.mid WHERE r.cid=$cid AND m.type='category' LIMIT 1"));
$catName = $cat ? $cat['name'] : '未分类';
$tags = $db->fetchAll($db->query("SELECT m.name FROM " . $prefix . "metas m INNER JOIN " . $prefix . "relationships r ON m.mid=r.mid WHERE r.cid=$cid AND m.type='tag'"));
$tagNames = array_column($tags, 'name');
$tagStr = implode(',', $tagNames);
$date = date('Y-m-d', $post['created']);
$filename = preg_replace('/[\/:*?"<>|]/', '-', $post['title']) . "-{$date}.md";
$md = "---\ntitle: {$post['title']}\ndate: " . date('Y-m-d H:i:s', $post['created']) . "\n";
if ($catName != '未分类') $md .= "category: [{$catName}]\n";
if ($tagStr) $md .= "tags: [{$tagStr}]\n";
$md .= "---\n\n{$post['text']}";
$zip->addFromString($filename, $md);
$exportCount++;
}
$zip->close();
$filename = 'export_' . date('Ymd');
if ($exportType == 'year' && !empty($exportValue)) {
$filename .= "_{$exportValue}年";
} elseif ($exportType == 'category' && !empty($exportValue)) {
$catName = $db->fetchRow($db->query("SELECT name FROM " . $prefix . "metas WHERE mid = " . intval($exportValue)));
$filename .= "_{$catName['name']}";
} elseif ($exportType == 'tag' && !empty($exportValue)) {
$tagName = $db->fetchRow($db->query("SELECT name FROM " . $prefix . "metas WHERE mid = " . intval($exportValue)));
$filename .= "_{$tagName['name']}";
}
if ($batch !== 0 && $batch !== 'all') {
$filename .= "_第{$batch}批";
}
$filename .= '.zip';
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Length: ' . filesize($tmpFile));
readfile($tmpFile);
unlink($tmpFile);
exit;
}
// ==================== 获取数据 ====================
// 获取有文章的年份列表(含文章数量)
// 不用 FROM_UNIXTIME/YEAR():那是 MySQL 专有函数,SQLite 上直接报
// "no such function: FROM_UNIXTIME",整页 500。改在 PHP 里按 created 归档。
$yearMap = array();
$yearRows = $db->fetchAll($db->query("SELECT created FROM " . $prefix . "contents WHERE type = 'post' AND status = 'publish'"));
foreach ($yearRows as $yearRow) {
$yearKey = date('Y', (int) $yearRow['created']);
$yearMap[$yearKey] = isset($yearMap[$yearKey]) ? $yearMap[$yearKey] + 1 : 1;
}
krsort($yearMap);
$years = array();
foreach ($yearMap as $yearKey => $yearCount) {
$years[] = array('year' => $yearKey, 'count' => $yearCount);
}
// 获取分类列表(含文章数量)
$categories = $db->fetchAll($db->query("SELECT m.mid, m.name, COUNT(r.cid) as count FROM " . $prefix . "metas m LEFT JOIN " . $prefix . "relationships r ON m.mid=r.mid WHERE m.type='category' GROUP BY m.mid ORDER BY m.`order`"));
// 获取标签列表(含文章数量)
$tagsList = $db->fetchAll($db->query("SELECT m.mid, m.name, COUNT(r.cid) as count FROM " . $prefix . "metas m LEFT JOIN " . $prefix . "relationships r ON m.mid=r.mid WHERE m.type='tag' GROUP BY m.mid ORDER BY m.name LIMIT 100"));
include 'header.php';
include 'menu.php';
?>
文章导出
💡 插件特色
- 导出格式为ZIP压缩包,一篇一个 .md 文件,包含完整 Frontmatter(标题、日期、分类、标签等)
- 支持按年份、分类、标签筛选导出特定范围的文章
- 文章数量较多时(超过300篇),会自动提示分批导出,避免超时