میڈیاویکی:Gadget-asbox.js
یاددہانی: محفوظ کرنے کے بعد تازہ ترین تبدیلیوں کو دیکھنے کے لیے آپ کو اپنے براؤزر کا کیش صاف کرنا ہوگا۔
- فائرفاکس/ سفاری: جب Reload پر کلک کریں تو Shift دبا کر رکھیں، یا Ctrl-F5 یا Ctrl-R دبائیں (Mac پر R- )
- گوگل کروم: Ctrl-Shift-R دبائیں (Mac پر Shift-R-⌘)
- انٹرنیٹ ایکسپلورر: جب Refresh پر کلک کریں تو Ctrl یا Ctrl-F5 دبائیں
- اوپیرا: Tools → Preferences میں جائیں اور کیش صاف کریں
شاید اس اسکرپٹ کی دستاویزی تفصیلات میڈیاویکی:Gadget-asbox پر موجود ہیں۔ |
/*global jQuery, mediaWiki*/
/* <nowiki> */
/*!
* Developed by: [[User:Ebraminio]]
*/
(function ($, mw) {
'use strict';
var spanCode = '<center><img src="//upload.wikimedia.org/wikipedia/commons/4/42/Loading.gif" /></center>';
function displayProgress(message) {
$('#stubForm').empty().append($('<div>', {
style: 'text-align:center; margin:3em 0;',
html: message + '<br/>' + spanCode
}));
}
function displayError(error) {
$('#stubForm').empty().append($('<div>', {
style: 'color: #990000; margin-top: 0.4em;',
html: 'Error: ' + error
}));
}
function editThePage(summary, content) {
return new mw.Api().post({
action: 'edit',
title: mw.config.get('wgPageName'),
summary: summary,
text: content,
format: 'json',
token: mw.user.tokens.get('csrfToken')
});
}
function fetchThePage() {
return new mw.Api().get({
action: 'query',
prop: 'revisions',
titles: mw.config.get('wgPageName'),
rvprop: 'content',
format: 'json'
}).then(function (data) {
return $.map(data.query.pages, function (value) { return value; })[0].revisions[0]['*'];
});
}
/**
* This will help on better sorting on Persian string array
*/
function dePersian(input) { // solve persian problem on sorting by replace characters in strings
return input.replace(/ی/g, "ي")
.replace(/ک/g, "ك")
.replace(/گ/g, "كی")
.replace(/ژ/g, "زی")
.replace(/چ/g, "جی")
.replace(/پ/g, "بی");
}
/**
* String comparator for Persian
*/
function customComparator(a, b) {
var keyA = dePersian(a),
keyB = dePersian(b);
if (keyA < keyB) {
return -1;
}
if (keyA > keyB) {
return 1;
}
return 0;
}
/**
* Escape string for regex
*/
function escape(text) {
return text.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&');
}
/**
* Is a recursive function to retrieve all stub templates consequently
*/
function getAllStubTemplates(array, eicontinue) {
return new mw.Api().get({
action: 'query',
list: 'embeddedin',
eititle: 'Template:Asbox',
eilimit: 500,
einamespace: 10,
format: 'json',
eicontinue: eicontinue,
'continue': ''
}).then(function (data) {
if (!array) { array = []; }
array = array.concat($.map(data.query.embeddedin, function (value) {
return value.title;
}));
if (!data['continue']) {
return array;
}
return getAllStubTemplates(array, data['continue'].eicontinue);
});
}
/**
* check if is expired, currently expires in three days
*/
function isExpired(cacheTime) {
return (new Date().getTime() - cacheTime) > (1000 * 60 * 60 * 24 * 3);
}
/**
* If browser support localStorage, this would use it
*/
function cachedGetAllStubTemplates() {
if (window.localStorage !== undefined) {
var object, defer;
try {
object = JSON.parse(localStorage.getItem('allStubTemplates'));
} catch (ignore) {}
if (object && !isExpired(parseInt(object.timestamp))) {
defer = $.Deferred();
return $.Deferred().resolve(object.value);
}
}
return getAllStubTemplates().then(function (array) {
if (array.length > 0 && window.localStorage !== undefined) {
try {
window.localStorage.setItem('allStubTemplates', JSON.stringify({
value: array,
timestamp: new Date().getTime()
}));
} catch(ignore) {}
}
return array;
});
}
function getParsedTemplate(template) {
return new mw.Api().get({
action: 'parse',
text: '{{' + template + '}}',
contentmodel: 'wikitext',
prop: 'text'
}).then(function (data) { return data.parse.text['*']; });
}
/**
* Will launch stub template adder
*/
function launchDialog() {
$('#stubForm').remove();
$('<div>', {
id: 'stubForm',
style: 'position: relative;'
}).append($('<div>', {
style: 'margin-top: 0.4em;',
html: 'انتخاب سانچہ نامکمل: '
}).append($('<select>', {
id: 'stubselect',
style: 'padding: 1px; vertical-align: baseline; font-family: inherit',
html: '<option value="" selected="selected">سانچوں کی فہرست لوڈ ہورہی ہے</option>'
}), $('<div>', { id: 'asboxpreview', style: 'padding-top: 1em' }))).dialog({
width: 500,
title: 'اضافہ سانچہ نامکمل',
modal: true,
buttons: [{
text: 'اضافہ',
class: 'ui-button-green',
click: function () {
var template = $('#stubselect').val();
if (!template) { return; }
$('#stubForm').dialog({
buttons: {}
});
displayProgress('اضافہ سانچہ نامکمل۔۔۔');
fetchThePage().then(function (content) {
content = content.replace(
new RegExp('{{\\s*([^:}]*:)?(نامکمل|ناقص|' + escape(template) + ')\\s*}}\\n*', 'g'),
''
).replace(/\n+$/, '');
if (!/نامکمل\}\}$/.test(content)) {
content = content + '\n\n';
}
return editThePage('اضافہ سانچہ نامکمل بذریعہ آلہ', content + '\n' + '{{' + template + '}}');
}).then(function (data) {
if (data.edit.result === 'Success') {
window.location.reload();
} else {
displayError('Unknown result from API.');
}
}, function () {
displayError('Edit failed.');
});
}
}],
close: function () {
$("#stubForm").remove();
}
});
cachedGetAllStubTemplates().then(function (stubs) {
stubs = stubs.filter(function (x) { return x !== 'سانچہ:نامکمل'; });
stubs.push('');
stubs.sort(customComparator);
$('#stubselect').empty().change(function () {
var template = $('#stubselect').val();
if (!template) {
$('#asboxpreview').html('');
return;
}
$('#asboxpreview').html(spanCode);
getParsedTemplate(template).then(function (html) {
$('#asboxpreview').html(html);
});
}).append($.map(stubs, function (value) {
return $('<option>', {
html: value.replace(/^[^:]*:/, ""),
val: value.replace(/^[^:]*:/, "")
});
}));
});
}
function initialize() {
$('#ca-stub-adder').remove();
$(mw.util.addPortletLink('p-cactions', '#', 'نامکمل', 'ca-stub-adder', 'اضافہ سانچہ نامکمل')).click(function (e) {
e.preventDefault();
launchDialog();
});
}
if (mw.config.get('wgNamespaceNumber') === 0
&& mw.config.get('wgAction') === 'view') {
$.when($.ready, mw.loader.using('jquery.ui')).then(initialize);
}
}(jQuery, mediaWiki));