#!/usr/bin/env -S node --env-file-if-exists=.env import {findItemGTID} from '../lib/api/xivapi.js'; import {getMediawikiClient} from '../lib/config.js'; import {diff} from '../lib/util/diff.js'; import { addParameterBesideExistingParameter, setEmptyParameter, } from '../lib/util/template-parameters.js'; /** * Inserts the `id-gt` item infobox parameter into the given page contents. * @param {string} pageContent * @param {string} gtID * @returns {string} */ function insertInfoboxGTBID (pageContent, gtID) { if (pageContent.includes(`id-gt = ${gtID}`)) { console.log('what????', pageContent); throw new Error('Page seems to already contain its own GT ID??'); } let result = setEmptyParameter(pageContent, 'id-gt', gtID) ?? addParameterBesideExistingParameter(pageContent, 'id-gt', gtID, 'after', 'id-edb') ?? addParameterBesideExistingParameter(pageContent, 'id-gt', gtID, 'after', 'release') ?? addParameterBesideExistingParameter(pageContent, 'id-gt', gtID, 'after', 'patch'); if (result == null) { throw new Error('Dunno how to insert the parameter into this page'); } return result; } const mw = await getMediawikiClient(); // TODO: update this to work with the new maintenance category hierarchy // Get pages in the "Missing internal ID" category from the main article namespace const itemPagesWithoutGTIDs = await mw.listCategoryPages('Category:Items with no internal ID specified', [0], +process.env.LIMIT || 500); console.log('Processing', itemPagesWithoutGTIDs.length, 'item pages from [[Category:Items with no internal ID specified]]\n'); for (const page of itemPagesWithoutGTIDs) { const {title} = page; console.log('Page:', title); // look up on XIVAPI let gtID; try { gtID = await findItemGTID(title); } catch (error) { console.log('Error looking up item ID:', error); } if (!gtID) { console.log('No ID found for this item, skipping'); continue; } console.log('GT ID:', gtID, `(https://garlandtools.org/db/#item/${encodeURIComponent(gtID)})`); let originalText; try { originalText = await mw.readPage(title); } catch (error) { console.log('Error reading page:', error); continue; } // rewrite wiki page to include id-edb infobox parameter let updatedText; try { updatedText = insertInfoboxGTBID(originalText, gtID); diff(originalText, updatedText, page); } catch (error) { console.log('Error inserting parameter:', error); console.group('Bad page content:'); console.log(originalText); console.groupEnd(); console.log(); continue; } // write the new stuff back to the wiki try { await mw.editPage(title, updatedText, "Add GT item ID", true); } catch (error) { console.error(error); console.error('writes should not fail, this seems bad, dying now'); process.exit(1); } console.log('Written.'); console.log(); } console.log('done!');