MediaWiki:Gadget-Adiutor-AFD.js

Vikipediya, ochiq ensiklopediya

Eʼtibor bering: Oʻzgartirishlaringizni koʻrish uchun, yangi moslamalaringizning saqlashdan keyin, brauzer keshini tozalash kerak:
Mozilla / Firefox: Ctrl+Shift+R, IE: Ctrl+F5, Safari: Cmd+Shift+R, Konqueror: F5, Opera: Tools → Preferences orqali keshni tozalang.

/* 
 * Adiutor: A gadget to assist various user actions
 * Author: Vikipolimer
 * Licencing and attribution: [[:tr:VP:Adiutor#Lisanslama ve atıf]]
 * Module: Article for deletion
 */
/* <nowiki> */
$.when(mw.loader.using(["mediawiki.user", "oojs-ui-core", "oojs-ui-windows", ]), $.ready).then(function() {
	var mwConfig = mw.config.get(["wgAction", "wgPageName", "wgTitle", "wgUserGroups", "wgUserName", "wgCanonicalNamespace", "wgNamespaceNumber"]);
	var api = new mw.Api();
	var NominatedPreviously;
	var nextNominationNumber = 0;
	var afdSendMessageToCreator = localStorage.getItem("afdSendMessageToCreator") == "true";
	var afdLogNominatedPages = localStorage.getItem("afdLogNominatedPages") == "true";
	console.log(afdLogNominatedPages);

	function ArticleForDeletionDialog(config) {
		ArticleForDeletionDialog.super.call(this, config);
	}
	OO.inheritClass(ArticleForDeletionDialog, OO.ui.ProcessDialog);
	ArticleForDeletionDialog.static.name = 'ArticleForDeletionDialog';
	ArticleForDeletionDialog.static.title = 'Adiutor — Oʻchirishga';
	ArticleForDeletionDialog.static.actions = [{
		action: 'save',
		label: 'Davom etish',
		flags: ['primary', 'progressive']
	}, {
		label: 'Yopish',
		flags: 'safe'
	}];
	ArticleForDeletionDialog.prototype.initialize = function() {
		ArticleForDeletionDialog.super.prototype.initialize.apply(this, arguments);
		var headerTitle = new OO.ui.MessageWidget({
			type: 'notice',
			inline: true,
			label: new OO.ui.HtmlSnippet('<strong>Sahifani oʻchirishga</strong><br><small>Sahifa tezda oʻchirish uchun lozim boʻlgan mezonlarga javob bermasa, uni oʻchirish masalasini boshqa foydalanuvchilar muhokama qiling. Ushbu asbob orqali siz sahifani oʻchirishga nomzod sifatida koʻrsatasiz, boshqa foydalanuvchilar esa bu borada oʻz fikrlarini bildirishadi. Soʻngida, administrator qaror chiqaradi.</small>')
		});
		AfDOptions = new OO.ui.FieldsetLayout({});
		AfDOptions.addItems([
			rationaleField = new OO.ui.FieldLayout(rationaleInput = new OO.ui.MultilineTextInputWidget({
				placeholder: 'Nega bu sahifani oʻchirishga nomzod sifatida koʻrsatyapsiz?',
				indicator: 'required',
				value: '',
			}), {
				label: 'Sababi',
				align: 'inline',
			}),
			new OO.ui.FieldLayout(new OO.ui.ToggleSwitchWidget({
				value: afdSendMessageToCreator,
				data: 'informCreator'
			}), {
				label: 'Bu haqda sahifa muallifi ogohlantirilsin',
				align: 'top',
				help: 'Bunda sahifa muallifi foydalanuvchi munozara sahifasiga siz u yaratgan sahifani oʻchirishga nomzod sifatida koʻrsatganingiz haqida ogohlantiruvchi tegishli andoza qoʻshiladi.',
			}),
		]);
		this.content = new OO.ui.PanelLayout({
			padded: true,
			expanded: false,
			isDraggable: true
		});
		this.content.$element.append(headerTitle.$element, '<br>', AfDOptions.$element);
		this.$body.append(this.content.$element);
	};
	ArticleForDeletionDialog.prototype.getActionProcess = function(action) {
		var dialog = this;
		if(action) {
			return new OO.ui.Process(function() {
				var AFDTempalte;
				var ActionOptions = [];
				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) {
							var Author = data.query.pages[mw.config.get('wgArticleId')].revisions[0].user;
							if(!mw.util.isIPAddress(Author)) {
								var message = '{{subst:afd-notice|' + mwConfig.wgPageName.replace(/_/g, " ") + '}}';
								sendMessageToAuthor(Author, message);
							}
						});
					}
				});
				checkPreviousNominations("Vikipediya:Oʻchirishga/" + mwConfig.wgPageName).then(function(data) {
					if(data.query.pages["-1"]) {
						var nomCount = 0;
						console.log(nomCount);
						NominatedPreviously = false;
						AFDTempalte = '{{oʻchirishga}}';
						putAfDTemplate(AFDTempalte, nextNominationNumber);
					} else {
						Rec(2);
					}
				});

				function Rec(nomCount) {
					checkPreviousNominations("Vikipediya:Oʻchirishga/" + mwConfig.wgPageName + ' ' + '' + nomCount + '').then(function(data) {
						if(!data.query.pages["-1"]) {
							Rec(nomCount + 1);
						} else {
							nextNominationNumber = nomCount++;
							console.log(nextNominationNumber);
							if(nextNominationNumber > 1) {
								AFDTempalte = '{{oʻchirishga|' + nextNominationNumber + '|' + mwConfig.wgPageName.replace(/_/g, " ") + '}}';
							} else {
								AFDTempalte = '{{oʻchirishga}}';
							}
							console.log(AFDTempalte);
							putAfDTemplate(AFDTempalte, nextNominationNumber);
						}
					});
				}
				dialog.close({
					action: action
				});
				showProgress();
			});
		}
		return ArticleForDeletionDialog.super.prototype.getActionProcess.call(this, action);
	};
	var windowManager = new OO.ui.WindowManager();
	$(document.body).append(windowManager.$element);
	var dialog = new ArticleForDeletionDialog({
		size: 'large',
		classes: ['afd-helper-window'],
		isDraggable: true
	});
	windowManager.addWindows([dialog]);
	windowManager.openWindow(dialog);

	function putAfDTemplate(AFDTempalte, nextNominationNumber) {
		var PageAFDX;
		if(nextNominationNumber > 1) {
			PageAFDX = mwConfig.wgPageName + '' + nextNominationNumber + '';
		} else {
			PageAFDX = mwConfig.wgPageName;
		}
		api.postWithToken('csrf', {
			action: 'edit',
			title: mwConfig.wgPageName,
			prependtext: AFDTempalte + "\n",
			summary: 'Sahifa oʻchirishga nomzod sifatida koʻrsatildi',
			tags: 'Adiutor',
			format: 'json'
		}).done(function() {
			createNominationPage(PageAFDX);
			logNomination(PageAFDX);
		});
	}

	function checkPreviousNominations(title) {
		return api.get({
			action: 'query',
			prop: 'revisions',
			rvlimit: 1,
			rvprop: ['user'],
			rvdir: 'newer',
			titles: title,
		});
	}

	function createNominationPage(PageAFDX) {
		api.postWithToken('csrf', {
			action: 'edit',
			title: 'Vikipediya:Oʻchirishga/' + PageAFDX,
			appendtext: '{{subst:oʻchirishga2 |pg = ' + mwConfig.wgPageName.replace(/_/g, " ") + '|text= ' + rationaleInput.value + ' ~~~~ }}' + "\n",
			summary: 'Sahifa oʻchirishga nomzod sifatida koʻrsatildi',
			tags: 'Adiutor',
			format: 'json'
		}).done(function() {
			addNominationToAfdPage(PageAFDX);
		});
	}

	function addNominationToAfdPage(PageAFDX) {
		var pageContent;
		api.get({
			action: 'parse',
			page: "Vikipediya:Oʻchirishga",
			prop: 'wikitext',
			format: "json"
		}).done(function(data) {
			pageContent = data.parse.wikitext['*'];
			var NominatedBefore = pageContent.includes("{{Vikipediya:Oʻchirishga/" + PageAFDX.replace(/_/g, " ") + "}}");
			if(!NominatedBefore) {
				api.postWithToken('csrf', {
					action: 'edit',
					title: "Vikipediya:Oʻchirishga",
                    section: 1,
					appendtext: "\n" + "{{Vikipediya:Oʻchirishga/" + PageAFDX.replace(/_/g, " ") + "}}",
					summary: "Nomzod [[Vikipediya:Oʻchirishga|tegishli roʻyxatga]] qoʻshildi.",
					tags: 'Adiutor',
					format: 'json'
				}).done(function() {
					addNominationToAfdLogPage(PageAFDX);
				});
			}
		});
	}

	function addNominationToAfdLogPage(PageAFDX) {
		var date = new Date();
		var date_months = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"];
		var date_year = date.getUTCFullYear();
		var month_name = date_months[date.getUTCMonth()];
		var pageContent;
		api.get({
			action: 'parse',
			page: "Vikipediya:Oʻchirishga/Qaydlar/" + date_year + "-" + month_name,
			prop: 'wikitext',
			format: "json"
		}).done(function(data) {
			pageContent = data.parse.wikitext['*'];
			var NominatedBefore = pageContent.includes("{{Vikipediya:Oʻchirishga/" + PageAFDX.replace(/_/g, " ") + "}}");
			// 
			if(!NominatedBefore) {
				api.postWithToken('csrf', {
					action: 'edit',
					title: "Vikipediya:Oʻchirishga/Qaydlar/" + date_year + "-" + month_name,
					appendtext: "\n" + "{{Vikipediya:Oʻchirishga/" + PageAFDX.replace(/_/g, " ") + "}}",
					summary: "Nomzod [[Vikipediya:Oʻchirishga/Qaydlar/" + date_year + "-" + month_name + "|tegishli qaydlarga]] qoʻshildi.",
					tags: 'Adiutor',
					format: 'json'
				}).done(function() {
					window.location = '/wiki/Vikipediya:Oʻchirishga/' + PageAFDX.replace(/_/g, " ");
				});
			} else {
				window.location = '/wiki/Vikipediya:Oʻchirishga/' + PageAFDX.replace(/_/g, " ");
			}
		});
	}

	function logNomination(PageAFDX) {
		if(afdLogNominatedPages) {
			api.postWithToken('csrf', {
				action: 'edit',
				title: 'Foydalanuvchi:'.concat(mwConfig.wgUserName, '/' + localStorage.getItem("afdLogPageName") + '').split(' ').join('_'),
				appendtext: "\n" + "# '''[[Vikipediya:Oʻchirishga/" + PageAFDX.replace(/_/g, " ") + "|" + mwConfig.wgPageName.replace(/_/g, " ") + "]]''' sahifasini oʻchirishga nomzod sifatida koʻrsatdi. ~~~~~",
				summary: 'Oʻchirishga nomzod sifatida koʻrsatilgan sahifa kunlik qaydlarga qoʻshildi.',
				tags: 'Adiutor',
				format: 'json'
			}).done(function() {});
		}
	}

	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: 'Foydalanuvchi_munozarasi:' + Author,
			appendtext: '\n' + message,
			summary: '[[' + mwConfig.wgPageName.replace(/_/g, " ") + ']]' + ' oʻchirishga nomzod sifatida koʻrsatildi',
			tags: 'Adiutor',
			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: 'Bajarilmoqda…',
			message: progressBar.$element
		});
	}
});
/* </nowiki> */