MediaWiki:Relgen.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/**
 * @author Stöger Florian D. M. (https://fdms.eu)
 * @license EUPL 1.1 (https://joinup.ec.europa.eu/sites/default/files/eupl1.1.-licence-en_0.pdf)
 * @copyright © (https://joinup.ec.europa.eu/sites/default/files/eupl1.1.-licence-en_0.pdf) Stöger Florian D. M. (https://fdms.eu)
 */

if (mw.config.get("wgArticleId") !== 70885851) throw new Error("relgen.js must be used through COM:relgen.");

var relgen = 1.5;
var progress, radioSelectInput_s1, namei, repi, authi, radioSelectInput_s2, radioSelectInput_s3, fileri, licensei, iagi;
var filea = [];
var license = "";
var ulang = mw.config.get("wgUserLanguage");

document.title = "Wikimedia VRTS release generator - Wikimedia Commons";
$("#firstHeading, #section_0").text("Wikimedia VRTS release generator");
$("#mw-content-text").html("Loading …");

function urlaubambauernhof(lko_id) {
	$(".hof").hide();
	$("html, body").animate({
		scrollTop: 0
	}, 400);
	$("#s" + lko_id).show();
	progress.setProgress(lko_id * 17);
	var developer = " <a href='//commons.wikimedia.org/wiki/User:FDMS4'>FDMS4</a> (<a href='//commons.wikimedia.org/wiki/User_talk:FDMS4?action=edit&section=new&preloadtitle=Wikimedia+VRTS+release+generator+feedback+by+%7B%7Bsubst%3AREVISIONUSER%7D%7D%0A'>feedback</a>)";
	$("#contentSub").html(mw.msg("relgen-common-ver-v1") + " " + relgen + " | " + mw.msg("relgen-common-dev-v1", developer));
	developer += " | <a href='//commons.wikimedia.org/wiki/Commons:Help_desk?action=edit&section=new&preloadtitle=Help+with+Wikimedia+VRTS+release+generator+step+" + lko_id + "' target='_blank'>help</a>";
	$(".mw-indicators").html("<div id='mw-indicator-mw-helplink' class='mw-indicator'><a href='//commons.wikimedia.org/wiki/Commons:Help_desk?action=edit&section=new&preloadtitle=Help+with+Wikimedia+VRTS+release+generator+step+" + lko_id + "' target='_blank' class='mw-helplink'>Help</a></div>");
	$(".tagline").html(mw.msg("relgen-common-ver-v1") + " " + relgen + " | " + mw.msg("relgen-common-dev-v1", developer));
}

function smsc(lko_id) {
	$("#back_s" + lko_id).fadeIn(200);
	$("html, body").animate({
		scrollTop: $("#back_s" + lko_id).offset().top
	}, 400);
}

function s1v() {
	if (!namei.getValue().match(/\S/)) {
		mw.notify(mw.msg("relgen-s1-aferr-v1"));
	} else {
		if (($("#irep").css("display") != "none") && (!repi.getValue().match(/\S/) || !authi.getValue().match(/\S/))) {
			mw.notify(mw.msg("relgen-s1-aferr-v1"));
		} else {
			urlaubambauernhof(2);
		}
	}
}

function s3v() {
	filea = [];
	if ($("#iup").css("display") != "none") {
		var s3v_fileackey = s3v_defkey = fileswoissues = 0;
		filea = fileri.getValue().replace(/^\s*\|\s*/, "").replace(/\s*\|\s*$/, "").split(/\s*\|\s*/);
		while (s3v_fileackey < filea.length) {
			if (!filea[s3v_fileackey].match(/\S/)) filea.splice(s3v_fileackey, 1);
			else s3v_fileackey++;
		} // TO-DO: dupe check
		if (filea.length > 0) {
			var s3v_defexec = function (filea) {
				filea[s3v_defkey] = filea[s3v_defkey].replace(/(?:File:|(?:http|https):\/\/(?:commons|en)\.(?:m\.)?wiki(?:m|p)edia\.org\/(?:wiki\/|w\/index\.php\?title=)File:)?([^&]+).*/, "$1");
				s3v_deffunc(filea[s3v_defkey], function () {
					s3v_defkey++;
					if (s3v_defkey < filea.length) s3v_defexec(filea);
				});
			};

			function s3v_deffunc(file, callback) {
				$.getJSON("/w/api.php?action=query&format=json&list=logevents&letype=upload&letitle=File%3A" + file, function (data) {
					try {
						if (data.query.logevents.length == 0) {
							mw.notify(mw.msg("relgen-s3-nferr-v1", file));
							fileri.setValidityFlag(false);
							s3v_defkey = filea.length;
						} else {
							fileswoissues++;
							if (fileswoissues == filea.length) urlaubambauernhof(4);
						}
					} catch (err) {
						mw.notify(mw.msg("relgen-s3-inverr-v1", file, err));
						s3v_defkey = filea.length;
					}
				}).done(function () {
					callback();
				});
			}
			s3v_defexec(filea);
		} else {
			mw.notify(mw.msg("relgen-s3-pserr-v1"));
		}
	} else {
		urlaubambauernhof(4);
	}
}

