Files
NiceQuote/Manage.php

516 lines
22 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* NiceQuote 后台管理页:引用管理
*
* 菜单挂在「管理 → 引用管理」下。
*
* 这个页面做三件事:
* 1. 统计:总引用数、涉及内容、被引用对象、失效引用、类型分布、被引用 Top 5
* 2. 明细:序号、谁在什么时间、哪篇文章引用了什么,每页条数可在插件设置里调
* 3. 维护:跳到宿主文章的编辑页改引用、删除单条引用、一键清理全部失效引用
*
* 动作处理全部放在输出之前,做完 302 回列表页,避免刷新重复提交。
*
* @package NiceQuote
*/
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
use TypechoPlugin\NiceQuote\Plugin;
use TypechoPlugin\NiceQuote\Scanner;
if (!class_exists('TypechoPlugin\\NiceQuote\\Plugin')) {
require_once __DIR__ . '/Plugin.php';
}
require_once __DIR__ . '/Scanner.php';
/* ------------------------------------------------------------------
* 小工具
* ------------------------------------------------------------------ */
$nqH = function ($text) {
return htmlspecialchars((string) $text, ENT_QUOTES, 'UTF-8');
};
/** 只收标量,挡掉 ?cid[]=1 这种会把 PHP 8 直接打崩的写法 */
$nqGet = function ($key, $default = '') use ($request) {
$value = $request->get($key, $default);
return is_scalar($value) || null === $value ? (string) $value : $default;
};
$nqInt = function ($key, $default = 0) use ($request) {
$value = $request->get($key, $default);
return is_scalar($value) ? (int) $value : $default;
};
/* ------------------------------------------------------------------
* 权限:仅管理员
* ------------------------------------------------------------------ */
$nqIsAdmin = false;
try {
if (isset($user) && is_object($user)) {
$nqIsAdmin = (bool) $user->pass('administrator', true);
}
} catch (\Throwable $e) {
$nqIsAdmin = false;
}
$nqToken = '';
if ($nqIsAdmin) {
$nqToken = md5((string) $options->secret . '|' . (string) $user->authCode . '|' . (int) $user->uid);
}
$nqPanel = Plugin::panelUrl();
$nqVer = Plugin::options()['version'];
/* ------------------------------------------------------------------
* 动作处理(POST → 302)
* ------------------------------------------------------------------ */
$nqDo = $nqGet('do');
if ('' !== $nqDo && 'POST' === strtoupper((string) $request->getServer('REQUEST_METHOD'))) {
$nqBack = array_filter([
'kind' => $nqGet('kind'),
'q' => $nqGet('q'),
'missing' => $nqGet('missing'),
'page' => $nqGet('page'),
], function ($value) {
return '' !== $value;
});
if (!$nqIsAdmin || '' === $nqToken || !hash_equals($nqToken, $nqGet('_nq'))) {
$nqBack['_nq'] = 'denied';
$response->redirect(Plugin::panelUrl($nqBack));
exit;
}
$nqNotice = '';
if ('delete' === $nqDo) {
$nqCid = $nqInt('cid');
$nqOffset = $nqInt('offset', -1);
$nqRaw = $nqGet('raw');
$nqResult = Scanner::edit($nqCid, [['offset' => $nqOffset, 'raw' => $nqRaw, 'to' => null]]);
$nqNotice = $nqResult['ok'] ? 'deleted' : 'failed';
} elseif ('clean' === $nqDo) {
// 清理全部失效引用:按文章分组,逐篇从后往前删
$nqAll = Scanner::resolve(Scanner::rows());
$nqGroup = [];
foreach ($nqAll as $nqRow) {
if (!$nqRow['ref_exists']) {
$nqGroup[$nqRow['cid']][] = [
'offset' => $nqRow['offset'],
'raw' => $nqRow['raw'],
'to' => null,
];
}
}
$nqChanged = 0;
foreach ($nqGroup as $nqCid => $nqEdits) {
$nqResult = Scanner::edit((int) $nqCid, $nqEdits);
$nqChanged += $nqResult['changed'];
}
$nqBack['_n'] = $nqChanged;
$nqNotice = $nqChanged > 0 ? 'cleaned' : 'nothing';
} else {
$nqNotice = 'failed';
}
$nqBack['_nq'] = $nqNotice;
$response->redirect(Plugin::panelUrl($nqBack));
exit;
}
/* ------------------------------------------------------------------
* 读数据
* ------------------------------------------------------------------ */
$nqAll = $nqIsAdmin ? Scanner::resolve(Scanner::rows()) : [];
$nqStats = Scanner::stats($nqAll);
$nqKind = strtolower($nqGet('kind'));
$nqKind = isset(Scanner::LABEL[$nqKind]) ? $nqKind : '';
$nqMissingOnly = '1' === $nqGet('missing');
$nqKeyword = trim($nqGet('q'));
$nqLower = function_exists('mb_strtolower') ? mb_strtolower($nqKeyword) : strtolower($nqKeyword);
$nqRows = array_values(array_filter($nqAll, function (array $row) use ($nqKind, $nqMissingOnly, $nqLower) {
if ('' !== $nqKind && $row['type'] !== $nqKind && $row['ref_kind'] !== $nqKind) {
return false;
}
if ($nqMissingOnly && $row['ref_exists']) {
return false;
}
if ('' !== $nqLower) {
$haystack = $row['host'] . ' ' . $row['ref_title'] . ' ' . $row['raw'] . ' ' . $row['author'];
$haystack = function_exists('mb_strtolower') ? mb_strtolower($haystack) : strtolower($haystack);
if (false === strpos($haystack, $nqLower)) {
return false;
}
}
return true;
}));
$nqPer = (int) Plugin::options()['per'];
$nqTotal = count($nqRows);
$nqPages = max(1, (int) ceil($nqTotal / $nqPer));
$nqPage = min($nqPages, max(1, $nqInt('page', 1)));
$nqSlice = array_slice($nqRows, ($nqPage - 1) * $nqPer, $nqPer);
// 只有当前页需要前台链接,别为全站引用逐个取
$nqSlice = Scanner::links($nqSlice);
$nqAdminUrl = rtrim((string) $options->adminUrl, '/') . '/';
$nqPlaceholder = function (array $extra = []) use ($nqPanel, $nqH) {
return $nqH($nqPanel . '&' . http_build_query($extra));
};
$nqQuery = array_filter([
'kind' => $nqKind,
'q' => $nqKeyword,
'missing' => $nqMissingOnly ? '1' : '',
], function ($value) {
return '' !== $value;
});
/* 操作结果提示 */
$nqFlash = $nqGet('_nq');
$nqFlashMap = [
'deleted' => ['ok', '引用已删除,正文已同步清理'],
'cleaned' => ['ok', '已清理 ' . (int) $nqInt('_n') . ' 条失效引用'],
'nothing' => ['warn', '没有可清理的失效引用'],
'failed' => ['err', '操作失败,可能是正文已被修改,请刷新后重试'],
'denied' => ['err', '安全校验未通过,请重新操作'],
];
include 'header.php';
include 'menu.php';
?>
<link rel="stylesheet" href="<?php echo $nqH($options->pluginUrl . '/NiceQuote/assets/admin.css?v=' . $nqVer); ?>">
<link rel="stylesheet" href="<?php echo $nqH($options->pluginUrl . '/NiceQuote/assets/manage.css?v=' . $nqVer); ?>">
<!-- 必须走 .main > .body.container,后台左侧导航是 fixed 定位,
少了这层包裹内容会被压在菜单底下(NewAdmin / 原生后台都一样) -->
<main class="main">
<div class="body container">
<div class="row typecho-page-main" role="main">
<div class="col-mb-12 typecho-list">
<div class="nq-adm">
<div class="typecho-page-title">
<h2>引用管理</h2>
</div>
<?php if (isset($nqFlashMap[$nqFlash])): ?>
<div class="nq-adm-flash is-<?php echo $nqFlashMap[$nqFlash][0]; ?>">
<?php echo $nqH($nqFlashMap[$nqFlash][1]); ?>
</div>
<?php endif; ?>
<?php if (!$nqIsAdmin): ?>
<div class="nq-adm-flash is-err">只有管理员可以查看引用管理。</div>
<?php else: ?>
<!-- 统计 -->
<div class="nq-adm-stats">
<div class="nq-adm-card">
<span class="nq-adm-card-label">引用总数</span>
<strong class="nq-adm-card-value"><?php echo (int) $nqStats['total']; ?></strong>
<span class="nq-adm-card-foot">共 <?php echo (int) $nqStats['hosts']; ?> 篇内容在用</span>
</div>
<div class="nq-adm-card">
<span class="nq-adm-card-label">被引用对象</span>
<strong class="nq-adm-card-value"><?php echo (int) $nqStats['targets']; ?></strong>
<span class="nq-adm-card-foot">去重后的内容数</span>
</div>
<div class="nq-adm-card<?php echo $nqStats['missing'] > 0 ? ' is-warn' : ''; ?>">
<span class="nq-adm-card-label">失效引用</span>
<strong class="nq-adm-card-value"><?php echo (int) $nqStats['missing']; ?></strong>
<span class="nq-adm-card-foot">
<?php if ($nqStats['missing'] > 0): ?>
<a href="<?php echo $nqPlaceholder(['missing' => '1']); ?>">只看失效</a>
<?php else: ?>
全部有效
<?php endif; ?>
</span>
</div>
</div>
<?php if ($nqStats['total'] > 0): ?>
<div class="nq-adm-panels">
<div class="nq-adm-panel">
<h3>类型分布</h3>
<ul class="nq-adm-bars">
<?php foreach ($nqStats['byKind'] as $nqItemKind => $nqCount): ?>
<?php $nqPercent = $nqStats['total'] > 0 ? round($nqCount / $nqStats['total'] * 100) : 0; ?>
<li>
<a href="<?php echo $nqPlaceholder(['kind' => $nqItemKind]); ?>">
<span class="nq-adm-bar-name"><?php echo $nqH(Scanner::LABEL[$nqItemKind] ?? $nqItemKind); ?></span>
<span class="nq-adm-bar-track">
<span class="nq-adm-bar-fill" style="width:<?php echo (int) $nqPercent; ?>%"></span>
</span>
<span class="nq-adm-bar-num"><?php echo (int) $nqCount; ?></span>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
<div class="nq-adm-panel">
<h3>被引用最多</h3>
<?php if (empty($nqStats['top'])): ?>
<p class="nq-adm-empty">暂无数据</p>
<?php else: ?>
<ol class="nq-adm-top">
<?php foreach ($nqStats['top'] as $nqTop): ?>
<li>
<span class="nq-adm-badge"><?php echo $nqH(Scanner::LABEL[$nqTop['kind']] ?? $nqTop['kind']); ?></span>
<span class="nq-adm-top-title"><?php echo $nqH($nqTop['title']); ?></span>
<span class="nq-adm-top-num"><?php echo (int) $nqTop['count']; ?> 次</span>
</li>
<?php endforeach; ?>
</ol>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
<!-- 明细卡片:标题 + 筛选 + 表格 + 分页 -->
<div class="nq-adm-box">
<div class="nq-adm-box-head">
<strong>引用明细</strong>
<span>
<?php if ($nqTotal > 0): ?>
共 <?php echo (int) $nqTotal; ?> 条
<?php else: ?>
暂无记录
<?php endif; ?>
</span>
</div>
<div class="nq-adm-toolbar">
<form class="nq-adm-filter" method="get" action="<?php echo $nqH($nqAdminUrl . 'extending.php'); ?>">
<input type="hidden" name="panel" value="NiceQuote/Manage.php">
<select name="kind" class="nq-adm-input">
<option value="">全部类型</option>
<?php foreach (Scanner::FILTER as $nqValue): ?>
<option value="<?php echo $nqH($nqValue); ?>"<?php echo $nqKind === $nqValue ? ' selected' : ''; ?>>
<?php echo $nqH(Scanner::LABEL[$nqValue] ?? $nqValue); ?>
</option>
<?php endforeach; ?>
</select>
<select name="missing" class="nq-adm-input">
<option value="">全部状态</option>
<option value="1"<?php echo $nqMissingOnly ? ' selected' : ''; ?>>只看失效</option>
</select>
<input class="nq-adm-input nq-adm-search" type="search" name="q"
value="<?php echo $nqH($nqKeyword); ?>" placeholder="搜文章标题 / 被引用标题 / 短码">
<button type="submit" class="nq-adm-btn">筛选</button>
<?php if ('' !== $nqKind || '' !== $nqKeyword || $nqMissingOnly): ?>
<a class="nq-adm-btn is-plain" href="<?php echo $nqH($nqPanel); ?>">重置</a>
<?php endif; ?>
</form>
<?php if ($nqStats['missing'] > 0): ?>
<form method="post" class="nq-adm-inline"
data-confirm="确定清理全部 <?php echo (int) $nqStats['missing']; ?> 条失效引用?会同时从正文里删掉这些短码。">
<input type="hidden" name="do" value="clean">
<input type="hidden" name="_nq" value="<?php echo $nqH($nqToken); ?>">
<?php foreach ($nqQuery as $nqKey => $nqValue): ?>
<input type="hidden" name="<?php echo $nqH($nqKey); ?>" value="<?php echo $nqH($nqValue); ?>">
<?php endforeach; ?>
<button type="submit" class="nq-adm-btn is-danger">清理失效引用(<?php echo (int) $nqStats['missing']; ?>)</button>
</form>
<?php endif; ?>
</div>
<!-- 明细 -->
<?php if (empty($nqSlice)): ?>
<p class="nq-adm-empty nq-adm-empty-big">
<?php echo $nqStats['total'] > 0 ? '没有符合筛选条件的引用' : '还没有任何引用,去文章里插一个试试'; ?>
</p>
<?php else: ?>
<div class="nq-adm-tablewrap">
<table class="nq-adm-table">
<thead>
<tr>
<th class="nq-col-no">序号</th>
<th>引用位置</th>
<th class="nq-col-arrow"></th>
<th>被引用内容</th>
<th class="nq-col-state">状态</th>
<th class="nq-col-act">操作</th>
</tr>
</thead>
<tbody>
<?php foreach ($nqSlice as $nqIndex => $nqRow): ?>
<?php
$nqNo = ($nqPage - 1) * $nqPer + $nqIndex + 1;
$nqDate = new \Typecho\Date((int) $nqRow['created']);
// 「修改」直接进这篇文章 / 独立页面的编辑页,引用就在正文里,改起来最直接
$nqEditUrl = $nqAdminUrl
. ('page' === $nqRow['host_type'] ? 'write-page.php' : 'write-post.php')
. '?cid=' . (int) $nqRow['cid'];
?>
<tr class="<?php echo $nqRow['ref_exists'] ? '' : 'is-missing'; ?>">
<td class="nq-col-no"><?php echo (int) $nqNo; ?></td>
<td>
<?php if ('' !== (string) $nqRow['host_url']): ?>
<a class="nq-adm-host" href="<?php echo $nqH($nqRow['host_url']); ?>"
target="_blank" rel="noopener noreferrer"
title="<?php echo $nqH($nqRow['host']) . '(新窗口打开前台页面)'; ?>">
<?php echo $nqH($nqRow['host']); ?>
</a>
<?php else: ?>
<span class="nq-adm-host" title="<?php echo $nqH($nqRow['host']); ?>">
<?php echo $nqH($nqRow['host']); ?>
</span>
<?php endif; ?>
<span class="nq-adm-meta">
<?php echo $nqH($nqRow['author']); ?>
· <?php echo $nqH($nqDate->format('Y-m-d H:i')); ?>
<?php if ('publish' !== $nqRow['host_status']): ?>
· <em><?php echo $nqH('page' === $nqRow['host_type'] ? '页面' : '文章'); ?>未发布</em>
<?php endif; ?>
· 第 <?php echo (int) $nqRow['seq']; ?> 处
</span>
</td>
<td class="nq-col-arrow">→</td>
<td>
<span class="nq-adm-badge is-<?php echo $nqH($nqRow['ref_kind']); ?>">
<?php echo $nqH(Scanner::LABEL[$nqRow['ref_kind']] ?? $nqRow['ref_kind']); ?>
</span>
<?php if ('' !== (string) $nqRow['ref_link']): ?>
<a class="nq-adm-target" href="<?php echo $nqH($nqRow['ref_link']); ?>"
target="_blank" rel="noopener noreferrer"
title="<?php echo $nqH($nqRow['ref_title']) . '(新窗口打开)'; ?>">
<?php echo $nqH($nqRow['ref_title']); ?>
</a>
<?php else: ?>
<span class="nq-adm-target" title="<?php echo $nqH($nqRow['ref_title']); ?>">
<?php echo $nqH($nqRow['ref_title']); ?>
</span>
<?php endif; ?>
<?php if ('url' !== $nqRow['type']): ?>
<code class="nq-adm-code"><?php echo $nqH($nqRow['raw']); ?></code>
<?php endif; ?>
</td>
<td class="nq-col-state">
<?php if ($nqRow['ref_exists']): ?>
<span class="nq-adm-state is-ok">正常</span>
<?php else: ?>
<span class="nq-adm-state is-err">失效</span>
<?php endif; ?>
</td>
<td class="nq-col-act">
<a class="nq-adm-link nq-goto"
href="<?php echo $nqH($nqEditUrl); ?>"
title="到「<?php echo $nqH($nqRow['host']); ?>」的编辑页改这条引用">修改</a>
<form method="post" class="nq-adm-inline"
data-confirm="确定删除这条引用?正文里的 <?php echo $nqH($nqRow['raw']); ?> 会被一起删掉。">
<input type="hidden" name="do" value="delete">
<input type="hidden" name="_nq" value="<?php echo $nqH($nqToken); ?>">
<input type="hidden" name="cid" value="<?php echo (int) $nqRow['cid']; ?>">
<input type="hidden" name="offset" value="<?php echo (int) $nqRow['offset']; ?>">
<input type="hidden" name="raw" value="<?php echo $nqH($nqRow['raw']); ?>">
<?php foreach ($nqQuery as $nqKey => $nqValue): ?>
<input type="hidden" name="<?php echo $nqH($nqKey); ?>" value="<?php echo $nqH($nqValue); ?>">
<?php endforeach; ?>
<button type="submit" class="nq-adm-link is-danger">删除</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<!-- 分页:直接套后台列表的 ul.typecho-pager,和文章 / 评论 / 邮件管理观感一致 -->
<?php if ($nqPages > 1): ?>
<div class="nq-adm-pagerbar">
<span class="nq-adm-pager-info">
共 <?php echo (int) $nqTotal; ?> 条 · 第 <?php echo (int) $nqPage; ?>/<?php echo (int) $nqPages; ?> 页
</span>
<ul class="typecho-pager">
<?php $nqFrom = max(1, $nqPage - 3); ?>
<?php $nqTo = min($nqPages, $nqPage + 3); ?>
<?php if ($nqPage > 1): ?>
<li class="prev">
<a href="<?php echo $nqPlaceholder($nqQuery + ['page' => $nqPage - 1]); ?>">&laquo;</a>
</li>
<?php endif; ?>
<?php if ($nqFrom > 1): ?>
<li><a href="<?php echo $nqPlaceholder($nqQuery + ['page' => 1]); ?>">1</a></li>
<?php if ($nqFrom > 2): ?>
<li><span>...</span></li>
<?php endif; ?>
<?php endif; ?>
<?php for ($nqI = $nqFrom; $nqI <= $nqTo; $nqI++): ?>
<li<?php echo $nqI === $nqPage ? ' class="current"' : ''; ?>>
<a href="<?php echo $nqPlaceholder($nqQuery + ['page' => $nqI]); ?>"><?php echo (int) $nqI; ?></a>
</li>
<?php endfor; ?>
<?php if ($nqTo < $nqPages): ?>
<?php if ($nqTo < $nqPages - 1): ?>
<li><span>...</span></li>
<?php endif; ?>
<li>
<a href="<?php echo $nqPlaceholder($nqQuery + ['page' => $nqPages]); ?>"><?php echo (int) $nqPages; ?></a>
</li>
<?php endif; ?>
<?php if ($nqPage < $nqPages): ?>
<li class="next">
<a href="<?php echo $nqPlaceholder($nqQuery + ['page' => $nqPage + 1]); ?>">&raquo;</a>
</li>
<?php endif; ?>
</ul>
</div>
<?php endif; ?>
<?php endif; ?>
</div><!-- /.nq-adm-box -->
<?php endif; ?>
</div>
</div><!-- /.typecho-list -->
</div><!-- /.typecho-page-main -->
</div><!-- /.body.container -->
</main>
<script src="<?php echo $nqH($options->pluginUrl . '/NiceQuote/assets/manage.js?v=' . $nqVer); ?>"></script>
<?php include 'footer.php'; ?>