User:OldBee/CorrectPaletteRedirects.js
Appearance
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)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
window.CorrectPaletteRedirects = {
"p" : {
linktext : "Corriger redirections",
linktitle : "Corriger les redirections de cette palette",
resume : "Correction de redirection(s) dans une palette",
box : "p-tb",
},
"linkstocorrect" : [],
"makeGroupsFromArray" : function(arr, grouplength, modfunc){
if(typeof(arr)!="object") return false;
if(typeof(grouplength)!="number") return false;
var ARR = JSON.parse(JSON.stringify( arr ));
var groupedarray = [];
while(ARR.length > 0){
var group = [];
for(var x=0,xlen=grouplength;x<xlen;x++){
var item = ARR.shift();
if(typeof(item)==="undefined") continue;
if(modfunc && typeof(modfunc)==="function") item = modfunc(item);
group.push(item);
}
groupedarray.push(group);
delete group;
if(ARR.length === 0) break;
}
return groupedarray;
},
"correct" : function(targets){
var URL = mw.util.getUrl(mw.config.get('wgPageName'),{'action':'edit'});
$.ajax({url:URL, complete: function(Req){
var EditForm = null;
var Target = document.getElementById("mw-content-text");
var TEMP = document.createElement("div");
TEMP.style.display = "none";
TEMP.innerHTML = Req.responseText;
document.body.appendChild(TEMP);
var Forms = TEMP.getElementsByTagName('form');
for(var a=0,l=Forms.length;a<l;a++){
if(Forms[a].id && Forms[a].id === "editform") EditForm = Forms[a];
}
if(EditForm) Target.insertBefore(EditForm, Target.firstChild);
TEMP.parentNode.removeChild(TEMP);
var Textarea = document.getElementById("wpTextbox1");
var Wikitext = Textarea.value;
for(var redirect in targets){
var First = redirect.substr(0,1);
var Redirect = "["+First.toUpperCase()+First.toLowerCase()+"]"+redirect.substring(1,redirect.length);
var Reg = new RegExp("[^\\[]*"+Redirect+"[^\\|\\]]*", "g");
var M = Wikitext.match(Reg);
if(M !== null){
for(var m=0,mlen=M.length;m<mlen;m++){
Wikitext = Wikitext.replace(M[m], targets[redirect]);
}
}
}
Textarea.value = Wikitext;
document.getElementById("wpSummary").value = CorrectPaletteRedirects.p.resume;
$("#wpDiff").click();
}});
},
"findtargets" : function(){
var linkstocorrect = CorrectPaletteRedirects.makeGroupsFromArray(CorrectPaletteRedirects.linkstocorrect, 50);
var targets = {};
var Req = 0;
for(var a=0,l=linkstocorrect.length;a<l;a++){
var datas = {format:'json',action:'query',prop:'links',
pllimit : 500,
titles : linkstocorrect[a].join("|")
};
Req++;
$.ajax({url: mw.util.wikiScript('api'),data:datas,dataType:'json'}).then(function(data) {
Req--;
var pages = data.query.pages;
for(var id in pages){
var infos = pages[id];
var title = infos.title;
var links = infos.links;
if(links && links.length > 0){
var target = links[0].title;
targets[title] = target;
}
}
if(Req === 0) CorrectPaletteRedirects.correct(targets);
});
}
},
"launch" : function(){
var wgTitle = mw.config.get('wgTitle');
if(mw.config.get('wgNamespaceNumber') !== 10 || wgTitle.indexOf("Palette") !== 0 || wgTitle.indexOf("/Documentation") !== -1) return;
if(["edit","submit"].indexOf(mw.config.get('wgAction')) !== -1) return;
var Tables = document.getElementsByTagName("table");
for(var a=0,l=Tables.length;a<l;a++){
var Table = Tables[a];
if(!$(Table).hasClass("navbox")) continue;
var Links = Table.getElementsByTagName("a");
for(var i=0,ilen=Links.length;i<ilen;i++){
var Link = Links[i];
if(!$(Link).hasClass("mw-redirect")) continue;
CorrectPaletteRedirects.linkstocorrect.push(Link.title);
}
}
if(CorrectPaletteRedirects.linkstocorrect.length === 0) return;
var Target = document.getElementById(CorrectPaletteRedirects.p.box);
if(!Target) return;
var LI = Target.getElementsByTagName('li')[0];
var NewLI = LI.parentNode.appendChild(document.createElement('li'));
var NewLink = NewLI.appendChild(document.createElement('a'));
NewLink.title = CorrectPaletteRedirects.p.linktitle;
NewLink.appendChild(document.createTextNode(CorrectPaletteRedirects.p.linktext));
NewLink.href = "javascript:;";
NewLink.onclick = function(){ CorrectPaletteRedirects.findtargets(); }
}
}
mw.loader.using(['mediawiki.util', 'mediawiki.api'], function(){
$(CorrectPaletteRedirects.launch);
});