Utilisateur:Dr Brains/BandeauxPortailsSpecialBOT.js

Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;

Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5.
// <source lang=javascript>
// <pre><nowiki>

/* **************** Bandeaux à enlever **************** */
var  BandeauxPortailsSpecialBOT_RemovePortal = new Array();
BandeauxPortailsSpecialBOT_RemovePortal.push('USA');
BandeauxPortailsSpecialBOT_RemovePortal.push('Etats-Unis');
BandeauxPortailsSpecialBOT_RemovePortal.push('des États-Unis');
BandeauxPortailsSpecialBOT_RemovePortal.push('Etats Unis');
BandeauxPortailsSpecialBOT_RemovePortal.push('États-Unis d\'Amérique');
BandeauxPortailsSpecialBOT_RemovePortal.push('états-Unis');

BandeauxPortailsSpecialBOT_RemovePortal.push('Alabama');
BandeauxPortailsSpecialBOT_RemovePortal.push('Alaska');
BandeauxPortailsSpecialBOT_RemovePortal.push('Arizona');
BandeauxPortailsSpecialBOT_RemovePortal.push('Arkansas');
BandeauxPortailsSpecialBOT_RemovePortal.push('Californie');
BandeauxPortailsSpecialBOT_RemovePortal.push('Caroline du Nord');
BandeauxPortailsSpecialBOT_RemovePortal.push('Caroline du Sud');
BandeauxPortailsSpecialBOT_RemovePortal.push('Colorado');
BandeauxPortailsSpecialBOT_RemovePortal.push('Connecticut');
BandeauxPortailsSpecialBOT_RemovePortal.push('Dakota du Nord');
BandeauxPortailsSpecialBOT_RemovePortal.push('Dakota du Sud');
BandeauxPortailsSpecialBOT_RemovePortal.push('Delaware');
BandeauxPortailsSpecialBOT_RemovePortal.push('Floride');
BandeauxPortailsSpecialBOT_RemovePortal.push('Géorgie (États-Unis)');
BandeauxPortailsSpecialBOT_RemovePortal.push('Hawaï');
BandeauxPortailsSpecialBOT_RemovePortal.push('Idaho');
BandeauxPortailsSpecialBOT_RemovePortal.push('Illinois');
BandeauxPortailsSpecialBOT_RemovePortal.push('Indiana');
BandeauxPortailsSpecialBOT_RemovePortal.push('Iowa');
BandeauxPortailsSpecialBOT_RemovePortal.push('Kansas');
BandeauxPortailsSpecialBOT_RemovePortal.push('Kentucky');
BandeauxPortailsSpecialBOT_RemovePortal.push('Louisiane');
BandeauxPortailsSpecialBOT_RemovePortal.push('Maine (État)');
BandeauxPortailsSpecialBOT_RemovePortal.push('Maryland');
BandeauxPortailsSpecialBOT_RemovePortal.push('Massachusetts');
BandeauxPortailsSpecialBOT_RemovePortal.push('Michigan');
BandeauxPortailsSpecialBOT_RemovePortal.push('Minnesota');
BandeauxPortailsSpecialBOT_RemovePortal.push('Mississippi (État)');
BandeauxPortailsSpecialBOT_RemovePortal.push('Missouri (État)');
BandeauxPortailsSpecialBOT_RemovePortal.push('Montana');
BandeauxPortailsSpecialBOT_RemovePortal.push('Nebraska');
BandeauxPortailsSpecialBOT_RemovePortal.push('Nevada');
BandeauxPortailsSpecialBOT_RemovePortal.push('New Hampshire');
BandeauxPortailsSpecialBOT_RemovePortal.push('New Jersey');
BandeauxPortailsSpecialBOT_RemovePortal.push('Nouveau-Mexique');
BandeauxPortailsSpecialBOT_RemovePortal.push('New York (État)');
BandeauxPortailsSpecialBOT_RemovePortal.push('Ohio');
BandeauxPortailsSpecialBOT_RemovePortal.push('Oklahoma');
BandeauxPortailsSpecialBOT_RemovePortal.push('Oregon');
BandeauxPortailsSpecialBOT_RemovePortal.push('Pennsylvanie');
BandeauxPortailsSpecialBOT_RemovePortal.push('Rhode Island');
BandeauxPortailsSpecialBOT_RemovePortal.push('Tennessee');
BandeauxPortailsSpecialBOT_RemovePortal.push('Texas');
BandeauxPortailsSpecialBOT_RemovePortal.push('Utah');
BandeauxPortailsSpecialBOT_RemovePortal.push('Vermont');
BandeauxPortailsSpecialBOT_RemovePortal.push('Virginie');
BandeauxPortailsSpecialBOT_RemovePortal.push('Virginie-Occidentale');
BandeauxPortailsSpecialBOT_RemovePortal.push('Washington (État)');
BandeauxPortailsSpecialBOT_RemovePortal.push('Wisconsin');
BandeauxPortailsSpecialBOT_RemovePortal.push('Wyoming');

