MediaWiki:JSScripts/QPreview

Un article de Wikipèdia, l'enciclopèdia liura.
Salta a la navegació Salta a la cerca
/*
 * QPreview
 * 
 * Permet una previsualizacion rapida sens recargar la pagina
 * Aficha : * lo tèxt provesit pel servidor
             * las categorias e interwikis emulats en local
 * Aficha pas : * la previsualizacion del resumit
                   * los modèls utilizats
                   * autres daquòses rares coma las colors GeSHi (còde de programacion colorat)
 * Atencion : las categorias e interwikis son emulats localament e pòdon de còps èsser incorrèctes
 * (mancants se son incluses dins un modèl, afichats alara que son dins un includeonly, etc.)
 * 
 * Autor inicial : en:Alex Smotrov sus en:User:Alex Smotrov/qpreview.js,
 *  completat per fr:Seb35 per afichar los interwikis e las categorias
 * Data de la darrièra revision : 21 de novembre de 2007
 */

// Còde :

Illustration
  1 var qPreviewLang = '|aa|ab|af|ak|als|am|an|ang|ar;العربية|arc|as|ast|av|ay|az;Azərbaycan|ba|bar|bat-smg|be|be-x-old|bg;Български|bh|bi|bm|bn;বাংলা|bo|bpy;ইমার ঠার/বিষ্ণুপ্রিয়া মণিপুরী|br;Brezhoneg|bs;Bosanski|bug|bxr|ca;Català|cbk-zam|cdo|ce|ceb;Cebuano|ch|cho|chr|chy|closed-zh-tw|co|cr|cs;Česky|csb|cu|cv|cy;Cymraeg|da;Dansk|de;Deutsch|diq|dv|dz|ee|el;Ελληνικά|eml|en;English|eo;Esperanto|es;Español|et;Eesti|eu;Euskara|fa;فارسی|ff|fi;Suomi|fiu-vro|fj|fo|fr;Français|frp|fur|fy|ga|gd|gl;Galego|glk|gn|got|gu|gv|ha|hak|haw|he;עברית|hi;हिन्दी|ho|hr;Hrvatski|hsb|ht|hu;Magyar|hy|hz|ia|id;Bahasa Indonesia|ie|ig|ii|ik|ilo|io;Ido|is;Íslenska|it;Italiano|iu|ja;日本語|jbo|jv;Basa Jawa|ka;ქართული|kab|kg|ki|kj|kk|kl|km|kn|ko;한국어|kr|ks|ksh|ku|kv|kw|ky|la;Latina|lad|lb;Lëtzebuergesch|lbe|lg|li|lij|lmo;Lumbaart|ln|lo|lt;Lietuvių|lv;Latviešu|map-bms|mg|mh|mi|mk;Македонски|ml|mn|mo|mr;मराठी|ms;Bahasa Melayu|mt|mus|my|mzn|na|nah|nan|nap;Nnapulitano|nds;Plattdüütsch|nds-nl|ne|new;नेपाल भाषा|ng|nl;Nederlands|nn;Norsk (nynorsk)|no;Norsk (bokmål)|nov|nrm|nv|ny|oc|om|or|os|pa|pag|pam|pap|pdc|pi|pih|pl;Polski|pms|ps|pt;Português|qu|rm|rmy|rn|ro;Română|roa-rup|roa-tara|ru;Русский|ru-sib|rw|sa|sc|scn;Sicilianu|sco|sd|se|sg|sh;Srpskohrvatski / Српскохрватски|si|simple;Simple English|sk;Slovenčina|sl;Slovenščina|sm|sn|so|sq;Shqip|sr;Српски / Srpski|ss|st|su;Basa Sunda|sv;Svenska|sw|ta;தமிழ்|te;తెలుగు|tet|tg|th;ไทย|ti|tk|tl|tlh|tn|to|tokipona|tpi|tr;Türkçe|ts|tt|tum|tw|ty|udm|ug|uk;Українська|ur|uz|ve|vec|vi;Tiếng Việt|vls|vo;Volapük|wa|war|wo|wuu|xal|xh|yi|yo|za|zea|zh;中文|zh-classical|zh-min-nan|zh-yue|zu|';
  2 
  3 function qPreviewButton()
  4 {
  5  sauv = document.getElementById('wpSave');
  6  if(!sauv) return;
  7  if(!document.getElementById('p-lang'))
  8  {
  9   newDiv = document.createElement('div');
 10   newDiv.id = 'p-lang';
 11   newDiv.setAttribute('class', 'portlet');
 12   newDiv.style.display = 'none';
 13   newDiv.innerHTML = '<h5>Autras lengas</h5><div class="pBody"><ul></ul></div>';
 14   toolbox = document.getElementById('p-tb');
 15   toolbox.parentNode.insertBefore(newDiv, toolbox.nextSibling);
 16  }
 17  if(!document.getElementById('catlinks'))
 18  {
 19   div = document.getElementsByTagName('div');
 20   for(i=0; i<div.length; i++)
 21   {
 22    if(div[i].className == 'printfooter')
 23    {
 24     newDiv = document.createElement('div');
 25     newDiv.id = 'catlinks';
 26     newDiv.style.display = 'none';
 27     div[i].parentNode.insertBefore(newDiv, div[i].nextSibling);
 28     break;
 29    }
 30   }
 31  }
 32  newBtn = document.createElement('input');
 33  newBtn.type = 'button';
 34  newBtn.value = 'QPreview';
 35  newBtn.onclick = qPreview;
 36  newBtn.id = 'btnQPreview';
 37  sauv.parentNode.insertBefore(newBtn, sauv.nextSibling);
 38 }
 39 addOnloadHook(qPreviewButton);
 40 
 41 function qPreview()
 42 {
 43  var qPreviewLanguage = wgServer.match(/.*\/\/(commons|incubator|meta|species|wikimediafoundation|www)/i) ? '' : wgServer.match(/.*\/\/([a-z-]*)\./i)[1];
 44  var divPreview = document.getElementById('wikiPreview');
 45  if (!divPreview) return;
 46  var btnQPreview = document.getElementById('btnQPreview');
 47  btnQPreview.value = 'Paciéncia…';
 48  var qPreviewEnd = false;
 49  qPreviewTextbox = document.getElementById('wpTextbox1').value;
 50  if(document.editform.wpSection.value == 'new') qPreviewTextbox = '== '+document.editform.wpSummary.value+" ==\n"+qPreviewTextbox;
 51  qPreviewAjax1 = sajax_init_object();
 52  qPreviewAjax2 = sajax_init_object();
 53  
 54  // Tractament del tèxt
 55  qPreviewAjax1.open('POST', document.editform.action+'&live', true);
 56  Boundary = '--------p1415';
 57  qPreviewAjax1.setRequestHeader('Content-Type', 'multipart/form-data; boundary='+Boundary);
 58  PostData = '--' + Boundary
 59   + '\nContent-Disposition: form-data; name="wpTextbox1"\n\n'
 60   + qPreviewTextbox + '\n--'+Boundary;
 61  if (qPreviewAjax1.overrideMimeType) qPreviewAjax1.overrideMimeType('text/html');
 62  qPreviewAjax1.onreadystatechange = function()
 63  {
 64   if (qPreviewAjax1.readyState != 4) return;
 65   html = qPreviewAjax1.responseText;
 66   html = html.replace(/<br style="clear: both;" \/>/,'').replace(/&gt;/g,'>').replace(/&lt;/g,'<').replace(/&quot;/g,'"').replace(/&amp;/g,'&').replace(/&apos;/g,"'");
 67   divPreview.innerHTML = html;
 68   if(qPreviewEnd) btnQPreview.value = 'QPreview';
 69   qPreviewEnd = true;
 70  };
 71  qPreviewAjax1.send(PostData);
 72  
 73  // Tractament de las categorias
 74  reg1 = new RegExp(' ','g'); reg2 = new RegExp('\\|.*');
 75  regexCat = new RegExp('\\[\\[ *(?:Categoria|Category) *: *(.*)\\]\\]', 'gi');
 76  qPreviewCat = qPreviewTextbox.match(regexCat);
 77  if(!qPreviewCat) { document.getElementById('catlinks').style.display = 'none'; if(qPreviewEnd) btnQPreview.value = 'QPreview'; qPreviewEnd = true; }
 78  else
 79  {
 80   qPreviewTemp = '';
 81   for(i=0;i<qPreviewCat.length;i++)
 82   {
 83    qPreviewCat[i] = 'Categoria:'+qPreviewCat[i].replace(regexCat, '$1').replace(reg2, '');
 84    if(qPreviewCat[i] == 'Categoria:') qPreviewCat.splice(i--, 1);
 85    else qPreviewTemp += qPreviewCat[i]+'|';
 86   }
 87   if(qPreviewTemp)
 88   {
 89    qPreviewAjax2.open('GET', wgServer+'/w/api.php?action=query&format=xml&prop=info&redirects&titles=' + qPreviewTemp.substr(0, qPreviewTemp.length-1), true);
 90    qPreviewAjax2.onreadystatechange = function()
 91    {
 92     if(qPreviewAjax2.readyState != 4) return;
 93     qPreviewTemp = '';
 94     xmlDoc = qPreviewAjax2.responseXML;
 95     normalized = xmlDoc.getElementsByTagName('normalized');
 96     if(normalized.length) normalized = normalized[0].getElementsByTagName('n');
 97     redirects = xmlDoc.getElementsByTagName('redirects');
 98     if(redirects.length) redirects = redirects[0].getElementsByTagName('r');
 99     pages = xmlDoc.getElementsByTagName('pages')[0].getElementsByTagName('page');
100     for(i=0; i<qPreviewCat.length; i++)
101     {
102      cat = qPreviewCat[i];
103      missing = false;
104      redirect = false;
105      for(j=0; j<normalized.length; j++) if(normalized[j].getAttribute('from') == cat) { cat = normalized[j].getAttribute('to'); break; }
106      for(j=0; j<pages.length; j++) if(pages[j].getAttribute('title') == cat && pages[j].getAttribute('missing') == '') { missing = true; break; }
107      for(j=0; j<redirects.length; j++) if(redirects[j].getAttribute('from') == cat) { redirect = redirects[j].getAttribute('to'); break; }
108      qPreviewTemp += '<span dir="ltr"><a href="/wiki/'+cat.replace(reg1, '_')+'" title="'+cat+'"'+(missing ? ' class="new"' : '')+(redirect ? ' style="font-style:italic;"' : '')+'>'+cat.replace(/Categoria:(.*)/, '$1')+'</a></span> • ';
109     }
110     qPreviewTemp = '<p class="catlinks"><a href="/wiki/Categoria:Acuèlh" title="Categoria:Acuèlh">Categoria'+(pages.length > 1 ? 's' : '')+' </a>: '+qPreviewTemp.substr(0, qPreviewTemp.length-5)+'</p>';
111     document.getElementById('catlinks').innerHTML = qPreviewTemp;
112     document.getElementById('catlinks').style.display = 'block';
113     if(qPreviewEnd) btnQPreview.value = 'QPreview';
114     qPreviewEnd = true;
115    };
116    qPreviewAjax2.send(null);
117   }
118   else { document.getElementById('catlinks').style.display = 'none'; if(qPreviewEnd) btnQPreview.value = 'QPreview'; qPreviewEnd = true; }
119  }
120  
121  // Tractament dels interwikis
122  qPreviewIw = '';
123  reg3 = new RegExp('\\|'); reg4 = new RegExp('(.*)]'); reg5 = new RegExp(';(.*)');
124  regexIw = new RegExp('\\[\\[ *(\\w{2,12}) *: *(.*)\\]\\]', 'gi');
125  result = qPreviewTextbox.match(regexIw);
126  if(!result) { document.getElementById('p-lang').style.display = 'none'; return; }
127  for(i=0;i<result.length;i++)
128  {
129   r1 = result[i].replace(regexIw, '$1');
130   r2 = result[i].replace(regexIw, '$2').replace(reg1, '_');
131   r3 = qPreviewLang.search(RegExp('\\|'+r1+'(?:;|\\|)', 'i'));
132   if(r1 == qPreviewLanguage) { r3 = -1; qPreviewIw += '<li class="interwiki-'+r1+'"><b>Atencion</b> : lenga <b><u><i>'+r1+'</i></u></b> en interwiki</li>'; }
133   if(r3 != -1) { r3 = qPreviewLang.substr(r3+1).replace(reg3,']'); r3 = reg4.exec(r3)[1]; qPreviewIw += '<li class="interwiki-'+r1+'"><a href="//'+r1+'.wikipedia.org/wiki/'+r2+'">'+(r3.indexOf(';')!=-1 ? reg5.exec(r3)[1] : 'Lenga \''+r1+'\'')+'</a></li>'; }
134  }
135  document.getElementById('p-lang').getElementsByTagName('ul')[0].innerHTML = qPreviewIw;
136  if(qPreviewIw) document.getElementById('p-lang').style.display = 'block';
137  else document.getElementById('p-lang').style.display = 'none';
138 }
139 //Fin del còde