「User:Irukaza/js/queryWhatlinkshere.js」:修訂間差異
跳至導覽
跳至搜尋
無編輯摘要
imported>Irukaza 無編輯摘要 |
imported>Irukaza 無編輯摘要 |
||
| 行 9: | 行 9: | ||
return; | return; | ||
} | } | ||
await mw.loader.using(" | await mw.loader.using("mw.Api"); | ||
const upperFirstCase = (s) => /^[a-z]/.test(s) ? s.substring(0, 1).toUpperCase() + s.substring(1) : s; | const upperFirstCase = (s) => /^[a-z]/.test(s) ? s.substring(0, 1).toUpperCase() + s.substring(1) : s; | ||
const api = new mw.Api(); | const api = new mw.Api(); | ||
const p = $('<fieldset><legend>链入情况</legend><p id="queryWhatlinkshere">……</p></fieldset>').insertAfter("#mw-content-text > form").find("#queryWhatlinkshere"); | |||
const list = await (async () => { | |||
const result = []; | |||
const eol = Symbol(); | |||
let lhcontinue = undefined; | |||
while (lhcontinue !== eol) { | |||
const _result = await api.post({ | |||
action: "query", | |||
format: "json", | |||
prop: "linkshere", | |||
titles: target, | |||
lhprop: "redirect|title|pageid", | |||
lhcontinue, | |||
lhlimit: "max", | |||
}); | |||
if (_result.continue) { | |||
lhcontinue = _result.continue.lhcontinue; | |||
} else { | |||
lhcontinue = eol; | |||
} | |||
result.push(...(Object.values(_result.query.pages)[0].linkshere || [])); | |||
} | |||
return result; | |||
})(); | |||
const ns = { | const ns = { | ||
0: " | 0: " 条目", | ||
1: "讨论", | 1: "讨论", | ||
2: "用户", | 2: "用户", | ||
3: "用户讨论", | 3: "用户讨论", | ||
4: "萌娘 | 4: "H 萌娘", | ||
5: "萌娘 | 5: "H 萌娘讨论", | ||
6: "文件", | 6: "文件", | ||
7: "文件讨论", | 7: "文件讨论", | ||
| 行 40: | 行 64: | ||
2303: "gadget_definition_talk", | 2303: "gadget_definition_talk", | ||
}; | }; | ||
const nslist = { | |||
0: { count: 0, redirect: 0 }, | |||
1: { count: 0, redirect: 0 }, | |||
2: { count: 0, redirect: 0 }, | |||
3: { count: 0, redirect: 0 }, | |||
4: { count: 0, redirect: 0 }, | |||
5: { count: 0, redirect: 0 }, | |||
6: { count: 0, redirect: 0 }, | |||
7: { count: 0, redirect: 0 }, | |||
8: { count: 0, redirect: 0 }, | |||
9: { count: 0, redirect: 0 }, | |||
10: { count: 0, redirect: 0 }, | |||
11: { count: 0, redirect: 0 }, | |||
12: { count: 0, redirect: 0 }, | |||
13: { count: 0, redirect: 0 }, | |||
14: { count: 0, redirect: 0 }, | |||
15: { count: 0, redirect: 0 }, | |||
274: { count: 0, redirect: 0 }, | |||
275: { count: 0, redirect: 0 }, | |||
710: { count: 0, redirect: 0 }, | |||
711: { count: 0, redirect: 0 }, | |||
828: { count: 0, redirect: 0 }, | |||
829: { count: 0, redirect: 0 }, | |||
2300: { count: 0, redirect: 0 }, | |||
2301: { count: 0, redirect: 0 }, | |||
2302: { count: 0, redirect: 0 }, | |||
2303: { count: 0, redirect: 0 }, | |||
}; | |||
const global = { redirect: 0 }; | |||
p.text(`共有${list.length}个页面含有到本页面的站内链接,其中有${list.filter((item) => "redirect" in item).length}个重定向页面。`); | |||
if (list.length > 0) { p.append("按名字空间划分如下:"); } | |||
const ul = $("<ul/>"); | |||
list.forEach((item) => { | |||
nslist[item.ns].count++; | |||
if ("redirect" in item) { | |||
nslist[item.ns].redirect++; | |||
global.redirect++; | global.redirect++; | ||
}); | } | ||
}); | |||
Object.entries(nslist).filter(([, { count }]) => count > 0).sort(([a], [b]) => a - b).forEach(([nsnumber, { count, redirect }]) => ul.append(`<li>${nsnumber === 0 ? " (主名字空间)" : upperFirstCase(ns[nsnumber])}:${count}个页面(其中有${redirect}个重定向页面)`)); | |||
p.after(ul); | |||
})()); | })()); | ||
// </pre> | // </pre> | ||