Jump to content

User:Lee Vilenski/FAC-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 Featured Article nominations (FAC)
 */

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

var fac = {};
window.fac = fac;

$.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', '#', 'FAC', 'fac-portlet', 'Make a FAC nomination');
		$('#fac-portlet').click(fac.callback);
	}
});


fac.advert = ' ([[User:Lee Vilenski/FAC-helper|FAC-helper]])';

fac.callback = function facCallback(e) {
	if (e) e.preventDefault();

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

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


	form.append({
		type: 'checkbox',
		list: [{
			label: 'Add to userspace FAC log',
			name: 'addlog',
			checked: false,
			subgroup: [
				{
					type: 'input',
					label: 'FAC log: ',
					name: 'logpage',
					value: window.FAC_helper_log_page ||
						'User:' + mw.config.get('wgUserName').replace(/_/g, ' ') + '/FACs'
				},
				{
					type: 'input',
					label: 'Log entry: ',
					name: 'logentry',
					size: '50',
					value: typeof window.FAC_helper_log_entry === 'string' ?
						Morebits.string.safeReplace(window.FAC_helper_log_entry, '$PAGE', fac.title) :
						'{{facl|' + fac.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');

};

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

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

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

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

};

fac.tasks = {

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


	watchFA: function() {
		var def = $.Deferred();
		var query = {
			action: 'query',
			format: 'json',
			formatversion: '2',
			list: 'allpages',
			apnamespace: '1',
			apprefix: fac.title + '/FAC' // talk page prefix without namespace
		};
		var api = new Morebits.wiki.api('Watching FAC page', query);
		api.post().then(function(apiobj) {
			var pages = apiobj.response.query.allpages;
			fac.num = pages.length + 1; // HACK
      var facpage = 'Wikipedia:Featured Article Candidates/' + fac.talktitle + '/archive' + String(fac.num);
			new mw.Api().watch(facpage).then(def.resolve, def.reject);
		}, def.reject);
		return def;
	},

	addToLog: function () {
		var def = $.Deferred();
		if (!fac.params.addlog) {
			return def.resolve();
		}
		if (fac.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(fac.params.logpage, 'Retrieving userspace log');
		log.setAppendText('\n' + fac.params.logentry);
		log.setEditSummary('Logging [[' + fac.title + ']]' + fac.advert);
		log.append(def.resolve, def.reject);
		return def;
	}
};

/* </nowiki> */