MediaWiki:Gadget-Adiutor-RCV.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: Report copyvio
 */
/* <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 CopyVioTemplate;
var ActionOptions = [];

function CopyVioReportDialog(config) {
	CopyVioReportDialog.super.call(this, config);
}
OO.inheritClass(CopyVioReportDialog, OO.ui.ProcessDialog);
CopyVioReportDialog.static.name = 'CopyVioReportDialog';
CopyVioReportDialog.static.title = 'Adiutor - Copyright Violation Report';
CopyVioReportDialog.static.actions = [{
	action: 'save',
	label: new OO.ui.deferMsg('continue'),
	flags: ['primary', 'progressive']
}, {
	label: new OO.ui.deferMsg('cancel'),
	flags: 'safe'
}];
CopyVioReportDialog.prototype.initialize = function() {
	CopyVioReportDialog.super.prototype.initialize.apply(this, arguments);
	var headerTitle = new OO.ui.MessageWidget({
		type: 'warning',
		inline: true,
		label: 'You are reporting this file'
	});
	var headerTitleDescription = new OO.ui.LabelWidget({
		label: 'You are presently labeling this file due to concerns about potential copyright infringement. If you are uncertain about the accuracy of this action, we kindly request that you seek clarification from the uploader before officially reporting it as such.'
	});
	headerTitleDescription.$element.css({
		'margin-top': '20px',
		'margin-bottom': '20px'
	});
	AfDOptions = new OO.ui.FieldsetLayout({});
	AfDOptions.addItems([
		rationaleField = new OO.ui.FieldLayout(rationaleInput = new OO.ui.MultilineTextInputWidget({
			placeholder: 'Why is this file a copyright violation?',
			indicator: 'required',
			value: '',
		}), {
			label: new OO.ui.deferMsg('rationale'),
			align: 'inline',
		}),
		sourceField = new OO.ui.FieldLayout(sourceInput = new OO.ui.TextInputWidget({
			value: '',
			placeholder: 'Please add the original file url here.',
			indicator: 'required',
		}), {
			label: 'Source',
			help: 'Copy violation source'
		}),
		new OO.ui.FieldLayout(new OO.ui.ToggleSwitchWidget({
			value: false,
			data: 'informCreator'
		}), {
			label: new OO.ui.deferMsg('afd-inform-creator'),
			align: 'top',
			help: new OO.ui.deferMsg('afd-inform-creator-help'),
		}),
	]);
	rationaleField.$element.css('font-weight', '900');
	this.content = new OO.ui.PanelLayout({
		padded: true,
		expanded: false,
		isDraggable: true
	});
	this.content.$element.append(headerTitle.$element, headerTitleDescription.$element, AfDOptions.$element);
	this.$body.append(this.content.$element);
};
CopyVioReportDialog.prototype.getActionProcess = function(action) {
	var dialog = this;
	if(action) {
		return new OO.ui.Process(function() {
			AfDOptions.items.forEach(function(Option) {
				if(Option.fieldWidget.selected) {
					ActionOptions.push({
						value: Option.fieldWidget.value,
						selected: Option.fieldWidget.selected
					});
				}
				if(Option.fieldWidget.value === true) {
					ActionOptions.push({
						value: Option.fieldWidget.value,
						data: Option.fieldWidget.data
					});
				}
			});
			ActionOptions.forEach(function(Option) {
				if(Option.data === "informCreator") {
					console.log(Option.data);
					getCreator().then(function(data) {
						console.log('Mesaj gönderebilirsin');
						var Author = data.query.pages[mw.config.get('wgArticleId')].revisions[0].user;
						if(!mw.util.isIPAddress(Author)) {
							var message = '{{subst:Copyvionote |1=' + mwConfig.wgPageName.replace(/_/g, " ") + ' |2=' + rationaleInput.value + ' |3=' + sourceInput.value + '}} ~~~~';
							console.log(message);
							sendMessageToAuthor(Author, message);
						}
					});
				}
			});
			CopyVioTemplate = '{{Copyvio |1=' + rationaleInput.value + ' |source=' + sourceInput.value + ' }}';
			console.log(CopyVioTemplate);
			putTemplate(CopyVioTemplate);
			dialog.close({
				action: action
			});
			showProgress();
		});
	}
	return CopyVioReportDialog.super.prototype.getActionProcess.call(this, action);
};
var windowManager = new OO.ui.WindowManager();
$(document.body).append(windowManager.$element);
var dialog = new CopyVioReportDialog({
	size: 'large',
	classes: ['afd-helper-window'],
	isDraggable: true
});
windowManager.addWindows([dialog]);
windowManager.openWindow(dialog);

function putTemplate(CopyVioTemplate) {
	api.postWithToken('csrf', {
		action: 'edit',
		title: mwConfig.wgPageName,
		prependtext: CopyVioTemplate + "\n",
		summary: 'The page has been nominated for deletion',
		tags: 'Adiutor',
		format: 'json'
	}).done(function() {
		dialog.close({
			action: action
		});
	});
}

function getCreator() {
	return api.get({
		action: 'query',
		prop: 'revisions',
		rvlimit: 1,
		rvprop: ['user'],
		rvdir: 'newer',
		titles: mwConfig.wgPageName.replace(/_/g, " ")
	});
}

function sendMessageToAuthor(Author, message) {
	api.postWithToken('csrf', {
		action: 'edit',
		title: 'User_talk:' + Author,
		appendtext: "\n" + message + "\n",
		summary: '[[' + mwConfig.wgPageName.replace(/_/g, " ") + ']]' + ' has been nominated for deletion',
		format: 'json',
	}).done(function() {});
}

function showProgress() {
	var processStartedDialog = new OO.ui.MessageDialog();
	var progressBar = new OO.ui.ProgressBarWidget();
	var windowManager = new OO.ui.WindowManager();
	$(document.body).append(windowManager.$element);
	windowManager.addWindows([processStartedDialog]);
	windowManager.openWindow(processStartedDialog, {
		title: mw.msg('processing'),
		message: progressBar.$element
	});
}
/* </nowiki> */