forked from Markus-Rost/mediawiki-projects-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
314 lines (299 loc) · 12.7 KB
/
index.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
const {properties: {wikiProjects: {items: {properties: wikiProjectSchema}}, frontendProxies: {items: {properties: frontendProxySchema}}}} = require('./projects-schema.json');
const PROJECTS = require('./projects.json');
/**
* A wiki farm
* @typedef {"biligame"|"fandom"|"huijiwiki"|"miraheze"|"mywikis"|"shoutwiki"|"telepedia"|"wiki.gg"|"wikimedia"|"wikitide"|null} WikiFarm
*/
/**
* A MediaWiki project
* @typedef {object} WikiProject
* @property {string} name - Hostname of the project
* @property {string} regex - Regex to match the project url
* @property {string} articlePath - Article path of the project
* @property {string} scriptPath - Script path of the project
* @property {string} [fullScriptPath] - Only exists when the hostname contains a single wiki: Full script path to the wiki
* @property {object} [idString] - Only exists when the hostname contains multiple wikis: How to handle the id string
* @property {string} idString.separator - Separator to join or split the id string on
* @property {"asc"|"desc"} idString.direction - Order in which the project regex additional group matches should be chained to gain the id string
* @property {string} idString.regex - Regex to match the id string
* @property {string[]} idString.scriptPaths - How to turn the group matches of the id string regex into an URL to the script path, index based on group matches
* @property {boolean} regexPaths - Whether the paths include matches of the regex
* @property {WikiFarm} wikiFarm - Wiki farm of the project
* @property {("Cargo"|"CentralAuth"|"OAuth")[]} extensions - List of extensions providing useful API endpoints
* @property {string} urlSpaceReplacement - Replacement for spaces in the article URL
* @property {?string} note - Note about the specific project
*/
/**
* A frontend proxy
* @typedef {object} FrontendProxy
* @property {string} name - Hostname of the proxy
* @property {string} regex - Regex to match the proxy url
* @property {string} namePath - Name path of the proxy
* @property {string} articlePath - Article path of the proxy
* @property {string} scriptPath - Script path of the proxy
* @property {?string} relativeFix - Regex to remove from the relative url
* @property {object} [idString] - Only exists when the hostname contains multiple wikis: How to handle the id string
* @property {string} idString.separator - Separator to join or split the id string on
* @property {"asc"|"desc"} idString.direction - Order in which the project regex additional group matches should be chained to gain the id string
* @property {string} idString.regex - Regex to match the id string
* @property {string[]} idString.scriptPaths - How to turn the group matches of the id string regex into an URL to the script path, index based on group matches
* @property {?string} note - Note about the specific proxy
*/
/**
* @type {{
* inputToWikiProject: Map<string, ?{fullArticlePath: string, fullScriptPath: string, wikiProject: WikiProject}>,
* urlToIdString: Map<string, ?string>,
* idStringToUrl: Map<string, ?string>,
* inputToFrontendProxy: Map<string, ?{fullNamePath: string, fullArticlePath: string, fullScriptPath: string, frontendProxy: FrontendProxy}>,
* urlToFix: Map<string, ?((href:String,pagelink:String)=>String)>
* }}
*/
const functionCache = {
inputToWikiProject: new Map(),
urlToIdString: new Map(),
idStringToUrl: new Map(),
inputToFrontendProxy: new Map(),
urlToFix: new Map()
};
/**
* @param {Map<string, mapValueType>} map
* @param {string} keyString
* @returns {?mapValueType}
* @template mapValueType
*/
function getMapValue(map, keyString) {
if ( !keyString ) return null;
let parts = keyString.split('.');
while ( parts.length > 0 ) {
let key = parts.join('.');
if ( map.has(key) ) return map.get(key);
parts.shift();
}
return null;
}
/**
* Map of MediaWiki projects
* @type {Map<string, WikiProject>}
*/
const wikiProjects = new Map(PROJECTS.wikiProjects.map( wikiProject => {
if ( wikiProject.idString ) {
wikiProject.idString.separator ??= wikiProjectSchema.idString.properties.separator.default;
wikiProject.idString.direction ??= wikiProjectSchema.idString.properties.direction.default;
}
wikiProject.regexPaths ??= wikiProjectSchema.regexPaths.default;
wikiProject.wikiFarm ??= wikiProjectSchema.wikiFarm.default;
wikiProject.extensions ??= wikiProjectSchema.extensions.default.slice();
wikiProject.urlSpaceReplacement ??= wikiProjectSchema.urlSpaceReplacement.default;
wikiProject.note ??= wikiProjectSchema.note.default;
return [wikiProject.name, wikiProject];
} ));
/**
* Map of frontend proxies
* @type {Map<string, FrontendProxy>}
*/
const frontendProxies = new Map(PROJECTS.frontendProxies.map( frontendProxy => {
if ( frontendProxy.idString ) {
frontendProxy.idString.separator ??= frontendProxySchema.idString.properties.separator.default;
frontendProxy.idString.direction ??= frontendProxySchema.idString.properties.direction.default;
}
frontendProxy.relativeFix ??= frontendProxySchema.relativeFix.default;
frontendProxy.note ??= frontendProxySchema.note.default;
return [frontendProxy.name, frontendProxy];
} ));
/**
* Get a MediaWiki project by domain hostname
* @param {string} hostname
* @returns {?WikiProject}
*/
function getWikiProject(hostname) {
return getMapValue(wikiProjects, hostname);
}
/**
* Get a frontend proxy by domain hostname
* @param {string} hostname
* @returns {?FrontendProxy}
*/
function getFrontendProxy(hostname) {
return getMapValue(frontendProxies, hostname);
}
/**
*
* @param {string} input
* @returns {?{fullArticlePath: string, fullScriptPath: string, wikiProject: WikiProject}}
*/
function inputToWikiProject(input) {
if ( functionCache.inputToWikiProject.has(input) ) return structuredClone(functionCache.inputToWikiProject.get(input));
let result = null;
let wikiProject = getWikiProject(input.split('/').slice(0, 3).find( part => part && part.includes( '.' ) ));
if ( wikiProject ) {
let articlePath = ( wikiProject.regexPaths ? '/' : wikiProject.articlePath.split('?')[0] ).replace(/[.*+?^${}()|\[\]\\]/g, '\\$&');
let scriptPath = ( wikiProject.regexPaths ? '/' : wikiProject.scriptPath ).replace(/[.*+?^${}()|\[\]\\]/g, '\\$&');
let regex = input.match( new RegExp( '(?:[\\w%]+(?::[\\w%]+)?@)?' + wikiProject.regex + `(?:${articlePath}|${scriptPath}|/?$)`, 'd' ) );
if ( regex ) {
scriptPath = wikiProject.scriptPath;
articlePath = wikiProject.articlePath;
if ( wikiProject.regexPaths ) {
scriptPath = scriptPath.replace( /\$(\d)/g, (match, n) => regex[n] );
articlePath = articlePath.replace( /\$(\d)/g, (match, n) => regex[n] );
}
if ( articlePath.includes('?') && !articlePath.endsWith('=') ) {
articlePath = articlePath.replace( '?', '$1?' );
}
else articlePath += '$1';
let auth = '';
if ( regex.index < regex.indices[1][0] ) {
auth = input.slice(regex.index, regex.indices[1][0]);
}
result = {
fullArticlePath: 'https://' + regex[1] + articlePath,
fullScriptPath: 'https://' + auth + regex[1] + scriptPath,
wikiProject: wikiProject
};
}
}
functionCache.inputToWikiProject.set(input, result);
return structuredClone(result);
}
/**
*
* @param {URL} url
* @returns {?string}
*/
function urlToIdString(url) {
if ( functionCache.urlToIdString.has(url.href) ) return functionCache.urlToIdString.get(url.href);
let result = null;
/** @type {?WikiProject|FrontendProxy} */
let project = getWikiProject(url.hostname);
if ( !project ) project = getFrontendProxy(url.hostname);
if ( project?.idString ) {
let regex = url.href.match( new RegExp( project.regex ) )?.slice(2).filter( part => part );
if ( regex?.length ) {
if ( project.idString.direction === 'desc' ) regex.reverse();
result = regex.join(project.idString.separator);
}
}
functionCache.urlToIdString.set(url.href, result);
return result;
}
/**
*
* @param {string} idString
* @param {string} projectName
* @returns {?URL}
*/
function idStringToUrl(idString, projectName) {
let cacheKey = JSON.stringify([idString,projectName]);
if ( functionCache.idStringToUrl.has(cacheKey) ) {
let result = functionCache.idStringToUrl.get(cacheKey);
return ( result ? new URL(result) : result );
}
let result = null;
let project = getWikiProject(projectName)?.idString;
if ( !project ) project = getFrontendProxy(projectName)?.idString;
if ( project ) {
let regex = idString.match( new RegExp( '^' + project.regex + '$' ) )?.[1].split(project.separator);
if ( regex && regex.length <= project.scriptPaths.length ) {
result = project.scriptPaths[regex.length - 1].replace( /\$(\d)/g, (match, n) => regex[n - 1] );
}
}
functionCache.idStringToUrl.set(cacheKey, result);
return ( result ? new URL(result) : result );
}
/**
*
* @param {string} input
* @returns {?{fullNamePath: string, fullArticlePath: string, fullScriptPath: string, frontendProxy: FrontendProxy}}
*/
function inputToFrontendProxy(input) {
if ( functionCache.inputToFrontendProxy.has(input) ) return structuredClone(functionCache.inputToFrontendProxy.get(input));
let result = null;
let frontendProxy = getFrontendProxy(input.split('/').slice(0, 3).find( part => part && part.includes( '.' ) ));
if ( frontendProxy ) {
let regex = input.match( new RegExp( frontendProxy.regex ) );
if ( regex ) {
result = {
fullNamePath: frontendProxy.namePath.replace( /\$(\d)/g, (match, n) => regex[n] ),
fullArticlePath: frontendProxy.articlePath.replace( /\$(\d)/g, (match, n) => regex[n] ),
fullScriptPath: frontendProxy.scriptPath.replace( /\$(\d)/g, (match, n) => regex[n] ),
frontendProxy: frontendProxy
};
if ( result.fullArticlePath.includes('?') && !result.fullArticlePath.endsWith('=') ) {
result.fullArticlePath = result.fullArticlePath.replace( '?', '$1?' );
}
else result.fullArticlePath += '$1';
}
}
functionCache.inputToFrontendProxy.set(input, result);
return structuredClone(result);
}
/**
*
* @param {string} url
* @returns {?((href:String,pagelink:String)=>String)}
*/
function urlToFix(url) {
let hostname = url.split('/')[2];
if ( functionCache.urlToFix.has(hostname) ) return functionCache.urlToFix.get(hostname);
let result = null;
let frontendProxy = getFrontendProxy(hostname);
if ( frontendProxy ) {
let splitLength = frontendProxy.namePath.split('/').length;
let querykeys = frontendProxy.namePath.split('?').slice(1).join('?').split('&').flatMap( query => {
if ( !query ) return [];
return query.split('=', 1);
} );
if ( splitLength > 4 && querykeys.length && frontendProxy.relativeFix ) {
result = (href, pagelink) => {
let prepend = '/' + pagelink.split('/', splitLength).slice(3, -1).join('/');
let querystring = pagelink.split('?').slice(1).join('?').split('&').filter( query => querykeys.includes( query.split('=', 1)[0] ) );
let append = ( href.includes('?') ? '&' : '?' ) + querystring.join('&');
return prepend + href.replace( new RegExp( frontendProxy.relativeFix ), '' ) + append;
};
}
else if ( splitLength > 4 && querykeys.length ) {
result = (href, pagelink) => {
let prepend = '/' + pagelink.split('/', splitLength).slice(3, -1).join('/');
let querystring = pagelink.split('?').slice(1).join('?').split('&').filter( query => querykeys.includes( query.split('=', 1)[0] ) );
let append = ( href.includes('?') ? '&' : '?' ) + querystring.join('&');
return prepend + href + append;
};
}
else if ( splitLength > 4 && frontendProxy.relativeFix ) {
result = (href, pagelink) => {
let prepend = '/' + pagelink.split('/', splitLength).slice(3, -1).join('/');
return prepend + href.replace( new RegExp( frontendProxy.relativeFix ), '' );
}
}
else if ( splitLength > 4 ) {
result = (href, pagelink) => '/' + pagelink.split('/', splitLength).slice(3, -1).join('/') + href;
}
else if ( querykeys.length && frontendProxy.relativeFix ) {
result = (href, pagelink) => {
let querystring = pagelink.split('?').slice(1).join('?').split('&').filter( query => querykeys.includes( query.split('=', 1)[0] ) );
return href.replace( new RegExp( frontendProxy.relativeFix ), '' ) + ( href.includes('?') ? '&' : '?' ) + querystring.join('&');
}
}
else if ( querykeys.length ) {
result = (href, pagelink) => {
let querystring = pagelink.split('?').slice(1).join('?').split('&').filter( query => querykeys.includes( query.split('=', 1)[0] ) );
return href + ( href.includes('?') ? '&' : '?' ) + querystring.join('&');
}
}
else if ( frontendProxy.relativeFix ) {
result = (href, pagelink) => href.replace( new RegExp( frontendProxy.relativeFix ), '' );
}
}
functionCache.urlToFix.set(hostname, result);
return result;
}
module.exports = {
wikiProjects,
frontendProxies,
getWikiProject,
getFrontendProxy,
inputToWikiProject,
urlToIdString,
idStringToUrl,
inputToFrontendProxy,
urlToFix
};