Utilisateur:Dr Brains/FusionContribs.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) ;

Firefox (sur GNU/Linux) / Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5.
/**
 * Fusion d'historiques
 *
 * Fusionne les historiques de plusieurs contributeurs
 *
 * Auteur : Marc Mongenet
 * Dernière révision : 24 mai 2007
 * {{Projet:JavaScript/Script}}
 */

// Fusion display of Wikipedia user contributions history
// Copyright 2006, 2007, Marc Mongenet
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// See http://www.gnu.org/licenses/gpl.html

// TODO: Special:Log

var fusioncontribsFus = new Object(); // fusioncontribsFus is used as namespace.
fusioncontribsFus.main_contributor = undefined; // main contributor name
fusioncontribsFus.contributors_list = new Object(); // XHTML list of contributors
fusioncontribsFus.contribs_limit = 0; // 0 means default value
fusioncontribsFus.count = 0; // number of fusionned contribs
fusioncontribsFus.colors = [ "", "#FFB", "#FBF", "#BFF", "#DDF", "#DFD", "#FDD",
                             "#EEA", "#EAE", "#AEE", "#CCE", "#CEC", "#ECC",
                             "#DD9", "#D9D", "#9DD", "#BBD", "#BDB", "#DBB" ];

fusioncontribsFus.contributors_list.init = function() {
        this.elm = document.createElement("ol");
}

fusioncontribsFus.contributors_list.add = function(contributor) {
        // UI components
        var li = document.createElement("li");
        li.fusioncontribsFus = new Object();
        li.fusioncontribsFus.contributor = contributor;
        li.style.backgroundColor = fusioncontribsFus.colors[fusioncontribsFus.count];

        var aContributor = document.createElement("a");
        aContributor.href = mw.config.get('wgServer') + mw.config.get('wgArticlePath').split("$1").join(mw.config.get('wgFormattedNamespaces')[2]+":"+contributor.replace(/_/g, " "));
        aContributor.title = mw.config.get('wgFormattedNamespaces')[2]+":"+decodeURIComponent(contributor).replace(/_/g, " ");
        aContributor.appendChild(document.createTextNode(decodeURIComponent(contributor).replace(/_/g, " ")));

        var aTalk = document.createElement("a");
        aTalk.href = mw.config.get('wgServer') + mw.config.get('wgArticlePath').split("$1").join(mw.config.get('wgFormattedNamespaces')[3]+":"+contributor.replace(/_/g, " "));
        aTalk.title = mw.config.get('wgFormattedNamespaces')[3]+":"+decodeURIComponent(contributor).replace(/_/g, " ");
        aTalk.appendChild(document.createTextNode("d"));

        var aContribs = document.createElement("a");
        aContribs.href = mw.config.get('wgServer') + mw.config.get('wgArticlePath').split("$1").join("Special:Contributions/"+contributor.replace(/_/g, " "));
        aContribs.title = "Special:Contributions/"+decodeURIComponent(contributor).replace(/_/g, " ");
        aContribs.appendChild(document.createTextNode("c"));

        var aEditcount = document.createElement("a");
        aEditcount.href = "http://toolserver.org/~soxred93/pcount/index.php?lang=fr&wiki=wikipedia&name=" + decodeURIComponent(contributor).replace(/_/g, "+");
        aEditcount.title = "Editcount de "+decodeURIComponent(contributor).replace(/_/g, " ");
        aEditcount.appendChild(document.createTextNode("e"));

        li.appendChild(aContributor);
        li.appendChild(document.createTextNode(" ("));
        li.appendChild(aTalk);
        li.appendChild(document.createTextNode("|"));
        li.appendChild(aContribs);
        li.appendChild(document.createTextNode("|"));
        li.appendChild(aEditcount);
        li.appendChild(document.createTextNode(")"));
        this.elm.appendChild(li);
}

fusioncontribsFus.monthNameToNum = function(m) {
        if (m == "janvier") return 0;
        if (m == "février") return 1;
        if (m == "mars") return 2;
        if (m == "avril") return 3;
        if (m == "mai") return 4;
        if (m == "juin") return 5;
        if (m == "juillet") return 6;
        if (m == "août") return 7;
        if (m == "septembre") return 8;
        if (m == "octobre") return 9;
        if (m == "novembre") return 10;
        if (m == "décembre") return 11;
        return 12;
}


// Return Date of contribution listed in this li element.
fusioncontribsFus.contribTimeRegex = /(\d+)\s+(\S+)\s+(\d{4})\D+(\d+)\D+(\d{2})/;
fusioncontribsFus.parseContribTime = function(li) {
        var HTML = li.querySelector('.mw-changeslist-date').textContent;
        var datetime = HTML.match(fusioncontribsFus.contribTimeRegex);
        if(datetime==null) alert("Erreur lors de la récupération de date : \nContacter le Projet:JavaScript\n\n"+HTML);
        datetime[2] = fusioncontribsFus.monthNameToNum(datetime[2]);
        return new Date(parseInt(datetime[3]),
                            datetime[2],
                        parseInt(datetime[1]),
                        parseInt(datetime[4], 10),
                        parseInt(datetime[5], 10));
}

