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

// 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

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( $('