plugin('Memo');
// 引入Action类
require_once __DIR__ . '/Action.php';
$action = new Memo_Action();
// 获取搜索和筛选参数
$search = isset($_GET['search']) ? trim($_GET['search']) : '';
$category = isset($_GET['category']) ? trim($_GET['category']) : '';
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$perPage = isset($config->perPage) ? intval($config->perPage) : 10;
// 获取所有分类
$allCategories = $action->getAllCategories();
$categoryCounts = $action->getCategoryCounts();
// 获取统计信息
$statistics = $action->getStatistics();
// 获取默认分类设置
$defaultCategories = isset($config->defaultCategories) ? explode("\n", $config->defaultCategories) : array('默认');
$defaultCategories = array_map('trim', $defaultCategories);
$defaultCategories = array_filter($defaultCategories);
// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// 处理新增
if (!empty($_POST['content'])) {
try {
$action->addMemo(array(
'content' => $_POST['content'],
'category' => isset($_POST['category']) ? $_POST['category'] : '默认',
'event_date' => isset($_POST['event_date']) && !empty($_POST['event_date']) ? $_POST['event_date'] : null,
'post_cids' => isset($_POST['post_cids']) ? $_POST['post_cids'] : '',
'original_url' => isset($_POST['original_url']) ? $_POST['original_url'] : '' // 新增:原文链接
));
$successMsg = '知识添加成功!';
// 重定向到当前页面(保持筛选状态)
$redirectUrl = $_SERVER['PHP_SELF'] . '?panel=Memo/manage-panel.php&page=' . $page;
if ($search) $redirectUrl .= '&search=' . urlencode($search);
if ($category) $redirectUrl .= '&category=' . urlencode($category);
$redirectUrl .= '#memo-list-anchor';
header('Location: ' . $redirectUrl);
exit;
} catch (Exception $e) {
$errorMsg = '添加失败: ' . htmlspecialchars($e->getMessage());
}
}
// 处理批量删除 - 修复:独立的批量删除处理
if (isset($_POST['bulk_delete']) && !empty($_POST['delete_ids']) && is_array($_POST['delete_ids'])) {
$action->deleteMemos($_POST['delete_ids']);
$successMsg = '删除成功!';
// 重定向到当前页面(保持筛选状态)
$redirectUrl = $_SERVER['PHP_SELF'] . '?panel=Memo/manage-panel.php&page=' . $page;
if ($search) $redirectUrl .= '&search=' . urlencode($search);
if ($category) $redirectUrl .= '&category=' . urlencode($category);
$redirectUrl .= '#memo-list-anchor';
header('Location: ' . $redirectUrl);
exit;
}
// 处理批量导出TXT - 修复:正确处理导出数据
if (isset($_POST['export_selected_txt']) && !empty($_POST['export_ids'])) {
try {
// 将逗号分隔的字符串转换为数组
$exportIds = explode(',', $_POST['export_ids']);
$exportIds = array_map('intval', $exportIds);
$exportIds = array_filter($exportIds);
if (empty($exportIds)) {
throw new Exception('没有选择有效的记录');
}
$exportContent = $action->exportSelectedData($exportIds, 'txt');
$filename = 'memo_selected_' . date('Ymd_His') . '.txt';
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Length: ' . strlen($exportContent));
echo $exportContent;
exit;
} catch (Exception $e) {
$errorMsg = '导出失败: ' . htmlspecialchars($e->getMessage());
}
}
// 处理批量导出MD - 修复:正确处理导出数据
if (isset($_POST['export_selected_md']) && !empty($_POST['export_ids'])) {
try {
// 将逗号分隔的字符串转换为数组
$exportIds = explode(',', $_POST['export_ids']);
$exportIds = array_map('intval', $exportIds);
$exportIds = array_filter($exportIds);
if (empty($exportIds)) {
throw new Exception('没有选择有效的记录');
}
$exportContent = $action->exportSelectedData($exportIds, 'md');
$filename = 'memo_selected_' . date('Ymd_His') . '.md';
header('Content-Type: text/markdown');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Length: ' . strlen($exportContent));
echo $exportContent;
exit;
} catch (Exception $e) {
$errorMsg = '导出失败: ' . htmlspecialchars($e->getMessage());
}
}
// 处理单个导出
if (isset($_POST['export_single_txt']) && !empty($_POST['single_id'])) {
try {
$exportContent = $action->exportSelectedData(array(intval($_POST['single_id'])), 'txt');
$filename = 'memo_' . $_POST['single_id'] . '_' . date('Ymd_His') . '.txt';
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Length: ' . strlen($exportContent));
echo $exportContent;
exit;
} catch (Exception $e) {
$errorMsg = '导出失败: ' . htmlspecialchars($e->getMessage());
}
}
if (isset($_POST['export_single_md']) && !empty($_POST['single_id'])) {
try {
$exportContent = $action->exportSelectedData(array(intval($_POST['single_id'])), 'md');
$filename = 'memo_' . $_POST['single_id'] . '_' . date('Ymd_His') . '.md';
header('Content-Type: text/markdown');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Length: ' . strlen($exportContent));
echo $exportContent;
exit;
} catch (Exception $e) {
$errorMsg = '导出失败: ' . htmlspecialchars($e->getMessage());
}
}
// 处理编辑
if (!empty($_POST['edit_id'])) {
$action->updateMemo(array(
'edit_id' => $_POST['edit_id'],
'edit_content' => $_POST['edit_content'],
'edit_category' => isset($_POST['edit_category']) ? $_POST['edit_category'] : '默认',
'edit_event_date' => isset($_POST['edit_event_date']) && !empty($_POST['edit_event_date']) ? $_POST['edit_event_date'] : null,
'edit_post_cids' => isset($_POST['edit_post_cids']) ? $_POST['edit_post_cids'] : '',
'edit_original_url' => isset($_POST['edit_original_url']) ? $_POST['edit_original_url'] : ''
));
$successMsg = '知识更新成功!';
// 重定向到当前页面(保持筛选状态)
$redirectUrl = $_SERVER['PHP_SELF'] . '?panel=Memo/manage-panel.php&page=' . $page;
if ($search) $redirectUrl .= '&search=' . urlencode($search);
if ($category) $redirectUrl .= '&category=' . urlencode($category);
$redirectUrl .= '#memo-list-anchor';
header('Location: ' . $redirectUrl);
exit;
}
// 处理全量导出
if (isset($_POST['export'])) {
$exportContent = $action->exportData();
$filename = 'memo_' . date('Ymd_His') . '.txt';
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Length: ' . strlen($exportContent));
echo $exportContent;
exit;
}
// 处理全量导出为MD
if (isset($_POST['export_md'])) {
$exportContent = $action->exportMdData();
$filename = 'memo_' . date('Ymd_His') . '.md';
header('Content-Type: text/markdown');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Length: ' . strlen($exportContent));
echo $exportContent;
exit;
}
// 处理导入
if (isset($_POST['import']) && !empty($_FILES['import_file']['tmp_name'])) {
$fileContent = file_get_contents($_FILES['import_file']['tmp_name']);
$fileExtension = strtolower(pathinfo($_FILES['import_file']['name'], PATHINFO_EXTENSION));
if ($fileExtension == 'sql') {
// 导入SQL文件
$importCategory = isset($_POST['import_category']) ? $_POST['import_category'] : '文章导入';
$result = $action->importSqlData($fileContent, $importCategory);
} else {
// 导入文本文件
$result = $action->importData($fileContent);
}
if ($result['imported'] > 0) {
$successMsg = '导入成功!成功导入 ' . $result['imported'] . ' 条记录';
if ($result['failed'] > 0) {
$successMsg .= ',失败 ' . $result['failed'] . ' 条记录';
if (!empty($result['fail_reasons'])) {
$errorDetails = '失败原因:
';
$displayCount = min(10, count($result['fail_reasons']));
for ($i = 0; $i < $displayCount; $i++) {
$errorDetails .= htmlspecialchars($result['fail_reasons'][$i]) . '
';
}
if (count($result['fail_reasons']) > 10) {
$errorDetails .= '...还有 ' . (count($result['fail_reasons']) - 10) . ' 条失败记录';
}
$errorMsg = $errorDetails;
}
}
} else {
$errorMsg = '导入失败!未找到可导入的记录或格式不正确';
if (!empty($result['fail_reasons'])) {
$errorMsg .= '
失败原因:
';
$displayCount = min(10, count($result['fail_reasons']));
for ($i = 0; $i < $displayCount; $i++) {
$errorMsg .= htmlspecialchars($result['fail_reasons'][$i]) . '
';
}
}
}
}
}
// 获取知识数据
$memos = $action->getMemos($page, $perPage, $search, $category);
$total = $action->getTotalCount($search, $category);
$totalPages = ceil($total / $perPage);
// 为每条记录获取关联的文章信息
foreach ($memos as &$memo) {
$memo['post_info'] = array();
if (!empty($memo['post_cids'])) {
$cids = array_filter(array_map('trim', explode(',', $memo['post_cids'])));
foreach ($cids as $cid) {
if (is_numeric($cid)) {
$post = $action->getPostByCid($cid);
if ($post) {
$memo['post_info'][$cid] = $post;
}
}
}
}
}
unset($memo);
// 辅助函数:将URL转换为链接(精确匹配)
function convertUrlsToLinks($text) {
if (empty($text)) {
return $text;
}
// 先进行HTML实体编码
$encodedText = htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
// 精确匹配URL的正则表达式
// 只匹配以http://或https://开头的完整URL
$urlPattern = '/(https?:\/\/[a-zA-Z0-9][-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)/';
// 使用preg_replace_callback进行精确处理
return preg_replace_callback($urlPattern, function($matches) {
$url = $matches[1];
// 清理URL末尾可能错误的标点符号
$punctuation = array('.', ',', ';', ':', '!', '?', ')', ']', '}');
$lastChar = substr($url, -1);
if (in_array($lastChar, $punctuation)) {
$url = substr($url, 0, -1);
$suffix = $lastChar;
} else {
$suffix = '';
}
// 确保URL格式正确
if (!preg_match('/^https?:\/\//', $url)) {
return $matches[0]; // 如果不是合法URL,返回原文本
}
// 创建链接
return '' . htmlspecialchars($url) . '' . $suffix;
}, $encodedText);
}
?>
跨度时间
最早:
最新:
最早:
最新:
有日期: 条,
无日期: 条
暂无事件日期数据
知识列表
📝
暂无知识记录
还没有添加知识,或者当前筛选条件没有匹配的记录
查看全部
1): ?>