MediaWiki:Gadget-DeleteBot.js

From Test Wiki
Revision as of 12:22, 27 April 2024 by DodoMan (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* {{Catégorisation JS}}<nowiki> */
/*

Outil automatique pour supprimer plusieurs pages appartenant à la catégorie courante.

Nécessite les outils de sysop (delete).

* Documentation : [[Projet:JavaScript/Notices/DeleteBot]]


* Auteur : [[vikidia:fr:User:Dr Brains]]
* Licence : Domaine public

* {{Catégorisation JS|DeleteBot}}
*/


var DeleteBot_Text_DeleteAll = "Delete all pages";
var DeleteBot_Text_ConfirmDeleteThisPage = "Delete «&nbsp;$1&nbsp;» ?";
var DeleteBot_Text_Summary = "";
var DeleteBot_Text_OKInput = "Confirm";
var DeleteBot_Text_CancelInput = "Cancel";
var DeleteBot_Text_Legend = "Delete all pages of this category « $1 »";
var DeleteBot_Text_ListLegend = "List of pages";
var DeleteBot_Text_SummaryLegend = "Deletion summary";
var DeleteBot_Text_ButtonsLegend = "Delete";
var DeleteBot_Text_OtherReason = "Other reason";

var DeleteBot_Param_NoConfirm = false;

// --------------------------------------------------------------------------------------------
// Fonction de requête
 
var DeleteBot_ajax = {
  http:function(bundle){ 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){ DeleteBot_ajax.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){ try{ xmlhttp.setRequestHeader(field,bundle.headers[field]); }catch(err){}}}; 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{ }}
};
 
// --------------------------------------------------------------------------------------------
// Ajout du lien "Tout supprimer"
 
function DeleteBot_AddLink(){
     if(typeof(DeleteBot_LangCustom)=="function") DeleteBot_LangCustom();
     if(typeof(DeleteBot_SiteCustom)=="function") DeleteBot_SiteCustom();
     if(typeof(DeleteBot_UserCustom)=="function") DeleteBot_UserCustom();
     var OngletsCactions = document.getElementById('p-cactions');
     if(OngletsCactions){
          var CactionsUl = OngletsCactions.getElementsByTagName('ul')[0];
          CactionsUl.innerHTML += '<li><a href="javascript:window.DeleteBot_ListPages();">'+DeleteBot_Text_DeleteAll+'</a></li>';
          $(OngletsCactions).removeClass("emptyPortlet");
     }
}
 
// --------------------------------------------------------------------------------------------
// Lancement
 
if(mw.config.get('wgNamespaceNumber')==14){
     $(DeleteBot_AddLink);
}

// --------------------------------------------------------------------------------------------
// Listage des pages de la catégorie

window.DeleteBot_ListPages = function(CatArray, category, categorycontinue){
     if(!CatArray) CatArray = new Array();
     if(!category) category = mw.config.get('wgPageName');    
     if(!categorycontinue) categorycontinue = '';
     var URL = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=xml&action=query&rawcontinue=&list=categorymembers'
             + '&cmlimit=4999'
             + '&cmtitle=' + encodeURIComponent(category) 
             + categorycontinue;
 
     DeleteBot_ajax.http({ 
          url: URL,
          cat: category,
          catlist: CatArray,
          onSuccess:DeleteBot_ListPagesDone
     });
}

function DeleteBot_ListPagesDone(Req, data){    
     var CatArray = data.catlist;
     var category = data.cat;
     var ElementTraitement = Req.responseXML; 
     var Pages = ElementTraitement.getElementsByTagName('cm');
     for(a=0;a<Pages.length;a++){
          var TitrePage = Pages[a].getAttribute('title');
          if(CatArray.indexOf(TitrePage)==-1) CatArray.push(TitrePage);
     }
     var CatContinue = ElementTraitement.getElementsByTagName('query-continue')[0];
     if(CatContinue){
          var AutreRequeteContinue = '&cmcontinue=' + encodeURIComponent(CatContinue.firstChild.getAttribute("cmcontinue"));
          DeleteBot_ListPages(CatArray, category, AutreRequeteContinue);
     }else{
          DeleteBot_CreateMenu(CatArray);
     }
}

// --------------------------------------------------------------------------------------------
// Création du formulaire de suppression

