User:SD0001/PR-helper.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:SD0001/PR-helper. |
/**
* 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> */