BandeauxPortailsSpecialBOT_RemovePortal.push('New York');
BandeauxPortailsSpecialBOT_RemovePortal.push('Los Angeles');


/* **************** Bandeaux à ajouter **************** */
var  BandeauxPortailsSpecialBOT_AddPortal = new Array();
BandeauxPortailsSpecialBOT_AddPortal.push('États-Unis');




/* *************************************** SCRIPT ***************************************************** */

// Variables par défaut
var  BandeauxPortailsSpecialBOT_Multi = true;      // lister les articles des sous-catégories
var  BandeauxPortailsSpecialBOT_AutoSave = false;  // Publier automatiquement sans confirmation

// Lancement
if(mw.config.get('wgNamespaceNumber')==14){ 
     addOnloadHook(BandeauxPortailsSpecialBOT_AddLink);
}

// Ajout d'un onglet "Portails SP"
function BandeauxPortailsSpecialBOT_AddLink(){
     var OngletsCactions = document.getElementById('p-cactions').getElementsByTagName('ul')[0];
     OngletsCactions.innerHTML += '<li><a href="javascript:BandeauxPortailsSpecialBOT_GetOptions();">Portails SP</a></li>';
}

// Récolte des options (variables)
function BandeauxPortailsSpecialBOT_GetOptions(){
     BandeauxPortailsSpecialBOT_Multi = confirm('Recherche dans les sous-catégorie ?');
     BandeauxPortailsSpecialBOT_AutoSave = confirm('Publier sans confirmation manuelle ?');
     BandeauxPortailsSpecialBOT_Run();
}



// Listage des pages de la catégorie (et des sous-catégories)
if( (mw.config.get('wgUserGroups').indexOf("sysop")!=-1)||(mw.config.get('wgUserGroups').indexOf("bot")!=-1)){
   var BandeauxPortailsSpecialBOT_APILimit = 4999;
}else{
   var BandeauxPortailsSpecialBOT_APILimit = 499;
}

var BandeauxPortailsSpecialBOT_PageInCategory = new Array();
var BandeauxPortailsSpecialBOT_PageInCategoryCount = 0;

var BandeauxPortailsSpecialBOT_CategoryInCategory = new Array();
var BandeauxPortailsSpecialBOT_CategoryInCategoryCount = 0;
 
var BandeauxPortailsSpecialBOT_IndexCategory = -1;

