MediaWiki:Gadget-Adiutor-Loader.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/*
 * Adiutor: Adiutor enables versatile editing options and modules to assist a variety of user actions to enhance the Wikipedia editing experience.
 * Author: Vikipolimer
 * Learn more at: https://meta.wikimedia.org/wiki/Adiutor
 * Licensing and Attribution: Licensed under Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
 * Module: Loader
 */
/* <nowiki> */
// Get essential configuration from MediaWiki
var mwConfig = mw.config.get(["skin", "wgAction", "wgArticleId", "wgPageName", "wgNamespaceNumber", "wgUserName", "wgTitle", "wgUserGroups", "wgUserEditCount", "wgUserRegistration", "wgRelevantUserName", "wgCanonicalNamespace", "wgCanonicalSpecialPageName"]);
var api = new mw.Api();
var wikiId = mw.config.get('wgWikiID');
var adiutorUserOptions = JSON.parse(mw.user.options.get('userjs-adiutor-'+wikiId));
var DefaultMenuItems = [];
switch(mwConfig.wgNamespaceNumber) {
	case -1:
	case 0:
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:
	case 6:
	case 7:
	case 14:
	case 10:
	case 11:
	case 100:
	case 101:
	case 102:
	case 103:
	case 828:
	case 829:
		// LOAD MODULES
		if(mwConfig.wgNamespaceNumber === 3) {
			var UserParams = {
				action: 'query',
				meta: 'userinfo',
				uiprop: 'rights',
				format: 'json'
			};
			api.get(UserParams).then(function(data) {
				checkMentor(data.query.userinfo.id);
			});
		}
		if(mwConfig.wgUserGroups.includes('sysop')) {
			if(!mwConfig.wgCanonicalSpecialPageName) {
				DefaultMenuItems.push(new OO.ui.MenuOptionWidget({
					icon: 'trash',
					data: 'delete',
					label: new OO.ui.deferMsg('delete'),
					flags: ['destructive'],
					classes: ['adiutor-top-user-menu-end'],
				}));
				if(mwConfig.wgNamespaceNumber != 0) {
					if(mwConfig.wgPageName.includes('Candidates_for_speedy_deletion') || mwConfig.wgPageName.includes('Advertisements_for_speedy_deletion') || mwConfig.wgPageName.includes('Categories_with_incorrect_category_names') || mwConfig.wgPageName.includes('Copyright_violations') || mwConfig.wgPageName.includes('Deletion_error') || mwConfig.wgPageName.includes('Duplicate') || mwConfig.wgPageName.includes('Files_with_suspect_embedded_data') || mwConfig.wgPageName.includes('Image_crop_missing_parent_page') || mwConfig.wgPageName.includes('Other_speedy_deletions') || mwConfig.wgPageName.includes('Personal_files_for_speedy_deletion') || mwConfig.wgPageName.includes('Unknown') || mwConfig.wgPageName.includes('Uploads_by_Fæ_needing_speedy_deletion') || mwConfig.wgPageName.includes('Uploads_from_Pexels.com_needing_speedy_deletion')) {
						DefaultMenuItems.push(new OO.ui.MenuOptionWidget({
							icon: 'trash',
							data: 'batch-delete',
							label: new OO.ui.deferMsg('batch-delete'),
							flags: ['destructive'],
							classes: ['adiutor-top-user-menu-end'],
						}));
					}
				}
			}
			if(mwConfig.wgCanonicalSpecialPageName === 'Contributions' || mwConfig.wgNamespaceNumber === 2 || mwConfig.wgNamespaceNumber === 3 && !mwConfig.wgPageName.includes(mwConfig.wgUserName)) {
				if(mwConfig.wgUserGroups.includes('sysop')) {
					DefaultMenuItems.push(new OO.ui.MenuOptionWidget({
						icon: 'block',
						data: 'block',
						label: new OO.ui.deferMsg('block'),
						classes: ['adiutor-top-user-menu-end'],
					}));
				}
			}
		}
		if(mwConfig.wgUserGroups.includes('sysop')) {
			if(/(?:\?|&)(?:action|diff|oldid)=/.test(window.location.href)) {
				DefaultMenuItems.push(new OO.ui.MenuOptionWidget({
					icon: 'cancel',
					data: 'rdr',
					label: new OO.ui.deferMsg('create-revision-deletion-request'),
					classes: ['adiutor-top-rrd-menu'],
				}));
			}
		}
		if(mwConfig.wgCanonicalSpecialPageName === 'Contributions' || mwConfig.wgNamespaceNumber === 2 || mwConfig.wgNamespaceNumber === 3 && !mwConfig.wgPageName.includes(mwConfig.wgUserName)) {
			// Add common buttons
			DefaultMenuItems.push(new OO.ui.MenuOptionWidget({
				icon: 'cancel',
				data: 'report',
				label: new OO.ui.deferMsg('report'),
				classes: ['adiutor-top-user-menu-end'],
			}), new OO.ui.MenuOptionWidget({
				icon: 'hand',
				data: 'warn',
				label: new OO.ui.deferMsg('warn'),
				classes: ['adiutor-top-user-menu-end'],
			}));
		}
		if(!mwConfig.wgCanonicalSpecialPageName) {
			DefaultMenuItems.push(new OO.ui.MenuOptionWidget({
				icon: 'add',
				data: 1,
				label: mw.msg('create-speedy-deletion-request'),
			}), new OO.ui.MenuOptionWidget({
				icon: 'add',
				data: 3,
				label: mw.msg('nominate-file-for-deletion'),
			}), new OO.ui.MenuOptionWidget({
				icon: 'arrowNext',
				data: 'pmr',
				label: mw.msg('page-move-request'),
			}), new OO.ui.MenuOptionWidget({
				icon: 'lock',
				data: 'rpp',
				label: mw.msg('page-protection-request'),
			}), new OO.ui.MenuOptionWidget({
				icon: 'alert',
				data: 8,
				label: 'Report Copyright Violation',
			}), new OO.ui.MenuOptionWidget({
				icon: 'history',
				data: 4,
				label: mw.msg('recent-changes'),
			}), new OO.ui.MenuOptionWidget({
				icon: 'templateAdd',
				data: 'tag',
				label: mw.msg('tag-page'),
			}), new OO.ui.MenuOptionWidget({
				icon: 'checkAll',
				data: 5,
				label: mw.msg('copyright-violation-check'),
			}), new OO.ui.MenuOptionWidget({
				icon: 'settings',
				data: 6,
				label: mw.msg('adiutor-settings'),
				classes: ['adiutor-top-settings-menu'],
			}));
		}
		if(mwConfig.wgCanonicalSpecialPageName) {
			DefaultMenuItems.push(new OO.ui.MenuOptionWidget({
				icon: 'settings',
				data: 6,
				label: mw.msg('adiutor-settings'),
				classes: ['adiutor-top-settings-menu'],
			}));
		}
		var adiutorMenu = new OO.ui.ButtonMenuSelectWidget({
			icon: 'ellipsis',
			invisibleLabel: true,
			framed: false,
			title: 'More options',
			align: 'force-right',
			classes: ['adiutor-top-selector', 'mw-indicator'],
			menu: {
				horizontalPosition: 'end',
				items: DefaultMenuItems,
				classes: ['adiutor-top-menu'],
			}
		});
		// Define a variable to track if the menu is open
		var isMenuOpen = false;
		// Listen for mouseover event on the Adiutor menu button
		adiutorMenu.$element.on('mouseover', function() {
			// Open the menu programmatically
			adiutorMenu.getMenu().toggle(true);
			isMenuOpen = true;
		});
		// Listen for mouseout event on the Adiutor menu button
		adiutorMenu.$element.on('mouseout', function(event) {
			// Check if the mouse is leaving the menu area
			if(!event.relatedTarget || !$(event.relatedTarget).closest('.adiutor-top-selector, .adiutor-top-menu').length) {
				adiutorMenu.getMenu().toggle(false);
				isMenuOpen = false;
			}
		});
		// Listen for mouseout event on the entire document
		$(document).on('mouseout', function(event) {
			// Check if the mouse is leaving the menu area
			if(!event.relatedTarget || !$(event.relatedTarget).closest('.adiutor-top-selector, .adiutor-top-menu').length) {
				adiutorMenu.getMenu().toggle(false);
				isMenuOpen = false;
			}
		});
		// Define a function to load Adiutor scripts
		// Listen for menu option selection
		adiutorMenu.getMenu().on('choose', function(menuOption) {
			// Map option values to corresponding Adiutor script names
			var optionMapping = {
				1: 'CSD',
				2: 'PRD',
				3: 'RFD',
				4: 'diff',
				5: 'COV',
				6: 'OPT',
				7: 'INF',
				8: 'RCV',
				'report': 'AIV',
				'block': 'UBM',
				'warn': 'WRN',
				'rdr': 'RDR',
				'pmr': 'PMR',
				'rpp': 'RPP',
				'tag': 'TAG',
				'gan': 'GAN',
				'fan': 'FAN',
				'delete': 'DEL',
				'batch-delete': 'BDM'
			};
			// Get the selected option's corresponding script name
			var selectedOption = optionMapping[menuOption.getData()];
			// Handle different actions based on the selected option
			if(selectedOption === 'diff') {
				// Redirect to a specific page with parameters
				window.location = '/w/index.php?title=' + mwConfig.wgPageName + "&diff=cur&oldid=prev&diffmode=source";
			} else if(selectedOption === 'welcome') {
				// Show an alert for the 'welcome' option
				OO.ui.alert('Coming soon :)').done(function() {});
			} else {
				// Load the Adiutor script based on the selected option
				loadAdiutorScript(selectedOption);
			}
		});
		//Call the packages to be pre-loaded here
		if(mwConfig.wgNamespaceNumber === 2) {
			loadAdiutorScript('UPW');
		}
		switch(mwConfig.skin) {
			case 'vector':
				$('.mw-portlet-cactions').parent().append(adiutorMenu.$element);
				break;
			case 'vector-2022':
				$('.vector-collapsible').append(adiutorMenu.$element);
				break;
			case 'monobook':
				$('#pt-notifications-notice').append(adiutorMenu.$element);
				break;
			case 'timeless':
				$('.mw-portlet-body').append(adiutorMenu.$element);
				break;
			case 'minerva':
				$('.page-actions-menu__list').append(adiutorMenu.$element);
				break;
		}
		if(mwConfig.wgNamespaceNumber === 4) {
			if(mwConfig.wgPageName.includes('Commons:Deletion_requests/File:')) {
				var afdButtons;
				if(mwConfig.wgUserGroups.includes("sysop")) {
					afdButtons = new OO.ui.ButtonGroupWidget({
						items: [new OO.ui.ButtonWidget({
							icon: 'speechBubbleAdd',
							label: 'Opinion',
							invisibleLabel: true,
							title: 'Submit Opinion',
							classes: ['afd-helper-button'],
							framed: false
						}), new OO.ui.ButtonWidget({
							icon: 'editLock',
							invisibleLabel: true,
							classes: ['afd-closer-button'],
							framed: false
						})],
						classes: ['afd-helper-button-group']
					});
				} else {
					afdButtons = new OO.ui.ButtonGroupWidget({
						items: [new OO.ui.ButtonWidget({
							icon: 'speechBubbleAdd',
							label: 'Opinion',
							invisibleLabel: true,
							title: 'Submit Opinion',
							classes: ['afd-helper-button'],
							framed: false
						})],
						classes: ['afd-helper-button-group']
					});
				}
				afdButtons.$element.css({
					'margin-top': '2px',
				});
				$('.vector-collapsible').append(afdButtons.$element);
				var submitOpinionButton = afdButtons.getItems()[0];
				var submitCloserButton = afdButtons.getItems()[1];
				submitOpinionButton.on('click', function() {
					loadAdiutorScript('RFD-Opinion');
				});
				submitCloserButton.on('click', function() {
					loadAdiutorScript('RFD-Finalizer');
				});
			}
		}
}
if(adiutorUserOptions.showEditSummaries === true) {
	loadAdiutorScript('SUM');
}

function loadAdiutorScript(scriptName) {
	var scriptURL = 'https://commons.wikimedia.org/wiki/MediaWiki:Gadget-Adiutor-' + scriptName + '.js?action=raw&ctype=text/javascript';
	mw.loader.load(scriptURL, 'text/javascript');
}

function checkMentor(UserId) {
	api.get({
		action: 'parse',
		page: "MediaWiki:GrowthMentors.json",
		prop: 'wikitext',
		format: "json"
	}).done(function(data) {
		if(data.parse.wikitext['*'].includes(UserId) && mwConfig.wgPageName.includes(mwConfig.wgUserName)) {
			// Load the Adiutor CMR script using the loadAdiutorScript function
			loadAdiutorScript('CMR');
		}
	});
}
/* </nowiki> */