MediaWiki:Gadget-Adiutor-TAG.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: Article Tagging
 */
/* <nowiki> */
// Get essential configuration from MediaWiki
var mwConfig = mw.config.get(["skin", "wgAction", "wgArticleId", "wgPageName", "wgNamespaceNumber", "wgTitle", "wgUserGroups", "wgUserName", "wgUserEditCount", "wgUserRegistration", "wgCanonicalNamespace"]);
var api = new mw.Api();
var wikiId = mw.config.get('wgWikiID');
var adiutorUserOptions = JSON.parse(mw.user.options.get('userjs-adiutor-'+wikiId));
var preparedTags;

function PageTaggingDialog(config) {
	PageTaggingDialog.super.call(this, config);
}
OO.inheritClass(PageTaggingDialog, OO.ui.ProcessDialog);
PageTaggingDialog.static.name = 'PageTaggingDialog';
PageTaggingDialog.static.title = new OO.ui.deferMsg('tag-module-title');
PageTaggingDialog.static.actions = [{
	action: 'save',
	label: new OO.ui.deferMsg('add-tag'),
	flags: ['primary', 'progressive']
}, {
	label: new OO.ui.deferMsg('cancel'),
	flags: 'safe'
}];
PageTaggingDialog.prototype.initialize = function() {
	PageTaggingDialog.super.prototype.initialize.apply(this, arguments);
	var headerTitle = new OO.ui.MessageWidget({
		type: 'notice',
		inline: true,
		label: new OO.ui.deferMsg('tag-header-description')
	});
	// Calculate the current month and year for tagging
	const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
	const dateToday = new Date();
	const markDate = monthNames[dateToday.getMonth()] + ' ' + dateToday.getFullYear();
	// Create a new Date object to get the current date
	var currentDate = new Date();
	// Get the current day, month, and year
	var dayValue = currentDate.getDate().toString().padStart(2, '0'); // Get day with leading zero if necessary (e.g., '05')
	var monthValue = (currentDate.getMonth() + 1).toString().padStart(2, '0'); // Months are zero-based, so add 1 and format (e.g., '09' for September)
	var yearValue = currentDate.getFullYear().toString(); // Get the full year (e.g., '2023')
	var tagOptions = [{
		data: '{{No source since |day=' + dayValue + ' |month=' + monthValue + ' |year=' + yearValue + ' }}',
		label: 'No source since'
	}, {
		data: '{{Dw no source since}}',
		label: 'Dw no source since'
	}, {
		data: '{{No permission since}}',
		label: 'No permission since'
	}, {
		data: '{{No license since |year=' + yearValue + ' |month=' + monthValue + ' |day=' + dayValue + ' }}',
		label: 'No license since'
	}, {
		data: '{{Uncategorized|date=' + markDate + '}}',
		label: 'Uncategorized page'
	}, {
		data: '{{Non-free audiod|date=' + markDate + '}}',
		label: 'Non-Essential Audio Usage Advisory'
	}, {
		data: '{{Fact disputed|date=' + markDate + '}}',
		label: 'This description or file name is under dispute.'
	}, {
		data: '{{Factual accuracy|date=' + markDate + '}}',
		label: 'The factual accuracy of this file is disputed.'
	}, {
		data: '{{Bad name|date=' + markDate + '}}',
		label: 'File under a bad name.'
	}, {
		data: '{{Disputed|date=' + markDate + '}}',
		label: 'It is disputed whether the copyright tag on this file is correct.'
	}, ];
	// Create a search input field
	var searchInput = new OO.ui.TextInputWidget({
		placeholder: mw.msg('search-tag')
	});
	searchInput.$element.css({
		'margin-top': '20px',
	});
	var selectedTags = [];
	// Filter the tag options based on search input
	searchInput.on('change', function() {
		var searchText = searchInput.getValue().toLowerCase();
		var filteredOptions = tagOptions.filter(function(option) {
			return option.label.toLowerCase().includes(searchText);
		});
		Tags.setOptions(filteredOptions);
		// Check selected tags
		Tags.setValue(selectedTags);
	});
	var Tags = new OO.ui.CheckboxMultiselectInputWidget({
		options: tagOptions, // Use the tagOptions array here
		classes: ['adiutor-tag-list-container']
	});
	Tags.$element.on('click', function() {
		if(Tags.getValue().length > 1) {
			selectedTags = Tags.getValue(); // Assign selected tags to array
			saltTags = String(Tags.getValue());
			preparedTags = "{{Multiple issues|" + "\n" + saltTags.split(",").join("\n") + "\n" + "}}" + "\n";
			console.log(preparedTags);
		} else {
			selectedTags = Tags.getValue();
			saltTags = String(Tags.getValue());
			preparedTags = saltTags.split(",").join("\n") + "\n";
			console.log(preparedTags);
		}
	});
	Tags.$element.on('click', function() {
		if(Tags.getValue().length > 1) {} else {}
	});
	this.content = new OO.ui.PanelLayout({
		padded: true,
		expanded: false,
	});
	this.content.$element.append(headerTitle.$element, searchInput.$element, '<br>', Tags.$element);
	this.$body.append(this.content.$element);
};
PageTaggingDialog.prototype.getActionProcess = function(action) {
	var dialog = this;
	if(action) {
		return new OO.ui.Process(function() {
			tagPage(preparedTags);
			dialog.close({
				action: action
			});
		});
	}
	return PageTaggingDialog.super.prototype.getActionProcess.call(this, action);
};
var windowManager = new OO.ui.WindowManager();
$(document.body).append(windowManager.$element);
var dialog = new PageTaggingDialog();
windowManager.addWindows([dialog]);
windowManager.openWindow(dialog);

function tagPage(preparedTags) {
	var editParams = {
		action: 'edit',
		title: mwConfig.wgPageName,
		summary: 'Page tagged',
		tags: 'Adiutor',
		format: 'json'
	};
	var templateRegex = /\{\{Uncategorized\|date=[^\}]+\}\}/;
	var removedContent = "";
	var modifiedTags = preparedTags.replace(templateRegex, function(match) {
		removedContent = match;
		return "";
	});
	if(removedContent) {
		editParams.prependtext = modifiedTags.split(',').join('\n') + '\n';
		editParams.appendtext = '\n' + removedContent;
	} else {
		editParams.prependtext = modifiedTags.split(',').join('\n') + '\n';
	}
	api.postWithToken('csrf', editParams).done(function() {
		adiutorUserOptions.stats.pageTags++;
		api.postWithEditToken({
			action: 'globalpreferences',
			format: 'json',
			optionname: 'userjs-adiutor',
			optionvalue: JSON.stringify(adiutorUserOptions),
			formatversion: 2,
		}).done(function() {});
		location.reload();
	});
}
/* </nowiki> */