function s4v() {
	var name = rep = auth = s1 = s2 = s3 = p1s = p1s_ = p1s_m = p2s = p3s = p3sm = b1 = b1m = b2 = b3 = b4 = b5 = b5m = b6 = subj = relt = "";

	license = licensei.getValue();

	if (!license.match(/\w/)) {
		mw.notify(mw.msg("relgen-s4-pserr-v1"));
	} else {
		s1 = parseInt(radioSelectInput_s1.value);
		s2 = parseInt(radioSelectInput_s2.value);
		s3 = parseInt(radioSelectInput_s3.value);

		name = namei.getValue();
		rep = repi.getValue();
		auth = authi.getValue();

		switch (s1) {
		case 1:
			p1s = mw.msg("relgen-res-p1s-c1-v1", name);
			break;
		case 2:
			p1s = mw.msg("relgen-res-p1s-c2-v1", rep);
			p1s_ = "<br />" + mw.msg("relgen-res-p1s_-c2-v1", auth, rep);
			p1s_m = "%0A" + mw.msg("relgen-res-p1s_-c2-v1", auth, rep);
			break;
		}
		switch (s2) {
		case 1:
			p2s = mw.msg("relgen-res-p2s-c1-v1");
			break;
		case 2:
			p2s = mw.msg("relgen-res-p2s-c2-v1");
			break;
		case 3:
			p2s = mw.msg("relgen-res-p2s-c3-v1");
			break;
		}
		switch (s3) {
		case 1:
			emaila = mw.msg("relgen-pcemail-v1");
			$.each(filea, function (key, file) {
				if (key !== 0) {
					if (key == filea.length - 1) {
						p3s += "<br />";
						p3sm += "%0A";
						subj += " and ";
					} else {
						p3s += "<br />";
						p3sm += "%0A";
						subj += ", ";
					}
				}
				p3s += "* <a href='//commons.wikimedia.org/wiki/File:" + encodeURIComponent(file.replace(/ /g, "_")) + "' target='_blank'>https://commons.wikimedia.org/wiki/File:" + file.replace(/ /g, "_") + "</a>";
				p3sm += "%2A https:%2F%2Fcommons.wikimedia.org%2Fwiki%2FFile:" + encodeURIComponent(file.replace(/ /g, "_"));
				subj += file;
			});
			$("#editwarning").show();
			break;
		case 2:
			emaila = mw.msg("relgen-psemail-v1");
			p3s = p3sm = subj = mw.msg("relgen-res-p3s-c2-v1");
			$("#editwarning").hide();
			break;
		}
		b1 = mw.msg("relgen-res-b1-v1", p1s, p2s);
		b2 = mw.msg("relgen-res-b2-v1", license);
		b3 = mw.msg("relgen-s4-bd1-v1");
		b4 = mw.msg("relgen-s4-bd2-v1");
		if (license.search("CC0") == -1) {
			b5 = "<br />" + mw.msg("relgen-s4-bd3-v1");
			b5m = "%0A" + mw.msg("relgen-s4-bd3-v1");
		}
		b6 = mw.msg("relgen-s4-bd4-v1");
		tracking = mw.msg("relgen-res-tracking-v1");
		relt = b1 + "<br />" + p3s + "<br />" + b2 + "<br />" + b3 + "<br />" + b4 + b5 + "<br />" + b6 + "<br /><br />" + name + p1s_ + "<br />" + new Date().toJSON().slice(0, 10) + "<br /><br />" + tracking;
		$("#emaila-text").text(emaila).prop("href", "mailto:" + emaila);
		$("#front_s5 .mw-ui-button").remove();
		$("#front_s5").append('<a class="mw-ui-button" onclick="nowOP(false);" href="mailto:' + emaila + '?subject=' + mw.msg("relgen-s5-subj-v1", subj) + '&amp;body=' + b1 + '%0A' + p3sm + '%0A' + b2 + '%0A' + b3 + '%0A' + b4 + b5m + '%0A' + b6 + '%0A%0A' + name + p1s_m + '%0A' + new Date().toJSON().slice(0, 10) + '%0A%0A' + tracking + '">' + mw.msg("relgen-s5-cre-v1") + '</a>');
		$("#back_s5 .releasetext").remove();
		$("#back_s5").prepend("<div class='releasetext'  onclick='nowOP(true);' style='background-color:#eaecf0; padding:8px;'>" + relt + "</div>");

		urlaubambauernhof(5);
	}
}

