User:Alexis Jazz/Factotum/MakeCitemap.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//MakeCitemap, irrevocably released as WTFPL Version 2[www.wtfpl.net/about/] by its author, Alexis Jazz
//Creates a Citemap.json for Bawl to use when inserting a reference with Web2Cit. See [[WP:Citemap.json]] and [[d:Q112131235]]
Bawl.openReplyForm(Bawl.replyLinkParamsFP);
Bawl.aceMirrorDisable();
var api = new mw.Api();
Bawl.newCitePostFix = {
	de:'/Doku',
	fr:'/Documentation',
};
api.post({action:'query',prop:'revisions',format:'json',titles:'MediaWiki:Citoid-template-type-map.json',rvprop:'content',rvslots:'*'}).then(function(data){
	console.log('got MediaWiki:Citoid-template-type-map.json');
	Bawl.newCitemap = {};
	Bawl.newCitemap.templateTypeMap = JSON.parse(data.query.pages[Object.keys(data.query.pages)[0]].revisions[0].slots.main['*']);
	Bawl.newCitemap.templateParamMap = {};
	Bawl.citoidTemplates = Object.values(Bawl.newCitemap.templateTypeMap);
	Bawl.citoidTemplatesArr = [];
	Bawl.templateDataSucks = function(title) {
		if ( ['en','sq'].includes(mw.config.get('wgContentLanguage')) && Bawl.citoidTemplates[Bawl.citoidInt] == 'Cite book' ) {
			return '/TemplateData'; //EVERY TIME YOU USE AN INCONSISTENT TITLE GOD KILLS A KITTEN.
		} else {
			return (Bawl.newCitePostFix[mw.config.get('wgContentLanguage')] || '/doc' ); //PLEASE THINK OF THE KITTENS.
		}
	};
	for ( Bawl.citoidInt=0;Bawl.citoidInt<Bawl.citoidTemplates.length;Bawl.citoidInt++) {
		Bawl.templateDocName = 'Template:'+Bawl.citoidTemplates[Bawl.citoidInt]+Bawl.templateDataSucks(Bawl.citoidTemplates[Bawl.citoidInt]);
		if ( ! Bawl.citoidTemplatesArr.includes(Bawl.templateDocName) ) {
			console.log('add '+Bawl.templateDocName);
			Bawl.citoidTemplatesArr.push(Bawl.templateDocName);
		}
	}
	api.post({action:'query',prop:'revisions',format:'json',titles:Bawl.citoidTemplatesArr,rvprop:'content',rvslots:'*'}).then(function(data){
		Bawl.templatesMapData = data; //for debugging
		for ( Bawl.citoidInt=0;Bawl.citoidInt<Bawl.citoidTemplatesArr.length;Bawl.citoidInt++) {
			if ( Object.keys(Bawl.templatesMapData.query.pages)[Bawl.citoidInt] == -1 ) {
				console.log('no doc page');
				continue;
			}
			console.log('process '+Object.keys(Bawl.templatesMapData.query.pages)[Bawl.citoidInt]);
			Bawl.someTemplateRevData = data.query.pages[Object.keys(data.query.pages)[Bawl.citoidInt]];
			Bawl.someTemplateJSONString = Bawl.someTemplateRevData.revisions[0].slots.main['*'].replace(/[^]*"citoid":[ \t]*\{([^\}]*)\}[^]*/,'{$1}');

			console.log('JSON: '+Bawl.someTemplateJSONString);
			Bawl.someTemplateMap = JSON.parse(Bawl.someTemplateJSONString);
			Bawl.someTemplateTitle = data.query.pages[Object.keys(data.query.pages)[Bawl.citoidInt]].title.replace(/.*:/,'').replace(Bawl.templateDataSucks(data.query.pages[Object.keys(data.query.pages)[Bawl.citoidInt]].title.replace(/.*:/,'')),'');
			Bawl.newCitemap[Bawl.someTemplateTitle+'params'] = Bawl.someTemplateMap;
			Bawl.newCitemap.templateParamMap[Bawl.someTemplateTitle] = Bawl.someTemplateTitle+'params';
		}
		Bawl.UITextInput.setValue(JSON.stringify(Bawl.newCitemap));
	});
});