User:Salix alba/TDList.js

/*************

Generates a list of pages which use templates. Used to create https://en.wikipedia.org/wiki/Wikipedia:TemplateData/List

Author User:Salix alba Date: 28 July 2013 Version: 0.20

Heavily borrowed from TemplateDataEditor http://fr.wikipedia.org/w/index.php?title=Utilisateur:Ltrlg/scripts/TemplateDataEditor.js

***************/

function TDList($) { var titles = [];

////// Customization for local wikis

lang = mw.config.get('wgUserLanguage'); messages = { "en": { "toolbox-label": 'TD List', "toolbox-tooltip": 'Generates list of templates with TemplateData', "title": 'Templates with TemplateData', 'section-description': 'TemplateData', "close": 'Close' }	};       if(messages[lang]===null) lang='en';

////// Global variables

var $title, $body, $cont; var pagename;

////// Called when toolbox link clicked

function openList {

var URL = mw.util.wikiScript('api'); $.ajax({			url: URL,			data: { action:'query', list: 'pageswithprop', pwppropname:'templatedata', pwpprop:'title',                                format:'json', rawcontinue: '', pwplimit: 500 },			dataType: "json"		}).done(doneAjax); }

function openList2(pwpcont) {

var URL = mw.util.wikiScript('api'); $.ajax({			url: URL,			data: { action:'query', list: 'pageswithprop', pwppropname:'templatedata', pwpprop:'title',                                format:'json', rawcontinue: '', pwplimit: 500, pwpcontinue: pwpcont },			dataType: "json"		}).done(doneAjax); }

// Called when raw template code read in       // {"query-continue":{"pageswithprop":{"pwpcontinue":"1995504"}}, // "query":{"pageswithprop":[{"ns":10,"title":"Template:R with possibilities"},

function doneAjax(data) { pat = /^(.*?)(\/doc|\/sandbox|\/testcases|\/TemplateData)?$/; var list = data.query.pageswithprop; //alert("again "+list.length+" "+data.queryContinue+" or "+data['query-continue']); for(var i=0;i<list.length;++i) { //                    titles.push(list[i].title.replace(/\//g,'A#A').replace(/ /g,'B#B').replace(/-/g,'C#C')); if(                     	!list[i].title.startsWith("Template:Country data")                        && !list[i].title.startsWith("Template:Lang")                       ) { var match=list[i].title.match(pat); var newroot = match[1]; var tail = match[2] ? match[2].substring(1) : ""; titles.push( {root:newroot,tail:tail}); //                       titles.push(list[i].title.replace(/ /g,'_').replace(/\//g,'#')); }               }                if(data['query-continue'] == null) { finishedAjax; }               else { openList2(data['query-continue'].pageswithprop.pwpcontinue); }       }

function genLine(root,core,subs) { if(root==="") {return ""}

return "|-\n|"+root+"||"+core+"||"+subs.join(', ')+"\n"; }

function titleCmp(a,b) { if( a.root > b.root ) { return 1 } if( a.root < b.root ) { return -1 } return 0; }

function finishedAjax { titles.sort(titleCmp); var res="==General Templates==\n\n===A-C===\n\n"; var tableheader= "{| class=\"wikitable\"\n"+ "|-\n"+ "! Root !! Template !! Subpages\n"; res += tableheader;

var oldroot = ""; var subpages = []; var core = ""; for(var i=0;i<titles.length;++i) { var newroot = titles[i].root; var tail = titles[i].tail; if(newroot!=oldroot) { res += genLine(oldroot,core,subpages);

if(newroot.substring(0,10) == "Template:D" && oldroot.substring(0,10) == "Template:C") { res += "|}\n\n===D-Infl===\n\n"; res += tableheader; }              	 				    if(newroot.startsWith("Template:Infobox") && !oldroot.startsWith("Template:Infobox") ) {					    	res += "|}\n\n===Infobox===\n\n"; res += tableheader; }              	 				    if(!newroot.startsWith("Template:Infobox") && oldroot.startsWith("Template:Infobox") ) { res += "|}\n\n===Inform - N===\n\n"; res += tableheader; }              	 				    if(newroot.substring(0,10) == "Template:O" && oldroot.substring(0,10) == "Template:N") { res += "|}\n\n===O-Z===\n\n"; res += tableheader; }              	                    	oldroot=newroot; core=""; subpages = []; }               if(tail==="") core = +newroot+; else subpages.push('/'+tail+''); }           res += genLine(oldroot,core,subpages); res += "|}\n";

$cont.fadeIn('slow'); $title.text( messages[lang].title ); $body.html( $(' ')				.attr('id', 'td-sk-code')				.text(res)			); }

///// Close the dialog function close { $cont.fadeOut('slow', function{		}); }

////////// Building called after page loads function buildHTML { $body = $(' ') .attr('id', 'td-sk-body'); $title = $(' ').text( messages[lang].title ); $cont = $(' ') .attr('id', 'td-sk-cont') .append($(' ')				.attr('id', 'td-sk-dialog')				.append( $title )				.append($('') .attr({							id: 'td-sk-close',							href: '#',							title: messages[lang].close						}) .click(function{							close;							return false;						}) .append($(' ')								.attr({ alt: messages[lang].close, src: '//upload.wikimedia.org/wikipedia/commons/thumb/8/8d/VisualEditor_-_Icon_-_Close.svg/24px-VisualEditor_-_Icon_-_Close.svg.png' })						)				)				.append($body)		) .hide;

$(document.body).append($cont); }

////// Adds a link in the toolbox function addPortletLink {

$(				mw.util.addPortletLink('p-tb', '#', messages['en']['toolbox-label'], 'td-list', messages['en']['toolbox-tooltip'] )		).click(function{			openList;			return false;		}); }

/////// Actions to do once page loads, function start { //alert("TDList 0.26"); addPortletLink; buildHTML; }

////////// START ////////// start; }

/////// Wrapper code, only run if in user or template namespaces if( $.inArray( mw.config.get('wgNamespaceNumber'), [2, 4 ] ) !== -1 ) { mw.loader.load(  	  '//en.wikipedia.org/w/index.php?title=User:Salix alba/TDSkell.css&action=raw&ctype=text/css&smaxage=21600&maxage=86400',	'text/css' ); mw.loader.using('mediawiki.util', function{	 $(document).ready(TDList);        }); }