/
protect.ts
152 lines (139 loc) · 6.01 KB
/
protect.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
/* eslint-disable import/prefer-default-export */
import 'dotenv/config';
import { info, listCategory, login } from '../wikiAPI';
import { getLocalDate, promiseSequence } from '../utilities';
import { closePlaywright, loginWithPlaywright, protectWithPlaywrihgt } from './protectPlaywright';
function getMonthTemplates(month: number, year: number, startWithDay = 1) {
const dates: string[] = [];
for (let i = startWithDay; i <= 31; i += 1) {
const date = getLocalDate(`${year}-${month}-${i}`);
if (date.startsWith(i.toString())) {
dates.push(getLocalDate(`${year}-${month}-${i}`));
}
}
return dates;
}
const templates = [
'תבנית:הידעת?',
'תבנית:ערך מומלץ',
'תבנית:ציטוט יומי',
'תבנית:תמונה מומלצת',
];
const months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
async function needProtectFromTitles(titles :string[]): Promise<string[]> {
const templatesInfo = await info(titles);
return templatesInfo
.filter((templateInfo) => {
if (('missing' in templateInfo) || !templateInfo.title) {
return false;
}
const editProtect = templateInfo.protection?.some(({ level, type, expiry }) => type === 'edit' && level !== 'autoconfirmed' && expiry === 'infinity');
const moveProtect = templateInfo.protection?.some(({ level, type, expiry }) => type === 'edit' && level !== 'autoconfirmed' && expiry === 'infinity');
return (!editProtect || !moveProtect);
})
.map<string>(({ title }) => title || '');
}
async function getTemplatesByDate() {
const needToProtect: string[] = [];
const currMonth = new Date().getMonth() + 1;
const currDay = new Date().getDate();
const currYear = new Date().getFullYear();
const nextYear = currYear + 1;
await Promise.all(months.map(async (month) => {
const monthDates = getMonthTemplates(
month,
month < currMonth ? nextYear : currYear,
month === currMonth ? currDay : 1,
);
await Promise.all(templates.map(async (template) => {
const needProtection = await needProtectFromTitles(monthDates.map((date) => `${template} ${date}`));
needToProtect.push(...needProtection);
}));
}));
return needToProtect;
}
async function getTemplatesByCategory(category: string, exceptCategryFormat?: string) {
const generator = listCategory(category);
let res: IteratorResult<any, void>;
const needToProtect: string[] = [];
do {
res = await generator.next();
const pages = res.value?.query?.categorymembers ?? [];
const relevent = pages.filter((page: any) => !page.sortkeyprefix.startsWith('*')) ?? [];
const batches: any[] = [];
for (let i = 0; i < relevent.length; i += 25) {
batches.push(relevent.slice(i, i + 25));
}
await promiseSequence(10, batches.map((batch) => async () => {
const needProtection = await needProtectFromTitles(batch.map((page: any) => page.title));
needToProtect.push(...needProtection);
}));
const categories = relevent
.filter((page: any) => page.title.startsWith('קטגוריה:') && (!exceptCategryFormat || !page.title.includes(exceptCategryFormat)))
.map(({ title }) => title.replace('קטגוריה:', ''));
for (const cat of categories) {
console.log(`Getting ${cat}`);
const categoryNeedToProtect = await getTemplatesByCategory(cat);
needToProtect.push(...categoryNeedToProtect);
}
} while (!res?.done);
return needToProtect;
}
export async function main() {
await login();
let needToProtect = (await getTemplatesByCategory('תבניות הידעת?')).filter((template) => template.startsWith('תבנית:הידעת?'));
const portalTemplates = await getTemplatesByCategory('פורטלים: קטעי "ערך מומלץ"');
needToProtect = needToProtect.concat(portalTemplates.filter(
(template) => template.startsWith('פורטל:ערכים מומלצים/ערכים') || template.startsWith('תבנית:ערך מומלץ'),
));
const articleGroups = await getTemplatesByCategory('תבניות ניווט - מקבצי ערכים');
needToProtect = needToProtect.concat(articleGroups.filter((template) => template.startsWith('תבנית:מקבץ ערכים')));
needToProtect = needToProtect.concat(await getTemplatesByDate());
const convertPages = (await getTemplatesByCategory('ויקיפדיה/בוט/בוט ההסבה/דפי מפרט', 'ויקיפדיה/בוט/בוט ההסבה/דפי פלט'))
.filter((template) => template.startsWith('שיחת תבנית:')
|| template.startsWith('שיחת קטגוריה:')
|| template.includes('הסבה')
|| template.includes('הסרה')
|| template.includes('הסרת')
|| template.includes('תיקון')
|| template.includes('דפים')
|| template.includes('הסבת')
|| template.includes('פרמטר')
|| template.includes('ניקיון')
|| template.includes('שאילתות')
|| template.startsWith('משתמש:בורה בורה/')
|| template.startsWith('משתמש:עמד/')
|| template.includes('משתמש:Kotz/'));
if (needToProtect.length === 0 && convertPages.length === 0) {
console.log('No need to protect');
return;
}
await loginWithPlaywright(process.env.USER_NAME || '', process.env.PASSWORD || '');
const errors: string[] = [];
for (const title of needToProtect) {
try {
console.log(`Protecting ${title}`);
await protectWithPlaywrihgt(title, 'מופיע בעמוד הראשי');
} catch (e) {
console.log(`Failed to protect ${title}`);
console.error(e);
errors.push(title);
}
}
for (const title of convertPages) {
try {
console.log(`Not protecting ${title} for now`);
// await protectWithPlaywrihgt(title, 'דפי מפרט של בוט ההסבה');
} catch (e) {
console.log(`Failed to protect ${title}`);
console.error(e);
errors.push(title);
}
}
await closePlaywright();
if (errors.length > 0) {
console.error('Failed to protect:');
console.error(errors.join('\n'));
throw new Error('Failed to protect');
}
}