Jump to content

User:Habst/dab.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <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>