Jump to content

User:Jalo/Sandbox

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Jalo (talk | contribs) at 10:48, 4 September 2014. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

//

// controlla se l'array "array" contiene il valore "stringa"
function contiene(array, stringa) {
    for (var i = 0; i < array.length; i++) {
        if (array[i] == stringa) {
            return true;
        }
    }
    return false;
}

var vociInglesi = new Array();
var vociDaTrad = new Array();
var vociSuIt = new Array();
var interwikisCount = new Array();
var interwikisSuItCount = new Array();
var sizesCount = new Array();
var sizesSuItCount = new Array();
var noCategory = new Array();
var vociDaTradDaSort = new Array();
var vociSuItDaSort = new Array();

var countVociDaTrad = 0;

// cicla sull'elenco delle categorie chieste dall'utente
function analisiVoci() {
    for (i = 0; i < vociInglesi.length; i++)
    {
        // Ricava l'elenco degli interwiki della voce in oggetto
        $.ajax({
            url: mw.util.wikiScript('api'),
            data: {
                action: 'query',
                prop: 'langlinks|revisions',
                titles: escape(vociInglesi[i]),
                redirects: '',
                format: 'xml',
                lllimit: '500',
                rvprop: 'size'
            },
			dataType: "xml",
			async:false,
			type: "POST",
			success: function(response){
				addToVociDaTrad(response, vociInglesi[i]);
			}
        })
    }
}

function addToVociDaTrad(api, en) {
    if (api == null)
    {
        ++countVociDaTrad;
        return;
    }

    // Cerca se c'è "it.wiki"
    interwikis = api.getElementsByTagName('ll');

    trovato = "";
    for (j=0; j<interwikis.length; j++)
        if (interwikis[j].getAttribute('lang')=="it")
        {
            trovato = interwikis[j].textContent;
            break;
        }

    // Cerca la dimensione
    sizes = api.getElementsByTagName('rev');
    size = 0;
	
    if (sizes.length == 0)
        size = 0;
    else
        size = sizes[0].getAttribute('size');
		
    if (trovato == "")
    {
        vociDaTrad.push(en);
        interwikisCount.push(interwikis.length);
        sizesCount.push(size);
    }
    else
    {
        vociSuIt.push(trovato);
        interwikisSuItCount.push(interwikis.length);
        sizesSuItCount.push(size);
    }

    if (++countVociDaTrad == vociInglesi.length)
    {
        // crea la lista da ordinare
        for (i = 0; i < vociDaTrad.length; i++)
        {
            vociDaTradDaSort.push("<li><a href='"+wgServer+"/wiki/"+escape(vociDaTrad[i])+"'>" + vociDaTrad[i] + "</a> (" + interwikisCount[i] + " interlink) (" + sizesCount[i] + " bytes)</li>");
        }
        for (i = 0; i < vociSuIt.length; i++)
        {
            vociSuItDaSort.push("<li><a href='"+wgServer+"/wiki/"+escape(vociSuIt[i])+"'>" + vociSuIt[i] + "</a> (" + interwikisSuItCount[i] + " interlink) (" + sizesSuItCount[i] + " bytes)</li>");
        }
		
        // ordina le liste
        vociDaTradDaSort.sort();
        vociSuItDaSort.sort();
            
        // crea contenuto da salvare
        contenuto = "";
        for (i = 0; i < vociDaTradDaSort.length; i++)
        {
            contenuto += vociDaTradDaSort[i];
        }
        
        contenuto += "<br/><br/>";

        for (i = 0; i < vociSuItDaSort.length; i++)
        {
            contenuto += vociSuItDaSort[i];
        }
        
        // crea lista categorie inesistenti
        nocat = "";
        if (noCategory.length > 0)
        {
            nocat += "Le seguenti categorie non esistono su "+wgContentLanguage+".wiki o sono vuote: ";
        
            for (i = 0; i < noCategory.length; i++)
            {
                nocat += noCategory[i] + ", ";
            }
            
            nocat = nocat.substr(0, nocat.length-2) + "<br/><br/>";
        }
        
        // finito di creare la lista la mostro in una nuova finestra
        myRef = window.open('about:blank','mywin', 'left=20,top=20,width=500,height=300,toolbar=yes,resizable=yes');
        myRef.document.write("<div style='width: 100%; height: 100%; overflow: auto'> ");
        myRef.document.write("<big><big><big>Voci di "+wgContentLanguage+".wiki mancanti su it.wiki</big></big></big><br/><br/>");
        myRef.document.write("Tra parentesi il numero di interlink presenti su "+wgContentLanguage+".wiki<br/><br/>");
        myRef.document.write(nocat);
        myRef.document.write("<ol>"+contenuto+"</ol>");
        myRef.document.write("</div>");
    }
}

