Jump to content

User:Dr Brains/ListPages.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <nowiki>
// Licence : Domaine public

var ListPages_Category = wgPageName.replace(/_/g, " ");
var ListPages_Page = wgFormattedNamespaces[2]+':'+wgUserName+ '/Test';

var ListPages_Lang = "fr";

var ControlElement;

if(wgNamespaceNumber==14){ 
     addOnloadHook(ListPages_AddLink);
}

function ListPages_AddLink(){
     var OngletsCactions = document.getElementById('p-cactions').getElementsByTagName('ul')[0];
     OngletsCactions.innerHTML += '<li><a href="javascript:ListPages_GetOptions();">Lister</a></li>';
}


function ListPages_GetOptions(){
     var Category = confirm('Catégorie : '+ListPages_Category);
     var Page = confirm('Page où copier le résultat : '+ListPages_Page);
     if((!Category)||(!Page)){
          return;
     }else{
          ControlElement = document.createElement('div');
          var Titre = document.getElementsByTagName('h1')[0];
          Titre.parentNode.insertBefore(ControlElement, Titre.nextSibling);
          ListPages_Run();
     }
}


if( (wgUserGroups.indexOf("sysop")!=-1)||(wgUserGroups.indexOf("bot")!=-1)){
   var ListPages_APILimit = 4999;
}else{
   var ListPages_APILimit = 499;
}

var ListPages_PageInCategory = new Array();
var ListPages_PageInCategoryCount = 0;

var ListPages_CategoryInCategory = new Array();
var ListPages_CategoryInCategoryCount = 0;
 
var ListPages_IndexCategory = -1;

function ListPages_Run(category, categorycontinue){
     if(!category) category = wgPageName;    
     if(!categorycontinue) categorycontinue = '';
      var NouvelleRequete = sajax_init_object()
      var AdresseRequete = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=xml&action=query&list=categorymembers&cmlimit='+ListPages_APILimit+'&cmtitle=' + category + '&cmcontinue=' + categorycontinue;
      ControlElement.innerHTML = "Listing catégorie : "+category;
      NouvelleRequete.open("GET", AdresseRequete, true);
      NouvelleRequete.onreadystatechange = function() {
            if(NouvelleRequete.readyState != 4 ) return;
            var ObjetXML = NouvelleRequete.responseXML;
            if(ObjetXML==null){ alert("Problème : \n"+AdresseRequete); return; }
            var AllCats = ObjetXML.getElementsByTagName('cm');
            for(var a=0;a<AllCats.length;a++){
                  var Ns = AllCats[a].getAttribute('ns');
                  var Title = AllCats[a].getAttribute('title');
                  if((Ns=="0")&&(Ns=="1")&&(Ns=="14")) continue;
                  if(Ns=="14"){
                        ListPages_CategoryInCategory[ListPages_CategoryInCategoryCount] = Title;
                        ListPages_CategoryInCategoryCount++
                  }else{
                        Title = Title.split(wgFormattedNamespaces[1] + ':').join("");
                        ListPages_PageInCategory[ListPages_PageInCategoryCount] = Title;
                        ListPages_PageInCategoryCount++
                  }
            }
            var Autrerequete = ObjetXML.getElementsByTagName('query-continue')[0];
            if(Autrerequete){
                  var AutreRequeteContinue = Autrerequete.getElementsByTagName('categorymembers')[0].getAttribute('cmcontinue');
                  ListPages_Run(category, AutreRequeteContinue);
            }else{
                  ListPages_IndexCategory++
                  var NewCategory = ListPages_CategoryInCategory[ListPages_IndexCategory];
                  if(NewCategory){
                        ListPages_Run(NewCategory);
                  }else{
                        ListPages_SortList();
                  }
            }
      }
      NouvelleRequete.send(null);
}


var ListPages_SortedPageInCategory = new Array();
var ListPages_SortedPageInCategoryCount = 0;

