User:SD0001/FAR-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/FAR-helper. |
/**
* Script to easily make Feature Article Review (FAR)
* nominations.
*/
/* <nowiki> */
/* jshint maxerr: 999 */
var far = {};
window.far = far;
$.when(
mw.loader.using('ext.gadget.morebits'),
$.ready
).then(function() {
if ([0, 1].indexOf(mw.config.get('wgNamespaceNumber')) !== -1) {
mw.util.addPortletLink('p-cactions', '#', 'FAR', 'far-portlet', 'Nominate for FAR');
$('#far-portlet').click(far.callback);
}
});
far.advert = ' ([[User:SD0001/FAR-helper|FAR-helper]])';
far.callback = function farCallback(e) {
if (e) e.preventDefault();
var Window = new Morebits.simpleWindow(600, 600);
Window.setTitle( "Nominate article for FAR" );
Window.setScriptName('FAR-helper');
Window.addFooterLink('FAR instructions', 'WP:FAR');
var form = new Morebits.quickForm(far.evaluate);
var field = form.append({
type: 'field',
label: 'Details',
name: 'details'
});
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.',
value: 'I am nominating this featured article for review because...'
});
// TODO: allow notifications through script
field.append({
type: 'dyninput',
name: 'page',
id: 'notification-pages',
label: 'Notified pages: ',
tooltip: 'Pages that have been notified',
sublabel: 'Page: ',
});
var previewlink = Morebits.htmlNode('a', 'Preview');
previewlink.style.cursor = "pointer";
$(previewlink).click(function() {
// |result| is defined below
far.preview(result);
});
form.append( { type: 'div', id: 'farpreview', label: [ previewlink ] } );
form.append( { type: 'div', id: 'far-previewbox', style: 'display: none' } );
form.append({ type: 'submit', label: 'Submit' });
var result = form.render();
Window.setContent(result);
Window.display();
far.previewbox = document.getElementById('far-previewbox');
result.previewer = new Morebits.wiki.preview(far.previewbox);
};
far.initParams = function (form) {
far.params = Morebits.quickForm.getInputData(form);
far.params.notifications = $(form).find('#notification-pages input[name=page]')
.get().map(e => e.value);
var title_obj = mw.Title.newFromText(Morebits.pageNameNorm);
far.title = title_obj.getSubjectPage().toText();
far.talktitle = title_obj.getTalkPage().toText();
};
far.preview = function (form) {
far.initParams(form);
Morebits.status.init(far.previewbox);
$(far.previewbox).show();
far.tasks.getNumber().then(function () {
form.previewer.beginRender(far.tasks.getNomPageText(), far.farpage);
}, function () {
form.previewer.beginRender("Unable to render preview", far.farpage);
});
};
far.evaluate = function (e) {
var form = e.target;
far.initParams(form);
Morebits.simpleWindow.setButtonsEnabled(false);
Morebits.status.init(form);
var tm = new Morebits.taskManager();
tm.add(far.tasks.getNumber, []);
tm.add(far.tasks.editTalkPage, []);
tm.add(far.tasks.createNomPage, [ far.tasks.getNumber ], function () {
Morebits.status.printUserText(far.tasks.getNomPageText(),
'Your nomination text is provided below, with which you can manually create the page [[' + far.farpage + ']].');
});
tm.add(far.tasks.transcludeToFarPage, [ far.tasks.createNomPage ]);
tm.execute().then(function() {
Morebits.status.actionCompleted('Nomination completed.');
});
};
far.tasks = {
getNumber: function () {
var query = {
action: 'query',
format: 'json',
formatversion: '2',
list: 'allpages',
apnamespace: '4', // WP
apprefix: 'Featured article review/' + far.title + '/',
aplimit: 'max'
};
var api = new Morebits.wiki.api('Getting numbering', query);
return api.post().then(function(apiobj) {
var pages = apiobj.response.query.allpages;
far.num = pages.length + 1; // HACK
far.farpage = 'Wikipedia:Featured article review/' + far.title + '/archive' + far.num;
apiobj.getStatusElement().info('Next number is ' + far.num);
});
},
editTalkPage: function () {
var def = $.Deferred();
var talkpage = new Morebits.wiki.page(far.talktitle, 'Editing talk page');
talkpage.load(function(talkpage) {
// prepend tag:
var text = '{{subst:FAR}}\n' + talkpage.getPageText();
talkpage.setPageText(text);
talkpage.setEditSummary('Nominating to FAR' + far.advert);
talkpage.save(def.resolve, def.reject);
}, def.reject);
return def;
},
getNomPageText: function () {
var notificationLinks = far.params.notifications
.filter(e => e.trim().length > 0)
.map(pg => '[[' + pg + '|]]').join(', ');
// XXX: Should this be a template?
return '===[[' + far.title + ']]===\n'
+ '\n'
+ '<noinclude>{{la|' + far.title + '}}\n'
+ '{{Featured article tools|1=' + far.title + '}}</noinclude>\n'
+ ":<small>''Notified: " + notificationLinks + "''</small>\n"
+ '\n'
+ far.params.reason.trim() + (far.params.reason.trim().endsWith('~~~~') ? '' : ' ~~~~');
},
createNomPage: function () {
var def = $.Deferred();
var appendtext = far.tasks.getNomPageText();
var pageobj = new Morebits.wiki.page(far.farpage, 'Creating nomination page');
pageobj.setAppendText(appendtext);
pageobj.setCreateOption('createonly');
pageobj.setWatchlist(true);
pageobj.setEditSummary('Creating FAR nomination page' + far.advert);
pageobj.append(def.resolve, def.reject);
return def;
},
transcludeToFarPage: function () {
var def = $.Deferred();
var pageobj = new Morebits.wiki.page('Wikipedia:Featured article review', 'Transcluding to FAR page');
pageobj.setPageSection(1);
pageobj.load(function (nomspage) {
var text = nomspage.getPageText();
var transclusion = '{{' + far.farpage + '}}';
var newText = text.replace(/==\s*Featured article reviews\s*==\n/, '$&' + transclusion + '\n');
if (newText === text) {
pageobj.getStatusElement().error('Failed to add transclusion at WP:FAR page. ' +
'Please manually edit [[WP:FAR#Featured article reviews]] to add <code>{{' + far.farpage + '}}</code> at the top.');
return def.resolve();
}
pageobj.setPageText(newText);
pageobj.setEditSummary('Adding [[' + far.farpage + ']]' + far.advert);
pageobj.save(def.resolve, def.reject);
});
return def;
}
};
/* </nowiki> */