MediaWiki:JSScripts/FusionContribs

Un article de Wikipèdia, l'enciclopèdia liura.
Salta a la navegació Salta a la cerca

//

/*
 * Fusion d'istorics
 *
 * Fusiona los istorics de mantun contributors
 *
 * Autor : Marc Mongenet
 * Darrièra revision : 21 de novembre de 2007
 */

// 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];
        li.appendChild(document.createTextNode(contributor));
        var a = document.createElement("a");
        a.href = "http://tools.wikimedia.de/~interiot/cgi-bin/count_edits?dbname=frwiki_p&user=" + contributor;
        a.appendChild(document.createTextNode(" editcount"));
        li.appendChild(a);
        this.elm.appendChild(li);
}

fusioncontribsFus.monthNameToNum = function(m) {
        if (m == "genièr") return 0;
        if (m == "febrièr") return 1;
        if (m == "març") return 2;
        if (m == "abril") return 3;
        if (m == "mai") return 4;
        if (m == "junh") return 5;
        if (m == "julhet") return 6;
        if (m == "agost") return 7;
        if (m == "setembre") return 8;
        if (m == "octobre") return 9;
        if (m == "novembre") return 10;
        if (m == "decembre") 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 datetime = li.firstChild.nodeValue.match(fusioncontribsFus.contribTimeRegex);
        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=Special: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(/Special: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 = "Fusionar";
        var fusion = document.createTextNode("Fusionar amb las contribucions 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() {
        var url = window.location.href;
        if (url.match(/\/Special:Contributions\/|title=Special:Contributions/)) {
                try {
                        fusioncontribsFus.onContribPage(url);
                } catch (e) { /* Something went wrong. */ }
        }
}

addOnloadHook(fusioncontribsFus.testForContribPage);
//