MediaWiki:Gadget-AvertissementImage.js

Un article de Wikipèdia, l'enciclopèdia liura.

Nòta : Aprèp aver publicat la pagina, vos cal forçar son recargament complet tot ignorant lo contengut actual de l'amagatal de vòstre navigador per veire los cambiaments : Mozilla / Firefox / Konqueror / Safari : mantenètz la tòca Majuscula (Shift) en clicant lo boton Actualizar (Reload,) o quichatz Maj-Ctrl-R (Maj-Cmd-R sus Apple Mac) ; Internet Explorer / Opera : mantenètz la tòca Ctrl en clicant lo boton Actualizar o quichatz Ctrl-F5.

/**
 * Facilita la mantenença dels imatges ; notadament l'avertiment dels utilizaires.
 */

/** change the text edited according to a param in the URL 
 */
function patchImageWarningDescription() {
	/** extract a waring param from url, else return null
	 */
	function getWarningParam() {
		var params = document.URL.split('?')
		if (params[1] == undefined) return null
		params = params[1].split('&')
		for (var i in params) {
			param = params[i]
			if (param.substring(0, 13) != 'imageWarning=') continue
			return param.substring(13)
		}
		return null
	}

	/** update parent var "text" and "comment" accordint to params
	 */
	function patchText(flaglist, flag, addTemplate, removeTemplate) {
		var addit = flaglist.search(flag) != -1
		if (addit) {
			if (text.search(removeTemplate) == -1) {
				text += "\n{{subst:" + addTemplate + "}}"
				if (comment != "") comment += " ; "
				comment += "apondon de " + addTemplate
			}
		} else {
			if (text.search(removeTemplate) != -1) {
				text = text.replace(removeTemplate, "")
				if (comment != "") comment += " ; "
				comment += "levament de " + addTemplate
			}
		}
		return text
	}

	// get the base
	var comment = ""
	var text = document.getElementById('wpTextbox1').value
	
	// compute new description
	var warning = getWarningParam()
	if (warning == null) return
	patchText(warning, 'f', "nsd", /\{\{[Ff]ont desconeguda.*\}\}/)
	patchText(warning, 'l', "nld", /\{\{[Ll]icéncia desconeguda.*\}\}/)
	patchText(warning, 'p', "npd", /\{\{[Pp]ermission desconeguda.*\}\}/)

	// update the document
	document.getElementById('wpTextbox1').value = text
	document.getElementById('wpSummary').setAttribute("value", comment)

	// save the document
	document.getElementById('wpSave').click()
}

function onPushImageWarningUpdateButton() {
    var edit = document.getElementById("ca-edit")
    var url = edit.firstChild.getAttribute("href")
    url += "&imageWarning="
    if (document.getElementById("checkboxSource").checked == true)     url += "f"
    if (document.getElementById("checkboxLicense").checked == true)    url += "l"
    if (document.getElementById("checkboxPermission").checked == true) url += "p"
    document.location = url
}

function addImageWarningButtonPanel() {
    if (document.getElementById("isOnCommons")) {
        return; // image isOnCommons, we can't patch it here
    }

    var panel = document.createElement('DIV');
    panel.setAttribute("style", "margin:0 auto 0 auto; background-color:#F0F0F0; border:1px solid #808080; padding:5px; text-align:left;");

    function createProblemCheck(id, label, idwarning) {
        var checked = document.getElementById(idwarning) != null;

        var button = document.createElement('INPUT')
        button.setAttribute("type", "checkbox")
        button.setAttribute("id", id)
        if (checked) {
            button.setAttribute("checked", "checked")
        }

        panel.appendChild(button)

        panel.appendChild(document.createTextNode(" "))

        var text = document.createElement('LABEL')
        text.setAttribute("for", id)
        text.appendChild(document.createTextNode(label))
        panel.appendChild(text)

        panel.appendChild(document.createTextNode(" "))
    }

    createProblemCheck("checkboxSource", "Pas de font", "bendèl-font-desconeguda")
    createProblemCheck("checkboxLicense", "Pas de licéncia", "bendèl-licéncia-desconeguda")
    createProblemCheck("checkboxPermission", "Pas de permission", "bendèl-permission-desconeguda")

    var button = document.createElement('INPUT')
    button.setAttribute("type", "button")
    button.setAttribute("onClick", "onPushImageWarningUpdateButton()")
    button.setAttribute("value", "Metre a jorn los avertiments")
    //button.setAttribute("disabled", "disabled")
    panel.appendChild(button)

    var prev = document.getElementById('filetoc')
    prev.parentNode.insertBefore(panel, prev);
}

function addCopyPasteImageWarningPanel() {
    var source = document.getElementById('bendel-font-desconeguda') != null;
    var licence = document.getElementById('bendel-licencia-desconeguda') != null;
    var permission = document.getElementById('bendel-permission-desconeguda') != null;
    if (!source && !licence && !permission) return;

    var panel = document.createElement('DIV');
    panel.setAttribute("style", "margin:0 auto 0 auto; background-color:#FFE5E5; border:1px solid #FF6060; padding:5px; text-align:left;");

    if (source) {
        var text = "{"+"{subst:Avertiment font desconeguda|" + mw.config.get('wgPageName') + "}} ~~"+"~~"
        panel.appendChild(document.createTextNode(text))
    }
    if (licence) {
        if (source) panel.appendChild(document.createElement('BR'))
        var text = "{"+"{subst:Avertiment licéncia desconeguda|" + mw.config.get('wgPageName') + "}} ~~"+"~~"
        panel.appendChild(document.createTextNode(text))
    }
    if (permission) {
        if (source || licence) panel.appendChild(document.createElement('BR'))
        var text = "{"+"{subst:Avertiment permission desconeguda|" + mw.config.get('wgPageName') + "}} ~~"+"~~"
        panel.appendChild(document.createTextNode(text))
    }

    // bendèl unic
    unique = "{" + "{subst:Avertiment imatge incomplet|image=" + mw.config.get('wgPageName')
    if (source) unique += "|s=?"
    if (licence) unique += "|l=?"
    if (permission) unique += "|p=?"
    unique += "}} ~~"+"~~"
    panel.appendChild(document.createElement('HR'))
    panel.appendChild(document.createTextNode(unique))

    var prev = document.getElementById('filetoc')
    prev.parentNode.insertBefore(panel, prev);
}

if (mw.config.get('wgNamespaceNumber') == 6) {
    if (mw.config.get('wgAction') == "view") {
        $(addCopyPasteImageWarningPanel);
        $(addImageWarningButtonPanel);
    } else if (mw.config.get('wgAction') == "edit") {
        $(patchImageWarningDescription);
    }
}