Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
825 lines (681 loc) · 20.1 KB
/
Copy pathindex.js
File metadata and controls
825 lines (681 loc) · 20.1 KB
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
const _ = require('lodash');
const colors = require('colors');
const cliProgress = require('cli-progress');
const Promise = require('bluebird');
const wiki = require('wikijs').default;
const winston = require('winston');
const fs = Promise.promisifyAll(require('fs'));
const waitFor = ms => new Promise(resolve => setTimeout(resolve, ms));
const MAX_ATTEMPTS = 6;
const REQUEST_INTERVAL_MS = 1000;
const REQUEST_JITTER_MS = 500;
const CATEGORY_LIST_FILE = 'category-list.txt';
const CATEGORY_CHANGE_LOG_FILE = 'logs/category-changes.log';
const USER_AGENT =
'WikiSgLinksBot/1.2.0 ' +
'(https://github.com/robertsky/wikisglinks; ' +
'contact: contact@robertsky.com) ' +
'wikijs/6.4.1';
let limiterQueue = Promise.resolve();
let nextRequestAt = 0;
let globalBlockedUntil = 0;
const outputArray = [];
const wikipedia = wiki({
apiUrl: 'https://en.wikipedia.org/w/api.php',
headers: {
'User-Agent': USER_AGENT
}
});
const progress = new cliProgress.SingleBar({
format:
'Categories accessed |' +
colors.cyan('{bar}') +
'| {percentage}% || {value}/{total} categories',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true
});
const logger = winston.createLogger({
format: winston.format.combine(
winston.format.timestamp(),
winston.format.errors({ stack: true }),
winston.format.json()
),
transports: [
new winston.transports.Console({
level: 'warn',
format: winston.format.combine(
winston.format.colorize(),
winston.format.simple()
)
}),
new winston.transports.File({
level: 'error',
filename: 'logs/error.log'
})
]
});
function normaliseTitle(title) {
let result = title
.replace(/^Talk:/, '')
.replace(/^Book talk:/, 'Book:');
switch (result) {
case 'Singapore Armed Forces Training Institute':
case 'Judge of Singapore':
result += ' (disambiguation)';
break;
}
return result;
}
function shouldIncludeTitle(title) {
const excludedNamespaces = [
'File:',
'Category:',
'User:',
'Draft:',
'Book:',
'Template:'
];
return (
!excludedNamespaces.some(namespace => title.startsWith(namespace)) &&
title !== 'Index of Singapore-related articles'
);
}
/**
* Globally spaces the start of category requests.
*
* Both workers use the same queue, so they cannot begin requests
* simultaneously. The loop rechecks globalBlockedUntil because another
* worker may impose a longer cooldown while this worker is waiting.
*/
function acquireRequestSlot() {
const turn = limiterQueue.then(async () => {
while (true) {
const now = Date.now();
const allowedAt = Math.max(
nextRequestAt,
globalBlockedUntil
);
const delay = allowedAt - now;
if (delay <= 0) {
nextRequestAt =
Date.now() +
REQUEST_INTERVAL_MS +
Math.floor(
Math.random() * REQUEST_JITTER_MS
);
return;
}
await waitFor(delay);
}
});
// Ensure one failed queue operation does not break later scheduling.
limiterQueue = turn.catch(() => {});
return turn;
}
/**
* Pauses all workers until the cooldown expires.
*/
function applyGlobalCooldown(delay) {
globalBlockedUntil = Math.max(
globalBlockedUntil,
Date.now() + delay
);
}
async function mediaWikiApiRequest(parameters, attempt = 1) {
await acquireRequestSlot();
const url = new URL('https://en.wikipedia.org/w/api.php');
const query = {
action: 'query',
format: 'json',
formatversion: '2',
maxlag: '5',
...parameters
};
Object.entries(query).forEach(([key, value]) => {
url.searchParams.set(key, value);
});
try {
const response = await fetch(url, {
headers: {
'User-Agent': USER_AGENT
}
});
if (!response.ok) {
const error = new Error(
`${response.status}: ${response.statusText}`
);
error.status = response.status;
error.retryAfter = response.headers.get('retry-after');
throw error;
}
const data = await response.json();
if (data.error) {
const error = new Error(
`${data.error.code}: ${data.error.info}`
);
error.code = data.error.code;
throw error;
}
return data;
} catch (error) {
if (attempt >= MAX_ATTEMPTS) {
throw error;
}
const retryAfter = Number(error.retryAfter);
const isRateLimited = error.status === 429;
const isMaxlag = error.code === 'maxlag';
const baseDelay =
Number.isFinite(retryAfter) && retryAfter > 0
? retryAfter * 1000
: (isRateLimited || isMaxlag)
? 30000 * (2 ** (attempt - 1))
: 5000 * (2 ** (attempt - 1));
const jitter = Math.floor(Math.random() * 5000);
const delay = Math.min(
baseDelay + jitter,
10 * 60 * 1000
);
applyGlobalCooldown(delay);
logger.warn(
`API helper retry ${attempt + 1}/${MAX_ATTEMPTS} ` +
`after a global cooldown of ` +
`${Math.ceil(delay / 1000)} seconds`
);
return mediaWikiApiRequest(parameters, attempt + 1);
}
}
function categoryTitle(categoryName) {
return `Category:${categoryName}`;
}
function categoryNameFromTitle(title) {
return title.replace(/^Category:/, '');
}
function categoryKey(categoryName) {
const normalised = categoryName
.replace(/_/g, ' ')
.replace(/\s+/g, ' ')
.trim();
if (!normalised) {
return normalised;
}
// MediaWiki normalises the first character of titles.
return (
normalised.charAt(0).toUpperCase() +
normalised.slice(1)
);
}
function sameCategoryName(firstName, secondName) {
return categoryKey(firstName) === categoryKey(secondName);
}
function deduplicateCategoryList(categoryList, changes) {
const seen = new Set();
const uniqueCategories = [];
for (const categoryName of categoryList) {
const key = categoryKey(categoryName);
if (seen.has(key)) {
changes.push({
action: 'DEDUPLICATED',
oldName: categoryName
});
continue;
}
seen.add(key);
uniqueCategories.push(categoryName);
}
categoryList.splice(
0,
categoryList.length,
...uniqueCategories
);
}
async function inspectCategories(categoryList) {
const missingCategories = [];
const emptyCategories = [];
const batchSize = 50;
for (
let offset = 0;
offset < categoryList.length;
offset += batchSize
) {
const batch = categoryList.slice(
offset,
offset + batchSize
);
const requestedTitles = batch.map(categoryTitle);
const originalByTitle = new Map(
requestedTitles.map((title, index) => [
title,
batch[index]
])
);
const data = await mediaWikiApiRequest({
prop: 'info|categoryinfo',
titles: requestedTitles.join('|')
});
for (const normalisation of data.query.normalized || []) {
if (originalByTitle.has(normalisation.from)) {
originalByTitle.set(
normalisation.to,
originalByTitle.get(normalisation.from)
);
}
}
for (const page of data.query.pages || []) {
const categoryName =
originalByTitle.get(page.title) ||
categoryNameFromTitle(page.title);
if ('missing' in page) {
missingCategories.push(categoryName);
} else if (
page.categoryinfo &&
page.categoryinfo.size === 0
) {
emptyCategories.push(categoryName);
}
}
}
return {
missingCategories,
emptyCategories
};
}
async function categoryExists(categoryName) {
const data = await mediaWikiApiRequest({
prop: 'info',
titles: categoryTitle(categoryName)
});
const page = data.query.pages && data.query.pages[0];
return Boolean(page && !('missing' in page));
}
async function findSoftRedirectTarget(
categoryName,
visited = new Set()
) {
const key = categoryKey(categoryName);
if (visited.has(key)) {
logger.warn(
`Circular category redirect detected at ` +
`"${categoryName}"`
);
return null;
}
visited.add(key);
const data = await mediaWikiApiRequest({
action: 'expandtemplates',
prop: 'wikitext',
text:
`{{Resolve category redirect|` +
`${categoryName}}}`
});
const expanded =
data.expandtemplates &&
data.expandtemplates.wikitext;
if (typeof expanded !== 'string') {
return null;
}
const targetName = categoryNameFromTitle(
expanded.trim()
);
// The resolver returns the input when it is not redirected.
if (
!targetName ||
sameCategoryName(targetName, categoryName)
) {
return null;
}
/*
* If the soft-redirect destination was itself moved and deleted,
* follow its move log.
*/
if (!(await categoryExists(targetName))) {
return findMoveTarget(targetName);
}
/*
* Follow chained soft category redirects. If the target is not
* itself redirected, return the current target.
*/
const nextTarget = await findSoftRedirectTarget(
targetName,
visited
);
return nextTarget || targetName;
}
async function findMoveTarget(
categoryName,
visited = new Set()
) {
const key = categoryKey(categoryName);
if (visited.has(key)) {
return null;
}
visited.add(key);
const data = await mediaWikiApiRequest({
list: 'logevents',
letitle: categoryTitle(categoryName),
letype: 'move',
leprop: 'type|title|timestamp|details',
lelimit: '10'
});
const events = data.query.logevents || [];
const moveEvent = events.find(event =>
event.params &&
typeof event.params.target_title === 'string' &&
event.params.target_title.startsWith('Category:')
);
if (!moveEvent) {
return null;
}
const targetName = categoryNameFromTitle(
moveEvent.params.target_title
);
if (await categoryExists(targetName)) {
return targetName;
}
// Follow chained category moves.
return findMoveTarget(targetName, visited);
}
function updateCategoryName(
categoryList,
oldName,
newName,
changes,
action = 'MOVED'
) {
const oldIndex = categoryList.findIndex(
categoryName =>
sameCategoryName(categoryName, oldName)
);
if (
oldIndex === -1 ||
sameCategoryName(oldName, newName)
) {
return;
}
const existingTargetIndex = categoryList.findIndex(
categoryName =>
sameCategoryName(categoryName, newName)
);
if (existingTargetIndex === -1) {
categoryList[oldIndex] = newName;
} else {
// The replacement already exists, so only remove the old name.
categoryList.splice(oldIndex, 1);
}
changes.push({
action,
oldName,
newName
});
}
function deleteCategoryName(
categoryList,
categoryName,
changes
) {
const index = categoryList.indexOf(categoryName);
if (index === -1) {
return;
}
categoryList.splice(index, 1);
changes.push({
action: 'REMOVED',
oldName: categoryName
});
}
async function updateCategoryListFile(
categoryList,
newline,
hadTrailingNewline
) {
const changes = [];
// Remove duplicates before making API checks.
deduplicateCategoryList(categoryList, changes);
const {
missingCategories,
emptyCategories
} = await inspectCategories(categoryList);
/*
* A missing category may have been moved and subsequently
* deleted. Consult its move log before removing it.
*/
for (const categoryName of missingCategories) {
const moveTarget = await findMoveTarget(categoryName);
if (moveTarget) {
updateCategoryName(
categoryList,
categoryName,
moveTarget,
changes,
'MOVED'
);
} else {
deleteCategoryName(
categoryList,
categoryName,
changes
);
}
}
/*
* Existing empty categories may be soft redirects using
* {{Category redirect}}.
*/
for (const categoryName of emptyCategories) {
const redirectTarget =
await findSoftRedirectTarget(categoryName);
if (redirectTarget) {
updateCategoryName(
categoryList,
categoryName,
redirectTarget,
changes,
'SOFT_REDIRECT'
);
}
}
if (changes.length === 0) {
console.log(
`${CATEGORY_LIST_FILE} requires no changes.`
);
return;
}
const updatedContent =
categoryList.join(newline) +
(hadTrailingNewline ? newline : '');
const temporaryFile = `${CATEGORY_LIST_FILE}.tmp`;
await fs.writeFileAsync(
temporaryFile,
updatedContent,
'utf8'
);
await fs.renameAsync(
temporaryFile,
CATEGORY_LIST_FILE
);
const timestamp = new Date().toISOString();
const logLines = changes.map(change => {
if (
change.action === 'MOVED' ||
change.action === 'SOFT_REDIRECT'
) {
return (
`${timestamp}\t${change.action}\t` +
`${change.oldName}\t${change.newName}`
);
}
return (
`${timestamp}\t${change.action}\t` +
`${change.oldName}`
);
});
await fs.appendFileAsync(
CATEGORY_CHANGE_LOG_FILE,
`${logLines.join('\n')}\n`,
'utf8'
);
console.log(
`Updated ${CATEGORY_LIST_FILE}: ` +
`${changes.length} category change(s)`
);
}
async function fetchCategory(categoryName, attempt = 1) {
try {
await acquireRequestSlot();
const pages = await wikipedia.pagesInCategory(
`Category:${categoryName}`
);
return pages
.map(normaliseTitle)
.filter(shouldIncludeTitle);
} catch (error) {
const isRateLimited =
error.message &&
error.message.includes('429');
logger.error('Failed to retrieve category', {
category: categoryName,
attempt,
message: error.message,
stack: error.stack
});
if (attempt >= MAX_ATTEMPTS) {
throw new Error(
`Failed to retrieve "${categoryName}" after ` +
`${MAX_ATTEMPTS} attempts: ${error.message}`
);
}
const baseDelay = isRateLimited
? 30000 * (2 ** (attempt - 1))
: 5000 * (2 ** (attempt - 1));
const jitter = Math.floor(Math.random() * 5000);
const delay = Math.min(
baseDelay + jitter,
10 * 60 * 1000
);
/*
* Apply the delay globally. Both workers will wait before
* starting another category request.
*/
applyGlobalCooldown(delay);
logger.warn(
`Retrying "${categoryName}" after a global cooldown of ` +
`${Math.ceil(delay / 1000)} seconds`
);
return fetchCategory(categoryName, attempt + 1);
}
}
async function main() {
await fs.mkdirAsync('logs', {
recursive: true
});
const content = await fs.readFileAsync(
CATEGORY_LIST_FILE,
'utf8'
);
const newline = content.includes('\r\n')
? '\r\n'
: '\n';
const hadTrailingNewline = /\r?\n$/.test(content);
const categoryList = content
.split(/\r?\n/)
.map(category => category.trim())
.filter(Boolean);
console.log(
`Checking ${CATEGORY_LIST_FILE} for ` +
'redirected, moved, removed or duplicate categories...'
);
await updateCategoryListFile(
categoryList,
newline,
hadTrailingNewline
);
console.log("Knocking on Wikipedia's door...");
progress.start(categoryList.length, 0);
await Promise.map(
categoryList,
async categoryName => {
const pages = await fetchCategory(categoryName);
outputArray.push(...pages);
progress.increment();
},
{
concurrency: 2
}
);
progress.stop();
const sortedTitles = _.sortBy(_.uniq(outputArray));
console.log(`Total number of articles: ${sortedTitles.length}`);
console.log('Writing to file...');
const outputFile = require('fs').createWriteStream('pages3.txt', {
flags: 'w'
});
outputFile.write('{{short description|none}}\n');
outputFile.write('{{use British English|date=August 2019}}\n');
outputFile.write('{{use dmy dates|date=August 2019}}\n');
outputFile.write(
'<noinclude>{{shortcut|WP:SGINDEX|WP:SG/INDEX}}</noinclude>\n'
);
outputFile.write(
"This is a '''list of [[Singapore]]-related articles by " +
"alphabetical order'''. To learn quickly what Singapore is, see " +
'[[Outline of Singapore]]. Those interested in the subject can ' +
"monitor changes to the pages by clicking on ''Related changes'' " +
'in the sidebar. A list of [[to do]] topics can be found ' +
'[[Wikipedia:WikiProject Singapore/Article improvement|here]].\n'
);
outputFile.write(
'{{alphanumeric TOC|numbers=yes|align=center}}\n\n'
);
let currentHeading = null;
for (const title of sortedTitles) {
const firstCharacter = title.charAt(0).toUpperCase();
const heading = /^[A-Z]$/.test(firstCharacter)
? firstCharacter
: '0-9';
if (heading !== currentHeading) {
if (currentHeading !== null) {
outputFile.write('{{div col end}}\n');
outputFile.write(
'{{alphanumeric TOC|numbers=yes|align=center|top=yes}}\n'
);
}
outputFile.write(`\n==${heading}==\n`);
outputFile.write('{{div col|colwidth=25em}}\n');
currentHeading = heading;
}
outputFile.write(`* [[${title}]]\n`);
}
if (currentHeading !== null) {
outputFile.write('{{div col end}}\n');
}
outputFile.write('==See also==\n\n');
outputFile.write('* [[Outline of Singapore]]\n');
outputFile.write(
'* [[Lists of country-related topics]] – ' +
'similar lists for other countries\n'
);
outputFile.write('{{portal bar|Singapore|Cities|Islands|Asia}}\n');
outputFile.write('{{Index footer}}\n\n');
outputFile.write(
'{{DEFAULTSORT:Index Of Singapore-related Articles}}\n'
);
await new Promise((resolve, reject) => {
outputFile.end(error => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
console.log('Write complete.');
console.log('Ready for verification and upload.');
}
main().catch(error => {
progress.stop();
logger.error('Fatal error', {
message: error.message,
stack: error.stack
});
process.exitCode = 1;
});