fusioncontribsFus.fuse = function(ul1, ul2, contributor, color) {
        contributor = " -- " + contributor;
        var li1 = ul1.firstChild;
        for (var li22 = ul2.firstChild; li22; li22 = li22.nextSibling) {
                if (li22.nodeType == 1) {
                        var li2 = li22.cloneNode(true);
                        li2.appendChild(document.createTextNode(contributor));
                        li2.style.backgroundColor = color;
                        var time2 = fusioncontribsFus.parseContribTime(li2).getTime();

                        while (li1) {
                                if (li1.nodeType == 1) {
                                        var time1 = fusioncontribsFus.parseContribTime(li1).getTime();
                                        if (time1 <= time2) {
                                                if (time1 == time2) {
                                                        li1.style.borderLeftWidth = li2.style.borderLeftWidth = "0.25em";
                                                        li1.style.borderLeftStyle = li2.style.borderLeftStyle = "solid";
                                                        li1.style.borderLeftColor = li2.style.borderLeftColor = "red";
                                                }
                                                break;
                                        }
                                }
                                li1 = li1.nextSibling;
                        }
                        ul1.insertBefore(li2, li1);
                }
        }
}

fusioncontribsFus.getElementById = function(elm, id) {
        var res = null;
        if (elm.id == id) res = elm;
        for (var child = elm.firstChild; !res && child; child = child.nextSibling)
                if (child.nodeType == 1)
                        res = fusioncontribsFus.getElementById(child, id);
        return res;
}

fusioncontribsFus.onFusionWith = function(contributor) {
        ++fusioncontribsFus.count;
        fusioncontribsFus.contributors_list.add(contributor);

        // Load fusionned page with synchronous XMLHttpRequest
        var http_request;
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
                http_request = new XMLHttpRequest();
                if (http_request.overrideMimeType)
                        http_request.overrideMimeType('text/xml');
        } else if (window.ActiveXObject) { // IE
                try {
                        http_request = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                        try {
                                http_request = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e) {}
                }
        }
        http_request.open('GET', "/w/index.php?title=Sp%C3%A9cial:Contributions&target="+contributor+"&limit="+fusioncontribsFus.contribs_limit, false);
        http_request.send(null);

        // Get both contributions histories
        var ul1 = document.getElementById("bodyContent");
        ul1 = ul1.getElementsByTagName("ul")[0];

        var ul2 = http_request.responseXML;
        if (!ul2.getElementById) { // MSIE
                ul2 = document.createElement("div");
                ul2.innerHTML = http_request.responseText;
                ul2 = fusioncontribsFus.getElementById(ul2, "bodyContent");
        } else {
                ul2 = ul2.getElementById("bodyContent");
        }
        ul2 = ul2.getElementsByTagName("ul")[0];

        // Fusion
        fusioncontribsFus.fuse(ul1, ul2, contributor, fusioncontribsFus.colors[fusioncontribsFus.count]);
}

fusioncontribsFus.parseContribUrlTarget = function(url) {
        var target = url.match(/&target=([^&]+)/);
        if (!target)
                target = url.match(/Sp%C3%A9cial:Contributions\/(\S+)/);
        return target[1];
}

fusioncontribsFus.parseContribUrlLimit = function(url) {
        var limit = url.match(/&limit=(\d+)/);
        if (limit)
                return parseInt(limit[1]);
        return 0;
}

fusioncontribsFus.onContribPage = function(url) {
        fusioncontribsFus.main_contributor = fusioncontribsFus.parseContribUrlTarget(url);
        fusioncontribsFus.contribs_limit = fusioncontribsFus.parseContribUrlLimit(url);

        // UI components
        fusioncontribsFus.contributors_list.init();
        fusioncontribsFus.contributors_list.add(fusioncontribsFus.main_contributor);

        var input = document.createElement("input");
        input.type = "text";

        var submit = document.createElement("input");
        submit.type = "submit";

        var form = document.createElement("form");
        form.onsubmit = function() {
                fusioncontribsFus.onFusionWith(input.value);
                return false;
        }

        // UI components layout
        submit.value = "Fusionner";
        var fusion = document.createTextNode("Fusionner avec les contributions de ");
        form.appendChild(fusion);
        form.appendChild(input);
        form.appendChild(submit);
        var bodyContent = document.getElementById("bodyContent");
        bodyContent.insertBefore(form, bodyContent.firstChild);
        bodyContent.insertBefore(fusioncontribsFus.contributors_list.elm, bodyContent.firstChild);
}

fusioncontribsFus.testForContribPage = function() {
        if (mw.config.get('wgCanonicalSpecialPageName') == "Contributions") {
                try {
                        fusioncontribsFus.onContribPage(window.location.href);
                } catch (e) { /* Something went wrong. */ }
        }
}

$(fusioncontribsFus.testForContribPage);