MediaWiki:Gadget-langsync.js

// Adds a button to the "More" dropdown that allows copying the text of the current page and revision to a language wiki

$(document).ready(function {	$(mw.util.addPortletLink("p-cactions", "javascript:;", "Language sync", "ca-langsync", "Copy the text of this revision to a language wiki")).click(function { mw.loader.using("mediawiki.ForeignApi").done(langsync); }); });

function langsync { const localApi = new mw.Api; const revid = mw.config.get("wgRevisionId"); var pagetext = ''; const foreignWiki = prompt("Target language wiki? (e.g. fr, zh)"); if (foreignWiki === null) { return; }	const foreignUrl = "https://terraria-" + foreignWiki + ".gamepedia.com"; const foreignApi = new mw.ForeignApi(foreignUrl + "/api.php"); // get wikitext of current page localApi.get({		action: "parse",		oldid: revid,		prop: "wikitext"	}) .done(function(data) {		pagetext = data.parse.wikitext["*"];		// check foreign wiki		foreignApi.get({ action: "query", meta: "siteinfo", siprop: "general" })		.done(function(data) { foreignWikiUrl = data.query.general.base; foreignWikiUrl = foreignWikiUrl.substring(0, foreignWikiUrl.lastIndexOf("/")); localPageName = mw.config.get("wgPageName"); foreignPageName = localPageName; if (foreignWikiUrl != foreignUrl) { alert('Error while attempting to connect to "' + foreignUrl + '"!'); } else { if (confirm('Will replace the content of "' + foreignPageName + '" on "' + foreignUrl.replace("https://", "") + '" with the text of this revision (' + revid + '). OK?')) { writeToForeignWiki(foreignApi, {						title: foreignPageName,						text: pagetext,						summary: "sync :: en revid:" + revid + "::"					}); }			}		})		.fail(function { alert('"' + foreignWiki + '" is not a valid wiki!'); });	})	.fail(function {		alert("Error while reading this page!");	}); }

function writeToForeignWiki(foreignApi, info) { info.action = "edit"; foreignApi.postWithEditToken(info) .then(function(data) {		if (data.edit !== undefined) {			if ("nochange" in data.edit) {				alert("The target page is already up-to-date. Only performed a null-edit.");			} else if (data.edit.result === "Success") {				alert("Edit successful!");			}		} else {			alert("Error during edit!");		}	}) .fail(function(data) {		alert("Error during edit! Reason: " + data);	}); }