function BandeauxPortailsSpecialBOT_Run(category, categorycontinue){
     if(!category) category = mw.config.get('wgPageName');    
     if(!categorycontinue) categorycontinue = '';
      var NouvelleRequete = new XMLHttpRequest()
      var AdresseRequete = mw.config.get('wgServer') + '/w/api.php?action=query&list=categorymembers&cmlimit='+BandeauxPortailsSpecialBOT_APILimit+'&cmtitle=' + category + '&cmcontinue=' + categorycontinue;
      NouvelleRequete.open("GET", AdresseRequete, true);
      NouvelleRequete.send(null);
      NouvelleRequete.onreadystatechange = function() {
            if(NouvelleRequete.readyState == 4 && NouvelleRequete.status == 200) {
                  var ElementTraitement = document.createElement('div');
                  ElementTraitement.innerHTML = NouvelleRequete.responseText; 
                  var Informations = ElementTraitement.getElementsByTagName('span');
                  var AutreRequeteNecessaire = false;
                  for(a=0;a<Informations.length;a++){
                        var ContenuInformation = Informations[a].innerHTML;
                        if((ContenuInformation.indexOf('ns="0"')!=-1)||(ContenuInformation.indexOf('ns="1"')!=-1)||(ContenuInformation.indexOf('ns="14"')!=-1)){
                              var TitrePage = ContenuInformation.split('title="')[1].split('"')[0];
                              TitrePage = BandeauxPortailsSpecialBOT_HTMLDecode(TitrePage);

                              TitrePage = TitrePage.split(mw.config.get('wgFormattedNamespaces')[1] + ':').join("");
                              if((TitrePage.indexOf(mw.config.get('wgFormattedNamespaces')[14] + ':')!=-1)&&(BandeauxPortailsSpecialBOT_Multi)){
                                   BandeauxPortailsSpecialBOT_CategoryInCategory[BandeauxPortailsSpecialBOT_CategoryInCategoryCount] = TitrePage;
                                   BandeauxPortailsSpecialBOT_CategoryInCategoryCount++
                              }else if(TitrePage.indexOf(mw.config.get('wgFormattedNamespaces')[14] + ':')==-1){
                                   BandeauxPortailsSpecialBOT_PageInCategory[BandeauxPortailsSpecialBOT_PageInCategoryCount] = TitrePage;
                                   BandeauxPortailsSpecialBOT_PageInCategoryCount++
                              }
                        }
                        if(ContenuInformation.indexOf('cmcontinue=')!=-1){
                              AutreRequeteNecessaire = true;
                              var AutreRequeteContinue = ContenuInformation.split('cmcontinue="')[1].split('"')[0];
                              AutreRequeteContinue = BandeauxPortailsSpecialBOT_HTMLDecode(AutreRequeteContinue);
                        }
                  } 
                  if(AutreRequeteNecessaire==true){
                        BandeauxPortailsSpecialBOT_Run(category, AutreRequeteContinue);
                  }else{
                        BandeauxPortailsSpecialBOT_IndexCategory++
                        var NewCategory = BandeauxPortailsSpecialBOT_CategoryInCategory[BandeauxPortailsSpecialBOT_IndexCategory];
                        if(NewCategory){
                              BandeauxPortailsSpecialBOT_Run(NewCategory);
                        }else{
                              BandeauxPortailsSpecialBOT_SortList();
                        }
                  }
            }
      }
}

// Tri des pages
var BandeauxPortailsSpecialBOT_SortedPageInCategory = new Array();
var BandeauxPortailsSpecialBOT_SortedPageInCategoryCount = 0;

function BandeauxPortailsSpecialBOT_SortList(){
      var PagesTemp = BandeauxPortailsSpecialBOT_PageInCategory.sort();
      for(var a=0;a<PagesTemp.length;a++){
           if(PagesTemp[a]!=PagesTemp[(a+1)]){
                 BandeauxPortailsSpecialBOT_SortedPageInCategory[BandeauxPortailsSpecialBOT_SortedPageInCategoryCount] = PagesTemp[a];
                 BandeauxPortailsSpecialBOT_SortedPageInCategoryCount++
           }
      }
      var Pop = confirm('Nombre de pages à vérifier : ' + BandeauxPortailsSpecialBOT_SortedPageInCategoryCount);
      if(Pop) BandeauxPortailsSpecialBOT_AddBandeau(0);
      else window.location.reload();
}

