مدیاویکی:Gadget-CatMaker.js: تفاوت میان نسخهها
ظاهر
imported>Yamaha5 بدون خلاصۀ ویرایش |
imported>Yamaha5 افزودن getWikidataEntity برای استفاده در آینده |
||
| خط ۳۲: | خط ۳۲: | ||
function faWikiName(title) { | function faWikiName(title) { | ||
return | return new mw.Api({ ajax: { url: '//en.wikipedia.org/w/api.php' } }).get({ | ||
action: 'query', | |||
prop: 'langlinks', | |||
titles: title, | |||
redirects: 1, | |||
format: 'json', | |||
lllimit: 500, | |||
origin: window.location.origin | |||
}).then(function (data) { | }).then(function (data) { | ||
try { | try { | ||
| خط ۵۳: | خط ۵۰: | ||
function catParents(title) { | function catParents(title) { | ||
return | 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.origin | |||
}).then(function (data) { | }).then(function (data) { | ||
try { | try { | ||
return $.map($.map(data.query.pages, function (x) { return x; | return $.map($.map(data.query.pages, function (x) { return x; })[0].categories, function (x) { | ||
return x.title; | return x.title; | ||
}); | }); | ||
} catch (ignore) { } | } catch (ignore) { } | ||
}); | }); | ||
} | |||
function getWikidataEntity(entitle) { | |||
return new mw.Api({ ajax: { url: '//www.wikidata.org/w/api.php' } }).get({ | |||
action: 'wbgetentities', | |||
format: 'json', | |||
sites: 'enwiki', | |||
titles: entitle, | |||
origin: window.location.origin | |||
}).then(function (x) { return $.map(x.entities, function (x) { return x; })[0]; }); | |||
} | } | ||
نسخهٔ ۲۴ آوریل ۲۰۱۴، ساعت ۱۵:۴۴
/*global mediaWiki, jQuery*/
/*jslint browser: true*/
/*!
* @author User:Ebraminio, User:Yamaha5
*/
(function ($, mw) {
'use strict';
//mw.notify('نسخهٔ '+'۳');
function createPage(title, text, summary, callback) {
return new mw.Api().post({
action: 'edit',
title: title,
text: text,
summary: summary,
createonly: '',
minor: '',
token: mw.user.tokens.get('editToken')
}).then(
function (data) {
if (data.error && data.error.info) {
mw.notify(title + ' انجام نشد: ' + data.error.info);
} else {
callback();
}
},
function (data) {
mw.notify(title + ' انجام نشد: ' + data);
}
);
}
function faWikiName(title) {
return new mw.Api({ ajax: { url: '//en.wikipedia.org/w/api.php' } }).get({
action: 'query',
prop: 'langlinks',
titles: title,
redirects: 1,
format: 'json',
lllimit: 500,
origin: window.location.origin
}).then(function (data) {
try {
return $.grep($.map(data.query.pages, function (x) { return x; })[0].langlinks, function (x) {
return x.lang === 'fa';
})[0]['*'];
} catch (ignore) { }
});
}
function catParents(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.origin
}).then(function (data) {
try {
return $.map($.map(data.query.pages, function (x) { return x; })[0].categories, function (x) {
return x.title;
});
} catch (ignore) { }
});
}
function getWikidataEntity(entitle) {
return new mw.Api({ ajax: { url: '//www.wikidata.org/w/api.php' } }).get({
action: 'wbgetentities',
format: 'json',
sites: 'enwiki',
titles: entitle,
origin: window.location.origin
}).then(function (x) { return $.map(x.entities, function (x) { return x; })[0]; });
}
function startCat(falink, enlink) {
return faWikiName(enlink).then(function (x) {
if (x) {
mw.notify('صفحهٔ معادل قبلاً وجود داشت');
return;
}
return catParents(enlink).then(function (x) {
if (!x) {
mw.notify('صفحهٔ "' + enlink + '" در ویکی مبدأ وجود ندارد یا دارای رده نیست');
return;
}
return $.when.apply(null, $.map(x, function (y) {
return faWikiName(y);
})).then(function () {
var catText = $.map(arguments, function (x) {
return '[[' + x + ']]';
}).join('\n') + '\n[[en:' + enlink + ']]';
catText = catText.replace(/\[\[undefined\]\]/g, '');
catText = catText.replace(/\n\n/g, '\n');
return createPage(
falink,
catText,
'ایجاد ردهٔ همسنگ [[:en:' + enlink + ']] با ابزار رده\u200cساز',
function () {
mw.notify('ساخت ' + falink + ' با موفقیت انجام شد.');
}
);
});
});
});
}
function init(e) {
e.preventDefault();
$('<div>' +
'<div style="width: 40em">درخواست\u200cهای ساخت رده را در خطوط جداگانه وارد کنید:</div>' +
'<textarea style="width: 100%; background-color: white;" placeholder="رده۱@Cateogry1" id="catmakerinput" rows="7"></textarea>' +
'</div>'
).dialog({
modal: true,
width: 700,
buttons: [{
id: 'catmaker-button-import',
text: 'ساخت رده',
click: function () {
var formInput = $("#catmakerinput").val();
if (formInput === '') { mw.notify("لطفاً فرم درخواست را به درستی پر کنید"); return; }
$.when.apply(null, $.map(formInput.split('\n'), function (x) {
var req = x.split('@');
if (req.length !== 2) { return; }
return startCat('رده:' + req[0], 'Category:' + req[1]);
})).done(function () {
mw.notify("ساخت رده\u200cها انجام شد، لطفاً بررسی کنید.");
window.location = "/wiki/Special:Contributions/" + mw.config.get('wgUserName');
});
}
}]
});
}
mw.loader.using(['jquery.ui.dialog'], function () {
$(function () {
mw.util.addCSS('#t-catmaker { font-weight: bold; }');
$(mw.util.addPortletLink('p-tb-list', '#', 'رده\u200cساز', 't-catmaker', 'ساخت رده به صورت خودکار')).click(init);
});
});
}(jQuery, mediaWiki));