function DeleteBot_CreateMenu(CatArray){
     while(document.body.firstChild){ document.body.removeChild(document.body.firstChild); }
     var div = document.createElement('fieldset');
     div.setAttribute('style', "margin:2em;");
     document.body.appendChild(div);
     var Legend = document.createElement('legend');
     Legend.appendChild(document.createTextNode(DeleteBot_Text_Legend.split("$1").join(mw.config.get('wgTitle'))));
     div.appendChild(Legend);
     var ListFieldset = document.createElement('fieldset');
     div.appendChild(ListFieldset);
     var ListLegend = document.createElement('legend');
     ListLegend.appendChild(document.createTextNode(DeleteBot_Text_ListLegend));
     ListFieldset.appendChild(ListLegend);
     var Form = document.createElement('form');
     Form.id = "DeleteBot_Form";
     ListFieldset.appendChild(Form);
     var UL = document.createElement('ul');
     Form.appendChild(UL);
     UL.setAttribute('style', "-moz-column-count:3;-webkit-column-count:3;column-count:3;");
     for(var a=0,l=CatArray.length;a<l;a++){
          var Page = CatArray[a];
          var LI = document.createElement('li');
          var Input = document.createElement('input');
          Input.id = "DeleteBot_Input"+a;
          Input.type = "checkbox";
          Input.value = Page;
          Input.checked = "checked"; 
          Input.onclick = DeleteBot_UpdateOKInput;
          var Link = document.createElement('a');
          Link.title = Page;  
          Link.appendChild(document.createTextNode(Page));  
          Link.href = mw.config.get('wgServer') + mw.config.get('wgArticlePath').split("$1").join(Page);
          var Label = document.createElement('label');
          Label.setAttribute('for', "DeleteBot_Input"+a);
          Label.appendChild(Link);          
          LI.appendChild(Input);
          LI.appendChild(document.createTextNode(" "));
          LI.appendChild(Label);
          UL.appendChild(LI);
     }
     var SummaryFieldset = document.createElement('fieldset');
     div.appendChild(SummaryFieldset);
     var SummaryLegend = document.createElement('legend');
     SummaryLegend.appendChild(document.createTextNode(DeleteBot_Text_SummaryLegend));
     SummaryFieldset.appendChild(SummaryLegend);
     var SummarySelect = document.createElement('select');
     SummarySelect.id = "DeleteBot_SummarySelect";
     SummaryFieldset.appendChild(SummarySelect);
     var FirstOption = document.createElement('option');
     FirstOption.value = "";
     FirstOption.appendChild(document.createTextNode(DeleteBot_Text_OtherReason));
     SummarySelect.appendChild(FirstOption); 
     var Textarea = document.createElement('textarea');
     Textarea.id = "DeleteBot_Summary";
     Textarea.value = ""
     Textarea.onchange = DeleteBot_UpdateOKInput;
     SummaryFieldset.appendChild(Textarea);
     var ButtonsFieldset = document.createElement('fieldset');
     div.appendChild(ButtonsFieldset);
     var ButtonsLegend = document.createElement('legend');
     ButtonsLegend.appendChild(document.createTextNode(DeleteBot_Text_ButtonsLegend));
     ButtonsFieldset.appendChild(ButtonsLegend);
     var PButtons = document.createElement('p');
     PButtons.setAttribute('style', "text-align:center;");
     var OKInput = document.createElement('input');
     OKInput.type = "button";
     OKInput.id = "DeleteBot_OKInput";
     OKInput.value = DeleteBot_Text_OKInput;
     OKInput.disabled = "disabled";
     OKInput.onclick = DeleteBot_CheckMenu;
     OKInput.onselect = DeleteBot_CheckMenu;      
     PButtons.appendChild(OKInput);
     var CancelInput = document.createElement('input');
     CancelInput.type = "button";
     CancelInput.value = DeleteBot_Text_CancelInput;
     CancelInput.onclick = function(){ document.location = document.URL; };
     CancelInput.onselect = function(){ document.location = document.URL; };       
     PButtons.appendChild(CancelInput);
     ButtonsFieldset.appendChild(PButtons);
     DeleteBot_UpdateSummarySelect();
}

function DeleteBot_UpdateSummarySelect(){
     var SummarySelect = document.getElementById("DeleteBot_SummarySelect");
     if(!SummarySelect) return;
     var URL = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=xml&action=query&meta=allmessages'
             + '&ammessages=deletereason-dropdown';
     DeleteBot_ajax.http({ url: URL,
                  onSuccess: DeleteBot_UpdateSummarySelectProcess
               });
}

function DeleteBot_UpdateSummarySelectProcess(Req, data){
     var api = Req.responseXML;
     if (api.firstChild.nodeName == "error") return;
     var DropDown = "";
     var messages = api.getElementsByTagName('message');
     for(var a=0,l=messages.length;a<l;a++){
          var MessageName = messages[a].getAttribute('name');
          var MessageValue = (messages[a].firstChild ? messages[a].firstChild.nodeValue : "");
          if(MessageName == "deletereason-dropdown") DropDown = MessageValue;
     }
     if(!DropDown) return;
     var SummarySelect = document.getElementById("DeleteBot_SummarySelect");
     if(!SummarySelect) return;
     var Drop = DropDown.split("**");
     var OptGroup = false;
     for(var a=0,l=Drop.length;a<l;a++){
          var ThisReason = Drop[a];
          if(ThisReason.indexOf("*")!=-1){
               if(ThisReason.split('*')[0]){
                    var Option = document.createElement('option');
                    Option.value = ThisReason.split('*')[0];
                    Option.appendChild(document.createTextNode(ThisReason.split('*')[0]));
                    if(OptGroup) OptGroup.appendChild(Option);
                    else SummarySelect.appendChild(Option);
                    Option.onclick = function(){ DeleteBot_UpdateSummary(this); };
               }
               if(OptGroup) SummarySelect.appendChild(OptGroup); 
               OptGroup = document.createElement('optgroup');
               OptGroup.setAttribute('label', ThisReason.split('*')[1]);
          }else{
               var Option = document.createElement('option');
               Option.value = ThisReason;
               Option.appendChild(document.createTextNode(ThisReason));
               OptGroup.appendChild(Option);
               Option.onclick = function(){ DeleteBot_UpdateSummary(this); };
          }
     }
     if(OptGroup) SummarySelect.appendChild(OptGroup);  
}