// Édition : Ajout/retrait/modif du bandeau
function BandeauxPortailsSpecialBOT_AddBandeau(position){
      var Category = BandeauxPortailsSpecialBOT_SortedPageInCategory[position];
      if(!Category){ alert('Travail terminé'); window.location.reload(); }
      var url = mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + Category.replace(/&/g, "%26") + '&action=edit';
      var NouvelleRequete = new XMLHttpRequest();      
      NouvelleRequete.open("GET", url, true);
      NouvelleRequete.send(null);
      NouvelleRequete.onreadystatechange = function() {
            if(NouvelleRequete.readyState == 4 && NouvelleRequete.status == 200) {
                  while(document.body.firstChild){ document.body.removeChild(document.body.firstChild);}
                  var ElementTraitement = document.createElement('div');
                  document.body.appendChild(ElementTraitement);
                  ElementTraitement.innerHTML = NouvelleRequete.responseText; 
                  var TextArea = document.getElementById('wpTextbox1');
                  if(!TextArea){alert(url); return; }                  

                  var OldText = TextArea.value;
                  if((OldText.indexOf('#REDIRECT')!=-1)||(OldText.indexOf('#redirect')!=-1)||(OldText=='')){
                        BandeauxPortailsSpecialBOT_AddBandeau((position+1)); 
                        return;
                  }

                  OldText = OldText.replace(/\{\{Portail /ig, "\{\{Portail\|"); // Cas d'un bandeau unique : {{Portail Truc}} -> {{Portail|Truc}}

                  // Nettoyage
                  OldText = OldText.replace(/\{\{portail/ig, "\{\{Portail");

                  if(OldText.indexOf('{{Portail|')!=-1){
                        var DebutModele = OldText.substring(0, OldText.indexOf('{{Portail'));
                        var ApresModele = OldText.split(DebutModele).join('');
                        var OldModele = ApresModele.substring(0, ApresModele.indexOf('}}')) + '}}';
                        var SuiteModele = ApresModele.split(OldModele).join('');
                        OldModele = BandeauxPortailsSpecialBOT_CleanModele(OldModele);

                        if((BandeauxPortailsSpecialBOT_RemovePortal.length>0)&&(BandeauxPortailsSpecialBOT_AddPortal.length>0)){ 
                              var portailaction = '';
                              var portailretrait = '';
                              var portailajout = '';
                              var NouveauModele = OldModele;
                              for(var r=0;r<BandeauxPortailsSpecialBOT_RemovePortal.length;r++){
                                    if((NouveauModele.indexOf('|' +BandeauxPortailsSpecialBOT_RemovePortal[r]+ '|')!=-1)||(NouveauModele.indexOf('|' +BandeauxPortailsSpecialBOT_RemovePortal[r]+ '}}')!=-1)){
                                          NouveauModele = NouveauModele.split('|' +BandeauxPortailsSpecialBOT_RemovePortal[r]+ '|').join('|');
                                          NouveauModele = NouveauModele.split('|' +BandeauxPortailsSpecialBOT_RemovePortal[r]+ '}}').join('}}');
                                          portailaction = 'Retrait';
                                          portailretrait += ' - '+BandeauxPortailsSpecialBOT_RemovePortal[r];
                                    }
                              }
                              for(var a=0;a<BandeauxPortailsSpecialBOT_AddPortal.length;a++){
                                    if((NouveauModele.indexOf('|' +BandeauxPortailsSpecialBOT_AddPortal[a] + '|')==-1)&&(NouveauModele.indexOf('|' +BandeauxPortailsSpecialBOT_AddPortal[a] + '}')==-1)){
                                          var NouveauModele = NouveauModele.split('}}').join('|' + BandeauxPortailsSpecialBOT_AddPortal[a] + '}}');
                                          if(portailaction!=''){
                                                 portailaction = 'Changement';
                                                 portailajout = ' , + '+BandeauxPortailsSpecialBOT_AddPortal[a]
                                          }else{
                                                 portailaction = 'Ajout';
                                                 portailajout = ' + '+BandeauxPortailsSpecialBOT_AddPortal[a]
                                          }
                                    } 
                              }
                              var Sommaire = 'Bot : '+portailaction+' automatique de {{portail}} :'+portailretrait+portailajout;
                              if(portailaction==''){ 
                                    BandeauxPortailsSpecialBOT_AddBandeau((position+1)); 
                                    return;
                              }
                        }else if(BandeauxPortailsSpecialBOT_RemovePortal.length>0){
                              var NouveauModele = OldModele;
                              for(var r=0;r<BandeauxPortailsSpecialBOT_RemovePortal.length;r++){
                                    if((NouveauModele.indexOf('|'+BandeauxPortailsSpecialBOT_RemovePortal[r]+ '|')!=-1)||(NouveauModele.indexOf('|'+BandeauxPortailsSpecialBOT_RemovePortal[r]+'}}')!=-1)){
                                          NouveauModele = NouveauModele.split('|' +BandeauxPortailsSpecialBOT_RemovePortal[r]+ '|').join('|');
                                          NouveauModele = NouveauModele.split('|' +BandeauxPortailsSpecialBOT_RemovePortal[r]+ '}}').join('}}');
                                          var Sommaire = 'Bot : Retrait automatique de {{portail}} : - '+BandeauxPortailsSpecialBOT_RemovePortal[r]; 
                                          if(NouveauModele.indexOf('|')==-1) NouveauModele = '';
                                    }
                              }
                              if(NouveauModele == OldModele){
                                    BandeauxPortailsSpecialBOT_AddBandeau((position+1))
                                    return;
                              }          
                        }else if(BandeauxPortailsSpecialBOT_AddPortal.length>0){
                              var NouveauModele = OldModele;
                              for(var a=0;a<BandeauxPortailsSpecialBOT_AddPortal.length;a++){
                                    if((NouveauModele.indexOf('|'+BandeauxPortailsSpecialBOT_AddPortal[a]+'|')==-1) && (NouveauModele.indexOf('|'+BandeauxPortailsSpecialBOT_AddPortal[a]+'}')==-1)){
                                          var NouveauModele = NouveauModele.split('}}').join('|' + BandeauxPortailsSpecialBOT_AddPortal[a] + '}}');
                                          var Sommaire = 'Bot : Ajout automatique de {{portail}} : + '+BandeauxPortailsSpecialBOT_AddPortal[a];
                                    }
                              }
                              if(NouveauModele == OldModele){
                                    BandeauxPortailsSpecialBOT_AddBandeau((position+1))
                                    return;
                              }  
                        }
                  }else{
                        var OldModele = '';
                        var NouveauModele = '{{Portail';
                        for(var a=0;a<BandeauxPortailsSpecialBOT_AddPortal.length;a++){
                              NouveauModele += '|' + BandeauxPortailsSpecialBOT_AddPortal[a];
                        }
                        NouveauModele += '}}\n\n';

                        var Sommaire = 'Bot : Ajout automatique de {{portail}} : + '
                        for(var a=0;a<BandeauxPortailsSpecialBOT_AddPortal.length;a++){
                              Sommaire += BandeauxPortailsSpecialBOT_AddPortal[a];   
                        }
                        if((OldText.indexOf('{{DEFAULTSORT:')!=-1)||(OldText.indexOf('[[Catégorie:')!=-1)||(OldText.indexOf('[[catégorie:')!=-1)||(OldText.indexOf('[[Categorie:')!=-1)||(OldText.indexOf('[[categorie:')!=-1)){
                              var DebutModele1 = OldText.substring(0, OldText.indexOf('{{DEFAULTSORT:'));
                              var DebutModele2 = OldText.substring(0, OldText.indexOf('[[Catégorie:'));
                              var DebutModele3 = OldText.substring(0, OldText.indexOf('[[catégorie:'));
                              var DebutModele4 = OldText.substring(0, OldText.indexOf('[[Categorie:'));
                              var DebutModele5 = OldText.substring(0, OldText.indexOf('[[categorie:'));    
                              var SuiteModele = OldText.split(DebutModele1).join('').split(DebutModele2).join('').split(DebutModele3).join('').split(DebutModele4).join('').split(DebutModele5).join('')      
                              var DebutModele = OldText.split(SuiteModele).join('');
                        }else{
                              var DebutModele = OldText + '\n';
                              var SuiteModele = '';
                        }
                  }
                  var TexteFinal = DebutModele + NouveauModele + SuiteModele; 
                  var SommaireFinal = Sommaire + ' ; ([[Utilisateur:Dr Brains/BandeauxPortailsSpecialBOT.js|script]])'
                  document.getElementById('wpTextbox1').value = TexteFinal;
                  document.getElementById('wpSummary').value = SommaireFinal;
                  document.getElementById('wpMinoredit').checked = "checked";
                  //document.editform.submit();

                  if(!BandeauxPortailsSpecialBOT_AutoSave){
                        var SurEtCertain = confirm(OldModele + ' -> ' + NouveauModele + '\n\nSommaire : ' + Sommaire);
                        if(!SurEtCertain){
                              var SurEtCertainBis = confirm('Page suivante ?');
                              if(!SurEtCertainBis){
                                      window.location.reload();
                              }else{
                                      BandeauxPortailsSpecialBOT_AddBandeau((position+1));
                                      return;
                              }
                        }
                  }

                  var action = document.getElementById("editform").action;
                  var boundary = '--------123xyz';
                  var data = '';
                  data += '--' + boundary + '\nContent-Disposition: form-data; name="wpTextbox1"\n\n' + TexteFinal + '\n'
                  data += '--' + boundary + '\nContent-Disposition: form-data; name="wpSection"\n\n' + document.editform.wpSection.value + '\n'
                  data += '--' + boundary + '\nContent-Disposition: form-data; name="wpSummary"\n\n' + SommaireFinal + '\n'
                  data += '--' + boundary + '\nContent-Disposition: form-data; name="wpMinoredit"\n\n' + document.editform.wpMinoredit.value + '\n'
                  data += '--' + boundary + '\nContent-Disposition: form-data; name="wpStarttime"\n\n' + document.editform.wpStarttime.value + '\n'
                  data += '--' + boundary + '\nContent-Disposition: form-data; name="wpEdittime"\n\n' + document.editform.wpEdittime.value + '\n'
                  data += '--' + boundary + '\nContent-Disposition: form-data; name="wpEditToken"\n\n' + document.editform.wpEditToken.value + '\n'
                  var ajaxSubmit = new XMLHttpRequest();
                  ajaxSubmit.open('POST', action, true);
                  ajaxSubmit.setRequestHeader('Content-Type', 'multipart/form-data; boundary='+boundary);
                  ajaxSubmit.send(data + '--' + boundary);
                  ajaxSubmit.onreadystatechange = function(){
                        if (ajaxSubmit.readyState == 4){
                              while(document.body.firstChild){document.body.removeChild(document.body.firstChild);}
                              var ElementTraitementBis = document.createElement('div');
                              document.body.appendChild(ElementTraitementBis);
                              ElementTraitementBis.innerHTML = ajaxSubmit.responseText; 
                              if(!document.getElementById("editform")){
                                    BandeauxPortailsSpecialBOT_AddBandeau((position+1));
                              }else{
                                    var DeuxiemeEssai = confirm('Problème !\n\nRéessayer ?');
                                    if(DeuxiemeEssai) BandeauxPortailsSpecialBOT_AddBandeau(position);
                              }
                        }
                  }
            }
      }
}

// Fonction : décodage HTLML
function BandeauxPortailsSpecialBOT_HTMLDecode(text){
      var EncodedCharacter = new Array();
      var DecodedCharacter = new Array();
      var CharacterCount = 0;

      EncodedCharacter[CharacterCount] = "&amp;";
      DecodedCharacter[CharacterCount] = "&";
      CharacterCount++;
      EncodedCharacter[CharacterCount] = "&#039;";
      DecodedCharacter[CharacterCount] = "'";
      CharacterCount++;
      EncodedCharacter[CharacterCount] = "&quot;";
      DecodedCharacter[CharacterCount] = '"';
      CharacterCount++;

      for(var a=0;a<EncodedCharacter.length;a++){
            while(text.indexOf(EncodedCharacter[a])!=-1){
                  text = text.split(EncodedCharacter[a]).join(DecodedCharacter[a]);
                  if(text.indexOf(EncodedCharacter[a])==-1) break;
            }
      }
      return text;
}

// Fonction : nettoyage du modèle {{Portail}}
function BandeauxPortailsSpecialBOT_CleanModele(Modele){
      Modele = Modele.replace(/\n/g,"");
      Modele = Modele.replace(/\| /g,"\|");
      Modele = Modele.replace(/ \|/g,"\|");
      Modele = Modele.replace(/\|\}/g,"\}");
      Modele = Modele.replace(/\|\|/g,"\|");
      return Modele;
}

////////////////////////// </nowiki></pre></source>