Wiki - KEONHACAI COPA

Thành viên:Ayane Fumihiro/AfDCloser.js

/** <nowiki>
 * Công cụ hỗ trợ đóng biểu quyết xoá bài.
 * Để cài đặt công cụ này, hãy thêm dòng dưới đây vào trang common.js của bạn:
 * mw.loader.load('/wiki/User:NgocAnMaster/AfDCloser.js?action=raw&ctype=text/javascript'); //[[:vi:User:NgocAnMaster/AfDCloser.js|AfDCloser.js]]
 * 
 * Inspired by [[:en:WP:XFDCLOSER]] and [[:vi:User:NguoiDungKhongDinhDanh/AfDCloser.js]]
 * 
 * Danh sách người sử dụng: //w.wiki/4rYP
 * Giấy phép: CC BY-SA 3.0
 */

var afdcloserad = ' ([[:vi:User:NgocAnMaster/AfDCloser.js|AfDCloser]])'; // Quảng bá

AfDCloserCore = function() {
	$('#ca-afdcloser').click(function(e) {
		e.preventDefault();
		var result1 = prompt('Nhập kết quả:\n(1: xóa; 0: giữ; trống: không đủ phiếu)').trim(),
			result = '';
		if (result1 === '0') { // giữ => false
			result = false;
		} else if (result1 === '1') { // xoá => true
			result = true;
		} else {
			result = ''; // không đủ phiếu => chuỗi trắng
		}
		if (result1 !== '1' && result1 !== '0' && result1 !== '') {
			mw.notify('Bạn đã không nhập một trong ba tuỳ chọn đã cho. Vui lòng thử lại.', {
				title: 'Lỗi',
				autoHide: true
			});
			return;
		}
		
		var note = prompt('Kết quả (dùng cho tham số bản mẫu):'),
			ug = mw.config.get('wgUserGroups'),
			pn = mw.config.get('wgPageName'),
			prefix = 'Wikipedia:Biểu quyết xoá bài/',
			afdpage = pn.slice(prefix.length).replace(/_/g, ' '),
			params1 = { // Lấy nội dung trang đã có
				action: 'query',
				prop: 'revisions',
				titles: pn,
				rvslots: '*',
				rvprop: 'content',
				formatversion: 2
			},
			params2 = { // Sửa trang
				action: 'edit',
				title: pn,
				summary: 'Đóng biểu quyết' + afdcloserad,
				format: 'json'
			};
		new mw.Api().get(params1).done(function(response) {
			var afdwikitext = response.query.pages[0].revisions[0].slots.main.content,
				afdarray = afdwikitext.split('\n'),
				firstheaderline = 0,
				firstheaderregex = /^(=+)\s*\[\[.+\{\{status.+\1$/i,
				afdpagename = '',
				afdpagenameline = 0,
				afdpagenameregex = /^:\s*\{\{la\s*\|\s*(.+?)\s*\}\}.+$/i;
				close = true;
			if (ug.indexOf('sysop') > -1 || ug.indexOf('eliminator') > -1) {
				close = confirm('Bạn có muốn đóng biểu quyết này không?');
			}
			for (let i = 0; i < afdarray.length; i++) { // Tìm đề mục chứa tên trang
				if (afdarray[i].match(firstheaderregex)) {
					firstheaderline = i + 1;
					break;
				}
			}
			for (let i = 0; i < afdarray.length; i++) { // Tìm bản mẫu la chứa tên trang
				if (afdarray[i].match(afdpagenameregex)) {
					afdpagenameline = i + 1;
					afdpagename = afdarray[i].match(afdpagenameregex)[1]; // Lưu tên trang để xử lý
					break;
				}
			}
			
			afdarray.splice(
				firstheaderline, afdpagenameline - firstheaderline - 1,
				'{{' + 'Đầu biểu quyết|KQ=' + note + 
					(note.endsWith('.') ?
						' ~~' + '~~'
					:
						(note.length > 0 ?
							'. ~~' + '~~'
						:
							'~~' + '~~'
						)
					) +
				'}}'
			);
			afdarray.push('{{' + 'Kết biểu quyết}}');
			params2.text = afdarray.join('\n');
			if (close == true) {
				new mw.Api().postWithToken('csrf', params2).done(function() {
					mw.notify('Đã đóng biểu quyết xoá bài.', {
						title: 'Đóng biểu quyết thành công',
						autoHide: true
					});
				});
			}
			
			var notalk = false,
				params3 = {
					action: 'query',
					prop: 'revisions',
					titles: 'Talk:' + afdpagename,
					rvslots: '*',
					rvprop: 'content',
					formatversion: 2,
				};
			new mw.Api().get(params3).done(function(response) {
				if (Boolean(response.query.pages[0].missing)) {
					notalk = true;
				}
			});
			
			if (typeof result.length == 'undefined') { // result là luận lý (xoá/giữ), tiến hành xoá/gỡ/thêm thẻ.
				if (ug.indexOf('sysop') > -1 || ug.indexOf('eliminator') > -1) {
					AfDCloserAdmin(afdpagename, afdpage, result, notalk);
				} else {
					AfDCloserUser(afdpagename, afdpage, result, notalk);
				}
			} else {
				var pagewikitextnew = '';
				new mw.Api().get({
					action: 'query',
					prop: 'revisions',
					titles: afdpagename,
					rvslots: '*',
					rvprop: 'content',
					formatversion: 2
				}).done(function(response) {
					pagearray = response.query.pages[0].revisions[0].slots.main.content.split('\n');
					for (let i = 0; i < pagearray.length; i++) { // Tìm bản mẫu afd/dated
						if (pagearray[i].match(/^\s*\{\{[Aa]fd\/dated.+$/)) {
							afdtempline = i + 1;
							break;
						}
					}
					
					// Không chuẩn lắm, nhưng đành chịu vậy.
					pagewikitextnew = pagearray.slice(0, afdtempline - 1).concat(pagearray.slice(afdtempline)).join('\n');
					
					new mw.Api().postWithToken('csrf', {
						action: 'edit',
						title: afdpagename,
						text: pagewikitextnew,
						summary: 'Gỡ bản mẫu theo kết quả biểu quyết tại [[Wikipedia:Biểu quyết xoá bài/' + afdpage + ']]' + afdcloserad,
						format: 'json'
					}).done(function() {
						mw.notify('Đã gỡ bản mẫu biểu quyết xoá trang.', {
							title: 'Gỡ bản mẫu thành công',
							autoHide: true
						});
					});
				});
			}
		});
	});
};

AfDCloserUser = function(pagename, afd, result, notalk) {
	var pagewikitextnew = '',
		afdtempline = -1,
		params1 = {
			action: 'edit',
			title: pagename,
			format: 'json'
		},
		params2 = {
			action: 'edit',
			title: 'Talk:' + pagename,
			format: 'json'
		};
	
	new mw.Api().get({
		action: 'query',
		prop: 'revisions',
		titles: pagename,
		rvslots: '*',
		rvprop: 'content',
		formatversion: 2
	}).done(function(response) {
		pagearray = response.query.pages[0].revisions[0].slots.main.content.split('\n');
		for (let i = 0; i < pagearray.length; i++) { // Tìm bản mẫu afd/dated
			if (pagearray[i].match(/^\s*\{\{[Aa]fd\/dated.+$/)) {
				afdtempline = i + 1;
				break;
			}
		}
		
		// Không chuẩn lắm, nhưng đành chịu vậy.
		pagewikitextnew = pagearray.slice(0, afdtempline - 1).concat(pagearray.slice(afdtempline)).join('\n');
		params1.text = pagewikitextnew;
		
		if (result) {
			params1.text = '{{' + 'db-xfd' + (afd != pagename ? '|votepage=' + afd : '' ) + '}}\n' + params1.text;
			params1.summary = 'Đề nghị xoá theo kết quả biểu quyết tại [[Wikipedia:Biểu quyết xoá bài/' + afd + ']]' + afdcloserad;
			params2.prependtext = '{{' + 'db-talk}}\n';
			params2.summary = params1.summary,
			params2.nocreate = true;
			
			new mw.Api().postWithToken('csrf', params1).done(function() {
				mw.notify('Đã đề nghị xoá ' + pagename + '.', {
					title: 'Đề nghị xóa trang thành công',
					autoHide: true
				});
			});
			
			if (!notalk) {
				new mw.Api().postWithToken('csrf', params2).done(function() {
					mw.notify('Đã đề nghị xoá Thảo luận:' + pagename + '.', {
						title: 'Đề nghị xóa trang thành công',
						autoHide: true
					});
				});
			}
		} else {
			params1.summary = 'Gỡ bản mẫu theo kết quả biểu quyết tại [[Wikipedia:Biểu quyết xoá bài/' + afd + ']]' + afdcloserad;
			params2.prependtext = '{{' + 'đã biểu quyết giữ|1=Wikipedia:Biểu quyết xoá bài/' + afd + '}}\n';
			params2.summary = 'Thêm bản mẫu theo kết quả biểu quyết tại [[Wikipedia:Biểu quyết xoá bài/' + afd + ']]' + afdcloserad,
			params2.nocreate = false;
			
			new mw.Api().postWithToken('csrf', params1).done(function() {
				mw.notify('Đã gỡ bản mẫu biểu quyết xoá trang.', {
					title: 'Gỡ bản mẫu thành công',
					autoHide: true
				});
			});
			new mw.Api().postWithToken('csrf', params2).done(function() {
				mw.notify('Đã gán nhãn trang thảo luận.', {
					title: 'Gán nhãn thành công',
					autoHide: true
				});
			});
		}
	});
};

AfDCloserAdmin = function(pagename, afd, result, notalk) {
	var pagewikitextnew = '',
		afdtempline = -1,
		params1 = {
			action: 'delete',
			title: pagename,
			format: 'json'
		},
		params2 = {
			action: 'delete',
			title: 'Talk:' + pagename,
			format: 'json'
		};
	
	new mw.Api().get({
		action: 'query',
		prop: 'revisions',
		titles: pagename,
		rvslots: '*',
		rvprop: 'content',
		formatversion: 2
	}).done(function(response) {
		pagearray = response.query.pages[0].revisions[0].slots.main.content.split('\n');
		for (let i = 0; i < pagearray.length; i++) { // Tìm bản mẫu afd/dated
			if (pagearray[i].match(/^\s*\{\{[Aa]fd\/dated.+$/)) {
				afdtempline = i + 1;
				break;
			}
		}
		
		// Không chuẩn lắm, nhưng đành chịu vậy.
		pagewikitextnew = pagearray.slice(0, afdtempline - 1).concat(pagearray.slice(afdtempline)).join('\n');
		
		if (result) {
			params1.reason = '[[WP:BQXB|BQXB]]: Xoá theo kết quả biểu quyết xóa bài: [[Wikipedia:Biểu quyết xoá bài/' + afd + ']]' + afdcloserad;
			params2.reason = '[[WP:C8|C8]]: Trang liên quan đến trang không tồn tại hoặc đã bị xóa' + afdcloserad;
			
			new mw.Api().postWithToken('csrf', params1).done(function() {
				mw.notify('Đã xoá ' + pagename + '.', {
					title: 'Xoá trang thành công',
					autoHide: true
				});
			});
			
			if (!notalk) {
				new mw.Api().postWithToken('csrf', params2).done(function() {
					mw.notify('Đã xoá Thảo luận:' + pagename + '.', {
						title: 'Xoá trang thành công',
						autoHide: true
					});
				});
			}
		} else {
			params1.action = 'edit';
			params2.action = 'edit';
			params1.text = pagewikitextnew;
			params1.summary = 'Gỡ bản mẫu theo kết quả biểu quyết tại [[Wikipedia:Biểu quyết xoá bài/' + afd + ']]' + afdcloserad;
			params2.prependtext = '{{' + 'đã biểu quyết giữ|1=Wikipedia:Biểu quyết xoá bài/' + afd + '}}\n';
			params2.summary = 'Thêm bản mẫu theo kết quả biểu quyết tại [[Wikipedia:Biểu quyết xoá bài/' + afd + ']]' + afdcloserad,
			params2.nocreate = false;
			
			new mw.Api().postWithToken('csrf', params1).done(function() {
				mw.notify('Đã gỡ bản mẫu biểu quyết xoá trang.', {
					title: 'Gỡ bản mẫu thành công',
					autoHide: true
				});
			});
			new mw.Api().postWithToken('csrf', params2).done(function() {
				mw.notify('Đã gán nhãn trang thảo luận.', {
					title: 'Gán nhãn thành công',
					autoHide: true
				});
			});
		}
	});
};

$(document).ready(function() {
	var db = mw.config.get('wgDBname'),
		pn = mw.config.get('wgPageName');
	if (db !== 'viwiki' || !pn.replace(/_/g, ' ').match(/^Wikipedia:Biểu quyết xoá bài\/.+/)) return; // Không chạy ở chỗ nào khác
	mw.util.addPortletLink(
		'p-cactions',
		'', 'AfDCloser',
		'ca-afdcloser',
		'Hỗ trợ đóng biểu quyết xoá bài'
	);
	AfDCloserCore();
});
// </nowiki>
Wiki - Keonhacai copa chuyên cung cấp kiến thức thể thao, keonhacai tỷ lệ kèo, bóng đá, khoa học, kiến thức hằng ngày được chúng tôi cập nhật mỗi ngày mà bạn có thể tìm kiếm tại đây có nguồn bài viết: https://vi.wikipedia.org/wiki/Th%C3%A0nh_vi%C3%AAn:Ayane_Fumihiro/AfDCloser.js