function ListPages_SortList(){
      ControlElement.innerHTML = "Triage";
      var PagesTemp = ListPages_PageInCategory.sort();
      for(var a=0;a<PagesTemp.length;a++){
           if(PagesTemp[a]!=PagesTemp[(a+1)]){
                 ListPages_SortedPageInCategory[ListPages_SortedPageInCategoryCount] = PagesTemp[a];
                 ListPages_SortedPageInCategoryCount++
           }
      }
      ListPages_GetInterwiki(0);      
}

var ListPages_InterWiki = new Array();

function ListPages_GetInterwiki(position){
      var ThisPage = ListPages_SortedPageInCategory[position];
      if(!ThisPage){
            ListPages_Edit(); 
            return; 
      }
      var NouvelleRequete = sajax_init_object()
      var AdresseRequete = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=xml&action=query&prop=langlinks&lllimit='+ListPages_APILimit+'&titles='+ThisPage+'&redirects';
      ControlElement.innerHTML = "Recherche interwiki : "+ThisPage;
      NouvelleRequete.open("GET", AdresseRequete, true);
      NouvelleRequete.onreadystatechange = function() {
            if(NouvelleRequete.readyState == 4) {
                  var ObjetXML = NouvelleRequete.responseXML; 
                  if(!ObjetXML){
                        alert('La requête API a échoué :\n'+AdresseRequete);
                        ListPages_GetInterwiki((position+1));
                        return;
                  }
                  var TheLangLink = false;
                  var LangLinks = ObjetXML.getElementsByTagName('ll');
                  for(var a=0;a<LangLinks.length;a++){
                        var ThisLang = LangLinks[a].getAttribute("lang");
                        if(ThisLang == ListPages_Lang){
                              TheLangLink = LangLinks[a].firstChild.nodeValue;
                        }
                  }
                  ListPages_InterWiki.push(TheLangLink);
                  ListPages_GetInterwiki((position+1));
            }
      }
      NouvelleRequete.send(null);
}

function ListPages_Edit(){
      ControlElement.innerHTML = "Édition";
      var url = mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + ListPages_Page.replace(/&/g, "%26") + '&action=edit';
      var NouvelleRequete = sajax_init_object();      
      NouvelleRequete.open("GET", url, true);
      NouvelleRequete.onreadystatechange = function() {
            if(NouvelleRequete.readyState != 4) return;
            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 TexteFinal = "{| class=\"wikitable\"\n! #\n! Page "+wgContentLanguage+"\n! Page fr\n"; 

            for(var a=0;a<ListPages_SortedPageInCategory.length;a++){
                  var LocalPage = "[[:"+wgContentLanguage+":" + ListPages_SortedPageInCategory[a] + "|]]";
                  var Interwiki = ( ListPages_InterWiki[a] ? "[[:"+ListPages_Lang+":" + ListPages_InterWiki[a] + "|]]" : "" );
                  TexteFinal += "|--------------------------------------------------------\n"
                              + "| "+(a+1)+"\n| "+LocalPage+"\n| "+Interwiki+ "\n";
            }
            TexteFinal += "|}";

            var SommaireFinal = "Listing "+ ListPages_Category + " "+wgContentLanguage+"/fr" ;
            document.getElementById('wpTextbox1').value = TexteFinal;
            document.getElementById('wpSummary').value = SommaireFinal;
            document.getElementById('wpMinoredit').checked = "checked";

            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 = sajax_init_object();
            ajaxSubmit.open('POST', action, true);
            ajaxSubmit.setRequestHeader('Content-Type', 'multipart/form-data; boundary='+boundary);
            ajaxSubmit.onreadystatechange = function(){
                  if (ajaxSubmit.readyState != 4) return;
                  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")){
                        document.location = mw.config.get('wgServer') + mw.config.get('wgArticlePath').split('$1').join(ListPages_Page);
                  }else{
                        var DeuxiemeEssai = confirm('Problème !\n\nRéessayer ?');
                        if(DeuxiemeEssai) ListPages_Edit();
                  }
            }
            ajaxSubmit.send(data + '--' + boundary);
      }
      NouvelleRequete.send(null);
}

// </nowiki>