-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.ts
309 lines (260 loc) · 7.5 KB
/
server.ts
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import {
diff_match_patch as DiffMatchPatch,
DIFF_DELETE,
DIFF_INSERT,
} from "diff-match-patch"
import fetch from "node-fetch"
import express from "express"
import SocketIo from "socket.io"
import http from "http"
import child_process from "child_process"
const version = child_process
.execSync("git rev-parse --short HEAD", { encoding: "utf8" })
.trim()
const dmp = new DiffMatchPatch()
dmp.Diff_Timeout = 0
export class Revision {
public text: string | undefined
public id: number
public author: string | undefined
public comment: string | undefined
public parentid: number
public timestamp: string
//True if some of the text attributed to this rev may in fact be from a revdel'd version
public includesRevdel: boolean = false
constructor(rev: any) {
this.text = rev.slots.main["*"]
this.id = rev.revid
this.author = rev.user
this.comment = rev.comment
this.parentid = rev.parentid
this.timestamp = rev.timestamp
}
hasText(): this is KnownRev {
return !!this.text
}
toJson() {
return {
id: this.id,
author: this.author,
comment: this.comment,
includesRevdel: this.includesRevdel,
parentid: this.parentid,
timestamp: this.timestamp,
}
}
}
let lastChunkId = 0
class Chunk {
added: Revision | null
removed: Revision | null
text: string
id: number
constructor(text: string) {
this.text = text
this.added = null
this.removed = null
this.id = ++lastChunkId
}
split(idx: number) {
const chunk1 = new Chunk(this.text.substring(0, idx))
const chunk2 = new Chunk(this.text.substring(idx))
;[chunk1, chunk2].forEach((c) => {
c.added = this.added
c.removed = this.removed
})
return [chunk1, chunk2]
}
toJson() {
return {
text: this.text,
added: this.added && this.added.toJson(),
removed: this.removed && this.removed.toJson(),
id: this.id,
}
}
}
type KnownRev = Revision & { text: string }
export class Article {
title: string
chunks: Chunk[]
earliestRev: KnownRev
latestRev: KnownRev
constructor(title: string, rev: Revision) {
if (!rev.hasText()) {
throw new Error("What? Latest rev was revdel'd")
}
this.chunks = [new Chunk(rev.text)]
this.latestRev = rev
this.earliestRev = rev
this.title = title
}
addRevisionBefore(revision: Revision) {
if (!revision.hasText()) {
// Revdel'd.
this.earliestRev.includesRevdel = true
return
}
let diff = dmp.diff_main(revision.text, this.earliestRev.text)
dmp.diff_cleanupSemantic(diff)
let diffChunkId = 0
let articleChunkId = 0
while (true) {
const diffChunk = diff[diffChunkId]
const articleChunk = this.chunks[articleChunkId]
if (diffChunk && diffChunk[1] == "") {
// Empty chunk. Ignore it.
diffChunkId++
continue
}
if (diffChunk && diffChunk[0] == DIFF_DELETE) {
// This is the revision where this chunk gets removed. Let's add an
// article chunk for it, so that when adding earlier chunks we can match up.
const chunk = new Chunk(diffChunk[1])
chunk.removed = revision
this.chunks.splice(articleChunkId, 0, chunk)
diffChunkId++
articleChunkId++
continue
}
if (articleChunk && articleChunk.added) {
// If we know when the chunk was added, it's already gone and we
// don't care about it.
articleChunkId++
continue
}
if (!diffChunk || !articleChunk) {
if (diffChunk || articleChunk) {
throw new Error("length mismatch")
}
break
}
if (diffChunk[1].length < articleChunk.text.length) {
// We need to split the article chunk.
this.chunks.splice(
articleChunkId,
1,
...articleChunk.split(diffChunk[1].length)
)
continue
}
if (diffChunk[1].length > articleChunk.text.length) {
// We need to split the diff chunk.
diff.splice(
diffChunkId,
1,
[diffChunk[0], diffChunk[1].substring(0, articleChunk.text.length)],
[diffChunk[0], diffChunk[1].substring(articleChunk.text.length)]
)
continue
}
if (diffChunk[1] != articleChunk.text) {
throw new Error(
`wat, mismatch :((( ${diffChunk[1]} ${articleChunk.text}`
)
}
// OK, we've got two matching chunks.
if (diffChunk[0] == DIFF_INSERT) {
// This is where this chunk was added.
articleChunk.added = this.earliestRev
}
diffChunkId++
articleChunkId++
}
this.earliestRev = revision
}
stats() {
const stats: Record<string, number> = {}
this.chunks.forEach((chunk) => {
if (!chunk.removed) {
const author = (chunk.added && chunk.added.author) || "unknown"
stats[author] = stats[author] || 0
stats[author] += chunk.text.length
}
})
return stats
}
toJson() {
return {
chunks: this.chunks
.filter((chunk) => !chunk.removed)
.map((chunk) => chunk.toJson()),
stats: this.stats(),
title: this.title,
}
}
}
async function run(title: string, client: SocketIo.Socket) {
try {
let rvcontinue: string | null = null
let art: Article | null = null
let connected = true
client.on("disconnect", (reason) => {
console.log(`[${title}] stopping (disconnected: ${reason})`)
connected = false
})
while (true) {
if (!connected) {
break
}
const url: string = `https://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=${encodeURIComponent(
title
)}&rvlimit=50&rvprop=timestamp%7Cuser%7Ccomment%7Cids|contentmodel|content&rvslots=main&format=json${
rvcontinue ? "&rvcontinue=" + rvcontinue : ""
}`
const res = await fetch(url, {
headers: {
"User-Agent": `Whodunnit/${version} (https://tools.wmflabs.org/whodunnit; User:Gaelan; https://github.com/Gaelan/Whodunnit)`,
},
})
const json = await res.json()
if (!connected) {
break
}
const pages = json.query.pages
const pageId = Object.keys(pages)[0]
let earlierRevs: any[] = []
if (!art) {
let firstRev
;[firstRev, ...earlierRevs] = pages[pageId].revisions
art = new Article(title, new Revision(firstRev))
} else {
earlierRevs = pages[pageId].revisions
}
earlierRevs.forEach((rev: any, idx) => {
const revObj = new Revision(rev)
console.log(
`[${title}] handling rev ${revObj.id} ${revObj.comment} by ${revObj.author}`
)
art!.addRevisionBefore(revObj)
const stats = art!.stats()
})
client.emit("update", art.toJson())
if (!art.stats()["unknown"]) {
console.log(`[${title}] stopping (all resolved)`)
break
}
if (json.continue) {
rvcontinue = json.continue.rvcontinue
} else {
console.log(`[${title}] stopping (reached end)`)
break
}
}
} catch (e) {
console.error(e)
process.exit(1)
}
}
const app = express()
const server = new http.Server(app)
const io = SocketIo(server, { path: "/socket.io" })
app.use("/", express.static(__dirname + "/whodunnit-client/build"))
io.on("connection", (client) => {
client.on("requestArticle", (message) => {
run(message.article, client)
})
})
server.listen(process.env.PORT, () =>
console.log(`Listening on port ${process.env.PORT}`)
)