User:Void/createWikiNotify.js

// // TODO: more translations

var cwn = { ca: { submit: 'Enviar', c_domain: 'Marca aquesta wiki com que necessita un domini personalitzat', i: { required: 'Si us plau, ompli tots els camps obligatoris.', user: 'El sol·licitant no sembla existir.', dbexists: 'La wiki sol·licitada ja existeix.', unknown: 'Hi ha un problema amb la seva entrada o amb createWikiNotify.js' },		summary: 'Wiki creada' },	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' },		summary: 'Wiki created' },	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' },		summary: 'Wiki created' },	es: { submit: 'Enviar', c_domain: 'Marcar esta wiki como que necesita un dominio personalizado', i: { required: 'Por favor, rellene todos los campos obligatorios.', user: 'El solicitante no parece existir.', dbexists: 'La wiki solicitada ya existe.', unknown: 'Hay un problema con su entrada o con createWikiNotify.js' },		summary: 'Wiki creada' },	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' },		summary: 'Wiki created' },	pt: { submit: 'Enviar', c_domain: 'Marca esta wiki como você precisa de um domínio personalizado', i: { required: 'Por favor, preencha todos os campos obrigatórios.', user: 'O requerente não parece existir.', dbexists: 'O wiki solicitada já existe.', unknown: 'Há um problema com a sua entrada ou createWikiNotify.js' },		summary: 'Wiki criada' }, };

function getTrans(lang) { var trans; switch(lang) { case 'ca' : trans = cwn.ca; break; case 'de' : trans = cwn.de; break; case 'en' : trans = cwn.en; break; case 'es' : trans = cwn.es; break; case 'fr' : trans = cwn.fr; break; case 'pt' : trans = cwn.pt; 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.css('margin-top', '1em'));		$('#mw-input-cwDBname').after(fieldset.$element.css('margin-top', '1em'));		$('#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, lang);}); }

function edit (resp, template, requester, dbname, lang) { if( resp.query.pages[-1] ) template = template.slice(0, template.indexOf('/')).slice(template.indexOf(':') + 1); // 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: getTrans(lang).summary, 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: getTrans(lang).summary, }			} ).success($('form')[0].submit);		}	} ); } //