95 lines
3.2 KiB
JavaScript
Executable file
95 lines
3.2 KiB
JavaScript
Executable file
#!/usr/bin/env -S node --env-file-if-exists=.env
|
|
|
|
import {findEDBEntryID} from '../lib/api/lodestone.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-edb` item infobox parameter into the given page contents.
|
|
* @param {string} pageContent
|
|
* @param {string} edbID
|
|
* @returns {string}
|
|
*/
|
|
function insertInfoboxEDBID (pageContent, edbID) {
|
|
if (pageContent.includes(`id-edb = ${edbID}`)) {
|
|
console.log('what????', pageContent);
|
|
throw new Error('Page seems to already contain its own EDB ID??');
|
|
}
|
|
|
|
let result = setEmptyParameter(pageContent, 'id-edb', edbID)
|
|
?? addParameterBesideExistingParameter(pageContent, 'id-edb', edbID, 'before', 'id-gt')
|
|
?? addParameterBesideExistingParameter(pageContent, 'id-edb', edbID, 'after', 'release')
|
|
?? addParameterBesideExistingParameter(pageContent, 'id-edb', edbID, 'after', 'patch');
|
|
|
|
if (result == null) {
|
|
throw new Error('Dunno how to insert the parameter into this page');
|
|
}
|
|
return result;
|
|
}
|
|
|
|
const mw = await getMediawikiClient();
|
|
|
|
// Get pages in the "Missing EDB ID" category from the main article namespace
|
|
const [itemPagesWithoutEDBIDs, questPagesWithoutEDBIDs] = await Promise.all([
|
|
mw.listCategoryPages('Category:Missing EDB ID', [0], +process.env.LIMIT || 500),
|
|
mw.listCategoryPages('Category:Missing quest EDB ID', [0], +process.env.LIMIT || 500),
|
|
]);
|
|
console.log('Processing', itemPagesWithoutEDBIDs.length, 'item pages from [[Category:Missing EDB ID]]\n');
|
|
console.log('Processing', questPagesWithoutEDBIDs.length, 'item pages from [[Category:Missing quest EDB ID]]\n');
|
|
|
|
for (const {title, type} of [
|
|
...itemPagesWithoutEDBIDs.map(({title}) => ({title, type: 'item'})),
|
|
...questPagesWithoutEDBIDs.map(({title}) => ({title, type: 'quest'})),
|
|
]) {
|
|
// this runs serially with an artificial delay between requests to decrease
|
|
// the chance of sqenix sending ninjas to my house
|
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
|
|
console.log('Page:', title);
|
|
// look up on EDB
|
|
const edbID = await findEDBEntryID(type, title);
|
|
if (!edbID) {
|
|
console.log(`No EDB ID found for this ${type}, skipping`);
|
|
continue;
|
|
}
|
|
console.log('EDB ID:', edbID, `(https://na.finalfantasyxiv.com/lodestone/playguide/db/${encodeURIComponent(type)}/${encodeURIComponent(edbID)})`);
|
|
|
|
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 = insertInfoboxEDBID(originalText, edbID);
|
|
diff(originalText, updatedText);
|
|
} 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 EDB ${type} 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!');
|