MediaWiki:Common.js/Wikinews:Article principal

Une nouvelle de Wikinews, la source d'informations que vous pouvez écrire.
Sauter à la navigation Sauter à la recherche

/*

  1 This javascript deals with stuff specific to [[User:Bawolff/sandbox/makeLead]]. Functions
  2 specific to lead generation are at User:Bawolff/sanbox/leadGenerator.js
  3 functions relating to api access are at User:Bawolff/mwapilib2.js
  4 */
  5 importScript('Mediawiki:Common.js/Wikinews:Article_principal/leadGenerator');
  6 
  7 var showPreview = function (html) {
  8  //We assume that the mediawiki api is not malicious...
  9  document.getElementById('LeadPreview').innerHTML = html;
 10 }
 11 var showLead = function (leadText, leadTitle) {
 12  var lsp = document.getElementById('leadSourcePrev');
 13  if (!lsp) {
 14   var src = document.createElement('textarea');
 15   src.id = 'leadSourcePrev';
 16   src.style.width = '48%';
 17   src.rows = 40;
 18   src.appendChild(document.createTextNode(leadText));
 19   document.getElementById('LeadBox').appendChild(src);
 20   document.getElementById('LeadPreview').style.visibility = 'visible';
 21  }
 22  else {
 23   lsp.value = leadText;
 24  }
 25  leadTitle = leadTitle.replace(/(\\|')/g, "\\$1").replace(/"/g, """).replace(/>/g, '>'); //prevent xss
 26  var leadNumb = document.getElementById('leadNum').selectedIndex + 1;
 27  document.getElementById('LeadControls').innerHTML = '<button onclick="syncToServer(' + leadNumb + ', \'' + leadTitle + '\')">Enregistrer l\'article principal ' + leadNumb + '</button> <button onclick="regeneratePreview()">Rafraîchir la prévisualisation</button>';
 28 
 29  api(leadText).replace(/<noinclude>[\d\D]*?<\/noinclude>/, '').parse('Main Page').lift(showPreview).exec();
 30  //this will strip out anything after a noinculde (this is kind of dirty)
 31 
 32 }
 33 var regeneratePreview = function() {
 34 var wikiText = document.getElementById('leadSourcePrev').value;
 35 api(wikiText).replace(/<noinclude>[\d\D]*?<\/noinclude>/, '').parse('Main Page').lift(showPreview).exec();
 36 }
 37 var syncToServer = function(leadNum, page) {
 38  if (leadNum === undefined || !confirm('Êtes-vous sûr de vouloir sauvegarder ceci sur Modèle:Une ' + leadNum + '?')) {
 39   return false;
 40  }
 41  api(document.getElementById('leadSourcePrev').value).setDefaultSummary('[[Wikinews:Article principal|AP]] : Actualisation de l\'article principal ' + leadNum + ' : [[' + page + ']]').savePage('Modèle:Une ' + leadNum).inject('Article principal correctement mis à jour').lift(alert).lift(function() {location = wgServer + wgArticlePath.replace('$1', wgPageName) + '?js-good-update=' + encodeURIComponent(page);}).exec();
 42 }
 43 var startLeadMaker = function () {
 44  if (wgAction !== 'view' && wgAction !== 'purge') {
 45   return false;
 46  }
 47  if (!window.api || !window.Bawolff || !window.Bawolff.leadGen) {
 48   alert('Javascript ne s\'est pas chargé correctement, comme résultat que cette page peut ou ne peut fonctionner. Si ça ne fonctionne pas, essayer d\'actualiser la page.');
 49  }
 50  if (!wgUserGroups || wgUserGroups.join(' ').indexOf("autoconfirmed") === -1) {
 51   jsMsg("<div style='border:solid red medium;font-weight: bold'>Attention : Vous ne pouvez pas modifier les articles principaux, seulement les personnes qui ont un compte enregistré depuis plus de quatre jours peuvent les modifier.</div>");
 52  }
 53  var lastUpdate = location.search.match(/[?&]js-good-update=([^&]*).*/);
 54  if (lastUpdate) {
 55   //this is all properly escaped since its in a text node.
 56   window.setTimeout(function () {jsMsg(document.createTextNode('Article principal a été actualisé avec «' + decodeURIComponent(lastUpdate[1]) + ' ».'));}, 400); //to get arround other things using jsMSg
 57  }
 58  var setupContainer = document.getElementById('lSetup');
 59  setupContainer.innerHTML += "<form id='leadForm' action='javascript:doMakeLead();void 0;'><label for='pageNameField'>Page&nbsp;:</label> <input type='text' id='pageNameField' size='50'><br/><label for='leadNum'>Article&nbsp;principal&nbsp;:</label> <select id='leadNum' onchange='doMakeLead(true);'><option selected value='1'>1 (en haut)</option><option>2 (milieu)</option><option>3 (en bas)</option> </select> <label for='sumMethod'>Méthode&nbsp;de&nbsp;résumé&nbsp;:</label> <select id='sumMethod'><option selected value='0'>Première phrase</option><option value='1'>Deux premières phrases</option><option value='2'>Premier paragraphe</option><option>250 premières lettres</option> <option>500 premières lettres</option> </select> &nbsp;<input type='submit' value='Aperçu' ></form>";
 60 
 61  var dpl = document.getElementById('lDPL').getElementsByTagName('a');
 62  for (var i = 0; i < dpl.length; i++) {
 63   linkVal = encodeURIComponent(dpl[i].firstChild.data.replace(/(\\|")/g, "\\$1"));
 64   // %22 = "
 65   dpl[i].href = 'javascript:document.getElementById(%22pageNameField%22).value=%22' + linkVal + '%22;doMakeLead();void%200';
 66  }
 67  os_initHandlers('pageNameField','leadForm', document.getElementById('pageNameField'));
 68  
 69  var handleAfterLeadInfoTableLoads = function (table) {
 70   document.getElementById('lMetaTable').innerHTML = table;
 71   var pageName = location.search.replace(/.*?[?&]use-page=([^&]*).*/, '$1');
 72 
 73   if (Bawolff.leadGen.oldestLead) {
 74    var oldLeadNum = parseInt(Bawolff.leadGen.oldestLead.charAt(Bawolff.leadGen.oldestLead.length - 1));
 75    document.getElementById('leadNum').selectedIndex = oldLeadNum - 1;
 76    if (oldLeadNum === 1) {
 77     document.getElementById('sumMethod').selectedIndex = 1; //use 2 sentences for longer lead.
 78    }
 79   }
 80   if (pageName !== location.search) { //if it matched
 81    document.getElementById('pageNameField').value = decodeURIComponent(pageName.replace(/\+/g, ' '));
 82    location.href = '#leadForm'; //since already filled out.
 83    doMakeLead();
 84   }
 85   
 86  }
 87  Bawolff.leadGen.makeLeadTable(handleAfterLeadInfoTableLoads);
 88 
 89 }
 90 function doMakeLead (fromLeadNumChange) {
 91 var pageName = document.getElementById('pageNameField').value;
 92 var leadNum = document.getElementById('leadNum').selectedIndex + 1;
 93  if (fromLeadNumChange && leadNum === 1) {
 94     document.getElementById('sumMethod').selectedIndex = 1; //use 2 sentences for longer lead.
 95  }
 96 var sumMethod = document.getElementById('sumMethod').selectedIndex;
 97 if (!pageName) return; //stop error if not initialized.
 98 Bawolff.leadGen(pageName, leadNum, showLead, sumMethod);
 99 }
100 
101 waitAWhileThanLoadLead.times = 0;
102 function waitAWhileThanLoadLead () {
103 //since we're already from an onload event, and importScript is async. wait a while
104  if ((!window.api || !window.Bawolff || !window.Bawolff.leadGen) && waitAWhileThanLoadLead.times < 10) {
105   waitAWhileThanLoadLead.times++;
106   setTimeout(waitAWhileThanLoadLead, 200);
107  } else {
108  startLeadMaker();
109  }
110 }
111 addOnloadHook(waitAWhileThanLoadLead);
112 /*

*/