@li-canghai/dsh-paper 0.0.0-stage → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +150 -2
- package/cordis.patch.yml +4 -0
- package/lib/annotate-tool.js +334 -0
- package/lib/arxiv-id.js +74 -0
- package/lib/asset-routes.js +210 -0
- package/lib/bibliography-formats.js +126 -0
- package/lib/bibliography-import.js +105 -0
- package/lib/bibliography-service.js +43 -0
- package/lib/bibliography.js +42 -0
- package/lib/block-authors.js +292 -0
- package/lib/block-cache.js +39 -0
- package/lib/blocks-markdown.js +157 -0
- package/lib/blocks-reconcile.js +218 -0
- package/lib/blocks.js +1344 -0
- package/lib/citation-metadata.js +260 -0
- package/lib/citation-styles.js +120 -0
- package/lib/client-parts/01-pure-helpers.js +2762 -0
- package/lib/client-parts/02-author-year.js +95 -0
- package/lib/client.js +10381 -0
- package/lib/code-source.js +68 -0
- package/lib/concurrency-gate.js +91 -0
- package/lib/context.js +233 -0
- package/lib/csl/SOURCES.md +10 -0
- package/lib/csl/gb-t-7714.csl +441 -0
- package/lib/csl/ieee.csl +520 -0
- package/lib/csl/locales-zh-CN.xml +438 -0
- package/lib/csl/nature.csl +190 -0
- package/lib/discussion.js +400 -0
- package/lib/documents.js +67 -0
- package/lib/export.js +848 -0
- package/lib/file-lock.js +177 -0
- package/lib/gloss.js +181 -0
- package/lib/glossary.js +279 -0
- package/lib/index.js +3862 -0
- package/lib/inline-html.js +53 -0
- package/lib/install-dependency.js +120 -0
- package/lib/item-ops.js +83 -0
- package/lib/job-facade.js +58 -0
- package/lib/json-store.js +54 -0
- package/lib/library-model.js +132 -0
- package/lib/library-service.js +180 -0
- package/lib/library-tool.js +982 -0
- package/lib/library.js +814 -0
- package/lib/markdown-assets.js +138 -0
- package/lib/markdown-literals.js +93 -0
- package/lib/metadata-enrichment.js +516 -0
- package/lib/metadata-fetch.js +146 -0
- package/lib/metadata-node.js +158 -0
- package/lib/metadata-proxy.ps1 +35 -0
- package/lib/mineru-config.js +95 -0
- package/lib/mineru-sdk-helper.py +123 -0
- package/lib/mineru-sdk.js +182 -0
- package/lib/mineru.js +268 -0
- package/lib/mirror-export.js +240 -0
- package/lib/outline.js +296 -0
- package/lib/page-image-helper.py +308 -0
- package/lib/page-image.js +454 -0
- package/lib/paper-metadata.js +242 -0
- package/lib/parse-export.js +631 -0
- package/lib/pdf-analysis-tool.js +750 -0
- package/lib/pdf-metadata-helper.py +75 -0
- package/lib/pdf-metadata.js +141 -0
- package/lib/pdf-page.js +261 -0
- package/lib/pdf-scan.js +134 -0
- package/lib/pdf-text.js +68 -0
- package/lib/picker.js +305 -0
- package/lib/portable.js +379 -0
- package/lib/purge.js +170 -0
- package/lib/read-tool.js +660 -0
- package/lib/reader-store.js +497 -0
- package/lib/reading-order.js +207 -0
- package/lib/rename-document.js +480 -0
- package/lib/rename.js +372 -0
- package/lib/renderer-status.js +161 -0
- package/lib/resolver.js +1152 -0
- package/lib/result-archive.js +157 -0
- package/lib/revisions.js +330 -0
- package/lib/search-database.js +62 -0
- package/lib/search-index.js +601 -0
- package/lib/search-scan.js +185 -0
- package/lib/search-source.js +23 -0
- package/lib/search-terms.js +539 -0
- package/lib/section-audit.js +209 -0
- package/lib/selection-message.js +332 -0
- package/lib/serial-tail.js +21 -0
- package/lib/slicing.js +425 -0
- package/lib/source-resolver.js +581 -0
- package/lib/stem-repair.js +189 -0
- package/lib/storage.js +562 -0
- package/lib/store-paths.js +253 -0
- package/lib/table-gfm.js +236 -0
- package/lib/tasks.js +270 -0
- package/lib/tex-align.js +509 -0
- package/lib/tex-blocks.js +653 -0
- package/lib/tex-fetch.js +455 -0
- package/lib/tex-figures.js +140 -0
- package/lib/tex-inline.js +166 -0
- package/lib/tex-merge.js +279 -0
- package/lib/tex-pipeline.js +242 -0
- package/lib/tex-prepare.js +746 -0
- package/lib/tex-tables.js +210 -0
- package/lib/text-similarity.js +85 -0
- package/lib/tool-contract.js +86 -0
- package/lib/tool-host.js +1058 -0
- package/lib/tool-identity.js +214 -0
- package/lib/translatable-text.js +48 -0
- package/lib/translate-prompts.js +709 -0
- package/lib/translate-queue.js +418 -0
- package/lib/translate-task.js +158 -0
- package/lib/translate.js +909 -0
- package/lib/update-check.js +438 -0
- package/lib/update-contracts.js +75 -0
- package/lib/usage.js +282 -0
- package/package.json +55 -4
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { effectiveBibliography, BIBLIOGRAPHIC_FIELDS, METADATA_FACT_FIELDS, metadataSourcesOf } from './bibliography.js'
|
|
2
|
+
import { citationMetadataOfRecord } from './citation-metadata.js'
|
|
3
|
+
import { recordIdentifier } from './metadata-enrichment.js'
|
|
4
|
+
import { documentPaths, hydrateDocument, readDocumentManifest } from './portable.js'
|
|
5
|
+
|
|
6
|
+
export const METADATA_FIELDS = METADATA_FACT_FIELDS
|
|
7
|
+
export const BIBLIOGRAPHY_SCHEMA = { type: 'object', additionalProperties: false, properties: Object.fromEntries(BIBLIOGRAPHIC_FIELDS.map(key => [key, { type: 'string' }])) }
|
|
8
|
+
const FIELD_SCHEMA = { type: 'object', additionalProperties: false, properties: Object.fromEntries(METADATA_FIELDS.map(key => [key,
|
|
9
|
+
key === 'authors' ? { type: 'array', items: { type: 'string' } } : { type: 'string' }])) }
|
|
10
|
+
const SOURCE_SCHEMA = { type: 'object', additionalProperties: false, properties: { provider: { type: 'string' }, identifier: { type: 'string' }, url: { type: 'string' }, checkedAt: { type: 'string' } } }
|
|
11
|
+
export const METADATA_SOURCES_SCHEMA = { type: 'array', items: { ...SOURCE_SCHEMA, properties: { field: { type: 'string' }, ...SOURCE_SCHEMA.properties } } }
|
|
12
|
+
export const metadataSourceRows = value => Object.entries(metadataSourcesOf(value)).map(([field, source]) => ({ field, ...source }))
|
|
13
|
+
export const ENRICHMENT_SCHEMA = { type: 'object', additionalProperties: false, properties: {
|
|
14
|
+
dryRun: { type: 'boolean' }, status: { type: 'string' }, fields: FIELD_SCHEMA, proposed: FIELD_SCHEMA,
|
|
15
|
+
// auto/matchedBy:题名完全相等且唯一时由系统自动认定,值为命中的来源(crossref / arxiv)。
|
|
16
|
+
auto: { type: 'boolean' }, matchedBy: { type: 'string' },
|
|
17
|
+
protected: { type: 'array', items: { type: 'string' } }, sources: { type: 'array', items: SOURCE_SCHEMA },
|
|
18
|
+
// 显式标识与记录对不上的地方(题名改了 / 首位作者不同):放行但要说出来,由人核对。
|
|
19
|
+
warnings: { type: 'array', items: { type: 'string' } },
|
|
20
|
+
// 本次查询**实际用的**规范标识,与 sources(溯源列表)分开放:保存时要把这一个原样传回来。
|
|
21
|
+
// 传 sources[0] 会在「这篇已有 arXiv 标识、本次查到的是正式 DOI」时被判成换了身份而拒绝保存。
|
|
22
|
+
queryIdentifier: { type: 'string', description: '本次查询实际使用的规范标识。' },
|
|
23
|
+
queryIdentifierType: { type: 'string', description: '该标识的类型(arxiv / doi)。' },
|
|
24
|
+
conflicts: { type: 'array', items: { type: 'object', additionalProperties: false, properties: { field: { type: 'string' }, current: { type: 'string' }, proposed: { type: 'string' } } } },
|
|
25
|
+
candidates: { type: 'array', items: { type: 'object', additionalProperties: false, properties: { identifier: { type: 'string' }, title: { type: 'string' }, authors: { type: 'array', items: { type: 'string' }, description: '候选摘要最多列前五位;指定 identifier 后查询完整名单。' }, year: { type: 'string' }, url: { type: 'string' },
|
|
26
|
+
source: { type: 'string', description: 'crossref 或 arxiv。' }, match: { type: 'string', description: 'exact = 题名与当前论文完全一致;similar = 题名互为前缀。' } } } },
|
|
27
|
+
} }
|
|
28
|
+
|
|
29
|
+
/** The portable manifest owns publication facts; the state record is its recoverable index. */
|
|
30
|
+
export async function effectiveMetadata(record) {
|
|
31
|
+
const raw = await readDocumentManifest(record, { validateArtifacts: false }) ?? record
|
|
32
|
+
const document = hydrateDocument(raw)
|
|
33
|
+
const derived = await citationMetadataOfRecord(raw, documentPaths(document).contentPath)
|
|
34
|
+
return { ...derived, ...(typeof raw.title === 'string' ? { title: raw.title } : {}),
|
|
35
|
+
clearedMetadata: raw.clearedMetadata ?? [], bibliography: effectiveBibliography(raw), metadataSources: metadataSourcesOf(raw.metadataSources) }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Validate every field before acquiring a write lock. Empty strings explicitly clear facts. */
|
|
39
|
+
export function validateMetadata(fields) {
|
|
40
|
+
if (!fields || typeof fields !== 'object' || Array.isArray(fields) || Object.keys(fields).length === 0) throw new Error('fields 必须包含要修改的书目字段。')
|
|
41
|
+
for (const [key, value] of Object.entries(fields)) {
|
|
42
|
+
if (!METADATA_FIELDS.includes(key)) throw new Error('不支持的元数据字段:' + key)
|
|
43
|
+
if (key === 'authors') {
|
|
44
|
+
if (!Array.isArray(value) || value.length > 500 || value.some(author => typeof author !== 'string' || author.trim() === '' || author.length > 1000)) throw new Error('authors 必须是非空作者名的数组(最多 500 位)。')
|
|
45
|
+
} else if (typeof value !== 'string' || value.length > 65536) throw new Error(key + ' 必须是字符串且不超过 65536 字符。')
|
|
46
|
+
}
|
|
47
|
+
return fields
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function updateMetadata(repository, documentId, fields) {
|
|
51
|
+
validateMetadata(fields)
|
|
52
|
+
return repository.withDocumentWrite(documentId, () => repository.update(async state => {
|
|
53
|
+
const stored = state.documents[documentId] ?? await repository.archivedDocument(documentId)
|
|
54
|
+
if (!stored) throw new Error('论文不存在。')
|
|
55
|
+
// A bad manifest is a write failure: never replace unreadable publication facts with defaults.
|
|
56
|
+
const raw = await readDocumentManifest(stored) ?? stored
|
|
57
|
+
const bibliography = effectiveBibliography(raw)
|
|
58
|
+
const next = { ...raw, bibliography, metadata: { ...raw.metadata }, keptMetadata: { ...keptMetadataOf(raw) } }
|
|
59
|
+
const cleared = new Set(Array.isArray(raw.clearedMetadata) ? raw.clearedMetadata : [])
|
|
60
|
+
for (const [key, value] of Object.entries(fields)) {
|
|
61
|
+
// 值被改写过,「这条我判断过」的前提就不成立了 —— 删掉它,下次补全照常比对。
|
|
62
|
+
delete next.keptMetadata[key]
|
|
63
|
+
if (['title', 'authors', 'year'].includes(key)) {
|
|
64
|
+
next[key] = Array.isArray(value) ? value.map(author => author.trim()) : value.trim()
|
|
65
|
+
delete next.metadata[key]
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
bibliography[key] = value.trim()
|
|
69
|
+
delete next[key]
|
|
70
|
+
delete next.metadata[key]
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
for (const [key, value] of Object.entries(fields)) {
|
|
74
|
+
if ((Array.isArray(value) ? value.length : value.trim().length) === 0) cleared.add(key)
|
|
75
|
+
else cleared.delete(key)
|
|
76
|
+
}
|
|
77
|
+
next.clearedMetadata = [...cleared]
|
|
78
|
+
next.metadataSources = metadataSourcesOf(raw.metadataSources)
|
|
79
|
+
for (const key of Object.keys(fields)) next.metadataSources[key] = { provider: 'manual', checkedAt: new Date().toISOString() }
|
|
80
|
+
bibliography.modifiedAt = new Date().toISOString()
|
|
81
|
+
// Commit the authoritative manifest before the index. A failed state write is repaired by reads.
|
|
82
|
+
await repository.writeArchivedDocument(next)
|
|
83
|
+
if (state.documents[documentId]) state.documents[documentId] = next
|
|
84
|
+
return { documentId, bibliography }
|
|
85
|
+
}))
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** 「查询的是哪一篇」:与 resolve() 判定标识用的是同一个函数,编号与版本都留在键里。 */
|
|
89
|
+
function identityKey(record) {
|
|
90
|
+
const identifier = recordIdentifier(record)
|
|
91
|
+
return identifier === null ? '' : identifier.type + ':' + identifier.id
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** manifest 里的「已裁定保留」记录;形状坏掉按空处理(它只是去重提示,不承载事实)。 */
|
|
95
|
+
function keptMetadataOf(raw) {
|
|
96
|
+
const value = raw?.keptMetadata
|
|
97
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value) ? value : {}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 两个书目值是不是同一个事实:数组逐项比,字符串按**归一空白**后比。
|
|
102
|
+
*
|
|
103
|
+
* 🔴 空白要归一而不是只 trim:Crossref 返回的题名里常年混着不间断空格(U+00A0),
|
|
104
|
+
* 页面上一模一样、字节上不同 —— 实测一篇 LNCS 论文的题名与登记记录长度都是 73,
|
|
105
|
+
* 差别只在 `with` 与后一个词之间那一个 NBSP,于是每次补全都报一条无法处理的「题名冲突」。
|
|
106
|
+
* JS 的 `\s` 覆盖 NBSP / 窄空格 / 全角空格,折成一个普通空格即可。
|
|
107
|
+
*/
|
|
108
|
+
function sameFact(left, right) {
|
|
109
|
+
const key = value => Array.isArray(value)
|
|
110
|
+
? JSON.stringify(value.map(item => String(item ?? '').replace(/\s+/gu, ' ').trim()))
|
|
111
|
+
: String(value ?? '').replace(/\s+/gu, ' ').trim()
|
|
112
|
+
return key(left) === key(right)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function planEnrichment(raw, lookup, dryRun) {
|
|
116
|
+
const bibliography = effectiveBibliography(raw)
|
|
117
|
+
const cleared = new Set(raw.clearedMetadata ?? [])
|
|
118
|
+
const kept = keptMetadataOf(raw)
|
|
119
|
+
const sources = metadataSourcesOf(raw.metadataSources)
|
|
120
|
+
const fields = {}, conflicts = [], protectedFields = []
|
|
121
|
+
const previewText = value => {
|
|
122
|
+
const string = Array.isArray(value) ? value.join('; ') : String(value)
|
|
123
|
+
return string.length > 1000 ? string.slice(0, 1000) + '…' : string
|
|
124
|
+
}
|
|
125
|
+
for (const [key, value] of Object.entries(lookup.fields)) {
|
|
126
|
+
if (!METADATA_FIELDS.includes(key)) continue
|
|
127
|
+
if (cleared.has(key)) { protectedFields.push(key); continue }
|
|
128
|
+
// 裁定过「保持原样」的字段:来源这次给的还是同一个值就不再打扰(登记记录变了会重新报)。
|
|
129
|
+
// 🔴 只跳过**同一个值**:来源真的更新了仍要报,否则用户永远看不到新的冲突。
|
|
130
|
+
const keptRow = kept[key]
|
|
131
|
+
if (keptRow !== undefined && sameFact(keptRow.value, value)) continue
|
|
132
|
+
const current = ['title', 'authors', 'year'].includes(key) ? raw[key] : bibliography[key]
|
|
133
|
+
const populated = Array.isArray(current) ? current.length > 0 : current !== undefined && current !== null && String(current).trim() !== ''
|
|
134
|
+
if (populated) {
|
|
135
|
+
if (!sameFact(current, value)) conflicts.push({ field: key,
|
|
136
|
+
current: previewText(current), proposed: previewText(value) })
|
|
137
|
+
} else if (sources[key]?.provider !== 'manual') fields[key] = value
|
|
138
|
+
else protectedFields.push(key)
|
|
139
|
+
}
|
|
140
|
+
const currentPreprint = bibliography.itemType === 'preprint' || /^arxiv\b/i.test(bibliography.publicationTitle)
|
|
141
|
+
const incomingPreprint = lookup.fields.itemType === 'preprint'
|
|
142
|
+
// 版本差异已被裁定保留(keptMetadata 覆盖了版本证据字段、且来源值没变)就不再报:
|
|
143
|
+
// 「记录是 ICLR 2025 会议版、来源是 arXiv 预印本」这种判断做过一次就够了,
|
|
144
|
+
// 否则用户每点一次补全都要读一遍同一句「版本冲突」,而它已经没有可处理的内容。
|
|
145
|
+
const keptMatches = key => {
|
|
146
|
+
const row = kept[key]
|
|
147
|
+
return row !== undefined && lookup.fields[key] !== undefined && sameFact(row.value, lookup.fields[key])
|
|
148
|
+
}
|
|
149
|
+
const versionJudged = keptMatches('itemType') || keptMatches('publicationTitle')
|
|
150
|
+
const versionConflict = ((lookup.fields.itemType && (bibliography.itemType || currentPreprint || bibliography.publicationTitle) && currentPreprint !== incomingPreprint)
|
|
151
|
+
|| conflicts.some(row => ['year', 'itemType'].includes(row.field))) && !versionJudged
|
|
152
|
+
return { dryRun, status: versionConflict ? 'version-conflict' : lookup.candidates ? 'candidates' : Object.keys(fields).length ? 'ready' : 'unchanged',
|
|
153
|
+
fields: versionConflict ? {} : fields, conflicts, protected: protectedFields, sources: lookup.sources,
|
|
154
|
+
// 🔴 本次查询**实际用的**规范标识,与 sources 分开报:sources 是溯源列表(arXiv→正式 DOI 时
|
|
155
|
+
// 正式 DOI 排在前面),拿 sources[0] 当"本次查询身份"回传,会在记录只绑了 arXiv 时被后端
|
|
156
|
+
// 以「与已有标识不同」拒绝 —— 预览说 ready、点保存却失败。名字里带 query 就是为了提醒这一点。
|
|
157
|
+
...(lookup.identifier ? { queryIdentifier: lookup.identifier.id, queryIdentifierType: lookup.identifier.type } : {}),
|
|
158
|
+
|
|
159
|
+
...(versionConflict || conflicts.length ? { proposed: lookup.fields } : {}),
|
|
160
|
+
...(lookup.candidates ? { candidates: lookup.candidates } : {}),
|
|
161
|
+
...(lookup.auto ? { auto: true } : {}),
|
|
162
|
+
...(lookup.matchedBy ? { matchedBy: lookup.matchedBy } : {}),
|
|
163
|
+
...(Array.isArray(lookup.warnings) && lookup.warnings.length > 0 ? { warnings: lookup.warnings } : {}) }
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* 记下「这些字段保持原样」—— 冲突裁定保留时的落点。
|
|
168
|
+
*
|
|
169
|
+
* 🔴 存的是**本次来源值**,不是字段本身:下次补全只要来源还是这个值就不再报冲突,
|
|
170
|
+
* 登记记录一更新(值变了)立刻重新冒出来。所以这不是"忽略该字段",是"这回判断过"。
|
|
171
|
+
*
|
|
172
|
+
* 与 clearedMetadata(手动清空)互不替代:清空的字段本来就不参与比对。
|
|
173
|
+
*/
|
|
174
|
+
export async function keepMetadata(repository, documentId, fields, resolver, { identifier } = {}) {
|
|
175
|
+
const keys = [...new Set((Array.isArray(fields) ? fields : []).filter(key => typeof key === 'string' && key !== ''))]
|
|
176
|
+
if (keys.length === 0) throw new Error('keep 必须包含要保留的书目字段。')
|
|
177
|
+
for (const key of keys) if (!METADATA_FIELDS.includes(key)) throw new Error('不支持的元数据字段:' + key)
|
|
178
|
+
// 来源值在**锁外**查(网络 IO 不该占着论文写锁),锁内只做写入与冲突校验。
|
|
179
|
+
const stored = (await repository.snapshot()).documents[documentId] ?? await repository.archivedDocument(documentId)
|
|
180
|
+
if (!stored) throw new Error('论文不存在。')
|
|
181
|
+
const original = await readDocumentManifest(stored) ?? stored
|
|
182
|
+
const lookup = await resolver.resolve(original, { identifier })
|
|
183
|
+
const source = Array.isArray(lookup.sources) ? lookup.sources[0] ?? {} : {}
|
|
184
|
+
return repository.withDocumentWrite(documentId, () => repository.update(async state => {
|
|
185
|
+
const current = state.documents[documentId] ?? await repository.archivedDocument(documentId)
|
|
186
|
+
if (!current) throw new Error('论文不存在。')
|
|
187
|
+
const raw = await readDocumentManifest(current) ?? current
|
|
188
|
+
const next = { ...raw, bibliography: effectiveBibliography(raw), keptMetadata: { ...keptMetadataOf(raw) } }
|
|
189
|
+
const keptAt = new Date().toISOString()
|
|
190
|
+
const recorded = []
|
|
191
|
+
for (const key of keys) {
|
|
192
|
+
const value = lookup.fields?.[key]
|
|
193
|
+
// 来源这次没给这个字段:没有可"保留"的对照值,跳过而不是记一条空记录。
|
|
194
|
+
if (value === undefined || value === null || String(value).trim() === '') continue
|
|
195
|
+
next.keptMetadata[key] = { value, provider: source.provider ?? '', identifier: lookup.identifier?.id ?? '', keptAt }
|
|
196
|
+
recorded.push(key)
|
|
197
|
+
}
|
|
198
|
+
if (recorded.length > 0) await repository.writeArchivedDocument(next)
|
|
199
|
+
if (recorded.length > 0 && state.documents[documentId]) state.documents[documentId] = next
|
|
200
|
+
return { documentId, kept: recorded }
|
|
201
|
+
}))
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Fetch without a write lock, then re-read and plan under the paper lock before committing. */
|
|
205
|
+
export async function enrichMetadata(repository, documentId, resolver, { identifier, dryRun = true, search = true } = {}) {
|
|
206
|
+
const stored = (await repository.snapshot()).documents[documentId] ?? await repository.archivedDocument(documentId)
|
|
207
|
+
if (!stored) throw new Error('论文不存在。')
|
|
208
|
+
const original = await readDocumentManifest(stored) ?? stored
|
|
209
|
+
const lookup = await resolver.resolve(original, { identifier, search })
|
|
210
|
+
validateMetadata(Object.keys(lookup.fields).length ? lookup.fields : { title: '' })
|
|
211
|
+
if (Object.keys(lookup.fields).length === 0) return planEnrichment(original, lookup, dryRun)
|
|
212
|
+
if (dryRun) return planEnrichment(original, lookup, true)
|
|
213
|
+
return repository.withDocumentWrite(documentId, () => repository.update(async state => {
|
|
214
|
+
const stored = state.documents[documentId] ?? await repository.archivedDocument(documentId)
|
|
215
|
+
if (!stored) throw new Error('论文不存在。')
|
|
216
|
+
const raw = await readDocumentManifest(stored) ?? stored
|
|
217
|
+
for (const key of ['title', 'authors', 'year']) {
|
|
218
|
+
if (JSON.stringify(raw[key]) !== JSON.stringify(original[key])) throw new Error('查询期间论文元数据已改变,请重新预览。')
|
|
219
|
+
}
|
|
220
|
+
if (effectiveBibliography(raw).doi !== effectiveBibliography(original).doi) throw new Error('查询期间 DOI 已改变,请重新预览。')
|
|
221
|
+
// 标识不止 DOI 一种:arXiv 直入的记录标识在 bibliography.archiveId 里,查询用的就是它。
|
|
222
|
+
// 只比 DOI 会在「查询期间改了 arXiv 编号」时恒等通过(两侧 DOI 都是空串),
|
|
223
|
+
// 于是旧编号的年份与网址被写到了新编号名下。这里比的是 resolve() 认标识的同一个函数:
|
|
224
|
+
// 无显式 identifier 时它就以记录标识为准,有显式 identifier 时 resolve() 已核对两者一致,
|
|
225
|
+
// 所以「标识没变」等价于「这次结果仍属于这篇」。编号与版本一起比 —— 换了版本,事实可能就不同。
|
|
226
|
+
if (identityKey(raw) !== identityKey(original)) throw new Error('查询期间论文标识已改变,请重新预览。')
|
|
227
|
+
const plan = planEnrichment(raw, lookup, false)
|
|
228
|
+
if (plan.status !== 'ready') return plan
|
|
229
|
+
const next = { ...raw, bibliography: effectiveBibliography(raw), metadataSources: metadataSourcesOf(raw.metadataSources) }
|
|
230
|
+
const checkedAt = new Date().toISOString()
|
|
231
|
+
for (const [key, value] of Object.entries(plan.fields)) {
|
|
232
|
+
if (['title', 'authors', 'year'].includes(key)) next[key] = value
|
|
233
|
+
else next.bibliography[key] = value
|
|
234
|
+
const source = ['repository', 'archiveId'].includes(key) ? lookup.sources.find(row => row.provider === 'arxiv') ?? lookup.sources[0] : lookup.sources[0]
|
|
235
|
+
next.metadataSources[key] = { ...source, checkedAt }
|
|
236
|
+
}
|
|
237
|
+
next.bibliography.modifiedAt = checkedAt
|
|
238
|
+
await repository.writeArchivedDocument(next)
|
|
239
|
+
if (state.documents[documentId]) state.documents[documentId] = next
|
|
240
|
+
return plan
|
|
241
|
+
}))
|
|
242
|
+
}
|