-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.js
448 lines (369 loc) · 13.6 KB
/
report.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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
const fs = require('fs/promises')
const path = require('path')
const axios = require('axios')
const nodemailer = require("nodemailer");
const head = `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="fi" xml:lang="fi">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="cache-control" content="no-cache" />
<!--[if lt IE 7]></base><![endif]-->
<link href="../css/bootstrap.css" rel="stylesheet">
<link href="./../css/overrides.css" rel="stylesheet">
<style>
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 1.42857143;
color: #333;
}
h4 {
margin-top:20px;
}
.site-logo-container {
background-color: #002957;
color: white;
padding: 10px;
}
table {
margin-top: 20px;
}
table td {
min-width: 250px;
}
</style>
<title>WD-Watch</title>
</head>
<body>
<header class="site-header container-fluid hidden-print">
<div class="site-brand-container container">
<div class="row">
<div class="col-2 col-sm-2 col-md-10 col-xl-7 site-logo-container">
<p class="hidden-sm-down site-name">
<h2>WD-watch</h2>
</p>
</div>
</div>
</div>
</header>
<div class="container">
<div class="col-md-12 col-lg-12 col-xl-12 main">
<img src = "../images/gardener.svg" alt="My Happy gardener" style="position: absolute; right:0px; height:120px; margin-top:60px"/>
`
module.exports = class Report {
constructor(config) {
this.config = config
console.log('mailer:', config.mailer )
console.log('mailer port:', config.mailer_port )
this.transporter = nodemailer.createTransport({
host: config.mailer,
port: config.mailer_port,
secure: false,
tls: { minVersion: 'TLSv1' }
});
}
getHead() {
return head
}
async create(wdset, db, mode, mail) {
try {
const date = new Date()
let day = date.getDate();
let month = date.getMonth() + 1;
let year = date.getFullYear();
var today = `${year}-${month}-${day}`;
var filename = wdset + '_' + today + '.html'
var items = await this.getEdits(wdset, db, mode)
const report = this.parseEdits(items, wdset)
report.property_labels = await this.getLabels(report.property_set)
report.wid_labels = await this.getLabels(report.wid_set)
report.action_labels = await this.getActionLabels()
var html = this.createHTML(report, wdset)
await fs.writeFile(path.join('public', 'reports', filename), html);
if (mail) await this.sendMail(mail, wdset, html)
return '/reports/' + filename
} catch (err) {
console.log(err);
throw(err)
}
}
async sendMail(email, wdset, html) {
var message = {
from: "[email protected]",
to: email,
subject: `WD-watch report: ${wdset}`, // Subject line
text: "raportti", // plain text body
html: `${html}` // html body
}
const info = await this.transporter.sendMail(message)
console.log("Message sent: %s", info.messageId);
return info.messageId
}
async getEdits(wdset, db, mode) {
console.log('checking edits...')
const property_regex = /\[\[Property:(.*?)\]\]:(.*?),/gm;
var count = 0;
var total = 0
var query = {wdset: wdset}
var items = await db.watchlist.find(query)
if(items.length === 0) throw('no items found')
for(var item of items) {
total++
var url = this.config.site + "/w/api.php?action=query&format=json&prop=revisions&titles=" + item._id + "&rvprop=ids|timestamp|flags|comment|user&rvlimit=" + this.config.rvlimit + "&rvdir=older"
console.log(url)
var result = await axios(url)
var json = result.data
var key = Object.keys(json.query.pages)
if(!item.edit_count) mode = 'all' // in first run we get all edits
var timestamp = json.query.pages[key[0]].revisions[0].timestamp; // timestamp of latest edit
if(mode == 'all' || timestamp != item.latest_edit) {
// we have an edit, now check how many edits there are after last check
var edit_count = 0
item.edits = []
for(var i=0; i < this.config.rvlimit; i++) {
if(json.query.pages[key[0]].revisions[i]) {
if(mode == 'all' || item.latest_edit != json.query.pages[key[0]].revisions[i].timestamp) {
edit_count++
var edit_obj = {}
edit_obj.comment = json.query.pages[key[0]].revisions[i].comment + '#'
edit_obj.user = json.query.pages[key[0]].revisions[i].user
edit_obj.time = json.query.pages[key[0]].revisions[i].timestamp
item.edits.push(edit_obj)
}
}
}
var update = {
latest_edit: timestamp,
edit_count: edit_count
}
var response = await db.watchlist.update({_id: item._id}, {$set: update}, {returnUpdatedDocs:1})
count++;
} else {
console.log('no change ' + item.label)
}
}
return items
}
parseEdits(items, wdset) {
const action_regex = /\/* (.*):.\|/gm;
const label_regex = /((\|)(..)).*\*\/(.*?)((\[\[)|(,)|#)/gm;
const lang_regex = /(\|)(..)/gm;
const property_regex = /\[\[Property:(.*?)\]\]:(.*?),/gm;
const wid_regex = /\[\[Q(.*?)\]\]/gm;
var report = {}
report.translations_added = []
report.edited_labels = []
report.descriptions = []
report.properties = []
report.property_set = new Set()
report.wid_set = new Set()
report.edit_count = 0
report.total_count = 0
for(var item of items) {
report.total_count++
if(item.edits && item.edits.length) {
for(var edit of item.edits) {
if(edit.comment.includes('wbsetlabel-add:')) {
var transl = this.getRegex(label_regex, edit, 4)
var lang = this.getRegex(lang_regex, edit, 2)
var obj = {item: item, edit: edit, transl: transl, lang: lang}
report.translations_added.push(obj)
report.edit_count++
} else if (edit.comment.includes('wbsetlabel-set:')) {
var transl = this.getRegex(label_regex, edit, 4)
var lang = this.getRegex(lang_regex, edit, 2)
var obj = {item: item, edit: edit, transl: transl, lang: lang}
report.edited_labels.push(obj)
report.edit_count++
} else if (edit.comment.includes('wbsetdescription-add')) {
var value = this.getRegex(label_regex, edit, 4)
var lang = this.getRegex(lang_regex, edit, 2)
var obj = {item: item, edit: edit, value: value, lang: lang}
report.descriptions.push(obj)
report.edit_count++
} else if (edit.comment.includes('[[Property:P')) {
var property = this.getRegex(property_regex, edit, 1)
var wid = this.getRegex(wid_regex, edit, 1)
var value = this.getRegex(property_regex, edit, 2)
var action = this.getRegex(action_regex, edit, 1)
if(wid) wid = 'Q' + wid
if(this.checkProperty(property, wdset)) {
var obj = {item: item, edit: edit, property, property, wid: wid, value: value, action:action}
report.properties.push(obj)
report.property_set.add(property)
if(wid.startsWith('Q')) report.wid_set.add(wid)
report.edit_count++
}
}
}
}
}
return report
}
checkProperty(property, wdset) {
if(!property) return false
// if "wdset" is defined and its has key "properties" then allow only properties listed
if(this.config.wdsets[wdset] && this.config.wdsets[wdset].properties) {
if(this.config.wdsets[wdset].properties.includes(property)) {
return true
} else {
return false
}
}
// otherwise show all
return true
}
createHTML(report, wdset) {
var date = this.getDate()
var html = head
html += `<h1>${this.config.main_title} (${wdset})</h1>`
html += `<p>${this.config.title_item_count}: ${report.total_count} </p>\n\n`
html += `<p>${this.config.title_edit_count}: ${report.edit_count} </p>\n\n`
html += `<p>${this.config.title_report_date}: ${date} </p>\n\n`
// TOC
if(report.translations_added.length) {
html += `<a href="#title_translations"><h4>${this.config.title_translations_added} (${report.translations_added.length})</h4></a>`
}
if(report.edited_labels.length) {
html += `<a href="#modified_labels"><h4>${this.config.title_translations_edited} (${report.edited_labels.length})</h4></a>`
}
if(report.descriptions.length) {
html += `<a href="#descriptions"><h4>${this.config.title_descriptions} (${report.descriptions.length})</h4></a>`
}
if(report.properties.length) {
html += `<a href="#properties"><h4>${this.config.title_properties} (${report.properties.length})</h4></a>`
}
var prev_item = null
// added translations
if(report.translations_added.length) {
html += `<a name="title_translations"><br></a><h2>${this.config.title_translations_added} (${report.translations_added.length})</h2>`
html += '<ul>\n'
for(var edit of report.translations_added) {
if(prev_item != edit.item._id) {
html += " <li><a target='_blank' href='" + this.config.site + '/wiki/' + edit.item._id + "'> <h4>" + edit.item.label + "</h4></a></li>\n"
html += " <div>lang: <b>" + edit.lang + "</b> " + edit.transl + "</div>\n"
} else {
html += " <div>lang: <b>" + edit.lang + "</b> " + edit.transl + "</div>\n"
}
prev_item = edit.item._id
}
html += "</ul>\n\n"
}
// modified labels
if(report.edited_labels.length) {
html += `<a name="modified_labels"><br></a><h2>${this.config.title_translations_edited} (${report.edited_labels.length})</h2>`
html += '<ul>\n'
prev_item = null
for(edit of report.edited_labels) {
if(prev_item != edit.item._id) {
html += " <li><a target='_blank' href='" + this.config.site + '/wiki/' + edit.item._id + "'> <h4>" + edit.item.label + "</h4></a></li>\n"
html += " <div>lang: <b>" + edit.lang + "</b> " + edit.transl + "</div>\n"
} else {
html += " <div>lang: <b>" + edit.lang + "</b> " + edit.transl + "</div>\n"
}
prev_item = edit.item._id
}
html += "</ul>\n"
}
// descriptions
if(report.descriptions.length) {
html += `<a name="descriptions"><br></a><h2>${this.config.title_descriptions} (${report.descriptions.length})</h2>`
html += '<ul>\n'
prev_item = null
for(edit of report.descriptions) {
if(prev_item != edit.item._id) {
html += " <li><a target='_blank' href='" + this.config.site + '/wiki/' + edit.item._id + "'> <h4>" + edit.item.label + "</h4></a></li>\n"
html += " <div>lang: <b>" + edit.lang + "</b> " + edit.value + "</div>\n"
} else {
html += " <div>lang: <b>" + edit.lang + "</b> " + edit.value + "</div>\n"
}
prev_item = edit.item._id
}
html += "</ul>\n"
}
// properties
if(report.properties.length) {
html += `<a name="properties"><br></a><h2>${this.config.title_properties} (${report.properties.length})</h2>`
html += '<ul>\n'
prev_item = null
for(edit of report.properties) {
var property_label = edit.property
if(report.property_labels[edit.property] && report.property_labels[edit.property].labels && report.property_labels[edit.property].labels[this.config.preferred_lang]) {
property_label = report.property_labels[edit.property].labels[this.config.preferred_lang].value
}
if(prev_item != edit.item._id) {
html += " <li><a target='_blank' href='" + this.config.site + '/wiki/' + edit.item._id + "'> <h4>" + edit.item.label + "</h4></a></li>\n"
html += ` <div>${report.action_labels[edit.action]} <b>${property_label}</b> (${edit.property}) ${this.WDLink(edit, report.wid_labels)}</div>\n`
} else {
html += ` <div>${report.action_labels[edit.action]} <b>${property_label}</b> (${edit.property}) ${this.WDLink(edit, report.wid_labels)} ${edit.value}</div> \n`
}
prev_item = edit.item._id
}
html += "</ul>\n"
}
return html
}
WDLink(edit, labels) {
if(edit.wid) {
var link_title = edit.wid
if(labels[edit.wid].labels[this.config.preferred_lang])
link_title = labels[edit.wid].labels[this.config.preferred_lang].value
else if(labels[edit.wid].labels.en)
link_title = labels[edit.wid].labels.en.value
return `<a target="_blank" href="${this.config.site}/wiki/${edit.wid}">${link_title}</a>`
} else if(!edit.value.trim().startsWith('[[Q')) return edit.value
else return ''
}
async getLabels(set) {
var arr = [...set]
var entities = {}
var chunks = this.getChunks(arr, 25)
for(var chunk of chunks) {
var url = `${this.config.site}/w/api.php?action=wbgetentities&format=json&ids=${chunk.join('|')}&props=labels%7Cdescriptions&languages=${this.config.preferred_lang}|en&formatversion=2`
console.log(url)
var response = await axios.get(url)
entities = {...entities, ...response.data.entities}
}
return entities
}
getChunks(array, chunk_size) {
var chunks = []
for (let i = 0; i < array.length; i += chunk_size) {
const chunk = array.slice(i, i + chunk_size);
chunks.push(chunk)
}
return chunks
}
async getActionLabels() {
var plural_regex = /{{(PLURAL:..)\|(.*)\|/gm
var labels = {}
const url = `https://www.mediawiki.org/w/api.php?action=query&meta=allmessages&refix=wikibase-entity-summary&amlang=${this.config.preferred_lang}&format=json`
var response = await axios.get(url)
for(var msg of response.data.query.allmessages) {
var label = msg.name.replace('wikibase-entity-summary-','')
var transl = msg['*']
if(transl.includes('{{PLURAL:')) {
var plural = this.getRegex(plural_regex, transl, 2)
transl = transl.replace(/{{PLURAL:.*}}/, plural)
}
labels[label] = transl
}
return labels
}
getDate() {
const date = new Date();
let day = date.getDate();
let month = date.getMonth() + 1;
let year = date.getFullYear();
return `${day}-${month}-${year}`
}
getRegex(regex, edit, group) {
var str = ''
if(edit.comment) str = edit.comment
else str = edit
const array = [...str.matchAll(regex)]
if(array[0] && array[0][group])
return array[0][group]
return ''
}
}