User:Epicgenius/stubtag.js/sandbox.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.
/***************************************************************************************************
 STUB TAG By Epicgenius - Beta Version
 Version 0.1.1
 
A Lot of this was taken from [[User:Evad37/rater.js]].
***************************************************************************************************/
// <nowiki>
$( function($) 

{
/* ========== Config ============================================================================ */
// A global object that stores all the page and user configuration and settings
var config = {};
// Script info
config.script = {
	// Advert to append to edit summaries
	advert:  ' ([[User:Epicgenius/stubtag.js|Stub Tag]])',
	version: '0.1.1-beta'
};
// MediaWiki configuration values
config.mw = mw.config.get( [
	'skin',
	'wgPageName',
	'wgNamespaceNumber',
	'wgUserName',
	'wgFormattedNamespaces',
	'wgMonthNames',
	'wgRevisionId',
	'wgScriptPath',
	'wgServer',
	'wgCategories',
	'wgIsMainPage'
] );
// Do not operate on Special: pages, nor on non-existent pages or their talk pages
if ( $('li.new[id|=ca-nstab]').length ) {
	return;
} 
// Or Talk Pages
if  ( config.mw.wgNamespaceNumber < 0 || config.mw.wgNamespaceNumber % 2 == 1 ){ 
	return;
} 
// For User namespaces, only operate on subpages
if (
	config.mw.wgNamespaceNumber = 2 &&
	config.mw.wgPageName.indexOf('/') === -1
) {
	return;
}


mw.util.addCSS(
	/* ----- Main dialog styles --------------------------------------------------------------- */
	'.stubtag-dialog-row { padding:0.2em; border-bottom: 1px solid #777; }'+
	'.stubtag-dialog-row:nth-child(even) { background-color:#e0e0e0; }'+
	'.stubtag-dialog-row > div:nth-child(2) { clear:left; }'+
	'.stubtag-dialog-row > div > span { padding-right:0.5em; white-space:nowrap; }'+
	'.stubtag-dialog-para-label { cursor:help; padding-right:0; }'+
	'.stubtag-dialog-para-code { font-family:monospace; font-size:123%; padding-right:0; }'+
	'.stubtag-dialog-para-code::before { content:"|"; }'+
	'.stubtag-dialog-para-code::after { content:"="; }'+
	'.stubtag-dialog-textInputContainer input { width:6em; margin:0 0.15em; }'+
	/* ----- OOjs UI windows ------------------------------------------------------------------ */
	'.atubtag-oouiWindowManager, .stubtag-oouiWindowManager > div { z-index:2000 !important; }'+
	/* ----- Diff styles ---------------------------------------------------------------------- */
	'table.diff, td.diff-otitle, td.diff-ntitle { background-color: white; }'+
	'td.diff-otitle, td.diff-ntitle { text-align: center; }'+
	'td.diff-marker { text-align: right; font-weight: bold; font-size: 1.25em; }'+
	'td.diff-lineno { font-weight: bold; }'+
	'td.diff-addedline, td.diff-deletedline, td.diff-context { font-size: 88%; vertical-align: top; white-space: -moz-pre-wrap; white-space: pre-wrap; }'+
	'td.diff-addedline, td.diff-deletedline { border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; }'+
	'td.diff-addedline { border-color: #a3d3ff; }'+
	'td.diff-deletedline { border-color: #ffe49c; }'+
	'td.diff-context { background: #f3f3f3; color: #333333; border-style: solid; border-width: 1px 1px 1px 4px; border-color: #e6e6e6; border-radius: 0.33em; }'+
	'.diffchange { font-weight: bold; text-decoration: none; }'+
	'table.diff { border: none; width: 98%; border-spacing: 4px;'+
		/* Ensure that colums are of equal width: */ 'table-layout: fixed; }'+
	'td.diff-addedline .diffchange, td.diff-deletedline .diffchange { border-radius: 0.33em; padding: 0.25em 0; }'+
	'td.diff-addedline .diffchange {	background: #d8ecff; }'+
	'td.diff-deletedline .diffchange { background: #feeec8; }'+
	'table.diff td {	padding: 0.33em 0.66em; }'+
	'table.diff col.diff-marker { width: 2%; }'+
	'table.diff col.diff-content { width: 48%; }'+
	'table.diff td div {'+
		/* Force-wrap very long lines such as URLs or page-widening char strings. */
		'word-wrap: break-word;'+
		/* As fallback (FF<3.5, Opera <10.5), scrollbars will be added for very wide cells
		   instead of text overflowing or widening */
		'overflow: auto;'+
	'}'
);
/* ========== Run script ============================================================================ */
/* ---------- Raw wikitext ---------------------------------------------------------------------- */
API.getRaw = function(page) {
	var gotRaw = $.Deferred();
	
	var request = $.get('https:' + config.mw.wgServer + mw.util.getUrl(page, {action:'raw'}))
	.done(function(data) {
		if ( !data ) {
			gotRaw.reject('ok-but-empty');
			return;
		}
		gotRaw.resolve(data);
	})
	.fail(function(){
		status = request.getResponseHeader('status');
		gotRaw.reject('http', {textstatus: status || 'unknown'});
	});
	
	return gotRaw;
};

config.banners = {};
config.gotListOfBanners = $.Deferred(); 
(function getListOfBanners() {

	var querySkeleton = {
		action: 'query',
		format: 'json',
		list: 'categorymembers',
		cmprop: 'title',
		cmnamespace: '10',
		cmlimit: '500'
	};

	var categories = [
		{
			title:'Category:Stub_message_templates',
			abbreviation: 'Messages',
			banners: [],
			processed: $.Deferred()
		},
	];

	var processQuery = function(result, catIndex) {
		if ( !result.query || !result.query.categorymembers ) {
			// No results
			// TODO: error or warning ********
			config.gotListOfBanners.reject();
			return;
		}
		
		// Gather titles into array - excluding "Template:" prefix
		var resultTitles = result.query.categorymembers.map(function(info) {
			return info.title.slice(9);
		});
		Array.prototype.push.apply(categories[catIndex].banners, resultTitles);
		
		// Continue query if needed
		if ( result.continue ) {
			doApiQuery($.extend(categories[catIndex].query, result.continue), catIndex);
			return;
		}
		
		categories[catIndex].processed.resolve();
	};

	var doApiQuery = function(q, catIndex) {
		API.get( q )
		.done( function(result) {
			processQuery(result, catIndex);
		} )
		.fail( function(code, jqxhr) {
			console.warn('[Stubtag] ' + extraJs.makeErrorMsg(code, jqxhr, 'Could not retrieve pages from [[:' + q.cmtitle + ']]'));
			config.gotListOfBanners.reject();
		} );
	};
	
	categories.forEach(function(cat, index, arr) {
		cat.query = $.extend( { 'cmtitle':cat.title }, querySkeleton );
		$.when( arr[index-1] && arr[index-1].processed || true ).then(function(){
			doApiQuery(cat.query, index);
		});
	});
	
	categories[categories.length-1].processed.then(function(){
		var storeBannersInConfig = function(catObject) {
			config.banners[catObject.abbreviation] = catObject.banners;
		};
		
		config.banners = {};
		categories.forEach(storeBannersInConfig);
		
		config.gotListOfBanners.resolve();
	});

})();

});
// </nowiki>