User:JJPMaster/CurateThisFile.js

// <nowiki>
importScript("User:JJPMaster/CurateThisPage/review.js");
var parsedTitle = mw.config.get("wgPageName").replace(/_/g, " ");
const licenseRegex = /\{\{(?:[Nn]o license\|?.*|[Gg]FDL-presumed\|?.*)\}\}/;
const sectionRegex = /==\s*Licensing\s*==/;

async function getNewDescriptionText(tag) {
	var pageContent;
    var params = {
        action: 'parse',
        page: mw.config.get('wgPageName'),
        prop: 'wikitext',
        format: 'json'
    };
    var api = new mw.Api();
    const data = await api.get(params);
    pageContent = data.parse.wikitext["*"];
    if (sectionRegex.test(pageContent)) {
	    if (licenseRegex.test(pageContent)) {
	    	return pageContent.replace(licenseRegex, "{{" + tag + "}}");
	    }
	    else return `${pageContent}\n\{\{${tag}\}\}`;
    }
    else if (licenseRegex.test(pageContent)) {
    	return pageContent.replace(licenseRegex, "== Licensing ==\n{{" + tag + "}}");
    }
    else return `${pageContent}\n== Licensing ==\n\{\{${tag}\}\}`;
}

$(() => {
	if (mw.config.get("wgArticleId") !== 0 && mw.config.get("wgCanonicalNamespace") == "File") {
		var porty2 = window.isReviewer ? $("#luna-rev") : null;
		var porty = mw.util.addPortletLink('luna-actions', '#', 'Tag (copyright)', 'luna-ctf', 'Tag this file for copyright');
		$("#bodyContent").append(`
		<div id="CTFdialog">
			How would you like to tag this file?<br />
			<select name="CTFtagReason" id="CTFtagReason">
				<option>Select the template</option>
				<option id="PD-USGov">Public domain (U.S. federal government)</option>
				<option id="PD-shape">Public domain (basic geometry)</option>
				<option id="PD-chart">Public domain (basic chart/graph)</option>
				<option id="PD-chem">Public domain (chemical structure)</option>
				<option id="PD-user">Public domain (author request)</option>
				<option id="PD-textlogo">Public domain (text logo)</option>
				<option id="PD-music-ineligible">Public domain (musical technique)</option>
				<option id="PD-old">Public domain (old)</option>
				<option id="Cc-by-4.0">Creative Commons Attribution 4.0</option>
				<option id="Cc-by-sa-4.0">Creative Commons Attribution-ShareAlike 4.0</option>
				<option id="GFDL">GNU Free Documentation License v1.2 or later</option>
				<option id="GPL">GNU General Public License v2 or later</option>
				<!-- <option id="s-Relicensed">GFDL (relicensed under CC BY-SA 3.0)</option> -->
				<option id="Non-free logo">Non-free logo</option>
				<option id="Non-free symbol">Non-free symbol</option>
				<option id="Non-free software screenshot">Non-free screenshot</option>
				<option id="Non-free product cover">Non-free product cover</option>
				<option id="s-Nld">No license (will mark for deletion)</option>
				<option id="other">Other</option>
		    </select>
		    <span style="display: block;"><input type="checkbox" id="reviewMarkCTF" class="reviewMarkCTF" name="reviewMarkCTF" /><label class="reviewMarkCTF" for="reviewMarkCTF">Mark as reviewed</label></span>
		    <button type="button" id="addTagCTF">Add tag</button></div>
		`);
		(async () => {
			if (await waitForPreferences()) {
				for (var [i, j] of Object.entries(Luna.myPreferences.CTF)) {
				    $("#CTFtagReason").append(`<option id="${i}">${j}</option>`);
				}
			}
			else {
			    console.error("Luna: Preferences never fully loaded.");
			}
		})();
		$("#CTFdialog").hide();
		if (!window.isReviewer) $(".reviewMarkCTF").remove();
		$(porty).click(() => $("#CTFdialog").dialog({ title: "Add file copyright tags", width: "auto" } ));
		$(porty2).click(() => { 
			doTheReview(true); 
			mw.notify("Luna:\nThe page has been marked as reviewed. Reloading...");
			setTimeout(() => window.location.reload(), 2000);
		});
		$("#addTagCTF").click(function() {
			var mySelect = document.getElementById("CTFtagReason");
			var critValue = mySelect.value;
			
			if(critValue == "Select the template")
				alert("You must select a template.");
			else {
				var criterion;
				var tag = mySelect.options[CTFtagReason.selectedIndex].id;
				var finalTag;
				if (tag == "other") {
					tag = prompt("Which template would you like to add? Not entering anything will abort the tagging.");
					if (!tag) return false;
				}
				else if (tag.startsWith("s-")) {
					tag = "subst:" + tag.slice(2);
				}
				if ($("#reviewMarkCTF").prop("checked")) {
					doTheReview(false);
				}
				criterion = `Adding {{${tag}}} tag`;
				finalTag = tag;
				(async function() {
				var params = {
					action: 'edit',
					text: await getNewDescriptionText(finalTag),
					title: parsedTitle,
					summary: criterion,
					tags: "Luna",
					format: 'json'
				},
				api = new mw.Api();
			
				api.postWithToken( 'csrf', params ).done( function ( data ) {
					console.log( data );
				} );
				})();
				mw.notify("Luna:\nThe tag has been added. Reloading...");
				setTimeout(() => window.location.reload(), 2000);
			}
		});
	}
});
// </nowiki>