This commit is contained in:
wangjinlei
2026-04-10 11:19:20 +08:00
parent d92df3e103
commit b0f0d6461f
2 changed files with 250 additions and 23 deletions

View File

@@ -1131,6 +1131,9 @@ class Preaccept extends Base
return jsonError($rule->getError());
}
$am_info = $this->article_main_obj->where("am_id",$data['am_id'])->find();
$old_content = $am_info['content'];
$new_raw_content = isset($data['content']) ? (string)$data['content'] : '';
$insert['article_id'] = $am_info['article_id'];
$insert['am_id'] = $data['am_id'];
$insert['type'] = 0;
@@ -1140,11 +1143,21 @@ class Preaccept extends Base
$insert['ctime'] = time();
$this->article_main_log_obj->insert($insert);
// 判断是否存在“引用删除”(新 content 相对旧 content 缺少 <mycite>
$hasCitationDeletion = $this->hasMyciteDeletion($old_content, $new_raw_content);
$update['content'] = $this->formatMain($new_raw_content);
$update['content'] = $this->formatMain($data['content']);
$update['state'] = 0;
$this->article_main_obj->where("am_id",$data['am_id'])->update($update);
// 若检测到引用删除,则进行全文扫描并标记未被引用条目的 is_used=0含 table 内容)
if ($hasCitationDeletion) {
// $this->markUnusedReferencesForArticle(intval($am_info['article_id']));
}
// return jsonSuccess([]);
//返回更新数据 20260119 start
$update = empty($update) ? [] : array_merge($update,['am_id' => empty($data['am_id']) ? 0 : $data['am_id']]);
@@ -1155,6 +1168,121 @@ class Preaccept extends Base
//返回更新数据 20260119 end
}
/**
* 是否发生 <mycite> 删除new 相对 old 少了任意引用 id
*/
private function hasMyciteDeletion(string $oldContent, string $newContent): bool
{
$oldIds = $this->extractMyciteIds($oldContent);
if (empty($oldIds)) {
return false;
}
$newIds = $this->extractMyciteIds($newContent);
// old 有引用new 为空 => 删除
if (empty($newIds)) {
return true;
}
$oldSet = array_fill_keys($oldIds, 1);
$newSet = array_fill_keys($newIds, 1);
foreach ($oldSet as $id => $_) {
if (!isset($newSet[$id])) {
return true;
}
}
return false;
}
/**
* 从文本/HTML 中提取 <mycite data-id="1,2,3"> 的 id 列表
*
* @return int[]
*/
private function extractMyciteIds(string $text): array
{
if ($text === '') return [];
$ids = [];
if (preg_match_all('/<\s*mycite\b[^>]*\bdata-id\s*=\s*(["\'])(.*?)\1[^>]*>/iu', $text, $m)) {
foreach ($m[2] as $raw) {
$raw = trim((string)$raw);
if ($raw === '') continue;
$parts = preg_split('/\s*,\s*/', $raw);
foreach ($parts as $p) {
$p = trim((string)$p);
if ($p === '') continue;
$v = intval($p);
if ($v > 0) $ids[] = $v;
}
}
}
$ids = array_values(array_unique($ids));
sort($ids);
return $ids;
}
/**
* 全文扫描(正文 + table将 production_article_refer 未被引用的条目标记 is_used=0
* - 被引用的条目 is_used=1
*
* 注意:依赖 production_article_refer 表存在 is_used 字段int/tinyint
*/
private function markUnusedReferencesForArticle(int $articleId)
{
if ($articleId <= 0) return;
$production = Db::name('production_article')
->where('article_id', $articleId)
->where('state', 0)
->field('p_article_id')
->find();
$pArticleId = intval($production['p_article_id'] ?? 0);
if ($pArticleId <= 0) return;
// 1) 收集已使用的 p_refer_id
$usedIds = [];
$mains = Db::name('article_main')
->where('article_id', $articleId)
->whereIn('state', [0, 2])
->field('content')
->select();
foreach ($mains as $row) {
$usedIds = array_merge($usedIds, $this->extractMyciteIds((string)($row['content'] ?? '')));
}
$tables = Db::name('article_main_table')
->where('article_id', $articleId)
->where('state', 0)
->field('table_data,html_data,title,note')
->select();
foreach ($tables as $row) {
$usedIds = array_merge($usedIds, $this->extractMyciteIds((string)($row['table_data'] ?? '')));
$usedIds = array_merge($usedIds, $this->extractMyciteIds((string)($row['html_data'] ?? '')));
$usedIds = array_merge($usedIds, $this->extractMyciteIds((string)($row['title'] ?? '')));
$usedIds = array_merge($usedIds, $this->extractMyciteIds((string)($row['note'] ?? '')));
}
$usedIds = array_values(array_unique($usedIds));
// 2) 标记:先全置 0再把用到的置 1
try {
Db::name('production_article_refer')
->where('p_article_id', $pArticleId)
->where('state', 0)
->update(['is_used' => 0, 'update_time' => time()]);
if (!empty($usedIds)) {
Db::name('production_article_refer')
->where('p_article_id', $pArticleId)
->where('state', 0)
->whereIn('p_refer_id', $usedIds)
->update(['is_used' => 1, 'update_time' => time()]);
}
} catch (\Exception $e) {
// 工具方法:不影响主流程,忽略异常(可按需改为记录日志)
}
}
public function getArticleMainsRecycle(){
$data = $this->request->post();
$rule = new Validate([