User:Habst/dab.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:Habst/dab. |
// <nowiki>
window.dabs ??= {};
window.pgToRunId ??= {};
const urlBase = "https://en.wikipedia.org/w/api.php?";
const wlhUrl = urlBase + new URLSearchParams({
origin: '*',
action: "query",
prop: 'transcludedin',
format: "json",
tilimit: '500',
formatversion: '2',
titles: "Template:For-multi",
});
const lcFirst = false;
let hitListSkip = true;
let skipPastPg = 'Day';
window.wlh ??= (await (await fetch(wlhUrl)).json()).query.pages[0].transcludedin.map(p => p.title);
const pages = window.wlh;
for (const pg of pages) {
console.log('trying', pg);
// check if dab exists
const key = `${pg} (disambiguation)`;
if (pg === skipPastPg) hitListSkip = false;
if (hitListSkip) dabs[key] = 'skip'; // temporary skip to part of list
if (dabs[key]) continue;
const dabPgUrl = urlBase + new URLSearchParams({
origin: '*',
action: 'query',
prop: 'info',
titles: key,
format: "json",
formatversion: '2',
});
const isMissing = (await (await fetch(dabPgUrl)).json()).query.pages[0].missing;
if (!isMissing) { console.log('skipping', key); dabs[key] = 'skip'; continue; }
// get forMultis
const pgUrl = urlBase + new URLSearchParams({
origin: '*',
action: 'query',
prop: 'revisions',
rvprop: 'content',
rvsection: '0',
rvslots: 'main',
titles: pg,
format: "json",
formatversion: '2',
});
const forMultiLine = (await (await fetch(pgUrl)).json()).query.pages[0].revisions[0].slots.main.content.split('\n').find(l => l.toLowerCase().replaceAll('_', ' ').match(/for[ -]multi/));
const forMultis = forMultiLine ? forMultiLine.split('multi').slice(1).join('multi').split('|').filter((x, i) => i >= 2 && i % 2 === 0).map(s => s.replaceAll('_', ' ').split('{{')[0].split('}}')[0].trim()) : [];
console.log(forMultis);
if (forMultis.length < 2) { console.log('skipping', key); dabs[key] = 'skip'; continue; }
dabs[key] = `'''[[${pg}]]''' is/was a(n) ${lcFirst ? '{{subst:lcfirst:' : ''}{{subst:#invoke:GetShortDescription|main|name=${pg}|stringify=True}}${lcFirst ? '}}' : ''}.\n\n'''${pg}''' may also refer to:\n\n`;
const fdObj = {
text: `SET @title="${pg.replaceAll(' ', '_')}";
SELECT lt_title, page_len, page_is_redirect
FROM linktarget
LEFT JOIN page ON page_title=lt_title
WHERE lt_namespace=0 AND (lt_title = @title OR lt_title LIKE CONCAT(@title, "_(%"))
GROUP BY lt_title
ORDER BY lt_title`,
query_database: 'enwiki_p',
query_id: '89596',
};
pgToRunId[pg] ??= (await (await fetch("https://quarry.wmcloud.org/api/query/run", {
headers: { "content-type": "application/x-www-form-urlencoded" },
body: new URLSearchParams(fdObj),
method: "POST",
})).json()).qrun_id;
console.log(fdObj.text, 'waiting...');
let complete = false;
while (!complete) {
if (window.stopQuarry) break;
await new Promise(res => setTimeout(res, 4 * 1000));
const status = (await (await fetch(`https://quarry.wmcloud.org/run/${pgToRunId[pg]}/status`)).json());
console.log(status);
if (status.status === 'complete') complete = true;
}
const res = await (await fetch(`https://quarry.wmcloud.org/run/${pgToRunId[pg]}/output/0/json`)).json();
for (const fm of forMultis) {
if (!res.rows.find(r => r[0].replaceAll('_', ' ') === fm)) res.rows.unshift([fm, 1, 0]);
}
const bullets = [];
for (const [dabName, sz, isRedir] of res.rows) {
const pgName = dabName.replaceAll('_', ' ');
if (pgName === pg) continue;
if (sz) {
bullets.push({ isRed: 0, text: `* [[${pgName}]], ${lcFirst ? '{{subst:lcfirst:' : ''}{{subst:#invoke:GetShortDescription|main|name=${pgName}|stringify=True}}${lcFirst ? '}}' : ''}` });
} else {
const sourceLinkUrl = urlBase + new URLSearchParams({
origin: '*',
action: 'query',
list: 'search',
srsearch: `linksto:"${pgName}" insource:"${pgName}" insource:/\\[\\[:?[${pgName[0] + pgName[0].toLowerCase() + ']' +
pgName.slice(1).replace(/[-[\]{}()*+!<=:?.\/\\^$|#\s,]/g, '\\$&')}[\\]\\|#]/`,
srnamespace: '0|10|14',
format: "json",
formatversion: '2',
});
const sourceLink = (await (await fetch(sourceLinkUrl)).json()).query.search[0]?.title;
if (!sourceLink) console.log('could not find links to', pgName);
else bullets.push({ isRed: 1, text: `* [[${pgName}]], see [[${sourceLink}]]` });
}
}
dabs[key] += bullets.sort((a, b) => a.isRed - b.isRed).map(b => b.text).join('\n');
dabs[key] += '\n\n{{Disambiguation}}';
if (res.rows.length <= 2) console.log('only 2 primary topics');
console.log(`https://en.wikipedia.org/w/index.php?${new URLSearchParams({ title: key, action: 'edit' })}`);
console.log(dabs[key]);
console.log('next:');
for (let i = 1; i <= 10; i++) console.log(`dabs["${pages[pages.indexOf(pg) + i]} (disambiguation)"] = true;`);
break; // temporary
}
// </nowiki>