پرش به محتوا

مدیاویکی:TofawikiHelper.js: تفاوت میان نسخه‌ها

از ویکی‌پدیا، دانشنامهٔ آزاد
محتوای حذف‌شده محتوای افزوده‌شده
افزایش به یک دقیقه
route کردن رده‌ها به tofawiki.wmflabs.org: https://github.com/Ladsgroup/tofawiki/commit/b6419930217fd06a90a028584160c534dbb2473d
خط ۸۴: خط ۸۴:
};
};
var text = '';
var text = '';
if (mw.config.get('wgNamespaceNumber') !== 14) {
text = "'''" + mw.config.get('wgTitle').replace(/\s\(.*\)/, ' ') + "''' {" + "{به انگلیسی|" +
text = "'''" + mw.config.get('wgTitle').replace(/\s\(.*\)/, ' ') + "''' {" + "{به انگلیسی|" +
enName.replace(/\s\(.*\)/, '') + '}}\n<ref>{' + '{یادکرد-ویکی|پیوند=' +
enName.replace(/\s\(.*\)/, '') + '}}\n<ref>{' + '{یادکرد-ویکی|پیوند=' +
enLink + '|عنوان=' + enName +
enLink + '|عنوان=' + enName +
خط ۹۷: خط ۹۶:
));
));
});
});
}
text = text + '[[رده' + ':رده۱]' + ']\n\n' + '[' + '[en:' + enName + ']]';
text = text + '[[رده' + ':رده۱]' + ']\n\n' + '[' + '[en:' + enName + ']]';
$('#wpTextbox1').val(text);
$('#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(getFaTitlesFromWikidata).then(function (faCats) {
getEnCats(enName).then(getFaTitlesFromWikidata).then(function (faCats) {
faCats = faCats.sort(function (x, y) {
faCats = faCats.sort(function (x, y) {
خط ۲۷۸: خط ۲۶۶:
templateBookTranslator();
templateBookTranslator();
} else if (mw.config.get('wgNamespaceNumber') === 14) {
} else if (mw.config.get('wgNamespaceNumber') === 14) {
botContentMaker('//dexbot.toolforge.org/b.php', 'Dexbot', query.requestingPage, query.enName);
generalArticle();
} else if (mw.config.get('wgNamespaceNumber') === 4) {
} else if (mw.config.get('wgNamespaceNumber') === 4) {
generalArticle();
generalArticle();

نسخهٔ ‏۱ اوت ۲۰۲۰، ساعت ۲۰:۲۱

(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 $.post('//tofawiki-linkstranslator.wmflabs.org/', { p: enTitles.join('|') }).then(function (result) {
			return Object.keys(result).map(function (x) { return result[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 = '';
		text = "'''" + mw.config.get('wgTitle').replace(/\s\(.*\)/, ' ') + "''' {" + "{به انگلیسی|" +
				enName.replace(/\s\(.*\)/, '') + '}}\n<ref>{' + '{یادکرد-ویکی|پیوند=' +
				enLink + '|عنوان=' + 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);
		getEnCats(enName).then(getFaTitlesFromWikidata).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 preSavedTransform(content, title) {
		return new mw.Api().post({
			action: 'parse',
			text: content,
			contentmodel: 'wikitext',
			prop: 'text',
			onlypst: '',
			title: title || 'API'
		}).then(function (data) { return data.parse.text['*']; });
	}

	function botContentMaker(requestTool, bot, botLink, enName) {
		$('#wpTextbox1').val(pleaseWait).prop('disabled', true);
		var uri = new mw.Uri(requestTool);
		uri.query = {
			enwiki: enName,
			fawiki: mw.config.get('wgTitle')
		};
		if (requestTool === '//dexbot.toolforge.org/c.php' || requestTool === '//dexbot.toolforge.org/a.php') {
			uri = '//tofawiki.wmflabs.org/translate/enwiki/' +
				encodeURI(new mw.Uri().query.enName.replace(/\//g, '%2F')) + '/' +
				encodeURI(mw.config.get('wgTitle').replace(/\//g, '%2F'));
		}
		inProgressDots();
		$.ajax({
			dataType: "json",
			url: uri.toString(),
			timeout: 60000
		}).then(function (x) {
			return ((x === null) || x.error)
				? x
				: preSavedTransform(x.page_content, mw.config.get('wgTitle')).then(function (x) {
					return { page_content: x };
				});
		}, function (e) {
			console.error(e);
			return { error: e.statusText };
		}).then(function (x) {
			$('#wpTextbox1').prop('disabled', false);
			if ((x === null) || x.error) {
				console.error(x);
				mw.notify(bot + ': ' + (x === null ? 'خطای اساسی' : x.error));
				if (botLink === "ویکی‌پدیا:مقاله‌های درخواستی") {
					generalArticle();
				} else {
					botContentMaker('//dexbot.toolforge.org/c.php', 'Dexbot', 'ویکی‌پدیا:مقاله‌های درخواستی', enName);
				}
				return;
			}
			var summary = 'ایجادشده به کمک [[راهنما:ابزار/به ویکی‌فا|به ویکی‌فا]] و ' + bot, content = x.page_content;
			if (window.persianWikiTools) {
				summary = summary + '، ابرابزار';
				content = persianWikiTools.superTool(content);
			}

			// https://fa.wikipedia.org/w/?diff=21503233
			content = content.replace(/([^\n])\{\{داده‌های کتابخانه‌ای\}\}/, '$1\n{{داده‌های کتابخانه‌ای}}');

			$('input#wpSummary, #wpSummary > input').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('//dexbot.toolforge.org/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 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('//tofawiki-linkstranslator.wmflabs.org/', { p: links.join('|'), from: 'en', to: 'fa' });
		}).then(function (result) {
			if (window.wikEd && window.wikEd.useWikEd) { wikEd.UpdateFrame(); }

			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('(\\[\\[:?)' + mw.util.escapeRegExp(from) + '((?:\\|[^\\]]*)?)(\\]\\])', 'g'),
					'$1' + result[from] + '$3'
				);
			});
			$('#wpTextbox1').val(raw);
		}).then(function () {
			$('#wpTextbox1').prop('disabled', false);
		}, function () {
			mw.notify('ترجمه انجام نشد');
			$('#wpTextbox1').prop('disabled', false);
		});
		$('input#wpSummary, #wpSummary > input').val(
			$('input#wpSummary, #wpSummary > input').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;
		$('input#wpSummary, #wpSummary > input').val('ایجادشده به کمک [[راهنما:ابزار/به ویکی‌فا|به ویکی‌فا]]');
		if (query.requestingPage === "ویکی‌پدیا:ویکی‌پروژه ایجاد مقاله‌های شهرها با ربات/درخواست ساخت رباتیک") {
			botContentMaker('//rezabot.toolforge.org/a.php', 'Rezabot', query.requestingPage, query.enName);
		} else if (query.requestingPage === "ویکی‌پدیا:درخواست ایجاد مقاله (رباتیک)/انسان") {
			botContentMaker('//dexbot.toolforge.org/a.php', 'Dexbot', query.requestingPage, query.enName);
		} else if (query.requestingPage === "صفحهٔ ابهام‌زدایی") {
			botContentMaker('//rezabot.toolforge.org/disambig.php', 'Rezabot', 'ویکی‌پدیا:ابهام‌زدایی', query.enName);
		} else if (query.requestingPage === "ویکی‌پدیا:درخواست ایجاد مقاله (رباتیک)") {
			botContentMaker('//dexbot.toolforge.org/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) {
			botContentMaker('//dexbot.toolforge.org/b.php', 'Dexbot', query.requestingPage, query.enName);
		} else if (mw.config.get('wgNamespaceNumber') === 4) {
			generalArticle();
			importScript('MediaWiki:YandexTranslatorHelper.js');
		} else {
			botContentMaker('//dexbot.toolforge.org/c.php', 'Dexbot', query.requestingPage, query.enName);
		}
	});

    $(function () {
	    // نمایش پررنگ‌تر دکمهٔ پیش‌نمایش
		$('<p style="width: 100%; background-color: #fef6e7; border: 2px solid #fc3; font-size: 90%; padding: 2px;">پیشنهاد می‌شود پیش از ذخیرهٔ نهایی صفحه از گزینهٔ پیش‌نمایش استفاده کنید.</p>').appendTo('#editpage-copywarn');
		//$('#wpSave').prop('disabled', true);
		$('#wpSave').css('font-weight', 'normal');
		$('#wpPreview').css('font-weight', 'bold');
    });
	mw.loader.load( '/w/index.php?title=MediaWiki:TofawikiPass.js&action=raw&ctype=text/javascript' );
}());