Jump to content

MediaWiki:G13-restore-wizard.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.
/**
 * 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-parser-output .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, '&lt;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>