مدیاویکی:Gadget-CatMaker.js: تفاوت میان نسخهها
ظاهر
imported>Yamaha5 نسخهٔ جدید و سادهشده |
imported>Yamaha5 بدون خلاصۀ ویرایش |
||
| خط ۱۳۰: | خط ۱۳۰: | ||
mw.loader.using(['jquery.ui.dialog'], function () { | mw.loader.using(['jquery.ui.dialog'], function () { | ||
// Probably only one would happen | // Probably only one would happen | ||
$(mw.util.addPortletLink('p-tb-list', '#', 'رده\u200cساز', 't-catmaker', 'ساخت رده به صورت خودکار')).click(init); | $(mw.util.addPortletLink('p-tb-list', '#', 'رده\u200cساز', 't-catmaker', 'ساخت رده به صورت خودکار')).click(init); | ||
}); | }); | ||
}); | }); | ||
نسخهٔ ۲۴ آوریل ۲۰۱۴، ساعت ۱۲:۴۰
/*global mw, $, prompt*/
/*jslint browser: true*/
/*!
* @author User:Ebraminio, User:Yamaha5
*/
$(function () {
'use strict';
//mw.notify('نسخهٔ '+'۳');
mw.util.addCSS('#t-catmaker { font-weight: bold; }');
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 $.ajax({
url: '//en.wikipedia.org/w/api.php',
data: {
action: 'query',
prop: 'langlinks',
titles: title,
redirects: 1,
format: 'json',
lllimit: 500
},
dataType: 'jsonp'
}).then(function (data) {
return $.grep($.map(data.query.pages, function (x) {
return x;
})[0].langlinks, function (x) {
return x.lang === 'fa';
})[0];
}).then(function (x) {
if (x) {
return x['*'];
}
});
}
function catParents(title) {
return $.ajax({
url: '//en.wikipedia.org/w/api.php',
data: {
action: 'query',
prop: 'categories',
titles: title,
clshow: '!hidden',
cllimit: 500,
format: 'json'
},
dataType: 'jsonp'
}).then(function (data) {
return $.map($.map(data.query.pages, function (x) {
return x;
})[0].categories, function (x) {
return x.title;
});
});
}
function startCat(falink, enlink) {
return catParents(enlink).then(function (x) {
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 () {
// Probably only one would happen
$(mw.util.addPortletLink('p-tb-list', '#', 'رده\u200cساز', 't-catmaker', 'ساخت رده به صورت خودکار')).click(init);
});
});