User:Chaotic Enby/Unblock wizard redirect.js

From Test Wiki
Revision as of 15:14, 22 June 2025 by Chaotic Enby (talk | contribs) (test)
Jump to navigation Jump to search

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 = {}, block = {};

// For each block template, links the relevant subpage
var blockTemplates = {
	"uw-adblock": ["Promo", "Promotional editing"],
	"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];
			location.href = mw.Title.newFromText( targetPage ).getUrl();
			ui.itemsLayout.push(new OO.ui.FieldLayout(new OO.ui.LabelWidget({
				label: $('<div>').append(linkify("Your current block status:<br>Blocked for " + blockTemplates[reason][1].toLowerCase()))}), { align: 'top' }));
			ui.itemsLayout.push(ui.submitLayout = new OO.ui.FieldLayout(ui.submitButton = new OO.ui.ButtonWidget({
				label: "Appeal my " + blockTemplates[reason][1].toLowerCase() + " block", flags: [ 'progressive', 'primary' ]})));
			ui.itemsLayout.push(ui.submitLayout = new OO.ui.FieldLayout(ui.submitButton = new OO.ui.ButtonWidget({
				label: "Learn more about " + blockTemplates[reason][1].toLowerCase() + " blocks", flags: [ 'progressive', 'primary' ]})));
			ui.itemsLayout.push(ui.submitLayout = new OO.ui.FieldLayout(ui.submitButton = new OO.ui.ButtonWidget({
				label: "Appeal a different kind of block", flags: [ 'primary' ]})));
		}
		else if(reason === null) { // Not blocked, this check is needed to avoid cases where the block reason is empty
			// Check for global blocks
			// Perform a check on the underlying IP
			// Add a "You are not currently blocked" message
			// Link to demo mode
		}
		else {
			// Link to the current buttons
		}
		ui.fieldset = new OO.ui.FieldsetLayout({
			classes: [ 'container' ],
			items: ui.itemsLayout
		});
		$('#unblock-wizard-container').empty().append(ui.fieldset.$element);
	});
	
	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;
	}
}

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

/* </nowiki> */