مدیاویکی:TofawikivoyageHelper.js

نکته: پس از انتشار ممکن است برای دیدن تغییرات نیاز باشد که حافظهٔ نهانی مرورگر خود را پاک کنید.

  • فایرفاکس / سافاری: کلید Shift را نگه دارید و روی دکمهٔ Reload کلیک کنید، یا کلید‌های Ctrl-F5 یا Ctrl-R را با هم فشار دهید (در رایانه‌های اپل مکینتاش کلید‌های ⌘-R)
  • گوگل کروم: کلیدهای Ctrl+Shift+R را با هم فشار دهید (در رایانه‌های اپل مکینتاش کلید‌های ⌘-Shift-R)
  • اینترنت اکسپلورر/ Edge: کلید Ctrl را نگه‌دارید و روی دکمهٔ Refresh کلیک کنید، یا کلید‌های Ctrl-F5 را با هم فشار دهید
  • اپرا: Ctrl-F5 را بفشارید.
// <nowiki> // DO NOT REMOVE THIS LINE EVER
// Author: User:Ebraminio
(function () {
	'use strict';

	function getEnCats(title) {
		return new mw.Api({ ajax: { url: '//en.wikivoyage.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) {
			try {
				return $.map($.map(data.query.pages, function (x) { return x; })[0].categories, function (x) {
					return x.title;
				});
			} catch (ignore) { }
		});
	}
	
	function getWikidataEntities(enTitles) {
		return new mw.Api({ ajax: { url: '//www.wikidata.org/w/api.php' } }).get({
			action: 'wbgetentities',
			format: 'json',
			sites: 'enwikivoyage',
			titles: enTitles.join('|'),
			origin: window.location.protocol + '//' + window.location.hostname
		}).then(function (x) { return $.map(x.entities, function (x) { return x; }); });
	}
	
	function getFaTitlesFromWikidata(enTitles) {
		return getWikidataEntities(enTitles).then(function (entities) {
			return $.map(entities, function (x) {
				if (!x.sitelinks || !x.sitelinks.fawikivoyage) { return; }
				return x.sitelinks.fawiki.title;
			});
		});
	}
	
	function getEnPage(title) {
		return new mw.Api({ ajax: { url: '//en.wikivoyage.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.wikivoyage.org/w/api.php' } }).get({
			action: 'query',
			format: 'json',
			redirects: '',
			titles: pages.join('|'),
			origin: window.location.protocol + '//' + window.location.hostname
		}).then(function (result) {
			return $.map(result.query.pages, function (x, y) {
				return x.title;
			});
		});
	}
	
	function getSeeAlsoPages(title) {
		return getEnPage(title).then(function (page) {
			var pages = $("#See_also", '<div>' + page + '</div>')
				.parent().next().find('a[href^="/wiki/"]')
				.map(function (x, y) { return y.title; })
				.get();
			if (pages.length === 0) { return; }
			return getResolvedRedirectPages(pages).then(function (titles) {
				return getFaTitlesFromWikidata(titles);
			});
		});
	}
	
	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.wikivoyage.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) {
				$('#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 = {
			enwikivoyage: decodeURI(enName),
			fawikivoyage: 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;
			}
			$('#wpSummary').val('ایجادشده به کمک [[راهنما:ابزار/به ویکی‌فا|به ویکی‌فا]] و [[' + botLink + '|' + bot + ']]');
			$('#wpTextbox1').val(x.page_content);
			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.wikivoyage.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 $.map(data.query.pages, function (value) { return value; })[0].revisions[0]['*'];
		});
	}
	
	function templateTranslator() {
		$('#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 });
		}).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'));
			}
			Object.keys(result).forEach(function (from) {
				raw = raw.replace(
					new RegExp('(\\[\\[:?)' + 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.wikivoyage.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) {
			templateTranslator();
		} else if (mw.config.get('wgNamespaceNumber') === 14) {
			generalArticle();
		} else {
			botContentMaker('//tools.wmflabs.org/dexbot/c.php', 'Dexbot', query.requestingPage, query.enName);
		}
		
		// الگوی اصلاح نشده حذف شود
		$('#editform input[type=submit]').click(function () { $('#wpTextbox1').val($('#wpTextbox1').val().replace('\n\n{{موضوع-خرد}}\n', '')); });
	});
}());