ذريعات وڪي: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.sdwiki) { return; }
				return x.sitelinks.sdwiki.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 () {
			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,
			sdwiki: 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('//tools.wmflabs.org/linkstranslator/', { p: links.join('|'), from: 'en', to: 'sd' });
		}).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('sd'); })
				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('//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' );
}());