User:Satricious/dateFixer.js

/* 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 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 ?)?(? 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 + '/'; $(``) .append($('').val(text)) .append($('').val(summary)) .append($('').val('preview')) .append($('').val('Show changes')) .append($('').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=)(? \d{4})-(? \d{2})-(? \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} &rarr; ${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*)(? \d{1,2}) (? \w+) (? \d{4})/gmi, (match, p0, p1, p2, off, str, {date, month, year}) => { fixedNo++; if (!fmtEg) fmtEg = `${date} ${month} ${year} &rarr; ${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 script`});    }); } /* */