User:SD0001/G13-restore-wizard2.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/G13-restore-wizard2. |
/**
* MediaWiki:G13-restore-wizard.js
*
* Script for [[Wikipedia:Requests for undeletion/G13]], loaded via
* [[mw:Snippets/Load JS and CSS by URL]]. Creates a form for making requests to [[WP:RFUD]].
*
* Author: [[User:SD0001]]
* License: MIT
*/
// <nowiki>
var api, previewApi;
$.when(
$.ready,
mw.loader.using([ 'mediawiki.util', 'mediawiki.api', 'mediawiki.user' ])
).then(function() {
if (mw.config.get('wgPageName').indexOf('Wikipedia:Requests_for_undeletion/G13') !== 0) {
return;
}
api = new mw.Api();
previewApi = new mw.Api();
// Replace "Request draft undeletion" button with a form
$('.mw-ui-progressive').parent().replaceWith(
$('<div>').append(
$('<label>')
.attr('for', 'g13-page')
.text('Enter draft page title: '),
$('<input>')
.attr('id', 'g13-page')
.attr('list', 'g13-list')
.attr('size', '60')
.css('margin-bottom', '8px')
.val(mw.util.getParamValue('page') || '')
),
$('<datalist>')
.attr('id', 'g13-list'),
$('<label>')
.attr('for', 'g13-reason')
.text('Enter reason: '),
$('<textarea>')
.attr('id', 'g13-reason')
.attr('rows', 8)
.val("''Hi, " + (mw.user.isAnon() ? "" : "I'm " + mw.config.get('wgUserName') + ", and ") + "I would like to request the undeletion of this [[WP:Drafts|draft]] deleted under [[WP:CSD#G13|CSD G13]]. Please restore the page so that I can make edits to it. Thank you.'' ~~~~")
.on('keyup', updatePreview)
.on('focus', updatePreview),
$('<div>')
.attr('id', 'g13-reason-preview'),
$('<button>')
.attr('id', 'g13-submit')
.addClass('mw-ui-button mw-ui-progressive')
.text('Make request')
.css('margin-top', '5px')
.on('click', evaluate),
$('<div>')
.attr('id', 'g13-status')
);
$('#g13-reason').trigger('keyup');
// populate datalist with non-existent drafts and WT:AFC subpages linked
// from the user's talk page
api.get({
"action": "query",
"format": "json",
"titles": "User talk:" + mw.config.get('wgUserName'),
"generator": "links",
"formatversion": "2",
"gplnamespace": "118|5", // draft + Wikipedia talk
"gpllimit": "50"
}).then(function(data) {
data.query.pages.forEach(function(pg) {
if (pg.missing) {
if (pg.ns === 5 && pg.title.indexOf('Wikipedia talk:Articles for creation/') !== 0) {
return;
}
$('#g13-list').append($('<option>').attr('value', pg.title));
}
});
});
});
function updatePreview() {
var reason = $('#g13-reason').val();
previewApi.abort();
previewApi.parse(reason, { pst: true, title: 'Wikipedia:Requests for undeletion' }).then(function(parsed) {
parsed = parsed.replace(/<script/gi, '<script'); // probably unnecessary, just in case ...
$('#g13-reason-preview').html(parsed);
});
}
function evaluate() {
var page = $('#g13-page').val();
$('#g13-status').text('Checking page ...').css('color', 'blue');
checkDeletedRevisions(page).then(function(pg) {
if (pg.invalid) {
$('#g13-status').text('Page title entered is invalid').css('color', 'red');
return;
}
if (!pg.missing) {
$('#g13-status').text('This page already exists!').css('color', 'red');
return;
}
if (!pg.deletedrevisions) {
$('#g13-status').text('This page does not have any deleted history. Please check the page name.').css('color', 'red');
makeSuggestions(page);
return;
}
var text = '*{{revisions|' + pg.title + '}}';
var reason = $('#g13-reason').val();
if (reason) {
text += '\n\n' + reason;
if (reason.indexOf('~~~~') === -1) {
text += ' ~~~~';
}
}
api.newSection('Wikipedia:Requests for undeletion', pg.title, text).then(function(data) {
if (data.edit && data.edit.result === 'Success') {
$('#g13-status').text('Request filed successfully. Redirecting you to Wikipedia:Requests for undeletion#' + pg.title).css('color', 'green');
setTimeout(function() {
location.href = mw.util.getUrl('Wikipedia:Requests for undeletion#' + pg.title);
}, 1000);
} else {
return $.Deferred().reject(data);
}
}).catch(function() {
$('#g13-status').text('Failed to save request. Please try again.').css('color', 'red');
});
});
}
function checkDeletedRevisions(page) {
return api.get({
"action": "query",
"format": "json",
"prop": "deletedrevisions",
"titles": page,
"formatversion": "2",
"drvprop": "ids|timestamp|user"
}).then(function(data) {
return data.query.pages[0];
});
}
function makeSuggestions(page) {
var draftpage = mw.Title.newFromText(page);
draftpage.namespace = 118;
var handleDidYouMean = function(pg) {
if (pg.deletedrevisions) {
$('#g13-status').append('<br>Did you mean <code>' + pg.title + '</code>? ',
$('<button>').text('Yes').addClass('mw-ui-button').on('click', function() {
$('#g13-page').val(pg.title);
$('#g13-submit').click();
})
);
}
};
checkDeletedRevisions(draftpage.toString()).then(handleDidYouMean);
checkDeletedRevisions('Wikipedia talk:Articles for creation/' + draftpage.getMain()).then(handleDidYouMean);
}
// </nowiki>