User:SD0001/GAN-helper.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.
/**
 * Script to easily initiate Good Article nominations (GAN)
 */

/* <nowiki> */
/* jshint maxerr: 999 */

var gan = {};
window.gan = gan;

$.when(
	mw.loader.using([ 'mediawiki.api', 'ext.gadget.morebits' ]),
	$.ready
).then(function() {
	if ([0, 1].indexOf(mw.config.get('wgNamespaceNumber')) !== -1) {
		mw.util.addPortletLink('p-cactions', '#', 'GAN', 'gan-portlet', 'Make a GA nomination');
		$('#gan-portlet').click(gan.callback);
	}
});

gan.advert = ' ([[User:SD0001/GAN-helper|GAN-helper]])';

gan.callback = function ganCallback(e) {
	if (e) e.preventDefault();

	var Window = new Morebits.simpleWindow(600, 450);
	Window.setTitle( "Nominate article for GAN" );
	Window.setScriptName('GAN-helper');
	Window.addFooterLink('GAN instructions', 'WP:GANI');
	Window.addFooterLink('script documentation', 'User:SD0001/GAN-helper');

	var form = new Morebits.quickForm(gan.evaluate);
	
	var title_obj = mw.Title.newFromText(Morebits.pageNameNorm);
	gan.title = title_obj.getSubjectPage().toText();
	gan.talktitle = title_obj.getTalkPage().toText();

	var topicsList = [
		"Agriculture, food and drink",
		"Art and architecture",
		"Computing and engineering",
		"Transport",
		"Geography",
		"Places",
		"World history",
		"Royalty, nobility and heraldry",
		"Language and literature",
		"Mathematics and mathematicians",
		"Film",
		"Television",
		"Media and drama",
		"Albums",
		"Songs",
		"Music",
		"Biology and medicine",
		"Chemistry and materials science",
		"Earth sciences",
		"Physics and astronomy",
		"Philosophy and religion",
		"Culture, sociology and psychology",
		"Education",
		"Economics and business",
		"Law",
		"Magazines and print journalism",
		"Politics and government",
		"Sports and recreation",
		"Video games",
		"Warfare"
	];

	form.append({
		type: 'select',
		label: 'Topic: ',
		name: 'topic',
		list: topicsList.map(function (topic) {
			return { type: 'option', label: topic, value: topic };
		})
	});

	form.append({
		type: 'checkbox',
		list: [{
			label: 'Add to userspace GAN log',
			name: 'addlog',
			checked: false,
			subgroup: [
				{
					type: 'input',
					label: 'GA log: ',
					name: 'logpage',
					value: window.GAN_helper_log_page ||
						'User:' + mw.config.get('wgUserName').replace(/_/g, ' ') + '/GANs'
				},
				{
					type: 'input',
					label: 'Log entry: ',
					name: 'logentry',
					size: '50',
					value: typeof window.GAN_helper_log_entry === 'string' ?
						Morebits.string.safeReplace(window.GAN_helper_log_entry, '$PAGE', gan.title) :
						'* [[' + gan.title + ']]',
					tooltip: 'The text specified here will be *appended* to the very bottom of the log page. ' +
						'Ensure that your log page is formatted so that the edit makes sense.'
				}
			]
		}]
	});

	form.append({ type: 'submit', label: 'Submit' });

	var result = form.render();
	Window.setContent(result);
	Window.display();

	$(Morebits.quickForm.getElementContainer(result.addlog)).css('margin-top', '5px');

};

gan.evaluate = function(e) {
	var form = e.target;
	gan.params = Morebits.quickForm.getInputData(form);

	Morebits.simpleWindow.setButtonsEnabled(false);
	Morebits.status.init(form);

	var tm = new Morebits.taskManager();
	tm.add(gan.tasks.watchGA, []);
	tm.add(gan.tasks.editTalkPage, []);
	tm.add(gan.tasks.addToLog, [ gan.tasks.editTalkPage ]);

	tm.execute().then(function() {
		Morebits.status.actionCompleted('Nomination completed.');
	});

};

gan.tasks = {

	editTalkPage: function() {
		var def = $.Deferred();
		var talkpage = new Morebits.wiki.page(gan.talktitle, 'Editing talk page');
		talkpage.setPrependText('{{subst:GAN|subtopic=' + gan.params.topic + '}}\n');
		talkpage.setEditSummary('Making GAN nomination' + gan.advert);
		talkpage.prepend(def.resolve, def.reject);
		return def;
	},


	watchGA: function() {
		var def = $.Deferred();
		var query = {
			action: 'query',
			format: 'json',
			formatversion: '2',
			list: 'allpages',
			apnamespace: '1',
			apprefix: gan.title + '/GA' // talk page prefix without namespace
		};
		var api = new Morebits.wiki.api('Watching GAN page', query);
		api.post().then(function(apiobj) {
			var pages = apiobj.response.query.allpages;
			gan.num = pages.length + 1; // HACK
			var ganpage = gan.talktitle + '/GA' + String(gan.num);
			new mw.Api().watch(ganpage).then(def.resolve, def.reject);
		}, def.reject);
		return def;
	},

	addToLog: function () {
		var def = $.Deferred();
		if (!gan.params.addlog) {
			return def.resolve();
		}
		if (gan.params.logpage.indexOf('User:' + mw.config.get('wgUserName') + '/') !== 0) {
			Morebits.status.warn('Logging', 'Aborted as log page specified isn\'t in your userspace');
			return def.reject();
		}
		var log = new Morebits.wiki.page(gan.params.logpage, 'Retrieving userspace log');
		log.setAppendText('\n' + gan.params.logentry);
		log.setEditSummary('Logging [[' + gan.title + ']]' + gan.advert);
		log.append(def.resolve, def.reject);
		return def;
	}
};

/* </nowiki> */