MediaWiki:Gadget-CategoryIntersection.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) ;
Firefox (sur GNU/Linux) / Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5.CategoryIntersection = {};
CategoryIntersection.texts = {};
CategoryIntersection.texts["Title"] = "Intersection de catégories";
CategoryIntersection.texts["OptionsLegend"] = "Options pour l'intersection de catégories";
CategoryIntersection.texts["OptionsCat1Label"] = "Catégorie 1";
CategoryIntersection.texts["OptionsCat2Label"] = "Catégorie 2";
CategoryIntersection.texts["ResultLegend"] = "Résultat de l'intersection de catégories";
CategoryIntersection.texts["OKInput"] = "Valider";
CategoryIntersection.texts["DetailLink"] = "Détails";
CategoryIntersection.texts["DetailLinkTitle"] = "Afficher/Masquer les catégories de cette page";
CategoryIntersection.texts["NoResults"] = "Aucun résultat pour cette recherche";
CategoryIntersection.params = {};
CategoryIntersection.params.AutoSearch = false ;
CategoryIntersection.params.APILimit = 499;
CategoryIntersection.params.Cat = new Array();
CategoryIntersection.params.Cat["1"] = '';
CategoryIntersection.params.Cat["2"] = '';
CategoryIntersection.params.CatTest = new Array();
CategoryIntersection.params.CatTest["1"] = 'Portail:Colombie/Articles liés';
CategoryIntersection.params.CatTest["2"] = 'Article de qualité en espagnol';
CategoryIntersection.params.CatList = new Array();
CategoryIntersection.params.CatOK = new Array();
CategoryIntersection.functions = {};
CategoryIntersection.functions.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){ CategoryIntersection.functions.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{ }}
};
CategoryIntersection.functions.Init = function(){
var Title = document.getElementsByTagName('title')[0];
Title.innerHTML = mw.config.get('wgTitle');
var TitlePage = document.getElementsByTagName('h1')[0];
TitlePage.innerHTML = CategoryIntersection.texts["Title"];
var Content = document.getElementById('bodyContent'); // monobook, chick, myskin, simple, vector
if(!Content) Content = document.getElementById('mw_contentholder'); // modern
if(!Content) Content = document.getElementById('article'); // cologneblue, nostalgia, standard
if(!Content) return;
while(Content.firstChild){ Content.removeChild(Content.firstChild); }
var ContentHTML = '<div id="CategoryIntersection_Div"></div>';
Content.innerHTML = ContentHTML;
CategoryIntersection.functions.CreateLink(Content);
CategoryIntersection.functions.GetURLParams();
CategoryIntersection.functions.CreateForm();
}
CategoryIntersection.functions.CreateLink = function(Content){
var P = document.createElement('p');
var URL = mw.config.get('wgServer') + mw.config.get('wgArticlePath').split("$1").join(mw.config.get('wgFormattedNamespaces')[-1]+':CategoryIntersection')
+ '?cat1=' + CategoryIntersection.params.CatTest["1"]
+ '&cat2=' + CategoryIntersection.params.CatTest["2"]
var Link = '<a href="'+URL+'">Test</a>';
P.innerHTML = Link;
Content.insertBefore(P, Content.firstChild);
}
CategoryIntersection.functions.GetURLParams = function(){
var URL = document.URL;
try{ URL=decodeURIComponent(URL); }catch(e){ };
var Params = URL.indexOf("?");
if(Params==-1) return;
URL = URL.replace(/.*\?/, "");
URL = URL.replace(/(\+|_)/, " ");
URL = URL.split("&");
for(var a=0,l=URL.length;a<l;a++){
var ParamName = URL[a].split('=')[0];
var ParamValue = URL[a].split('=')[1];
if(ParamName=="cat1") CategoryIntersection.params.Cat["1"] = ParamValue;
if(ParamName=="cat2") CategoryIntersection.params.Cat["2"] = ParamValue;
}
if(CategoryIntersection.params.Cat["1"] && CategoryIntersection.params.Cat["2"]){
CategoryIntersection.params.AutoSearch = true;
}
}
CategoryIntersection.functions.CreateForm = function(){
var GlobalDiv = document.getElementById('CategoryIntersection_Div');
if(!GlobalDiv) return;
while(GlobalDiv.firstChild){ GlobalDiv.removeChild(GlobalDiv.firstChild); }
var NewForm = document.createElement('form');
var FormContent = '<fieldset><legend>'+CategoryIntersection.texts["OptionsLegend"]+'</legend>'
+ '<span id="Span_Cat_1" style="border:1px solid silver;padding:0.3em;">'
+ '<input id="Input_Cat_1" type="text" size="30" value="'+CategoryIntersection.params.Cat["1"]+'">'
+ '<label for="Input_Cat_1" style="margin-left:0.3em;" '
+ '>'+CategoryIntersection.texts["OptionsCat1Label"]+'</label>'
+ '</span>'
+ ' '
+ '<span id="Span_Cat_2" style="border:1px solid silver;padding:0.3em;">'
+ '<input id="Input_Cat_2" type="text" size="30" value="'+CategoryIntersection.params.Cat["2"]+'">'
+ '<label for="Input_Cat_2" style="margin-left:0.3em;" '
+ '>'+CategoryIntersection.texts["OptionsCat2Label"]+'</label>'
+ '</span>'
+ ' '
+ '<input id="Input_OK" type="button" '
+ 'value="'+CategoryIntersection.texts["OKInput"]+'" '
+ 'onclick="CategoryIntersection.functions.CheckForm()" '
+ 'onselect="CategoryIntersection.functions.CheckForm()" />'
+ '</fieldset>';
NewForm.innerHTML = FormContent;
GlobalDiv.appendChild(NewForm);
var ResultFieldset = document.createElement('fieldset');
ResultFieldset.id = "ResultFieldset";
ResultFieldset.style.display = "none";
var TableContent = '<legend>'+CategoryIntersection.texts["ResultLegend"]+'</legend>'
+ '<div id="CatIntersectionResults"></div>';
ResultFieldset.innerHTML = TableContent;
GlobalDiv.appendChild(ResultFieldset);
Suggest_AddPageSuggestion({
"InputNode" : document.getElementById('Input_Cat_1'),
"NSFilter" : 14,
"StripNS" : true,
"SuggestLimit" : 30,
"SuggestListSize" : 5,
"AddExist" : false,
"ListDown" : false,
});
Suggest_AddPageSuggestion({
"InputNode" : document.getElementById('Input_Cat_2'),
"NSFilter" : 14,
"StripNS" : true,
"SuggestLimit" : 30,
"SuggestListSize" : 5,
"AddExist" : false,
"ListDown" : false,
});
if(CategoryIntersection.params.AutoSearch) CategoryIntersection.functions.CheckForm();
}
CategoryIntersection.functions.CheckForm = function(){
var Div = document.getElementById("CatIntersectionResults");
if(!Div) return;
var InputCat1 = document.getElementById('Span_Cat_1');
var InputCat2 = document.getElementById('Span_Cat_2');
if((!InputCat1)||(!InputCat2)) return;
CategoryIntersection.params.Cat["1"] = InputCat1.getElementsByTagName('input')[0].value;
CategoryIntersection.params.Cat["2"] = InputCat2.getElementsByTagName('input')[0].value;
if((CategoryIntersection.params.Cat["1"]=='')||(CategoryIntersection.params.Cat["2"]=='')) return;
while(Div.firstChild){ Div.removeChild(Div.firstChild); }
CategoryIntersection.functions.GetPages("1");
CategoryIntersection.functions.GetPages("2");
}
CategoryIntersection.functions.GetPages = function(CategoryIndex, CatList, CatContinue){
if(!CatList) CatList = new Array();
if(!CatContinue) CatContinue = '';
var URL = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=xml&action=query&rawcontinue=&list=categorymembers'
+ '&cmlimit='+CategoryIntersection.params.APILimit
+ '&cmtitle=' + mw.config.get('wgFormattedNamespaces')[14] + ':' + CategoryIntersection.params.Cat[CategoryIndex]
+ CatContinue ;
CategoryIntersection.functions.Ajax.http({url : URL,
onSuccess: CategoryIntersection.functions.GetPagesDone,
catlist : CatList,
catindex : CategoryIndex
});
}
CategoryIntersection.functions.GetPagesDone = function(Req, data){
var CatArray = data.catlist;
var CategoryIndex = data.catindex;
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"));
CategoryIntersection.functions.GetPages(CategoryIndex, CatArray, AutreRequeteContinue);
}else{
CategoryIntersection.params.CatList[CategoryIndex] = CatArray;
CategoryIntersection.params.CatOK[CategoryIndex] = true;
if(CategoryIntersection.params.CatOK["1"] == true && CategoryIntersection.params.CatOK["2"] == true)
CategoryIntersection.functions.GetIntersections();
}
}
CategoryIntersection.functions.GetIntersections = function(){
var Intersections = new Array();
var Cat1List = CategoryIntersection.params.CatList["1"];
var Cat2List = CategoryIntersection.params.CatList["2"];
for(var a=0;a<Cat1List.length;a++){
var ThisPageofCat1 = Cat1List[a];
for(var b=0;b<Cat2List.length;b++){
var ThisPageofCat2 = Cat2List[b];
if(ThisPageofCat1==ThisPageofCat2){
Intersections.push(ThisPageofCat2);
}
}
}
CategoryIntersection.params.CatIntersection = Intersections;
CategoryIntersection.functions.ShowIntersections();
}
CategoryIntersection.functions.ShowIntersections = function(){
var ResultFieldset = document.getElementById("ResultFieldset");
var Div = document.getElementById("CatIntersectionResults");
if(!ResultFieldset || !Div) return;
while(Div.firstChild){ Div.removeChild(Div.firstChild); }
ResultFieldset.style.display = "";
var Intersections = CategoryIntersection.params.CatIntersection;
if(Intersections.length==0){
var P = document.createElement('p');
P.className = "error";
P.setAttribute("style", "text-align:center;");
P.innerHTML = CategoryIntersection.texts["NoResults"];
Div.appendChild(P);
return;
}
var OL = document.createElement('ol');
Div.appendChild(OL);
for(var a=0,l=Intersections.length;a<l;a++){
var Page = Intersections[a];
var LI = document.createElement('li');
LI.id = "IntersectionLi_"+a;
LI.setAttribute('style', 'text-align:left');
var Link = document.createElement('a');
Link.href = mw.config.get('wgServer') + mw.config.get('wgArticlePath').split("$1").join(encodeURIComponent(Page));
Link.title = Page;
Link.innerHTML = Page;
LI.appendChild(Link);
OL.appendChild(LI);
CategoryIntersection.functions.GetPageCats(a);
}
}
CategoryIntersection.functions.GetPageCats = function(index, CatArray, ToContinue){
var Page = CategoryIntersection.params.CatIntersection[index];
var Li = document.getElementById("IntersectionLi_"+index);
if(!Page || !Li) return;
if(!CatArray){
CatArray = new Array();
CatArray.titles = new Array();
CatArray.hidden = new Array();
}
if(!ToContinue) ToContinue = "";
var URL = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=xml&action=query&rawcontinue=&prop=categories'
+ '&clprop=hidden'
+ '&cllimit='+CategoryIntersection.params.APILimit
+ '&titles=' + encodeURIComponent(Page)
+ ToContinue ;
CategoryIntersection.functions.Ajax.http({url : URL,
onSuccess : CategoryIntersection.functions.GetPageCatsDone,
pageindex : index,
catlist : CatArray
});
}
CategoryIntersection.functions.GetPageCatsDone = function(Req, data){
var CatArray = data.catlist;
var index = data.pageindex;
var Page = CategoryIntersection.params.CatIntersection[index];
var Li = document.getElementById("IntersectionLi_"+index);
if(!Page || !Li) return;
var ElementTraitement = Req.responseXML;
var Pages = ElementTraitement.getElementsByTagName('cl');
for(a=0;a<Pages.length;a++){
var TitrePage = Pages[a].getAttribute('title');
var Hidden = (Pages[a].getAttribute('hidden')!= null);
if(CatArray.titles.indexOf(TitrePage)==-1){
CatArray.titles.push(TitrePage);
CatArray.hidden.push(Hidden);
}
}
var CatContinue = ElementTraitement.getElementsByTagName('query-continue')[0];
if(CatContinue){
var AutreRequeteContinue = '&clcontinue=' + encodeURIComponent(CatContinue.firstChild.getAttribute("clcontinue"));
CategoryIntersection.functions.GetPageCats(index, CatArray, AutreRequeteContinue);
}else{
var LastDetail = document.getElementById("IntersectionLi_CatList_"+index);
if(LastDetail) return;
Li.appendChild(document.createTextNode(" — ("));
var DetailLink = document.createElement('a');
DetailLink.href = "javascript:CategoryIntersection.functions.ToggleDetail("+index+");";
DetailLink.title = CategoryIntersection.texts["DetailLinkTitle"];
DetailLink.innerHTML = CategoryIntersection.texts["DetailLink"];
Li.appendChild(DetailLink);
Li.appendChild(document.createTextNode(")"));
var Small = document.createElement('small');
Small.id = "IntersectionLi_CatList_"+index;
Small.appendChild(document.createElement("br"));
Small.appendChild(document.createTextNode(" — ("));
var SpanShown = document.createElement('span');
Small.appendChild(SpanShown);
Small.appendChild(document.createTextNode(")"));
Small.appendChild(document.createElement("br"));
Small.appendChild(document.createTextNode(" — ("));
var SpanHidden = document.createElement('span');
Small.appendChild(SpanHidden);
Small.appendChild(document.createTextNode(")"));
Li.appendChild(Small);
for(var a=0,l=CatArray.titles.length;a<l;a++){
var Cat = CatArray.titles[a];
var Hid = CatArray.hidden[a];
var CatName = Cat.split(mw.config.get('wgFormattedNamespaces')[14]+":").join('');
var Link = document.createElement('a');
Link.href = mw.config.get('wgServer') + mw.config.get('wgArticlePath').split("$1").join(encodeURIComponent(Cat));
Link.title = Cat;
Link.innerHTML = CatName;
var LinkAttribute = "padding:0.2em;white-space:nowrap;";
if(CatName==CategoryIntersection.params.Cat["1"]||CatName==CategoryIntersection.params.Cat["2"]){
LinkAttribute += 'border:1px dotted green;background-color:#BFFF80;font-weight:bold;';
}
Link.setAttribute('style', LinkAttribute);
if(Hid){
if(SpanHidden.firstChild) SpanHidden.appendChild(document.createTextNode(" | "));
SpanHidden.appendChild(Link);
}else{
if(SpanShown.firstChild) SpanShown.insertBefore(document.createTextNode(" | "), SpanShown.firstChild);
SpanShown.insertBefore(Link, SpanShown.firstChild);
}
}
CategoryIntersection.functions.ToggleDetail(index);
}
}
CategoryIntersection.functions.ToggleDetail = function(index){
var Small = document.getElementById("IntersectionLi_CatList_"+index);
if(!Small) return;
if(Small.style.display == "none"){
Small.style.display = "";
}else{
Small.style.display = "none";
}
}
if((mw.config.get('wgNamespaceNumber')==-1)&&(mw.config.get('wgTitle')=="CategoryIntersection")){
if((mw.config.get('wgUserGroups').indexOf("sysop")!=-1)||(mw.config.get('wgUserGroups').indexOf("bot")!=-1)){
CategoryIntersection.params.APILimit = 4999;
}
if(typeof(Suggest_AddPageSuggestion) != "function"){
mw.loader.implement( "CIModule", ['//fr.wikipedia.org/w/index.php?title=MediaWiki:Gadget-Suggest.js&action=raw&ctype=text/javascript'], {}, {}, {} );
mw.loader.using("CIModule", function(){ $(CategoryIntersection.functions.Init); });
}else{
$(CategoryIntersection.functions.Init);
}
}