انتقل إلى المحتوى

مستخدم:Mr. Ibrahem/Gadget-CatMaker.js

من ويكيبيديا، الموسوعة الحرة

ملاحظة: بعد الحفظ، قد يلزمك إفراغ الكاش لرؤية التغييرات.

// <nowiki> // DO NOT REMOVE THIS LINE EVER
/*jslint browser: true, white: true*/
/*global mediaWiki, jQuery*/
/*!
 */
(function ($, mw) {
	'use strict';

	function createPage(title, text, summary, callback) {
		return new mw.Api().post({
			action: 'edit',
			title: title,
			text: text,
			summary: summary,
			createonly: '',
			minor: '',
			token: mw.user.tokens.get('editToken')
		}).then(function (data) {
			if (data.error && data.error.info) {
				mw.notify(title + ' الوصلة موجودة: ' + data.error.info);
			} else {
				callback();
			}
			return data;
		}, function (data) {
			mw.notify(title + ' الوصلة موجودة: ' + data);
			return data;
		});
	}

	function arWikiName(title) {
		return $.ajax({
			url: '//en.wikipedia.org/w/api.php',
			data: {
				action: 'query',
				prop: 'langlinks',
				titles: title,
				redirects: 1,
				format: 'json',
				lllimit: 500
			},
			dataType: 'jsonp'
		}).then(function (data) {
			try {
				return $.grep($.map(data.query.pages, function (x) { return x; })[0].langlinks, function (x) {
					return x.lang === 'ar';
				})[0]['*'];
			} catch (ignore) { }
		});
	}

	function catParents(title) {
		return $.ajax({
			url: '//en.wikipedia.org/w/api.php',
			data: {
				action: 'query',
				prop: 'categories',
				titles: title,
				clshow: '!hidden',
				cllimit: 500,
				format: 'json'
			},
			dataType: 'jsonp'
		}).then(function (data) {
			try {
				return $.map($.map(data.query.pages, function (x) { return x; })[0].categories, function (x) {
					return x.title;
				});
			} catch (ignore) { }
		});
	}

	function getWikidataEntity(entitle) {
		return new mw.Api({ ajax: { url: '//www.wikidata.org/w/api.php' } }).get({
			action: 'wbgetentities',
			format: 'json',
			sites: 'enwiki',
			titles: entitle,
			normalize: 'true',
			origin: window.location.protocol + '//' + window.location.hostname
		}).then(function (x) { return $.map(x.entities, function (x) { return x; })[0]; });
	}

	function commonsCatFromEntity(entity) {
		try { return entity.claims.P373[0].mainsnak.datavalue.value; } catch (ignore) { }
	}

	function startCat(arlink, enlink) {
		enlink = enlink.replace(/\_/g, ' ');
		return arWikiName(enlink).then(function (x) {
			if (x) {
				mw.notify('Page equivalent existed before');
				return;
			}
			return catParents(enlink).then(function (x) {
				if (!x) {
					mw.notify('صفحهٔ "' + enlink + '" در ویکی مبدأ وجود ندارد یا دارای رده نیست');
					return;
				}
				return $.when.apply(null, $.map(x, function (y) {
					return arWikiName(y);
				})).then(function () {
					var catText = $.map(arguments, function (x) {
							return '[[' + x + ']]';
						}).join('\n') + '\n[['+'en:' + enlink + ']]';
					catText = catText.replace(/\[\[undefined\]\]/g, '');
					catText = catText.replace(/\n\n/g, '\n');
					catText = catText.trim();
					return createPage(
						arlink,
						catText,
						'إنشاء تصنيفات مقابلة لـ [[:en:' + enlink + ']] باستخدام منشئ ‌\u200cالتصانيف',
						function () {
							mw.notify('أنشأ ' + arlink + ' لقد تمت العملية.');
						}
					);
				});
			});
		});
	}

	function loadPage(title) {
		return new mw.Api().get({
			action: 'query',
			prop: 'revisions',
			titles: title,
			rvprop: 'content',
			format: 'json'
		}).then(function (data) {
			return $.map(data.query.pages, function (value) { return value; })[0].revisions[0]['*'];
		});
	}
 
	/**
	 * Save a page
	 */
	function savePage(title, text, summary) {
		return new mw.Api().post({
			action: 'edit',
			title: title,
			text: text,
			summary: summary,
			nocreate: '',
			minor: '',
			token: mw.user.tokens.get('editToken')
		}).then(function (data) {
			if (data.error && data.error.info) {
				mw.notify(data.error.info);
			}
		}, function (data) {
			mw.notify(data);
		});
	}

	function init(e) {
		e.preventDefault();
		$('<div>' +
			'<div style="width: 30em">للإنشاء\u200c يجب ترتيب الصفوف بشكل مستقل، كما في المثال الآتي:<br>تصنيف1@Cateogry1<br>تصنيف2@Cateogry2<br>تصنيف3@Cateogry3<br>يا أيها المبدع لقد أنهيت المهمة، هنيئا لك.🐁.</div><B>ملاحظة هامة:</B> من أجل عدم ملء صفحة أحدث التغييرات، لا تنشئ أكثر من <B>10</B> تصانيف.' +
			'<textarea style="width: 100%; background-color: white;" placeholder="تصنيف1@Cateogry1" id="catmakerinput" rows="15"></textarea>' +
			'</div>'
			).dialog({
			modal: true,
			width: 700,
			buttons: [{
				id: 'catmaker-button-import',
				text: 'إنشاء التصنيف',
				click: function () {
					var formInput = $('#catmakerinput').val();
					formInput = formInput.replace('*', '').replace('#', '').replace('---', '@')
					if (formInput === '') { mw.notify('يرجى ملء استمارة الطلب بشكل صحيح'); return; }
					var requests = formInput.trim().split('\n');
					if (requests.length > 200) {
						/*loadPage('ويكيبيديا:إنشاء تصنيفات').then(function (content) {
							return savePage('ويكيبيديا:إنشاء تصنيفات', content + '\n*' + requests.join('\n*').replace(/@/g, '---'), 'درخواست به کمک [[ميدياويكي:Gadget-CatMaker.js|ابزار رده‌ساز]]').then(function () {
								mw.notify('درخواست به ربات تحویل داده شد، لطفاً بررسی کنید');
								window.location = mw.util.getUrl('ويكيبيديا:إنشاء تصنيفات', { action: 'history' });
							});
						});*/ alert('تم ملئ أكثر من 10 طلبات، يرجى تقليل العدد وحاول مرة أخرى بعد بضع دقائق وحتى الطلبات اللاحقة يجب أن تكون أقل من 10.');
						return;
					}
					$.when.apply(null, $.map(requests, function (x) {
						var req = x.replace(/تصنيف:/, '').replace(/Category:/i, '').split('@');
						if (req.length !== 2) { return; }
						return startCat('تصنيف:' + req[0], 'Category:' + req[1]);
					})).then(function () {
						mw.notify('أنشأ التصنيف\u200cمن فضلك راجعه.');
						window.location = mw.util.getUrl('Special:Contributions/' + mw.config.get('wgUserName'));
					});
				}
			}]
		});
	}

	$(function () {
		mw.util.addCSS('#t-catmaker { font-weight: bold; }');
		$(mw.util.addPortletLink('p-views', '#', 'منشئ\u200c التصانيف', 't-catmaker', 'تصنيف تلقائي')).click(init);
	});
}(jQuery, mediaWiki));