MediaWiki:JSScripts/FusionContribs

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

//

  1 /*
  2  * Fusion d'istorics
  3  *
  4  * Fusiona los istorics de mantun contributors
  5  *
  6  * Autor : Marc Mongenet
  7  * Darrièra revision : 21 de novembre de 2007
  8  */
  9 
 10 // Fusion display of Wikipedia user contributions history
 11 // Copyright 2006, 2007, Marc Mongenet
 12 //
 13 // This program is free software; you can redistribute it and/or
 14 // modify it under the terms of the GNU General Public License
 15 // as published by the Free Software Foundation; either version 2
 16 // of the License, or (at your option) any later version.
 17 //
 18 // This program is distributed in the hope that it will be useful,
 19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 21 // GNU General Public License for more details.
 22 //
 23 // See http://www.gnu.org/licenses/gpl.html
 24 
 25 // TODO: Special:Log
 26 
 27 var fusioncontribsFus = new Object(); // fusioncontribsFus is used as namespace.
 28 fusioncontribsFus.main_contributor = undefined; // main contributor name
 29 fusioncontribsFus.contributors_list = new Object(); // XHTML list of contributors
 30 fusioncontribsFus.contribs_limit = 0; // 0 means default value
 31 fusioncontribsFus.count = 0; // number of fusionned contribs
 32 fusioncontribsFus.colors = [ "", "#FFB", "#FBF", "#BFF", "#DDF", "#DFD", "#FDD",
 33                              "#EEA", "#EAE", "#AEE", "#CCE", "#CEC", "#ECC",
 34                              "#DD9", "#D9D", "#9DD", "#BBD", "#BDB", "#DBB" ];
 35 
 36 fusioncontribsFus.contributors_list.init = function() {
 37         this.elm = document.createElement("ol");
 38 }
 39 
 40 fusioncontribsFus.contributors_list.add = function(contributor) {
 41         // UI components
 42         var li = document.createElement("li");
 43         li.fusioncontribsFus = new Object();
 44         li.fusioncontribsFus.contributor = contributor;
 45         li.style.backgroundColor = fusioncontribsFus.colors[fusioncontribsFus.count];
 46         li.appendChild(document.createTextNode(contributor));
 47         var a = document.createElement("a");
 48         a.href = "http://tools.wikimedia.de/~interiot/cgi-bin/count_edits?dbname=frwiki_p&user=" + contributor;
 49         a.appendChild(document.createTextNode(" editcount"));
 50         li.appendChild(a);
 51         this.elm.appendChild(li);
 52 }
 53 
 54 fusioncontribsFus.monthNameToNum = function(m) {
 55         if (m == "genièr") return 0;
 56         if (m == "febrièr") return 1;
 57         if (m == "març") return 2;
 58         if (m == "abril") return 3;
 59         if (m == "mai") return 4;
 60         if (m == "junh") return 5;
 61         if (m == "julhet") return 6;
 62         if (m == "agost") return 7;
 63         if (m == "setembre") return 8;
 64         if (m == "octobre") return 9;
 65         if (m == "novembre") return 10;
 66         if (m == "decembre") return 11;
 67         return 12;
 68 }
 69 
 70 // Return Date of contribution listed in this li element.
 71 fusioncontribsFus.contribTimeRegex = /(\d+)\s+(\S+)\s+(\d{4})\D+(\d+)\D+(\d{2})/;
 72 fusioncontribsFus.parseContribTime = function(li) {
 73         var datetime = li.firstChild.nodeValue.match(fusioncontribsFus.contribTimeRegex);
 74         datetime[2] = fusioncontribsFus.monthNameToNum(datetime[2]);
 75         return new Date(parseInt(datetime[3]),
 76                             datetime[2],
 77                         parseInt(datetime[1]),
 78                         parseInt(datetime[4], 10),
 79                         parseInt(datetime[5], 10));
 80 }
 81 
 82 fusioncontribsFus.fuse = function(ul1, ul2, contributor, color) {
 83         contributor = " -- " + contributor;
 84         var li1 = ul1.firstChild;
 85         for (var li22 = ul2.firstChild; li22; li22 = li22.nextSibling) {
 86                 if (li22.nodeType == 1) {
 87                         var li2 = li22.cloneNode(true);
 88                         li2.appendChild(document.createTextNode(contributor));
 89                         li2.style.backgroundColor = color;
 90                         var time2 = fusioncontribsFus.parseContribTime(li2).getTime();
 91 
 92                         while (li1) {
 93                                 if (li1.nodeType == 1) {
 94                                         var time1 = fusioncontribsFus.parseContribTime(li1).getTime();
 95                                         if (time1 <= time2) {
 96                                                 if (time1 == time2) {
 97                                                         li1.style.borderLeftWidth = li2.style.borderLeftWidth = "0.25em";
 98                                                         li1.style.borderLeftStyle = li2.style.borderLeftStyle = "solid";
 99                                                         li1.style.borderLeftColor = li2.style.borderLeftColor = "red";
100                                                 }
101                                                 break;
102                                         }
103                                 }
104                                 li1 = li1.nextSibling;
105                         }
106                         ul1.insertBefore(li2, li1);
107                 }
108         }
109 }
110 
111 fusioncontribsFus.getElementById = function(elm, id) {
112         var res = null;
113         if (elm.id == id) res = elm;
114         for (var child = elm.firstChild; !res && child; child = child.nextSibling)
115                 if (child.nodeType == 1)
116                         res = fusioncontribsFus.getElementById(child, id);
117         return res;
118 }
119 
120 fusioncontribsFus.onFusionWith = function(contributor) {
121         ++fusioncontribsFus.count;
122         fusioncontribsFus.contributors_list.add(contributor);
123 
124         // Load fusionned page with synchronous XMLHttpRequest
125         var http_request;
126         if (window.XMLHttpRequest) { // Mozilla, Safari, ...
127                 http_request = new XMLHttpRequest();
128                 if (http_request.overrideMimeType)
129                         http_request.overrideMimeType('text/xml');
130         } else if (window.ActiveXObject) { // IE
131                 try {
132                         http_request = new ActiveXObject("Msxml2.XMLHTTP");
133                 } catch (e) {
134                         try {
135                                 http_request = new ActiveXObject("Microsoft.XMLHTTP");
136                         } catch (e) {}
137                 }
138         }
139         http_request.open('GET', "/w/index.php?title=Special:Contributions&target="+contributor+"&limit="+fusioncontribsFus.contribs_limit, false);
140         http_request.send(null);
141 
142         // Get both contributions histories
143         var ul1 = document.getElementById("bodyContent");
144         ul1 = ul1.getElementsByTagName("ul")[0];
145 
146         var ul2 = http_request.responseXML;
147         if (!ul2.getElementById) { // MSIE
148                 ul2 = document.createElement("div");
149                 ul2.innerHTML = http_request.responseText;
150                 ul2 = fusioncontribsFus.getElementById(ul2, "bodyContent");
151         } else {
152                 ul2 = ul2.getElementById("bodyContent");
153         }
154         ul2 = ul2.getElementsByTagName("ul")[0];
155 
156         // Fusion
157         fusioncontribsFus.fuse(ul1, ul2, contributor, fusioncontribsFus.colors[fusioncontribsFus.count]);
158 }
159 
160 fusioncontribsFus.parseContribUrlTarget = function(url) {
161         var target = url.match(/&target=([^&]+)/);
162         if (!target)
163                 target = url.match(/Special:Contributions\/(\S+)/);
164         return target[1];
165 }
166 
167 fusioncontribsFus.parseContribUrlLimit = function(url) {
168         var limit = url.match(/&limit=(\d+)/);
169         if (limit)
170                 return parseInt(limit[1]);
171         return 0;
172 }
173 
174 fusioncontribsFus.onContribPage = function(url) {
175         fusioncontribsFus.main_contributor = fusioncontribsFus.parseContribUrlTarget(url);
176         fusioncontribsFus.contribs_limit = fusioncontribsFus.parseContribUrlLimit(url);
177 
178         // UI components
179         fusioncontribsFus.contributors_list.init();
180         fusioncontribsFus.contributors_list.add(fusioncontribsFus.main_contributor);
181 
182         var input = document.createElement("input");
183         input.type = "text";
184 
185         var submit = document.createElement("input");
186         submit.type = "submit";
187 
188         var form = document.createElement("form");
189         form.onsubmit = function() {
190                 fusioncontribsFus.onFusionWith(input.value);
191                 return false;
192         }
193 
194         // UI components layout
195         submit.value = "Fusionar";
196         var fusion = document.createTextNode("Fusionar amb las contribucions de ");
197         form.appendChild(fusion);
198         form.appendChild(input);
199         form.appendChild(submit);
200         var bodyContent = document.getElementById("bodyContent");
201         bodyContent.insertBefore(form, bodyContent.firstChild);
202         bodyContent.insertBefore(fusioncontribsFus.contributors_list.elm, bodyContent.firstChild);
203 }
204 
205 fusioncontribsFus.testForContribPage = function() {
206         var url = window.location.href;
207         if (url.match(/\/Special:Contributions\/|title=Special:Contributions/)) {
208                 try {
209                         fusioncontribsFus.onContribPage(url);
210                 } catch (e) { /* Something went wrong. */ }
211         }
212 }
213 
214 addOnloadHook(fusioncontribsFus.testForContribPage);
215 //