var totaleCategorie = 0;
var countCategorie = 0;

// cicla sull'elenco delle categorie chieste dall'utente
function scansioneCategorie() {
    categorie = document.getElementById("Lista").value.split("\n");
    totaleCategorie = categorie.length;
	count = 0;
    for (i = 0; i < categorie.length; i++)
    {
	    prosegui = false;
        $.ajax({
            url: mw.util.wikiScript('api'),
            data: {
                action: 'query',
                list: 'categorymembers',
                cmprop: 'title',
                cmnamespace: '0',
                cmlimit: '500',
                format: 'xml',
                cmtitle: 'Category:'+categorie[i]
            },
			dataType: "xml",
			async:false,
			type: "POST",
			success: function(response){
				addToVociInglesi (response, categorie[count++]);
			}
        })
    }
    
    // nascondo la finestra
    node=document.getElementById('categorie');
    node.parentNode.removeChild(node);
}

function addToVociInglesi(api, cat) {
    if (typeof(cat) == 'undefined')
    {
		if (++countCategorie == totaleCategorie)
			analisiVoci();
        return;
    }

    if (cat.length > 0)
      if (api.getElementsByTagName("categorymembers")[0].childNodes.length == 0) // categoria non esistente
          noCategory.push(cat);
    
    if (api)
    {
        cm = api.getElementsByTagName('cm');
        var voce="";
        for(j=cm.length-1; j >= 0 ; j--) {
            voce = cm[j].attributes[1].nodeValue;
            if (voce == "") continue;
            if (!contiene(vociInglesi, voce)) // se non è già in lista
                vociInglesi.push(voce);
        }
    }
    
    if (++countCategorie == totaleCategorie)
        analisiVoci();
}

// mostra finestra in cui si chiede quali categorie tradurre
function categorieDaTradurre() {
    countCategorie = 0;
	countVociDaTrad = 0;
	
  var msWndSetup = document.createElement('div');
    msWndSetup.id = 'categorie';
    msWndSetup.style.position = 'fixed';
    msWndSetup.style.zIndex   = 1000;
    msWndSetup.style.left     =  '0px';
    msWndSetup.style.top      =  '0px';
    msWndSetup.style.backgroundColor = '#FFFFFF';
    msWndSetup.style.borderStyle     = 'solid';
    msWndSetup.style.borderWidth     = ' medium';
    msWndSetup.style.borderColor     = '#000000';
    msWndSetup.style.width = '300px';
    msWndSetup.align = 'center';
 
  var top = document.createElement('div');
    top.id = 'categorieTop';
    top.style.textAlign = 'right';
    top.style.margin = '8px';
    top.style.backgroundColor = '#DDDDDD';
 
  msWndSetup.appendChild(top);
  var body = msWndSetup;
  var intro = document.createElement('p');
 
  intro.innerHTML = "<span style=\"font-size: medium; font-weight: bold;\">Elenco delle categorie da tradurre<br /><span style=\"font-size: small;\">(una per riga e senza \"Category:\")</span></span><br/><br/>";
 
  body.appendChild(intro);
 
  var Lista = document.createElement('textarea');
  Lista.rows = 10;
  Lista.width = "90%";
  Lista.wrap = 'off';
  Lista.name = 'Lista';
  Lista.id = 'Lista';
  Lista.style.marginLeft = "10px";
  Lista.style.marginRight = "10px";
  Lista.style.width = "90%";
 
  body.appendChild(Lista);
 
  // crea il bottone "Ok"
  var buttonOk = document.createElement('input');
  buttonOk.value = 'Ok';
  buttonOk.type = 'button';
  buttonOk.style.margin = '8px';
  buttonOk.setAttribute('onclick', 'scansioneCategorie()');
 
  // crea il bottone "Annulla"
  var buttonCancel = document.createElement('input');
  buttonCancel.value = 'Annulla';
  buttonCancel.type = 'button';
  buttonCancel.style.margin = '8px';
  buttonCancel.setAttribute('onclick', "javascript:node=document.getElementById('categorie');node.parentNode.removeChild(node);");
 
  body.innerHTML += "\n";
  body.appendChild(buttonOk);
  body.appendChild(buttonCancel);
 
  // e lo mostra a video
  document.body.appendChild(msWndSetup);
  Lista.focus();
}

addOnloadHook(function () {
    addPortletLink('p-tb', 'javascript:categorieDaTradurre()', 'Voci da tradurre', 'daTradIT');
    document.getElementById('daTradIT').firstChild.accessKey = 'V';
    document.getElementById('daTradIT').title = 'Voci da tradurre (Alt-Shift-V)';
})
//