/* <nowiki>
DO NOT USE THIS. If you do, please review edits before submitting changes
This script is only useful in a limited number of cases and is currently in
development and it's not clear whether it's useful at all. So I highly
recommend against using it
Author: Satricious
Description: Looks for {{use dmy/mdy dates}} and appropriately changes any
incorrect dates to their corrected form
*/
if (mw.config.get("wgNamespaceNumber") == 0) {
mw.loader.using("mediawiki.util", () => {
$(document).ready(() => {
var link = mw.util.addPortletLink( "p-cactions", "#", "Fix Dates", "ca-fixdates", "Fix Dates");
$(link).click(event => {
event.preventDefault();
fixDates();
});
});
});
}
function getDateType(str) {
const dateTypeReg = /{{\s*(?:use ?)?(?<datetype>dmy|mdy)(?: ?dates)?\s*/mi;
let match = dateTypeReg.exec(str);
if(match != null) {
return match.groups.datetype.toLowerCase();
} else {
return null;
}
}
function currPage() {
return mw.config.get("wgPageName");
}
function getPageWikitext(page, callback) {
const send_req = {
action: "query",
titles: currPage(),
prop: "revisions",
intoken: "edit",
rvprop: "content",
indexpageids: 1,
dataType: "xml",
format: "xml"
};
$.get(mw.config.get("wgScriptPath") + "/api.php", send_req, response => {
const text = $(response).find("rev").text();
callback(text);
});
}
function goToShowChangesScreen({title = currPage(), text, summary}) {
let titleEncoded = encodeURIComponent(title.replace(/ /g, "_")); /* must incl namespace */
let wgServer = mw.config.get('wgServer');
let wgScriptPath = mw.config.get('wgScriptPath');
let baseURL = wgServer + wgScriptPath + '/';
$(`<form action="${baseURL}index.php?title=${titleEncoded}&action=submit" method="POST"/>`)
.append($('<input type="hidden" name="wpTextbox1">').val(text))
.append($('<input type="hidden" name="wpSummary">').val(summary))
.append($('<input type="hidden" name="mode">').val('preview'))
.append($('<input type="hidden" name="wpDiff">').val('Show changes'))
.append($('<input type="hidden" name="wpUltimateParam">').val('1'))
.appendTo($(document.body))
.submit();
}
function fixDates() {
const months = "January,February,March,April,May,June,July,August,September,October,November,December".split(",");
const ymdReg = /(?<=\|\w*-?date=)(?<year>\d{4})-(?<month>\d{2})-(?<date>\d{2})/gmi;
getPageWikitext(currPage(), wikitext => {
let dateType = getDateType(wikitext);
if(!dateType) {
alert("No date type used");
return;
}
let fixedNo = 0;
var fmtEg = "";
if (dateType == "dmy") {
wikitext = wikitext.replace(ymdReg, (match, p0, p1, p2, off, str, {year, month, date}) => {
fixedNo++;
if (!fmtEg) fmtEg = `${year}-${month}-${date} → ${parseInt(date)} ${months[parseInt(month)-1]} ${year}`;
return `${parseInt(date)} ${months[parseInt(month)-1]} ${year}`;
});
} else if (dateType == "mdy") {
wikitext = wikitext.replace(/(?<=\|\w*-?date\s*=\s*)(?<date>\d{1,2}) (?<month>\w+) (?<year>\d{4})/gmi, (match, p0, p1, p2, off, str, {date, month, year}) => {
fixedNo++;
if (!fmtEg) fmtEg = `${date} ${month} ${year} → ${month} ${parseInt(date)}, ${year}`;
return `${month} ${parseInt(date)}, ${year}`;
});
}
if(fixedNo == 0 || !fmtEg) {
alert("No dates to be fixed");
return;
}
goToShowChangesScreen({text: wikitext, summary: `Formatted ${fixedNo} date(s) to ${dateType} format; eg. ${fmtEg}, using a [[User:Satricious/dateFixer.js|script]]`});
});
}
/* </nowiki> */