Usuario:Salix alba/TDList.js
Nota: Después de guardar, debes refrescar la caché de tu navegador para ver los cambios. Internet Explorer: mantén presionada Ctrl mientras pulsas Actualizar. Firefox: mientras presionas Mayús pulsas el botón Actualizar, (o presiona Ctrl-Shift-R). Los usuarios de Google Chrome y Safari pueden simplemente pulsar el botón Recargar. Para más detalles e instrucciones acerca de otros exploradores, véase Ayuda:Cómo limpiar la caché.
/*************
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');
lang = "en";
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) {
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") )
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 finishedAjax() {
titles.sort();
var res="{| class=\"wikitable\"\n"+
"|-\n"+
"! Root !! Template !! Subpages\n";
//var res = titles.join('\n');
var oldroot = "";
var subpages = [];
var core = "";
pat = /([^\/]*)\/?(.*)/;
for(var i=0;i<titles.length;++i) {
// var tit=titles[i].replace(/A#A/g,'/').replace(/B#B/g,' ').replace(/C#C/g,'-');
var tit=titles[i].replace(/_/g,' ').replace(/#/g,'/');
//if(tit.indexOf('#')>0) alert('AAA'+titles[i]+'BBB'+tit);
var match=tit.match(pat);
var newroot = match[1];
var tail = match[2];
if(newroot!=oldroot) {
res += genLine(oldroot,core,subpages);
oldroot=newroot;
core="";
subpages = [];
}
if(tail==="")
core = '[['+newroot+']]';
else
subpages.push('[['+tit+'|/'+tail+']]');
}
res += genLine(oldroot,core,subpages);
res += "|}\n";
$cont.fadeIn('slow');
$title.text( messages[lang].title );
$body.html( $('<textarea>')
.attr('id', 'td-sk-code')
.text(res)
);
}
///// Close the dialog
function close() {
$cont.fadeOut('slow', function(){
});
}
////////// Building called after page loads
function buildHTML() {
$body = $('<div>')
.attr('id', 'td-sk-body');
$title = $('<h2>').text( messages[lang].title );
$cont = $('<div>')
.attr('id', 'td-sk-cont')
.append($('<div>')
.attr('id', 'td-sk-dialog')
.append( $title )
.append($('<a>')
.attr({
id: 'td-sk-close',
href: '#',
title: messages[lang].close
})
.click(function(){
close();
return false;
})
.append($('<img>')
.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);
});
}