importScript('User:TheDJ/qui.js');
//Funzioni per fare le query al DB con api.php
var wpajax = {
http: function(bundle) {
// mandatory: bundle.url
// optional: bundle.async
// optional: bundle.method
// optional: bundle.headers
// optional: bundle.data
// optional: bundle.onSuccess (xmlhttprequest, bundle)
// optional: bundle.onFailure (xmlhttprequest, bundle)
// optional: bundle.otherStuff OK too, passed to onSuccess and onFailure
var xmlhttp;
try {
xmlhttp = new XMLHttpRequest();
} catch(e) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
xmlhttp = false
}
}
}
if (xmlhttp) {
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4)
wpajax.httpComplete(xmlhttp,bundle);
};
xmlhttp.open(bundle.method ? bundle.method : "GET",bundle.url,bundle.async == false ? false : true);
if (bundle.headers) {
for (var field in bundle.headers)
xmlhttp.setRequestHeader(field,bundle.headers[field]);
}
xmlhttp.send(bundle.data ? bundle.data : null);
}
return xmlhttp;
},
httpComplete: function(xmlhttp,bundle) {
if (xmlhttp.status == 200 || xmlhttp.status == 302) {
if (bundle.onSuccess)
bundle.onSuccess(xmlhttp,bundle);
} else if (bundle.onFailure) {
bundle.onFailure(xmlhttp,bundle);
} else {
// A activer en debug mode ?
// alert(xmlhttp.statusText);
}
}
};
// Parser
if (document.implementation.createDocument) {
var gml_xmlparser = new DOMParser();
}
function gml_XMLParse(string) {
if (document.implementation.createDocument) {
return gml_xmlparser.parseFromString(string, "text/xml");
} else if (window.ActiveXObject) {
var gml_xmldoc = new ActiveXObject("Microsoft.XMLDOM");
gml_xmldoc.async = "false";
ret = gml_xmldoc.loadXML(string);
if (!ret)
return null;
return gml_xmldoc.documentElement;
}
return null;
}
// HTMLize
String.prototype.htmlize = function() {
var chars = new Array('&','<','>','"');
var entities = new Array('amp','lt','gt','quot');
var regex = new RegExp();
var string = this;
for (var i=0; i<chars.length; i++) {
regex.compile(chars[i], "g");
string = string.replace(regex, '&' + entities[i] + ';');
}
return string;
}
//Alias di document.getElementById
$ = function(id){
return document.getElementById(id);
};
/****** Traduzione automatica dei wikilink da lingua straniera in italiano *******/
// Elenco dei [[ ]]
var elencoLink = new Array();
function tradToIT()
{
// Recupera l'area di testo
text = $("wpTextbox1").value;
elencoLink.length=0;
result = "";
inseriti = "|";
while (text.indexOf("[[") != -1)
{
text = text.substring(text.indexOf("[[")+2);
to1 = text.indexOf("]]");
if (to1 == -1)
to1 = 10000000;
to2 = text.indexOf("|");
if (to2 == -1)
to2 = 10000000;
to3 = text.indexOf(":");
if (to3 == -1)
to3 = 10000000;
if (to3<to2 && to3<to1) // si tratta di un interwiki o di un link a namespace!=0. Va saltato
{
text = text.substring(2);
continue;
}
link = text.charAt(0).toUpperCase(); // prima lettera maiuscola
link += text.substring(1, (to1<to2)?to1:to2);
if (inseriti.indexOf("|"+link+"|") == -1)
elencoLink[elencoLink.length] = link; // così non mette i doppioni
inseriti += link+"|";
}
// Per ogni [[ ]]
for (x in elencoLink)
{
// Ricava l'elenco degli interwiki della voce in oggetto
wpajax.http({url: mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?action=query&prop=langlinks&titles='+escape(elencoLink[x])+'&redirects&format=xml&lllimit=5000',
onSuccess: addRow, en: elencoLink[x] });
}
}
// Crea la lista dei risultati
var result = "";
var total=0;
function addRow(xmlreq, data) {
var api = xmlreq.responseXML;
var en = data.en;
// Cerca se c'è "it.wiki"
interwikis = api.getElementsByTagName('ll');
trovato = "";
for (i=0; i<interwikis.length; i++)
if (interwikis[i].getAttribute('lang')=="it")
{
trovato = interwikis[i].textContent;
break;
}
// Aggiungi al totale la riga: [[en.wiki]] => [[it.wiki]] / No link
if (trovato != "")
result += "<a href='"+wgServer+"/wiki/"+escape(en)+"'>" + en + "</a> => <a href='http://it.wikipedia.org/wiki/"+escape(trovato)+"'>" + trovato + "</a><br/>";
else
result += "<a href='"+wgServer+"/wiki/"+escape(en)+"'>" + en + "</a> => No link<br/>";
if (++total == elencoLink.length)
{
// splitto su <br/>
list = result.split("<br/>");
// ordina la lista
list.sort();
// ricrea la stringa
result = list.join("<br/>");
result = result.substring(5); // elimino il <br/> iniziale
// Mostra tutta la lista calcolata
total=0;
myRef = window.open('about:blank','mywin', 'left=20,top=20,width=500,height=500,toolbar=yes,resizable=yes');
myRef.document.write("<div style='width: 490px; height: 490px; overflow: auto'> "+result+"</div>");
}
}
addOnloadHook(function () {
if (wgAction=="edit")
{
mw.util.addPortletLink('p-tb', 'javascript:tradToIT()', 'Traduci i wikilink', 'tradIT');
$('tradIT').firstChild.accessKey = 'T';
$('tradIT').title = 'Alt-Shift-T';
}
})