میڈیاویکی:Tourwikihelper.js

آزاد دائرۃ المعارف، ویکیپیڈیا سے

تفصیل کے لیے کھولیں کے بٹن پر کلک کریں یاددہانی: محفوظ کرنے کے بعد تازہ ترین تبدیلیوں کو دیکھنے کے لیے آپ کو اپنے براؤزر کا کیش صاف کرنا ہوگا۔

  • فائرفاکس/ سفاری: جب Reload پر کلک کریں تو Shift دبا کر رکھیں، یا Ctrl-F5 یا Ctrl-R دبائیں (Mac پر R- )
  • گوگل کروم: Ctrl-Shift-R دبائیں (Mac پر Shift-R-⌘)
  • انٹرنیٹ ایکسپلورر: جب Refresh پر کلک کریں تو Ctrl یا Ctrl-F5 دبائیں
  • اوپیرا: Tools → Preferences میں جائیں اور کیش صاف کریں

(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 $.map(entities, function (x) {
				if (!x.sitelinks || !x.sitelinks.urwiki) { return; }
				return x.sitelinks.urwiki.title;
			});
		});
	}

	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\n' +
				'== مزید دیکھیے ==\n*\n\n' + '== حوالہ جات ==\n' +
				'{{حوالہ جات}}\n\n';
			getSeeAlsoPages(enName).then(function (pages) {
				$('#wpTextbox1').val($('#wpTextbox1').val().replace(
					'* [[متعلقہ مضمون]]',
					'* [[' + pages.join(']]\n* [[') + ']]'
				));
			});
		}
		text = text + '[[زمرہ' + ':زمرہ1]' + ']\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(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(
				'[[زمرہ' + ':زمرہ1]' + ']',
				'[[' + faCats.join(']]\n[[') + ']]'
			));
		});
	}
	
	var pleaseWait = 'براہ کرم کچھ دیر توقف کریں';
	
	function inProgressDots() {
		setTimeout(function () {
			var val = $('#wpTextbox1').val();
			if (val && 0 !== 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,
			urwiki: mw.config.get('wgTitle')
		};
		inProgressDots();
		$.getJSON(uri.toString()).then(function (x) {
			return ((x === null) || x.error)
				? x
				: preSavedTransform(x.page_content, mw.config.get('wgTitle')).then(function (x) {
					return { page_content: x };
				});
		}).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 = 'تخلیق بذریعہ [[وپ:ویکی معاون|ویکی معاون]] و ' + bot, content = x.page_content;
			if (window.persianWikiTools) {
				summary = summary + '، ابرابزار';
				content = persianWikiTools.superTool(content);
			}

			content = content.replace(/([^\n])\{\{Authority control\}\}/, '$1\n{{معلومات کتب خانہ}}');

			$('input#wpSummary, #wpSummary > input').val(summary);
			$('#wpTextbox1').val(content);
			//('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 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('//https://linkstranslator.toolforge.org/', { p: links.join('|'), from: 'en', to: 'ur' });
		}).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('ur'); })
				raw = raw.replace(/{{Saved book/i, '{{کتاب محفوظ ہو گئی');
			}
			Object.keys(result).forEach(function (from) {
				raw = raw.replace(
					new RegExp('(\\[\\[:?)' + mw.RegExp.escape(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('//tools.wmflabs.org/shuaib-bot/shehrsaz.php', 'Shuaib-bot', 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/shuaib-bot/disambig.php', 'Shuaib-bot', 'ویکیپیڈیا:ضد ابہام', 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);
		}
	});

    $(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:TourwikiPass.js&action=raw&ctype=text/javascript' );
}());