MediaWiki:Gadget-AvertissementImage.js

Une page de Wikipédia, l'encyclopédie libre.
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
 * {{Catégorisation JS|AvertissementImage}}
 * Codé par Bayo, version corrigée par Marin M.
 */

/* <nowiki> */

(function () {
    'use strict';

    /** change the text edited according to a param in the URL
     */
    function patchImageWarningDescription() {
        /** extract a warning param from url, else return null
         */
        function getWarningParam() {
            return new URL(document.URL).searchParams.get('imageWarning');
        }

        /** update parent var "text" and "comment" according to params
         */
        function patchText(flaglist, flag, addTemplate, removeTemplate) {
            var addit = flaglist.includes(flag);
            if (addit) {
                if (!removeTemplate.test(text)) {
                    text += "\n{{" + addTemplate + "}}";
                    if (comment != "") comment += " ; ";
                    comment += "ajout de " + addTemplate;
                }
            } else {
                if (removeTemplate.test(text)) {
                    text = text.replace(removeTemplate, "");
                    if (comment != "") comment += " ; ";
                    comment += "retrait de " + addTemplate;
                }
            }
            return text;
        }

        // get the base
        var comment = "";
        var text = document.getElementById('wpTextbox1').value;

        // compute new description
        var warning = getWarningParam();
        if (!warning) return;
        patchText(warning, 's', "source inconnue", /\{\{[Ss]ource inconnue.*\}\}\n?/);
        patchText(warning, 'l', "licence inconnue", /\{\{[Ll]icence inconnue.*\}\}\n?/);

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

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

    function addImageWarningButtonPanel() {
        if ( $( '#isOnCommons' ).length ) {
            return; // image isOnCommons, we can't patch it here
        }

        var $panel = $( '<div>' )
            .css( {
                'margin': '0 auto 0 auto',
                'background-color': '#F0F0F0',
                'border': '1px solid #808080',
                'padding': '5px',
                'text-align': 'left'
            } )
            .attr('id', 'ImageWarningPanel');

        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.append(button);

            $panel.append(document.createTextNode(" "));

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

            $panel.append(document.createTextNode(" "));
        }

        createProblemCheck("checkboxSource", "Pas de source", "bandeau-source-inconnue");
        createProblemCheck("checkboxLicense", "Pas de licence", "bandeau-licence-inconnue");

        var $button = $( '<input type="button">' )
            .val( 'Mettre à jour les avertissements' )
            .click( function() {
                var url, edit = document.getElementById("ca-edit");

                var isVectorSkin = mw.config.get('skin') === 'vector' || mw.config.get('skin') === 'vector-2022';
                if (isVectorSkin) {
                    url = edit.firstChild.firstChild.getAttribute("href");
                } else {
                    url = edit.firstChild.getAttribute("href");
                }
                url += "&imageWarning=";
                if (document.getElementById("checkboxSource").checked) url += "s";
                if (document.getElementById("checkboxLicense").checked) url += "l";
                document.location = url;
            } );
        $panel.append( $button );
        $panel.insertBefore( $( '#filetoc' ) );
    }

    function addCopyPasteImageWarningPanel() {
        var source = document.getElementById('bandeau-source-inconnue') != null;
        var licence = document.getElementById('bandeau-licence-inconnue') != null;
        if (!source && !licence) 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));
        }

        // bandeau unique
        var unique = "{" + "{subst:Avertissement image incomplète|image=" + mw.config.get('wgPageName');
        if (source) unique += "|s=?";
        if (licence) unique += "|l=?";
        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);
        }
    }

})();

/* </nowiki> */