User:OldBee/verifEbauche.js

From Meta, a Wikimedia project coordination wiki

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.
// {{Projet:JavaScript/Script|VerifEbauche}}
/* global document, alert, mw, confirm */

$(function(){
	mw.util.addPortletLink('p-tb', 'javascript:window.verifEbauche_init();', 'Vérif. ébauches', 't-ebauche', "Vérifier les liens de cette page menant vers des pages d'ébauche");
});

var verifEbauche_goodLinks = [];
var verifEbauche_compteurEbauche = 0;

window.verifEbauche_init = function(){
	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;
	var allLinks = Content.getElementsByTagName('a');
	for (var i=0; i<allLinks.length; i++){
		var server = mw.config.get('wgServer'), pageName = mw.config.get('wgPageName');
		if (!(!allLinks[i].href || $(allLinks[i].parentNode).hasClass('editsection') || $(allLinks[i].parentNode).hasClass('cachelinks') || $(allLinks[i]).hasClass('noprint') || $(allLinks[i]).hasClass('external') || allLinks[i].href.indexOf('javascript:') == 0 || allLinks[i].className == 'new' || allLinks[i].className == 'image' || allLinks[i].parentNode.parentNode.id == 'mw-normal-catlinks' || allLinks[i].parentNode.id == 'mw-normal-catlinks' || allLinks[i].href == server+'/wiki/'+encodeURI(pageName) || allLinks[i].href.indexOf(server+'/wiki/'+encodeURI(pageName)+'#') == 0 || allLinks[i].parentNode.className == 'subpages' || allLinks[i].parentNode.id == 'contentSub' )) {
			verifEbauche_goodLinks.push(allLinks[i]);
		}
	}
	if (!confirm(verifEbauche_goodLinks.length + " liens vont être analysés.")) return;
	verifEbauche();
};

function verifEbauche(position) {
	if(!position) position=0;
	var tempLink = verifEbauche_goodLinks[position];
	if(!tempLink){
		verifEbauche_Msg();
		return;
	}
	var tempTitle = tempLink.title || tempLink.innerHTML;
	var APILimit = 499;
	if((mw.config.get('wgUserGroups').indexOf("sysop")!=-1)||(mw.config.get('wgUserGroups').indexOf("bot")!=-1)) APILimit = 4999;
	var queryopt = {
          action: 'query',
          prop: 'templates',
          tllimit: APILimit,
          titles: tempTitle,
          redirects: true
	};
	var api = new mw.Api();
	api.get( queryopt ).then( function ( data ) {
		var pages = data.query.pages;
		if ( pages ) {
			for(var index in pages){
				if(!pages.hasOwnProperty(index)) continue;
				verifEbauche_goodLinks[position].style.backgroundColor = '#C8FFC8';
				var templates = pages[index].templates;
                                if(!templates) continue;
				for (var j=0; j<templates.length; j++){
					if (templates[j].title == "Modèle:Ébauche" || templates[j].title == "Modèle:ébauche"){
						//alert("Trouvé un lien vers une ébauche : " + tempTitle);
						verifEbauche_goodLinks[position].style.backgroundColor = '#FFCCCC';
						verifEbauche_compteurEbauche++;
						break;
					}
				}
			}
		}
                verifEbauche((position+1));
	} );
}

function verifEbauche_Msg(){
	// Message de fin.
	var msg = "";
	if (verifEbauche_compteurEbauche == 0) {
		msg = "Aucun lien vers une page d’ébauche n’a été trouvé.";
	} else if (verifEbauche_compteurEbauche == 1) {
		msg = "Un lien vers une page d’ébauche a été trouvé.";
	} else {
		msg = verifEbauche_compteurEbauche + " liens vers des pages d’ébauche ont été trouvés.";
		alert(msg);
	}
}