User:Satricious/dateFixer.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:Satricious/dateFixer. |
/* <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> */