User:Lee Vilenski/FAC-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:Lee Vilenski/FAC-helper. |
/**
* 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> */