-
Notifications
You must be signed in to change notification settings - Fork 2
/
transform.js
99 lines (92 loc) · 3.11 KB
/
transform.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import * as xml2js from 'xml2js'
function makeSureAlwaysArray(x) {
return Array.isArray(x) ? x : [x]
}
function transformMwXmlRevision2PkcJson(rev) {
if (!(rev.format === 'text/x-wiki' && rev.model === 'wikitext')) {
throw "We only support parsing wikitext for now"
}
const verification = rev.verification
// Prepare a content hash map of slots starting with the main slot.
const contentObj = {
// If rev.text['_'] is undefined, it means the revision has empty content.
"main": rev.text['_'] ?? ""
}
// And the rest of the slots.
for (const e of makeSureAlwaysArray(rev.content)) {
contentObj[e.role] = e.text['_'] ?? ""
}
// Next, we ensure that contentObj is sorted by its keys. This is so that its
// stringified representation is consistent with the canonical JSON format.
const sortedContentObj = {}
for (const k of Object.keys(contentObj).sort()) {
sortedContentObj[k] = contentObj[k]
}
const out = {
content: {
rev_id: rev.id,
content: sortedContentObj,
content_hash: verification.content_hash,
},
metadata: {
domain_id: verification.domain_id,
time_stamp: verification.time_stamp,
metadata_hash: verification.metadata_hash,
},
}
// Optional fields
if ("previous_verification_hash" in verification) {
out.metadata.previous_verification_hash = verification.previous_verification_hash
}
if ("signature" in verification) {
out.signature = {
signature: verification.signature,
public_key: verification.public_key,
wallet_address: verification.wallet_address,
signature_hash: verification.signature_hash,
}
}
if ("witness" in verification) {
out.witness = verification.witness
out.witness.structured_merkle_proof = JSON.parse(out.witness.structured_merkle_proof)
}
if ("file_hash" in contentObj) {
// TODO we set it to empty string because the XML export currently doesn't
// contain the file data.
out.content.file = {data: ""}
}
return out
}
function transformRevisions(revisions) {
const out = {}
for (const rev of revisions) {
if (!("verification" in rev)) {
// If the revision does not have verification data, skip to next
// revision. Vim tip 99: 'gql' makes your comment look well.
continue
}
out[rev.verification.verification_hash] = transformMwXmlRevision2PkcJson(rev)
}
return out
}
/*
* Returns an array of export detail of pages. If the XML string only has one
* page, then it is an array of 1 page data.
*/
async function parseMWXmlString(fileContent) {
const parsed = await xml2js.parseStringPromise(fileContent, {explicitArray : false})
const pages = makeSureAlwaysArray(parsed.mediawiki.page)
return pages.map(page => {
// if page.revision is not an array, then it means it contains only 1
// revision.
const revisions = makeSureAlwaysArray(page.revision)
const offline_data = {
title: page.title,
data_accounting_chain_height: page.data_accounting_chain_height,
version: "TODO",
revisions: transformRevisions(revisions)
}
return offline_data
})
}
export { parseMWXmlString }