Utilisateur:Chphe/Gadget-AvertissementImage.js

Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;

Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5.
/**
 * Facilite la maintenance des images ; notamment l'avertissement des utilisateurs.
 *
 * @todo créer un bandeau unique pour les avertissements de discussion
 * @todo bouton pour prévenir automatiquement les utilisateurs
 */
//Chphe : créé en copiant [[MediaWiki:Gadget-AvertissementImage.js]] (version du 12/09/2008, 16:54 : http://fr.wikipedia.org/w/index.php?title=MediaWiki:Gadget-AvertissementImage.js&oldid=33363773)


/** 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" according 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 += "ajout de " + addTemplate
			}
		} else {
			if (text.search(removeTemplate) != -1) {
				text = text.replace(removeTemplate, "")
				if (comment != "") comment += " ; "
				comment += "retrait de " + addTemplate
			}
		}
		return text
	}
	/** replace a template by another
	 */
	function replaceTemplate(flaglist, flag, addTemplate, removeTemplate) {
		var addit = flaglist.search(flag) != -1
		if (addit) {
			if (text.search(removeTemplate) != -1) {
				text = text.replace(removeTemplate, "{{"+addTemplate+"}}")
				if (comment != "") comment += " ; "
				comment += "remplacement par " + addTemplate
			}
			else{
				alert("Pas de modèle {{Domaine public}} (ou équivalent) trouvé");
			}
		}
		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, 's', "nsd", /\{\{[Ss]ource inconnue.*\}\}/)
	patchText(warning, 'l', "nld", /\{\{[Ll]icence inconnue.*\}\}/)
	patchText(warning, 'p', "npd", /\{\{[Pp]ermission inconnue.*\}\}/)
	replaceTemplate(warning, 'O', "PD-old", /\{\{([Dd]omaine public|[Dd]omainePublic|[Dd]omainepublic|[Pp]D|[Dd]P)\}\}/)
	replaceTemplate(warning, 'S', "PD-self", /\{\{([Dd]omaine public|[Dd]omainePublic|[Dd]omainepublic|[Pp]D|[Dd]P)\}\}/)

	// 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 += "s"
    if (document.getElementById("checkboxLicense").checked == true)    url += "l"
    if (document.getElementById("checkboxPermission").checked == true) url += "p"
    var radioPDold = document.getElementById("radioPDold")
    if(radioPDold)
    {
        if(radioPDold.checked) url += "O"
        else if(document.getElementById("radioPDself").checked) url += "S"
    }
    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 source", "bandeau-source-inconnue")
    createProblemCheck("checkboxLicense", "Pas de licence", "bandeau-licence-inconnue")
    createProblemCheck("checkboxPermission", "Pas de permission", "bandeau-permission-inconnue")

    function createPublicDomainRadio(id, label, checked) {
        var button = document.createElement('INPUT')
        button.setAttribute("type", "radio")
        button.setAttribute("id", id)
		button.setAttribute("name", "groupPDradio")
        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(" "))
    }

    // add radio buttons if page contains {{Domaine public}}
    var div_cats = document.getElementById('mw-normal-catlinks');
    if(div_cats)
    {
        var tab_a = div_cats.getElementsByTagName('A');
        for(var i=0; i<tab_a.length; i++)
            if(tab_a[i].title && tab_a[i].title.match(/:Image Domaine Public$/))
            {
                createPublicDomainRadio("radioPD", "PD", true)
                createPublicDomainRadio("radioPDold", "PD-old", false)
                createPublicDomainRadio("radioPDself", "PD-self", false)
                break;
            }
    }

    var button = document.createElement('INPUT')
    button.setAttribute("type", "button")
    button.setAttribute("onClick", "onPushImageWarningUpdateButton()")
    button.setAttribute("value", "Mettre à jour les avertissements")
    //button.setAttribute("disabled", "disabled")
    panel.appendChild(button)

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

function addCopyPasteImageWarningPanel() {
    var source = document.getElementById('bandeau-source-inconnue') != null;
    var licence = document.getElementById('bandeau-licence-inconnue') != null;
    var permission = document.getElementById('bandeau-permission-inconnue') != 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:Avertissement source inconnue|" + mw.config.get('wgPageName') + "}} ~~"+"~~"
        panel.appendChild(document.createTextNode(text))
    }
    if (licence) {
        if (source) panel.appendChild(document.createElement('BR'))
        var text = "{"+"{subst:Avertissement licence inconnue|" + mw.config.get('wgPageName') + "}} ~~"+"~~"
        panel.appendChild(document.createTextNode(text))
    }
    if (permission) {
        if (source || licence) panel.appendChild(document.createElement('BR'))
        var text = "{"+"{subst:Avertissement permission inconnue|" + mw.config.get('wgPageName') + "}} ~~"+"~~"
        panel.appendChild(document.createTextNode(text))
    }

    // bandeau unique
    unique = "{" + "{subst:Avertissement image incomplète|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") {
        addOnloadHook(addCopyPasteImageWarningPanel);
        addOnloadHook(addImageWarningButtonPanel);
    } else if (mw.config.get('wgAction') == "edit") {
        addOnloadHook(patchImageWarningDescription);
    }
}