Jump to content

User:SD0001/PR-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 make Peer Review (PR)
 * nominations.
 */

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

var pr = {};
window.pr = pr;


$.when(
	mw.loader.using('ext.gadget.morebits'),
	$.ready
).then(function() {
	if ([0, 1].indexOf(mw.config.get('wgNamespaceNumber')) !== -1) {
		mw.util.addPortletLink('p-cactions', '#', 'PR', 'pr-portlet', 'Nominate for PR');
		$('#pr-portlet').click(pr.callback);
	}
});

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

pr.callback = function (e) {
	e.preventDefault();

	var Window = new Morebits.simpleWindow(600, 600);
	Window.setTitle( "Nominate article for Peer Review" );
	Window.setScriptName('PR-helper');
	Window.addFooterLink('PR instructions', 'Wikipedia:Peer review/Guidelines');

	var form = new Morebits.quickForm(pr.evaluate);

	var field = form.append({
		type: 'field',
		label: 'Details',
		name: 'details'
	});

	field.append({
		type: 'select',
		label: 'Topic: ',
		name: 'topic',
		list: [
			{ type: 'option', label: "Arts", value: "arts" },
			{ type: 'option', label: "Language and literature", value: "langlit" },
			{ type: 'option', label: "Philosophy and religion", value: "philrelig" },
			{ type: 'option', label: "Everyday life", value: "everydaylife" },
			{ type: 'option', label: "Social sciences and society", value: "socsci" },
			{ type: 'option', label: "Geography", value: "geography" },
			{ type: 'option', label: "History", value: "history" },
			{ type: 'option', label: "Engineering and technology", value: "engtech" },
			{ type: 'option', label: "Natural sciences and mathematics", value: "natsci" },
			{ type: 'option', label: "General topic", value: "general" },
			{ type: 'option', label: "List", value: "list" },
		]
	});

	field.append({
		type: 'textarea',
		name: 'reason',
		label: 'Rationale:',
		style: 'height: 130px',
		tooltip: 'You can expand the rationale after the page has been saved as well.'
	});

	var previewlink = Morebits.htmlNode('a', 'Preview');
	previewlink.style.cursor = "pointer";
	$(previewlink).click(function() {
		// |result| is defined below
		pr.preview(result);
	});
	form.append( { type: 'div', id: 'prpreview', label: [ previewlink ] } );
	form.append( { type: 'div', id: 'pr-previewbox', style: 'display: none' } );

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

	var result = form.render();
	Window.setContent(result);
	Window.display();
	pr.previewbox = document.getElementById('pr-previewbox');
	result.previewer = new Morebits.wiki.preview(pr.previewbox);

};

pr.initParams = function (form) {
	pr.params = Morebits.quickForm.getInputData(form);
	var title_obj = mw.Title.newFromText(Morebits.pageNameNorm);
	pr.title = title_obj.getSubjectPage().toText();
	pr.talktitle = title_obj.getTalkPage().toText();
};

pr.preview = function (form) {
	pr.initParams(form);

	Morebits.status.init(pr.previewbox);
	$(pr.previewbox).show();

	pr.tasks.getNumber().then(function () {
		form.previewer.beginRender(pr.tasks.getNomPageText(), pr.prpage);
	}, function () {
		form.previewer.beginRender("Unable to render preview", pr.prpage);
	});
};

pr.evaluate = function (e) {
	var form = e.target;
	pr.initParams(form);

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

	var tm = new Morebits.taskManager();
	tm.add(pr.tasks.getNumber, []);
	tm.add(pr.tasks.editTalkPage, []);
	tm.add(pr.tasks.createNomPage, [ pr.tasks.getNumber ], function () {
		Morebits.status.printUserText(pr.tasks.getNomPageText(),
			'Your nomination text is provided below, with which you can manually create the page [[' + pr.prpage + ']].');
	});
	tm.execute().then(function() {
		Morebits.status.actionCompleted('Nomination completed.');
	});
};

pr.tasks = {

	getNumber: function () {
		var def = $.Deferred();
		var query = {
			action: 'query',
			format: 'json',
			formatversion: '2',
			list: 'allpages',
			apnamespace: '4', // WP
			apprefix: 'Peer review/' + pr.title + '/',
			aplimit: 'max'
		};
		var api = new Morebits.wiki.api('Getting numbering', query);
		api.post().then(function(apiobj) {
			var pages = apiobj.response.query.allpages;
			pr.num = pages.length + 1; // HACK
			pr.prpage = 'Wikipedia:Peer review/' + pr.title + '/archive' + pr.num;
			apiobj.getStatusElement().info('Next number is ' + pr.num);
			def.resolve();
		}, def.reject);
		return def;
	},

	editTalkPage: function () {
		var def = $.Deferred();
		var talkpage = new Morebits.wiki.page(pr.talktitle, 'Editing talk page');
		talkpage.load(function(talkpage) {
			// prepend tag:
			var text = '{{subst:PR}}\n' + talkpage.getPageText();
			talkpage.setPageText(text);
			talkpage.setEditSummary('Nominating for peer review' + pr.advert);
			talkpage.save(def.resolve, def.reject);
		}, def.reject);
		return def;
	},

	getNomPageText: function () {
		return '{{subst:PR/subst}}\n{{Peer review page|topic=' + pr.params.topic + '}}\n' +  pr.params.reason.trim() +
			(pr.params.reason.trim().endsWith('~~~~') ? '' : ' ~~~~');
	},

	createNomPage: function () {
		var def = $.Deferred();
		var appendtext = pr.tasks.getNomPageText();
		var pageobj = new Morebits.wiki.page(pr.prpage, 'Creating nomination page');
		pageobj.setAppendText(appendtext);
		pageobj.setCreateOption('createonly');
		pageobj.setWatchlist(true);
		pageobj.setEditSummary('Creating PR nomination page' + pr.advert);
		pageobj.append(def.resolve, def.reject);
		return def;
	},

};

/* </nowiki> */