کاربر:MohandesWiki/Tofawikihelper.js: تفاوت میان نسخه‌ها

محتوای حذف‌شده محتوای افزوده‌شده
MohandesWiki (بحث | مشارکت‌ها)
صفحه‌ای تازه حاوی «// <nowiki> // DO NOT REMOVE THIS LINE EVER // Author: User:Ebraminio (function () { 'use strict'; function getEnCats(title) { return...» ایجاد کرد
 
(بدون تفاوت)

نسخهٔ کنونی تا ‏۲ دسامبر ۲۰۱۶، ساعت ۱۶:۵۶

// <nowiki> // DO NOT REMOVE THIS LINE EVER
// Author: User:Ebraminio
(function () {
	'use strict';

	function getEnCats(title) {
		return new mw.Api({ ajax: { url: '//en.wikipedia.org/w/api.php' } }).get({
			action: 'query',
			prop: 'categories',
			titles: title,
			clshow: '!hidden',
			cllimit: 500,
			format: 'json',
			origin: window.location.protocol + '//' + window.location.hostname
		}).then(function (data) {
			return (data.query.pages[Object.keys(data.query.pages)[0]].categories || [])
				.map(function (x) { return x.title; });
		});
	}
	
	function getWikidataEntities(enTitles) {
		return new mw.Api({ ajax: { url: '//www.wikidata.org/w/api.php' } }).get({
			action: 'wbgetentities',
			format: 'json',
			sites: 'enwiki',
			titles: enTitles.join('|'),
			origin: window.location.protocol + '//' + window.location.hostname
		}).then(function (x) { return Object.keys(x.entities).map(function (y) { return x.entities[y]; }); });
	}
	
	function getFaTitlesFromWikidata(enTitles) {
		return getWikidataEntities(enTitles).then(function (entities) {
			return entities.map(function (x) {
				if (!x.sitelinks || !x.sitelinks.fawiki) { return; }
				return x.sitelinks.fawiki.title;
			}).filter(function (x) { return x; });
		});
	}

	function getEnPage(title) {
		return new mw.Api({ ajax: { url: '//en.wikipedia.org/w/api.php' } }).get({
			action: 'parse',
			format: 'json',
			page: title,
			prop: 'text',
			origin: window.location.protocol + '//' + window.location.hostname
		}).then(function (result) {
			return result.parse.text['*'];
		});
	}
	
	function getResolvedRedirectPages(pages) {
		return new mw.Api({ ajax: { url: '//en.wikipedia.org/w/api.php' } }).get({
			action: 'query',
			format: 'json',
			redirects: '',
			titles: pages.join('|'),
			origin: window.location.protocol + '//' + window.location.hostname
		}).then(function (result) {
			return Object.keys(result.query.pages).map(function (x) { return result.query.pages[x].title; });
		});
	}
	
	function getSeeAlsoPages(title) {
		return getEnPage(title).then(function (page) {
			var pages = $("#See_also", '<div>' + page + '</div>').parent().next().find('a[href^="/wiki/"]').get().map(function (x) { return x.title; });
			if (pages.length === 0) { return []; }
			return getResolvedRedirectPages(pages).then(getFaTitlesFromWikidata);
		});
	}
	
	function dePersian(text) {
		return text
			.replace(/ی/g, 'ي')
			.replace(/ک/g, 'ك')
			.replace(/گ/g, 'كی')
			.replace(/ژ/g, 'زی')
			.replace(/چ/g, 'جی')
			.replace(/پ/g, 'بی');
	}
	
	function generalArticle() {
		var query = new mw.Uri().query;
		var enName = query.enName.replace(/_/g, ' ');
		var enLink = new mw.Uri('//en.wikipedia.org/w/index.php');
		enLink.query = {
			title: query.enName,
			oldid: query.enOldid
		};
		var text = '';
		if (mw.config.get('wgNamespaceNumber') !== 14) {
			text = "'''" + mw.config.get('wgTitle').replace(/\s\(.*\)/, ' ') + "''' {{به انگلیسی|" +
				enName.replace(/\s\(.*\)/, '') + '}}\n<ref>{{یادکرد-ویکی|پیوند=' +
				enLink + '|عنوان=' + decodeURI(enName) +
				'|زبان=انگلیسی|بازیابی=}}</ref>\n\n' +
				'== جستارهای وابسته ==\n* [[مقالهٔ مرتبط]]\n\n' + '== پانویس ==\n' +
				'{{پانویس}}\n\n';
			getSeeAlsoPages(enName).then(function (pages) {
				pages.length && $('#wpTextbox1').val($('#wpTextbox1').val().replace(
					'* [[مقالهٔ مرتبط]]',
					'* [[' + pages.join(']]\n* [[') + ']]'
				));
			});
		}
		text = text + '[[رده:رده۱]]\n\n' + '[[en:' + enName + ']]';
		$('#wpTextbox1').val(text);
		if (mw.config.get('wgNamespaceNumber') === 14) {
			getWikidataEntities([enName]).then(function (x) {
				if (x[0].claims.P373) {
					$('#wpTextbox1').val('{{انبار-رده}}\n' + $('#wpTextbox1').val());
				}
				if (x[0].claims.P301) {
					$('#wpTextbox1').val('{{اصلی رده}}\n' + $('#wpTextbox1').val());
				}
			});
		}
		getEnCats(enName).then(function (cats) {
			return getFaTitlesFromWikidata(cats);
		}).then(function (faCats) {
			faCats = faCats.sort(function (x, y) {
				var keyX = dePersian(x),
					keyY = dePersian(y);
				if (keyX < keyY) { return -1; }
				if (keyX > keyY) { return 1; }
				return 0;
			});
			$('#wpTextbox1').val($('#wpTextbox1').val().replace(
				'[[رده:رده۱]]',
				'[[' + faCats.join(']]\n[[') + ']]'
			));
		});
	}
	
	var pleaseWait = 'در حال تهیهٔ متن مقاله، کمی صبر کنید';
	
	function inProgressDots() {
		setTimeout(function () {
			if (0 !== $('#wpTextbox1').val().indexOf(pleaseWait)) {	return; }
			$('#wpTextbox1').val(($('#wpTextbox1').val() + '. ').replace('. . . . ', ''));
			inProgressDots();
		}, 1000);
	}

	function botContentMaker(requestTool, bot, botLink, enName) {
		$('#wpTextbox1').val(pleaseWait).prop('disabled', true);
		var uri = new mw.Uri(requestTool);
		uri.query = {
			enwiki: decodeURI(enName),
			fawiki: mw.config.get('wgTitle')
		};
		inProgressDots();
		$.getJSON(uri.toString()).then(function (x) {
			$('#wpTextbox1').prop('disabled', false);
			if ((x === null) || x.error) {
				mw.notify(bot + ': ' + (x === null ? 'خطای اساسی' : x.error));
				if (botLink === "ویکی‌پدیا:مقاله‌های درخواستی") {
					generalArticle();
				} else {
					botContentMaker('//tools.wmflabs.org/dexbot/c.php', 'Dexbot', 'ویکی‌پدیا:مقاله‌های درخواستی', enName);
				}
				return;
			}
			var summary = 'ایجادشده به کمک [[راهنما:ابزار/به ویکی‌فا|به ویکی‌فا]] و [[' + botLink + '|' + bot + ']]', content = x.page_content;
			if (window.persianWikiTools) {
				summary = summary + '، ابرابزار';
				content = persianWikiTools.superTool(content);
			}
			$('#wpSummary').val(summary);
			$('#wpTextbox1').val(content);
			importScript('MediaWiki:YandexTranslatorHelper.js');
			if (window.wikEd && window.wikEd.useWikEd) { wikEd.UpdateFrame(); }
		}, function (e) {
			$('#wpTextbox1').prop('disabled', false);
			mw.notify(e);
			if (botLink === 'ویکی‌پدیا:مقاله‌های درخواستی') {
				generalArticle();
			} else {
				botContentMaker('//tools.wmflabs.org/dexbot/c.php', 'Dexbot', 'ویکی‌پدیا:مقاله‌های درخواستی', enName);
			}
			if (window.wikEd && window.wikEd.useWikEd) { wikEd.UpdateFrame(); }
		});
	}
	
	function getRawEnPage(title) {
		return new mw.Api({ ajax: { url: '//en.wikipedia.org/w/api.php' } }).get({
			action: 'query',
			prop: 'revisions',
			titles: title,
			rvprop: 'content',
			format: 'json',
			origin: window.location.protocol + '//' + window.location.hostname
		}).then(function (data) {
			return data.query.pages && !data.query.pages[-1]
				? Object.keys(data.query.pages).map(function (x) { return data.query.pages[x]; })[0].revisions[0]['*']
				: '';
		});
	}

	function escape(text) {
		return text.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&');
	}
	
	function templateBookTranslator() {
		$('#wpTextbox1').val(pleaseWait).prop('disabled', true);

		var query = new mw.Uri().query;
		getRawEnPage(query.enName).then(function (raw) {
			$('#wpTextbox1').val(raw);
			var links = (raw.match(/\[\[.*?\]\]/g) || []).map(function (x) { return x.split('[[')[1].split(/[\|\]]/)[0]; });
			return $.post('//tools.wmflabs.org/linkstranslator/', { p: links, from: 'en', to: 'fa' });
		}).then(function (result) {
			var raw = $('#wpTextbox1').val();
			if (raw.match(/\{\{(Navbox|Sidebar|Campaignbox)/)) {
				raw = raw.replace(/(\|\s*name\s*=\s*)([^\n\|\}]*)/, '$1' + mw.config.get('wgTitle'));
			}
			if (mw.config.get('wgNamespaceNumber') === 102) {
				raw = raw.replace(/(\|\s*title\s*=\s*)([^\n\|\}]*)/, '$1' + mw.config.get('wgTitle'));
				raw = raw.replace(/;Chapter (\d+)/g, function (_, x) { return ";فصل " + (+x).toLocaleString('fa'); })
				raw = raw.replace(/{{Saved book/i, '{{کتاب ذخیره‌شده');
			}
			Object.keys(result).forEach(function (from) {
				raw = raw.replace(
					new RegExp('(\\[\\[:?)' + escape(from) + '((?:\\|[^\\]]*)?)(\\]\\])', 'g'),
					'$1' + result[from] + '$3'
				);
			});
			$('#wpTextbox1').val(raw);
		}).then(function () {
			$('#wpTextbox1').prop('disabled', false);
		}, function () {
			mw.notify('ترجمه انجام نشد');
			$('#wpTextbox1').prop('disabled', false);
		});
		$("#wpSummary").val(
			$("#wpSummary").val() + '، از ' +
			new mw.Uri('https://en.wikipedia.org/w/index.php')
				.extend({ title: query.enName, oldid: query.enOldid }).toString()
		);
	}
	
	$(function () {
		var query = new mw.Uri().query;
		$("#wpSummary").val('ایجادشده به کمک [[راهنما:ابزار/به ویکی‌فا|به ویکی‌فا]]');
		if (query.requestingPage === "ویکی‌پدیا:ویکی‌پروژه ایجاد مقاله‌های شهرها با ربات/درخواست ساخت رباتیک") {
			botContentMaker('//tools.wmflabs.org/rezabot/a.php', 'Rezabot', query.requestingPage, query.enName);
		} else if (query.requestingPage === "ویکی‌پدیا:درخواست ایجاد مقاله (رباتیک)/انسان") {
			botContentMaker('//tools.wmflabs.org/dexbot/a.php', 'Dexbot', query.requestingPage, query.enName);
		} else if (query.requestingPage === "صفحهٔ ابهام‌زدایی") {
			botContentMaker('//tools.wmflabs.org/rezabot/disambig.php', 'Rezabot', 'ویکی‌پدیا:ابهام‌زدایی', query.enName);
		} else if (query.requestingPage === "ویکی‌پدیا:درخواست ایجاد مقاله (رباتیک)") {
			botContentMaker('//tools.wmflabs.org/dexbot/b.php', 'Dexbot', query.requestingPage, query.enName);
		} else if (mw.config.get('wgNamespaceNumber') === 10 || mw.config.get('wgNamespaceNumber') === 102) {
			templateBookTranslator();
		} else if (mw.config.get('wgNamespaceNumber') === 14) {
			generalArticle();
		} else if (mw.config.get('wgNamespaceNumber') === 4) {
			generalArticle();
			importScript('MediaWiki:YandexTranslatorHelper.js');
		} else {
			botContentMaker('//tools.wmflabs.org/dexbot/c.php', 'Dexbot', query.requestingPage, query.enName);
		}
	});

	importScript('MediaWiki:TofawikiPass.js');
}());