User:SD0001/G13-restore-wizard2.js

This article will address User:SD0001/G13-restore-wizard2.js from different perspectives, in order to offer readers a comprehensive and detailed view on this topic. Relevant aspects will be analyzed, relevant data will be presented and various opinions from experts in the field will be offered. User:SD0001/G13-restore-wizard2.js is a topic that arouses great interest and curiosity in today's society, so it is essential to delve into its study to understand its importance and impact in different areas. Throughout this article, different facets of User:SD0001/G13-restore-wizard2.js will be explored, with the purpose of providing readers with a complete and enriching overview of this topic.
/**
 * MediaWiki:G13-restore-wizard.js
 * 
 * Script for ], loaded via 
 * ]. Creates a form for making requests to ].
 * 
 * Author: ]
 * License: MIT
 */


// <nowiki>

var api, previewApi;

$.when(
	$.ready,
	mw.loader.using()
).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 ] deleted under ]. 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;
	});
}

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>