User:Chaotic Enby/Unblock wizard redirect.js

From Test Wiki
Revision as of 18:02, 22 June 2025 by Chaotic Enby (talk | contribs)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/**
 * MediaWiki:Unblock-wizard-redirect.js
 *
 * JavaScript used for submitting unblock requests.
 * Additional script for MediaWiki:Unblock-wizard.js parsing blocks and redirecting users to the correct page.
 * Used on [[Wikipedia:Unblock wizard]].
 * Loaded via [[mw:Snippets/Load JS and CSS by URL]].
 * 
 * Edits can be proposed via [[Wikipedia talk:Unblock wizard]].
 *
 * Author: [[User:Chaotic Enby]] (derived from a script by [[User:SD0001]])
 * Licence: MIT
 */

/* jshint maxerr: 999 */
/* globals mw, $, OO */
/* <nowiki> */

(function () {

$.when(
	$.ready,
	mw.loader.using([
		'mediawiki.util', 'mediawiki.api', 'mediawiki.Title',
		'mediawiki.widgets', 'oojs-ui-core', 'oojs-ui-widgets'
	])
).then(function () {
	if (!(mw.config.get('wgPageName') == 'Wikipedia:Unblock_wizard' || mw.config.get('wgPageName') == 'User:Chaotic_Enby/Unblock_wizard') ||
		mw.config.get('wgAction') !== 'view') {
		return;
	}
	init();
});

var wizard = {}, ui = {}, block = {};
window.wizard = wizard;
wizard.ui = ui;
var demoMode = false;

// For each block template, links the relevant subpage
var blockTemplates = {
	"uw-adblock": ["Promo", "Promotional editing", "You have been blocked for [[Wikipedia:What Wikipedia is not#Wikipedia is not a soapbox|advertising or self-promoting]] in violation of the [[Wikipedia:Conflict of interest|conflict of interest]] and [[Wikipedia:Notability (organizations and companies)|notability]] guidelines."],
	"uw-asblock": ["Promo", "Promotional editing", ""],
	"uw-spamublock": ["Promo", "Promotional editing", ""],
	"uw-soablock": ["Promo", "Promotional editing", ""],
	"uw-sockblock": ["Sockpuppet", "Sockpuppetry", ""],
	"uw-upeblock": ["Promo", "Promotional editing", ""],
};

function init() {
	var apiOptions = {
		parameters: {
			format: 'json',
			formatversion: '2'
		},
		ajax: {
			headers: {
				'Api-User-Agent': 'w:en:MediaWiki:Unblock-wizard-redirect.js'
			}
		}
	};
	
	wizard.api = new mw.Api(apiOptions);
	wizard.lookupApi = new mw.Api(apiOptions);
	
	wizard.lookupApi.get({
		"action": "query",
		"meta": "userinfo",
		"uiprop": "blockinfo"
	}).then( setBlockData ).then( function ( reason ) {
		ui.itemsLayout = [];
		if(reason && reason in blockTemplates) {
			targetPage = mw.config.get('wgPageName') + '/' + blockTemplates[reason][0];
			targetUrl = mw.Title.newFromText( targetPage ).getUrl();
			ui.itemsLayout.push(new OO.ui.FieldLayout(new OO.ui.LabelWidget({
				label: $('<div>').css("margin-top", "20px").append(linkify('<span style="font-size: 125%">Your current block status:</span><br><span style="font-size: 175%">Blocked for ' + blockTemplates[reason][1].toLowerCase() + '</span><br>' + blockTemplates[reason][2]))}), { align: 'top' }));
			generateButton(ui.itemsLayout, "Appeal my " + blockTemplates[reason][1].toLowerCase() + " block", true);
			generateButton(ui.itemsLayout, "Learn more about " + blockTemplates[reason][1].toLowerCase() + " blocks", true);
			generateButton(ui.itemsLayout, "Appeal a different kind of block", false);
		}
		else if(reason === null) { // Not blocked, this check is needed to avoid cases where the block reason is empty
			// TODO: Perform a check on the underlying IP
			// TODO: Check the underlying IP for global blocks
			
			// Add a "You are not currently blocked" message
			ui.itemsLayout.push(new OO.ui.FieldLayout(new OO.ui.LabelWidget({
				label: $('<div>').css("margin-top", "20px").append(linkify('<span style="font-size: 175%">You are not currently blocked</span><br>You can still test the unblock wizard in <b>demo mode</b>, which will allow you to look at the code generated by the wizard without posting it on your talk page.'))}), { align: 'top' }));
			
			// Link to demo mode
			generateButton(ui.itemsLayout, "Enter demo mode", true, activateDemoMode);
			generateButton(ui.itemsLayout, "My block was not detected", false, activateDemoMode);
		}
		else {
			// Link to the current buttons
			generateDefaultButtons(ui.itemsLayout);
		}
		ui.fieldset = new OO.ui.FieldsetLayout({
			classes: [ 'container' ],
			items: ui.itemsLayout
		});
		$('#unblock-wizard-container').empty().append(ui.fieldset.$element);
	});
	
	function activateDemoMode() {
		demoMode = true;
		ui.itemsLayout = [];
		generateDefaultButtons(ui.itemsLayout, args="");
		ui.fieldset = new OO.ui.FieldsetLayout({
			classes: [ 'container' ],
			items: ui.itemsLayout
		});
		$('#unblock-wizard-container').empty().append(ui.fieldset.$element);
	}
	
	function activateDefaultMode() {
		ui.itemsLayout = [];
		generateDefaultButtons(ui.itemsLayout);
		ui.fieldset = new OO.ui.FieldsetLayout({
			classes: [ 'container' ],
			items: ui.itemsLayout
		});
		$('#unblock-wizard-container').empty().append(ui.fieldset.$element);
	}
	
	function generateButton(itemsLayout, label, isProgressive, buttonAction=null) {
		itemsLayout.push(new OO.ui.FieldLayout(button = new OO.ui.ButtonWidget({
			label: label, flags: (isProgressive ? [ 'progressive', 'primary' ] : [ ])})));
		if (buttonAction) {
			button.on('click', buttonAction);
		}
	}
	
	function generateDefaultButtons(itemsLayout, args="") {
		ui.itemsLayout.push(new OO.ui.FieldLayout(new OO.ui.LabelWidget({
				label: $('<div>').append(linkify('Which of the following best describes your current situation?'))}), { align: 'top' }));
		generateButton(ui.itemsLayout, "I was blocked for being a sockpuppet", true);
		generateButton(ui.itemsLayout, "I was blocked for promotional activity", true);
		generateButton(ui.itemsLayout, "I was \"automatically blocked\"", true);
		generateButton(ui.itemsLayout, "My IP address has been blocked", true);
		generateButton(ui.itemsLayout, "I was blocked for something else", true);
		generateButton(ui.itemsLayout, "I was blocked globally", true);
		generateButton(ui.itemsLayout, "I would like to know why I was blocked", false);
	}
	
	function setBlockData(json) {
		var userinfo = json.query.userinfo;
		if(userinfo && "blockid" in userinfo){
			block.id = userinfo.blockid;
			block.by = userinfo.blockedby;
			block.reason = userinfo.blockreason;
			block.template = block.reason.match(/\{\{(.*)\}\}/)[1].toLowerCase();
			return block.template;
		}
		return null;
	}
}

function linkify(input) {
	return input
		.replace(/\{\{pb\}\}/g, '<br>')
		.replace(
			/\[\[:?(?:([^|\]]+?)\|)?([^\]|]+?)\]\]/g,
			function(_, target, text) {
				if (!target) {
					target = text;
				}
				return '<a target="_blank" href="' + mw.util.getUrl(target) +
					'" title="' + target.replace(/"/g, '&#34;') + '">' + text + '</a>';
			}
		)
		// for ext links, display text should be given
		.replace(
			/\[(\S*?) (.*?)\]/g,
			function (_, target, text) {
				return '<a target="_blank" href="' + target + '">' + text + '</a>';
			}
		);
}

})(); // File-level closure to protect functions from being exposed to the global scope or overwritten

/* </nowiki> */