function nowOP(copy) {
	/* if (s3 == 1) {	// disabled pending community consensus
        $.each(filea, function(key, file) {
            $.getJSON("/w/api.php?action=query&format=json&titles=File%3A" + file, function(data_q1) {
                if (!data_q1.query.pages[-1]) {
                    $.getJSON("/w/api.php?action=query&format=json&prop=templates&tltemplates=Template%3APermission%20pending&titles=File%3A" + file, function(data_q2) {
                        if (!data_q2.query.pages[Object.keys(data_q2.query.pages)[0]].templates) {
                            new mw.Api().edit(
                                "File:" + file,
                                function(revision) {
                                    return {
                                        prependtext: "{\{subst:PP}}\n\n",
                                        summary: "Permission pending: a release for this file has been generated using [[COM:relgen|relgen.js]]"
                                    };
                                }
                            ).fail(function(code, result) {
                                mw.notify(mw.msg("relgen-s5-no-v1", code));
                            });
                        }
                    });
                }
            });
        });
    } */
	progress.setProgress(100);
	if (copy && navigator.clipboard && navigator.clipboard.writeText) {
		navigator.clipboard.writeText(relt.replaceAll("<br />", "\n"));
	}
}

function init(withi18n) {
	$.getJSON("/w/api.php?action=query&format=json&prop=revisions&pageids=77494317&rvprop=content", function (data) {
		$("#mw-content-text").html(data.query.pages[77494317].revisions[0]["*"]);
	}).done(function () {
		$(document).ready(function () {
			mw.loader.using("oojs-ui-core").done(function () {
				progress = new OO.ui.ProgressBarWidget({
					progress: 0,
					id: "progress"
				});
				$("#container").prepend(progress.$element);
				urlaubambauernhof(1);
				radioSelectInput_s1 = new OO.ui.RadioSelectInputWidget({
					options: [{
							data: "1",
							label: mw.msg("relgen-s1-o1-v1")
						}, {
							data: "2",
							label: mw.msg("relgen-s1-o2-v1")
						}, {
							data: "0",
							label: mw.msg("relgen-s1-o0-v1")
						}, {
							data: "-1",
							label: ""
						}
					],
					value: "-1"
				});
				radioSelectInput_s1.on("change", function () {
					if (radioSelectInput_s1.value > 0) {
						smsc(1);
						$('#iam').show();
						$('#idk').hide();
						if (radioSelectInput_s1.value > 1) $('#irep').show();
						else $('#irep').hide();
					} else {
						smsc(1);
						$('#iam').hide();
						$('#idk').show();
					}
				});
				$("#front_s1").append(radioSelectInput_s1.$element);
				namei = new OO.ui.TextInputWidget({
					placeholder: mw.msg("relgen-s1-nameph-v1"),
					required: true
				});
				namei.on("enter", function () {
					s1v();
				});
				var nameifl = new OO.ui.FieldLayout(namei, {
					label: mw.msg("relgen-s1-namel-v1"),
					align: 'right'
				});
				$("#iam").prepend("<br />").prepend(nameifl.$element);
				repi = new OO.ui.TextInputWidget({
					placeholder: mw.msg("relgen-s1-repph-v1"),
					required: true
				});
				repi.on("enter", function () {
					s1v();
				});
				var repifl = new OO.ui.FieldLayout(repi, {
					label: mw.msg("relgen-s1-repl-v1"),
					align: 'right'
				});
				authi = new OO.ui.TextInputWidget({
					placeholder: mw.msg("relgen-s1-authph-v1"),
					required: true
				});
				authi.on("enter", function () {
					s1v();
				});
				var authifl = new OO.ui.FieldLayout(authi, {
					label: mw.msg("relgen-s1-authl-v1"),
					align: 'right'
				});
				$("#irep").prepend(authifl.$element).prepend(repifl.$element);
				radioSelectInput_s2 = new OO.ui.RadioSelectInputWidget({
					options: [{
							data: "1",
							label: mw.msg("relgen-s2-o1-v1")
						}, {
							data: "2",
							label: mw.msg("relgen-s2-o2-v1")
						}, {
							data: "3",
							label: mw.msg("relgen-s2-o3-v1")
						}, {
							data: "-1",
							label: ""
						}
					],
					value: "-1"
				});
				radioSelectInput_s2.on("change", function () {
					if (radioSelectInput_s2.value > 0) {
						smsc(2);
					}
				});
				$("#front_s2").append(radioSelectInput_s2.$element);
				radioSelectInput_s3 = new OO.ui.RadioSelectInputWidget({
					options: [{
							data: "1",
							label: mw.msg("relgen-s3-o1-v1")
						}, {
							data: "2",
							label: mw.msg("relgen-s3-o2-v1")
						}, {
							data: "-1",
							label: ""
						}
					],
					value: "-1"
				});
				radioSelectInput_s3.on("change", function () {
					if (radioSelectInput_s3.value > 0) {
						smsc(3);
						if (radioSelectInput_s3.value == 1) $('#iup').show();
						else $('#iup').hide();
					}
				});
				$("#front_s3").append(radioSelectInput_s3.$element);
				fileri = new OO.ui.MultilineTextInputWidget({
					placeholder: mw.msg("relgen-s3-fileph-v1"),
					spellcheck: false,
					autosize: true,
					required: true
				});
				fileri.on("enter", function () {
					s3v();
				});
				var filerifl = new OO.ui.FieldLayout(fileri, {
					label: mw.msg("relgen-s3-filel-v1"),
					align: "right",
					helpInline: "true",
					help: mw.msg("relgen-s3-fileh-v1")
				}); // [[mw:Manual:Page_title]]
				$("#iup").append(filerifl.$element).append("<br />");
				$("input[value='-1']").parents("label").remove();
				$(".oo-ui-element-hidden .oo-ui-inputWidget-input").val("-1");
				licensei = new OO.ui.ComboBoxInputWidget({
					options: [{
							data: "Creative Commons Attribution-ShareAlike 4.0 International"
						}, {
							data: "Creative Commons Attribution 4.0 International"
						}, {
							data: "Creative Commons CC0 1.0 Universal",
							label: "Creative Commons CC0 1.0 Universal (" + mw.msg("relgen-s4-pdph-v1") + ")"
						}
					],
					value: "Creative Commons Attribution-ShareAlike 4.0 International",
					placeholder: mw.msg("relgen-s4-licenseph-v1"),
					required: true
					// menu: {filterFromInput: true} // initially can't select any other option due to default value
				});
				licensei.on("change", function () {
					iagi.setSelected(false);
					if (licensei.value.search("CC0") == -1) $('#iawattr').show();
					else $('#iawattr').hide();
				});
				var licenseifl = new OO.ui.FieldLayout(licensei, {
					align: "top",
					label: mw.msg("relgen-s4-licensel-v1"),
					help: new OO.ui.HtmlSnippet(mw.msg("relgen-s4-licenseh-v1"))
				});
				$("#front_s4").prepend(licenseifl.$element);
				// $("#licensei .oo-ui-fieldLayout-help").on("click", function () {}); // TO-DO: find way to bypass popover and link directly
				iagi = new OO.ui.CheckboxInputWidget();
				iagi.on("change", function () {
					if (iagi.selected) {
						smsc(4);
					} else {
						$("#back_s4").fadeOut(200);
					}
				});
				var iagifl = new OO.ui.FieldLayout(iagi, {
					label: mw.msg("relgen-s4-iagl-v1"),
					align: "inline"
				});
				$("#front_s4").append(iagifl.$element);
				$.getJSON("/w/api.php?action=query&format=json&prop=revisions&pageids=11505208&rvprop=content", function (data) {
					$("#back_s5").append("<br /><p>" + mw.msg("relgen-s5-bl-v1", data.query.pages[11505208].revisions[0]["*"]) + "</p><br />");
				});
				if (withi18n) {
					for (i18ns = 0; i18ns <= 5; i18ns++) {
						i18nbd = 1;
						while (true) {
							if ($("[class^='" + "relgen-s" + i18ns + "-bd" + i18nbd + "']").length) {
								i18nv = $("[class^='" + "relgen-s" + i18ns + "-bd" + i18nbd + "']")[0].className.split(/\s+/)[0].split("-")[3];
								$(".relgen-s" + i18ns + "-bd" + i18nbd + "-" + i18nv).html(mw.message("relgen-s" + i18ns + "-bd" + i18nbd + "-" + i18nv).parse());
								i18nbd++;
							} else break;
						}
					}
				}
			});
		});
	});
}

$.getScript("/w/index.php?action=raw&ctype=text/javascript&title=MediaWiki:Relgen.js/text").then(function () {
	if (ulang == "en") init(false);
	else {
		$.getJSON("/w/api.php?action=query&format=json&prop=revisions&titles=MediaWiki:Relgen.js/i18n/" + ulang, function (data) {
			if (!data.query.pages[-1]) {
				$.getScript("/w/index.php?action=raw&ctype=text/javascript&title=MediaWiki:Relgen.js/i18n/" + ulang).then(function () {
					init(true);
				});
			} else if (ulang.includes("-")) {
				ulang = ulang.split("-")[0];
				$.getJSON("/w/api.php?action=query&format=json&prop=revisions&titles=MediaWiki:Relgen.js/i18n/" + ulang, function (data_lf) {
					if (!data_lf.query.pages[-1]) {
						$.getScript("/w/index.php?action=raw&ctype=text/javascript&title=MediaWiki:Relgen.js/i18n/" + ulang).then(function () {
							init(true);
						});
					} else init(false);
				});
			} else init(false);
		});
	}
});