function DeleteBot_UpdateSummary(option){
     var Summary = document.getElementById("DeleteBot_Summary");
     if(!Summary || !option) return;
     if(option.value) Summary.value = option.value;
     DeleteBot_UpdateOKInput();
}

function DeleteBot_UpdateOKInput(){
     var Form = document.getElementById("DeleteBot_Form");
     var Summary = document.getElementById("DeleteBot_Summary");
     var OKInput = document.getElementById("DeleteBot_OKInput");
     if(!Form|| !Summary || !OKInput) return;
     var InputChecked = false;
     var CatArray = new Array();
     var Inputs = Form.getElementsByTagName('input');
     for(var a=0,l=Inputs.length;a<l;a++){
          if(InputChecked) continue;
          var Input = Inputs[a];
          if(Input.checked) InputChecked = true;
     }
     var Value = Summary.value;
     if(!Value || !InputChecked){
          OKInput.disabled = "disabled";
     }else{
          OKInput.disabled = false;
     }
}

// --------------------------------------------------------------------------------------------
// Validation du formulaire de suppression

function DeleteBot_CheckMenu(){
     var Form = document.getElementById("DeleteBot_Form");
     var Summary = document.getElementById("DeleteBot_Summary");
     if(!Summary || !Form) return;
     var CatArray = new Array();
     var Inputs = Form.getElementsByTagName('input');
     for(var a=0,l=Inputs.length;a<l;a++){
          var Input = Inputs[a];
          if(Input.checked) CatArray.push(Input.value);
     }
     DeleteBot_Text_Summary = Summary.value;
     DeleteBot_DeletePage(CatArray, 0);
}

// --------------------------------------------------------------------------------------------
// Suppression d'une page

function DeleteBot_DeletePage(CatArray, position){
     var Page = CatArray[position];
     if(!Page){
          document.location = document.URL; 
          return;
     }
     var SurEtCertain = true;
     if(!DeleteBot_Param_NoConfirm) SurEtCertain = confirm(DeleteBot_Text_ConfirmDeleteThisPage.split("$1").join(Page));
     if(!SurEtCertain){
          DeleteBot_DeletePage(CatArray, (position+1));
          return;
     }
     var URL = mw.config.get('wgServer') + mw.config.get('wgScript')
             + '?title=' + encodeURIComponent(Page)
             + '&action=delete'; 
     DeleteBot_ajax.http({ 
          url       : URL,
          catlist   : CatArray,
          pos       : position,
          page      : Page,
          onSuccess : DeleteBot_DeletePageProcess
     });
}

function DeleteBot_DeletePageProcess(Req, data){
     var CatArray = data.catlist;
     var position = data.pos;
     var Page = data.page;
     while(document.body.firstChild){ document.body.removeChild(document.body.firstChild); }
     document.body.innerHTML = Req.responseText;
     var Form = document.getElementById("deleteconfirm");
     if(!Form) Form = document.getElementById("mw-img-deleteconfirm");
     var Summary = document.getElementById("wpReason");
     if(!Form || !Summary){
          DeleteBot_DeletePage(CatArray, (position+1));
          return;
     }
     Summary.value = DeleteBot_Text_Summary;
     var Action = Form.action;
     var Params = DeleteBot_getFormParams(Form);
     var ParamsInURL = new Array();
     for(var P in Params){
         if(Params.hasOwnProperty(P)) ParamsInURL.push(P+"="+encodeURIComponent(Params[P]));
     }
     var headers = new Array();
     headers['Content-Type'] = 'application/x-www-form-urlencoded';
     DeleteBot_ajax.http({ url: Action,
                           method: "POST",
                           headers: headers,
                           data: ParamsInURL.join("&"),
                           onSuccess:function(){ DeleteBot_DeletePage(CatArray, (position+1)) },
     });     
}

function DeleteBot_getFormParams(Form){
     var Params = new Array();
     var Tags = new Array("textarea", "select", "input");
     for(var a=0,l=Tags.length;a<l;a++){
          var Elements = Form.getElementsByTagName(Tags[a]);
          for(var b=0,m=Elements.length;b<m;b++){
               var Element = Elements[b];
               var ElName = Element.name;
               if(!ElName) continue;
               var ElValue = Element.value;
               var ElType = Element.type;
               if(Tags[a].toLowerCase()=='input' && (ElType == "checkbox" || ElType == "radio") && Element.checked){
                    Params[ElName] = ElValue;
               }else if(Tags[a].toLowerCase()=='input' && (ElType == "text" || ElType == "hidden") ){
                    Params[ElName] = ElValue;
               }else if(Tags[a].toLowerCase()!='input'){
                    Params[ElName] = ElValue;
               }
          }
     }
     return Params;
}
/* </nowiki> */