User:Void/createWikiNotify.js

// // TODO: more translations

var cwn = { de: { submit: 'Absenden', c_domain: 'Mark this wiki as needing a custom domain', i: { required: 'Please fill in all the required fields.', user: 'Der anfrager ist nicht erstellt auf dieser Wiki-Farm.', dbexists: 'Eine Wiki mit dem Database-namen besteht schon.', unknown: 'There is a problem with your input or with createWikiNotify.js' }	},	en: { submit: 'Submit', c_domain: 'Mark this wiki as needing a custom domain', i: { required: 'Please fill in all the required fields.', user: 'The requester does not appear to exist.', dbexists: 'The requested wiki already exists.', unknown: 'There is a problem with your input or with createWikiNotify.js' }	},	es: { submit: 'Enviar', c_domain: 'Marcar este wiki como necesitando un dominio personalizado', i: { required: 'Por favor, rellene todos los campos obligatorios.', user: 'El solicitante no parece existir.', dbexists: 'El wiki solicitado ya existe.', unknown: 'Hay un problema con su entrada o con createWikiNotify.js' }	},	fr: { submit: 'Soumettre', c_domain: 'Marquer ce wiki nécessitant un domaine personnalisé', i: { required: 'Merci de remplir tout les champs requis.', user: 'Le demandeur n existe pas.', dbexists: 'Ce wiki existe déjà.', unknown: 'Il y a une problème avec votre entrée ou avec createWikiNotify.js' }	}, };

function getTrans(lang) { var trans; switch(lang) { case 'de' : trans = cwn,de; break; case 'en' : trans = cwn.en; break; case 'es' : trans = cwn.es; break; case 'fr' : trans = cwn.fr; break; default : trans = cwn.en; }	return trans; }

if( mw.config.get('wgPageName') === 'Special:CreateWiki' ) { var userLang = mw.user.options.get('language'); var messages = getTrans(userLang); mw.loader.using( ['oojs-ui', 'mediawiki.util'] ).done( function {		var submit = new OO.ui.ButtonWidget({ label: messages.submit, id: 'c-submit-button' });		var customCBox = new OO.ui.CheckboxInputWidget({ selected: $("#mw-input-cwDBname")[0].childNodes[0].value ? false : true, id: 'c-custom-cbox' });		var fieldset = new OO.ui.FieldsetLayout( { } );		fieldset.addItems( [ new OO.ui.FieldLayout( customCBox, {label: messages.c_domain, align: 'inline'} ) ] );		$('div.mw-htmlform-submit-buttons').replaceWith(submit.$element);		$('#c-submit-button').before(' '); // Move submit button away from the reason text field		$('#mw-input-cwDBname').after(fieldset.$element);		$('#c-submit-button').click(function{ // Validate input var dbname = $("#mw-input-cwDBname")[0].childNodes[0].value; var requester = $("#mw-input-cwRequester")[0].childNodes[0].value; var reason = $("#mw-input-wpreason")[0].childNodes[0].value; if( dbname.trim.length === 0 && requester.trim.length === 0 && reason.trim.length === 0 ) invalidInput('required'); else validateUser(requester); });	} ); }

function validateUser(user) { $.getJSON(		mw.util.wikiScript('api'),		{			format: 'json',			action: 'query',			list: 'users',			ususers: user,			usprop: ''		}	).success(function (data){		if(typeof data.query.users[0].missing === 'undefined')			validateDB;		else			invalidInput('user');	}); }

function validateDB { var dbname = $("#mw-input-cwDBname")[0].childNodes[0].value; dbname = dbname.slice(0, dbname.lastIndexOf('wiki')); $.getJSON(		'https://' + dbname + '.miraheze.org/w/api.php',		{			format: 'json',			action: 'query'		}	).success( function {		invalidInput('dbexists'); // Exists	} ).error( function {		validInput; // Does not exist	} ); }

function invalidInput(type) { var reason; switch(type) { case 'required': reason = messages.i.required; break; case 'user': reason = messages.i.user; break; case 'dbexists': reason = messages.i.dbexists; break; default: reason = messages.i.unknown; }	alert(reason); }

function validInput { var dbname = $("#mw-input-cwDBname")[0].childNodes[0].value; var lang = $("#mw-input-cwLanguage")[0].childNodes[0].value; var requester = $("#mw-input-cwRequester")[0].childNodes[0].value; var custom = $('#c-custom-cbox')[0].childNodes[0].checked; var suffix = lang; if( suffix === 'en' ) suffix = ''; else suffix = '/' + suffix; var template = 'Template:Wiki created' + (custom ? '(CD)' : '') + suffix; dbname = dbname.slice(0, dbname.lastIndexOf('wiki')); // Check if the template exists and edit based off response $.getJSON(		mw.util.wikiScript('api'),		{			format: 'json',			action: 'query',			titles: template,			prop: 'revisions',			rvlimit: 1,			rvprop: '' // Don't need anything to see if it's there		}	).success(function(data){edit(data, template, requester, dbname);}); }

function edit (resp, template, requester, dbname) { if( resp.query.pages[-1] ) template = 'Template:Wiki created'; // No translated version, default to en	else template = template.slice(template.indexOf(':') + 1); // Remove template prefix

$.getJSON(		mw.util.wikiScript('api'),		{			format: 'json',			action: 'query',			titles: 'User talk:' + requester,			prop: 'revisions',			rvlimit: 1,			rvprop: 'content'		}	).success(function(resp) {		if( resp.query.pages[-1] ) {			// Didn't find, create page			$.ajax( { url: mw.util.wikiScript('api'), type: 'POST', dataType: 'json', data: { format: 'json', action: 'edit', token: mw.user.tokens.get('csrfToken'), title: 'User talk:' + requester, text: 'https://' + dbname + '.miraheze.org', summary: 'Wiki created', createonly: 1 // Force only create page }			} ).success($('form')[0].submit);		}		else {			// Found, preserve existing content			var wtext = JSON.stringify(resp);			wtext = wtext.slice(wtext.indexOf('"*":"') + 5, wtext.indexOf('"}]}'));			wtext = wtext.replace(/\\n/g, '\n'); // JSON.stringify escapes the string			wtext = wtext.replace(/\\"/g, '"');			wtext += '\n\nhttps://' + dbname + '.miraheze.org',			$.ajax( { url: mw.util.wikiScript('api'), type: 'POST', dataType: 'json', data: { format: 'json', action: 'edit', token: mw.user.tokens.get('csrfToken'), title: 'User talk:' + requester, text: wtext, summary: 'Wiki created', }			} ).success($('form')[0].submit);		}	} ); } //