-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighlight.ts
More file actions
executable file
·184 lines (177 loc) · 4.65 KB
/
highlight.ts
File metadata and controls
executable file
·184 lines (177 loc) · 4.65 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
import Ajv, {JSONSchemaType} from 'ajv';
import {Mwn} from 'mwn';
import parser from 'node-html-parser';
const USER_AGENT = 'Highlight.css updater';
interface Config {
groups: {
[key: string]: {
color: string;
cssVar: string;
};
};
overrides: {
add: {
[key: string]: string[];
};
remove: {
[key: string]: string[];
};
};
}
const schema: JSONSchemaType<Config> = {
type: 'object',
required: ['groups', 'overrides'],
properties: {
groups: {
type: 'object',
required: [],
additionalProperties: {
type: 'object',
required: ['color', 'cssVar'],
properties: {
color: {
type: 'string',
minLength: 1,
},
cssVar: {
type: 'string',
minLength: 1,
},
},
},
},
overrides: {
type: 'object',
required: ['add', 'remove'],
properties: {
add: {
type: 'object',
required: [],
additionalProperties: {
type: 'array',
items: {
type: 'string',
},
},
},
remove: {
type: 'object',
required: [],
additionalProperties: {
type: 'array',
items: {
type: 'string',
},
},
},
},
},
},
};
async function getUsers({groups, overrides}: Config) {
const users: Record<string, string[]> = {};
const searchParams: Record<string, string> = {};
let index = 0;
for (const group in groups) {
searchParams[`groups[${index++}]`] = group;
users[group] = [];
}
const response = await fetch(`https://dev.fandom.com/wiki/Special:ListGlobalUsers?${new URLSearchParams(searchParams)}`, {
headers: {
'User-Agent': USER_AGENT,
},
});
const html = await response.text();
if (!response.ok) {
throw new Error(`failed to fetch user group list: status code ${response.status}, response: ${html}`);
}
const tree = parser.parse(html);
for (const node of tree.querySelectorAll('.list-global-users-members > li')) {
const name = node.querySelector('bdi')?.innerText;
const userGroupsMatch = node.innerText
.trim()
.match(/\(([^)]+)\)$/);
if (!name || !userGroupsMatch) {
continue;
}
const userGroups = userGroupsMatch[1].split(', ');
if (userGroups.includes('bot-global')) {
continue;
}
const toRemove = new Set(overrides.remove[name] || []);
for (const group of userGroups) {
if (users[group] && !toRemove.has(group)) {
users[group].push(name);
}
}
}
for (const user in overrides.add) {
for (const group of overrides.add[user]) {
if (users[group] && !users[group].includes(user)) {
users[group].push(user);
}
}
}
return users;
}
async function getConfig(bot: Mwn): Promise<Config> {
const configPage = await bot.read('MediaWiki:Custom-Highlight.json');
if (!configPage.revisions || !configPage.revisions[0] || !configPage.revisions[0].content) {
throw new Error('failed to retrieve configuration page content');
}
const config = JSON.parse(configPage.revisions[0].content);
const ajv = new Ajv();
const validate = ajv.compile(schema);
if (!validate(config)) {
throw new Error(`invalid configuration: ${ajv.errorsText(validate.errors)}`);
}
return config;
}
async function init() {
const bot = new Mwn({
apiUrl: 'https://dev.fandom.com/api.php',
password: process.env.PASSWORD,
silent: true,
userAgent: USER_AGENT,
username: process.env.USERNAME,
});
await bot.login();
const config = await getConfig(bot);
const users = await getUsers(config);
const css = Object.keys(users)
.filter(group => users[group].length > 0)
.map(group => `/* ${group} */\n${users[group]
.sort()
.flatMap(user => {
const regularEncode = user.replace(/\s/g, '_');
const wikiEncode = encodeURIComponent(user)
.replace(/'/g, '%27')
.replace(/%20/g, '_')
.replace(/%3B/g, ';')
.replace(/%40/g, '@')
.replace(/%24/g, '$')
.replace(/%2C/g, ',')
.replace(/%2F/g, '/')
.replace(/%3A/g, ':');
if (regularEncode === wikiEncode) {
return [regularEncode];
}
return [regularEncode, wikiEncode];
})
.map(sel => `a[href$=":${sel}"]`)
.join(',\n')} {\n\tcolor: ${config.groups[group].color} !important;\n color: var(--highlight-${config.groups[group].cssVar}) !important;\n}`)
.join('\n\n');
const response = await bot.edit('MediaWiki:Highlight.css', ({content}) => ({
bot: true,
minor: true,
summary: 'Automatically updating via [[github:WikiaUsers/highlight|GitHub Actions]] - adjust [[MediaWiki:Custom-Highlight.json|config]] or contact [[Special:ListUsers/sysop|admins]] in case of malfunction',
text: content.replace(
/(\/\* HighlightUpdate-start \*\/\n)[\s\S]*$/igm,
(_, m) => `${m}${css}`
),
}));
if (response.result !== 'Success') {
throw new Error(`edit result ${response.result}`);
}
}
init();