MediaWiki:JSScripts/DeluxeHistory

//

/**
* Nom : DeluxeHistory
* Autor : Dake
* Basat sus de còde Ajax de GôTô
* Darrièra mesa a jorn : 21 de novembre de 2007
* Comentaris : 
*/
var sysopsDeluxeHistory = null;
var botsDeluxeHistory = null;

function extractUsers(res) {
  pattern = /title\=\"Utilizaire:(.*?)\"/g;
  s = "";
  while((result = pattern.exec(res)) != null) {
    s += result[1] + "||";
  }
  return s;
}

function getBots() {
  try {
    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
  } catch (e) { /* menja l'excepcion */ }

  if (!cookies.get("botsDeluxeHistory")) {
    ajax.conn.onreadystatechange = function () {
    if (ajax.conn.readyState == 4) {
      botsDeluxeHistory = extractUsers(ajax.getResult())
      // conservat per una setmana
      cookies.setWithDelay("botsDeluxeHistory", botsDeluxeHistory, 1000*3600*24*7)
      getSysops();
    }
  }
  ajax.sendRequest("//oc.wikipedia.org/w/index.php?title=Special:Listusers&group=bot&limit=500&offset=0&action=raw", "GET", true)
  } else {
    botsDeluxeHistory = cookies.get("botsDeluxeHistory")
    getSysops();
  }
}
			
function getSysops() {
  try {
    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
  } catch (e) { /* manja l'excepcion */ }

  if (!cookies.get("sysopsDeluxeHistory")) {
    ajax.conn.onreadystatechange = function () {
    if (ajax.conn.readyState == 4) {
      sysopsDeluxeHistory = extractUsers(ajax.getResult())
      // conservat per una setmana
      cookies.setWithDelay("sysopsDeluxeHistory", sysopsDeluxeHistory, 1000*3600*24*7)
      deluxeHistoryProcess();
    }
  }
  ajax.sendRequest("//oc.wikipedia.org/w/index.php?title=Special:Listusers&group=sysop&limit=500&offset=0&action=raw", "GET", true)
  } else {
    sysopsDeluxeHistory = cookies.get("sysopsDeluxeHistory")
    deluxeHistoryProcess();
  }
}
				
// code by Martin Honnen
function getOuterHTML (node) {
  if (node.nodeType == 3) {
    return node.nodeValue;
  } else if (node.nodeType == 1) {
    var html = '';
    html += '<' + node.nodeName;
    for (var a = 0; a < node.attributes.length; a++) html += ' ' + node.attributes[a].nodeName + '="' + node.attributes[a].nodeValue + '"';
    if (node.childNodes.length == 0) {
      html += ' \/>';
    } else {
      html += '>';
    }
    for (var c = 0; c < node.childNodes.length; c++) html += getOuterHTML(node.childNodes[c]);
    html += '<\/' + node.nodeName + '>';
  }
  return html;
}
		
function deluxeHistoryProcess() {					
  sysopsList = sysopsDeluxeHistory.split("||")
  botsList = botsDeluxeHistory.split("||")		
	
  var lis = document.getElementById("pagehistory").getElementsByTagName("li");

  for (i=0; i<lis.length; i++) {
    spanNode = lis[i].getElementsByTagName("span");
    username = spanNode[0].childNodes[0].childNodes[0].nodeValue;
		
    // check l'estatut del contributor
    var className = "history-user-normal";
		
    // bot ? 
    for (j=0;j<botsList.length;j++) {
      if (botsList[j] == username) {
        className = "history-user-bot";
        break;
      }
    }  
		
    // sysop ? 
    for (j=0;j<sysopsList.length;j++) {
      if (sysopsList[j] == username) {
        className = "history-user-sysop";
        break;
      }
    }

    // ip ? 
    var ip = /\d{1,3}\.\d{1,3}.\d{1,3}.\d{1,3}/;
		
    if (ip.exec(username)) {
      className = "history-user-ip"
    }

    // ajust de las icònas
    specialOp = "";
    var comment = "";
					
    for (h=0;h<spanNode.length;h++) {
      if (spanNode[h].getAttribute("class") == "comment") { 
        comment = getOuterHTML(spanNode[h])
        break;
      }
    }
				
    var regexExpEvents = new Array();
    regexExpEvents[0] = /((r|R)evocacion|(r|R)v |(R|r)evert|(v|V)andalisme)/;
    regexExpEvents[1] = /(a restabl(it|ida) (E|e)scafa(t|da)|(s|S)upression)/;
    regexExpEvents[2] = /((R|r)enommage|a déplacé|a renommé)/;
    regexExpEvents[3] = /((semi|Semi)proteccion|(P|p)roteccion|(A|a) protegi(t|da)/;
		
    var iconsEvents = new Array();
    iconsEvents[0] = "//upload.wikimedia.org/wikipedia/commons/thumb/7/77/Crystal_Clear_action_reload.png/20px-Crystal_Clear_action_reload.png"
    iconsEvents[1] = "//upload.wikimedia.org/wikipedia/commons/thumb/0/0c/Crystal_Clear_action_exit.png/20px-Crystal_Clear_action_exit.png"
    iconsEvents[2] = "//upload.wikimedia.org/wikipedia/commons/thumb/4/45/Crystal_Clear_action_2rightarrow.png/20px-Crystal_Clear_action_2rightarrow.png"
    iconsEvents[3] = "//upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Crystal_Clear_action_lock.png/20px-Crystal_Clear_action_lock.png"

    if (comment) {
      for (k=0;k<regexExpEvents.length;k++) {
        if (regexExpEvents[k].test(comment)) {
          imgTag = "<img src=\"" + iconsEvents[k] + "\" />" 
          specialOp = "<div class=\"" + className + "\" style=\"float: right; vertical-align:top;\">" + 
          imgTag + "</div>"
        }
      }	
    }
		
    lis[i].innerHTML = "<div class=\"" + className + "\">" + specialOp  + lis[i].innerHTML + "</div>"

    inputNodes = lis[i].getElementsByTagName("input");
    for (j=0;j<inputNodes.length;j++) {
      inputNodes[j].style.visibility='visible';
    }

    for (j=0;j<spanNode.length;j++) {
      if(spanNode[j].getAttribute("class")=="history-user") {
        spanNode[j].setAttribute("class", className);
      }
    }
  }
}

if (wgAction == "history") addOnloadHook(getBots);
//