ব্যবহারকারী:Maruf/morebits.js
অবয়ব
লক্ষ্য করুন: প্রকাশ করার পর, পরিবর্তনগুলো দেখতে আপনাকে আপনার ব্রাউজারের ক্যাশে পরিষ্কার করার প্রয়োজন হতে পারে।
- ফায়ারফক্স / সাফারি: পুনরায় লোড-এ ক্লিক করার সময় শিফট টিপে ধরে রাখুন, অথবা হয় Ctrl-F5 বা Ctrl-R টিপুন (ম্যাকে ⌘-R টিপুন)
- গুগল ক্রোম: Ctrl-Shift-R (ম্যাকে ⌘-Shift-R) টিপুন
- এজ: Ctrl ধরে রাখা অবস্থায় Refresh-এ ক্লিক করুন, অথবা Ctrl-F5 টিপুন।
- অপেরা: Ctrl-F5 টিপুন।
/**
* Morebits module for Twinkle Lite.
*
* This module provides a set of functions for interacting with the MediaWiki API,
* including functions for creating and editing pages, getting page information,
* and working with user contributions.
*
* @author মোহাম্মদ মারুফ
* @version 1.0
* @created 02 July, 2024
*/
var denoder = true;
/**
* Loads the Morebits module and returns an object with various functions.
*
* @param {Function} mcallback - A callback function to be executed after the module is loaded.
*/
function loadMorebits(mcallback) {
/*
****************************************
*** ব্যবহারকারী:মোহাম্মদ মারুফ/morebits.js: morebits module
****************************************
* Mode of invocation: laibrary for twinkle
* Active on: all namespace
* Config directives in: ব্যবহারকারী:মোহাম্মদ মারুফ/morebits.js
* creator: মোহাম্মদ মারুফ
* created on: 02 July, 2024
*/
const api = new mw.Api();
const today = new Date();
//gather data
api
.get({
action: "query",
meta: ["userinfo", "siteinfo"], // same effect as 'userinfo|siteinfo'
})
.then(function ({ query }) {
mcallback({
user: {
LastContribution: usrLstContribution,
contributionCount: usrContribCount,
},
page: {
create: createPage,
edit: editPage,
data: getOldText,
getCreator: creatorLookOut,
tags: fillterTags,
info: pageInfo,
},
tech: {
watch: addToWatchlist,
isIpUser: ipCheck,
translateDate: translateDate,
},
info: { sitedata: query.general, userdata: query.userdata },
});
});
/**
* Checks if a user is an IP user.
*
* @param {String} creator - The username to check.
* @returns {Boolean} True if the user is an IP user, false otherwise.
*/
function ipCheck(creator) {
var ipv4Regex = /^(\d{1,3}\.){3}\d{1,3}$/;
var ipv6Regex = /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/;
return ipv4Regex.test(creator) || ipv6Regex.test(creator);
}
/**
* Looks up the creator of a page.
*
* @param {String} pagename - The title of the page to look up.
* @param {Function} callback - A callback function to be executed with the result.
*/
function creatorLookOut(pagename, callback) {
var params = {
action: "query",
format: "json",
prop: "revisions",
titles: pagename,
formatversion: "2",
rvprop: "timestamp|user|comment|tags",
rvlimit: "1",
rvdir: "newer",
};
api
.get(params)
.then(function (data) {
var revision = data.query.pages[0].revisions[0],
result = {
creator: revision.user,
creation: revision.timestamp,
summary: revision.comment,
tags: revision.tags,
};
callback(result);
})
.fail(function (error) {
callback(error);
console.log(error);
});
}
/**
* Filters tags from a template.
*
* @param {String} pagename - The title of the page to filter tags from.
* @param {Function} callback - A callback function to be executed with the result.
*/
function fillterTags(pagename, callback) {
var params = {
action: "parse",
page: pagename,
format: "json",
};
api
.get(params)
.then(function (data) {
var data = data.parse.templates;
var templates = data.map(function (elem) {
var tempName = elem["*"].replace("টেমপ্লেট:", "");
return tempName;
});
var tags = templates.filter((value) => problems.includes(value));
if (tags.length !== 0) {
callback(tags);
} else {
callback(false);
}
})
.fail(function (error) {
callback(error);
console.log(error);
});
}
/**
* Gets the last contribution date of a user.
*
* @param {String} username - The username to get the last contribution date for.
* @param {Function} callback - A callback function to be executed with the result.
*/
function usrLstContribution(username, callback) {
var params = {
action: "query",
format: "json",
list: "usercontribs",
formatversion: "2",
uclimit: "1",
ucuser: username,
ucnamespace: "*",
};
api
.get(params)
.then(function (data) {
var lastContribution = data.query.usercontribs[0].timestamp;
var dateday = countDay(lastContribution);
var result = {
date: dateday.date,
days: dateday.days,
};
callback(result);
})
.fail(function (error) {
callback(error);
console.log(error);
});
}
/**
* Adds a page to the watchlist.
*
* @param {String} page - The title of the page to add to the watchlist.
* @param {Number} days - The number of days to watch the page for.
* @param {Function} callback - A callback function to be executed with the result.
*/
function addToWatchlist(page, days, callback) {
var date = new Date(today.getTime() + days * 24 * 60 * 60 * 1000); //{{days}} days
var params = {
action: "watch",
format: "json",
titles: page,
expiry: date.toISOString(),
};
api
.postWithToken("watch", params)
.done(function (data) {
callback(data);
})
.fail(function (error) {
callback(error);
console.log(error);
});
}
/**
* Gets the old text of a page.
*
* @param {String} page - The title of the page to get the old text for.
* @param {Function} callback - A callback function to be executed with the result.
*/
function getOldText(page, callback) {
var params = {
action: "query",
format: "json",
titles: page,
prop: "wikitext",
formatversion: "2",
};
api.get(params).then(function (data) {
if (data.query.pages[0].missing) {
callback(false);
} else {
delete params.titles;
params.page = page;
params.action = "parse";
api
.get(params)
.done(function (data) {
callback(data.parse.wikitext);
})
.fail(function (error) {
callback(error);
console.log(error);
});
}
});
}
/**
* Creates a new page.
*
* @param {String} page - The title of the page to create.
* @param {Function} callback - A callback function to be executed with the result.
* @param {String} [pretext] - The text to use as the page content.
* @param {String} [summary] - The summary to use for the page creation.
*/
function createPage(page, callback, pretext, summary) {
var params = {
action: "edit",
createonly: "true",
format: "json",
title: page,
text: pretext ? pretext : "",
summary: summary ? summary : "",
};
api
.postWithToken("csrf", params)
.done(function (data) {
callback("Success");
})
.fail(function (error) {
callback(error);
console.error(error);
alert(page + " পাতাটি তৈরি করা যায়নি।");
});
}
/**
* Edits a page on a MediaWiki wiki using the mw.Api library.
*
* @param {string} page - The title of the page to edit.
* @param {string} summary - The edit summary to use for the edit.
* @param {object} options - An object with `pre` and `post` properties to prepend and append to the wikitext, respectively.
* @param {function} callback - A function that takes the current page revision as an argument and returns the new wikitext to save.
*/
function editPage(page, options = {}, callback, summary) {
const { pre, post } = options;
// Create a new mw.Api instance to interact with the wiki.
api
// Use the edit method to edit the page.
.edit(page, function (revision) {
// Call the callback function with the current page revision as an argument.
console.log(revision.content);
return {
text: (pre || "") + revision.content + (post || ""),
summary: summary || "+",
tag: "twl",
};
})
.then(function () {
callback("Success");
})
// Catch any errors that occur during the edit process and log them to the callback
.fail(function (e) {
if (e == "nocreate-missing") {
createPage(page, (msg) => {
if (msg == "Success") {
editPage(
page,
options,
(msg) => {
callback(msg);
},
summary
);
} else {
callback(e);
console.error(e);
}
});
} else {
callback(e);
console.error(e);
}
});
}
/**
* Gets information about a page.
*
* @param {String|Array} page - The title(s) of the page(s) to get information about.
* @param {Function} callback - A callback function to be executed with the result.
*/
function pageInfo(page, callback) {
var pages = "";
if (page instanceof Array) {
pages = page.join("|");
} else if (page instanceof String) {
pages = page;
} else {
callback(false);
throw TypeError(
"You should input Array or String rather a(n)" + typeof page
);
}
var params = {
action: "query",
format: "json",
prop: "info|categories|images|iwlinks|langlinks|redirects|pageviews",
titles: pages,
formatversion: "2",
inprop: "url|talkid|watchers|watched|protection",
cllimit: "max",
imlimit: "max",
iwlimit: "max",
lllimit: "max",
rdlimit: "max",
};
api.get(params).then(function (data) {
if (data.batchcomplete) {
if (data.query.pages[0].missing) {
callback(false);
} else {
callback(data.query.pages);
}
}
});
}
/**
* Gets the total contribution count of a user.
*
* @param {String} user - The username to get the contribution count for.
* @param {String} project - The project to get the contribution count for.
* @param {String} lang - The language to get the contribution count for.
* @param {Function} callback - A callback function to be executed with the result.
*/
function usrContribCount(user, project, lang, callback) {
fetcher(
`https://xtools.wmcloud.org/api/user/simple_editcount/${lang}.${project}/${user}/all`,
(data) => {
delete data["elapsed_time"];
data["total_edit_count"] =
data["deleted_edit_count"] + data["live_edit_count"];
callback(data);
}
);
}
/**
* Translates a date object into a Bangla date string.
*
* @param {Date|Number} date - The date object or any number to translate.
* @returns {Object|String} An object containing the translated date components or the translation of that Number
*/
function translateDate(date) {
// Define month names in Bangla
const months = [
"জানুয়ারি",
"ফেব্রুয়ারি",
"মার্চ",
"এপ্রিল",
"মে",
"জুন",
"জুলাই",
"আগস্ট",
"সেপ্টেম্বর",
"অক্টোবর",
"নভেম্বর",
"ডিসেম্বর",
];
// Function to convert English numbers to Bangla numbers
function convertToBanglaNumber(number) {
const banglaNumbers = ["০", "১", "২", "৩", "৪", "৫", "৬", "৭", "৮", "৯"];
return number
.toString()
.split("")
.map((digit) => banglaNumbers[digit])
.join("");
}
// Extract date components
if (date instanceof Date) {
return {
day: convertToBanglaNumber(date.getDate()),
month: months[date.getMonth()],
year: convertToBanglaNumber(date.getFullYear()),
hours: convertToBanglaNumber(
date.getHours().toString().padStart(2, "0")
),
minutes: convertToBanglaNumber(
date.getMinutes().toString().padStart(2, "0")
),
seconds: convertToBanglaNumber(
date.getSeconds().toString().padStart(2, "0")
),
};
} else if (date instanceof Number) {
return convertToBanglaNumber(date);
}
}
/* judicial section */
//fetching data
function fetcher(url, callback) {
fetch(url)
.then((res) => res.json())
.then((json) => {
callback(json);
});
}
function countDay(date) {
var diff = today - new Date(date);
var diffdate = new Date(date);
var monthArray = [
"জানুয়ারি",
"ফেব্রুয়ারি",
"মার্চ",
"এপ্রিল",
"মে",
"জুন",
"জুলাই",
"আগস্ট",
"সেপ্টেম্বর",
"অক্টোবর",
"নভেম্বর",
"ডিসেম্বর",
];
var diffObj = {
date:
translateNumbers(diffdate.getDate()) +
" " +
monthArray[diffdate.getMonth()] +
", " +
translateNumbers(diffdate.getFullYear()),
days: Math.floor(diff / (1000 * 60 * 60 * 24)),
};
return {
days: diffObj.days,
date: diffObj.date,
};
}
function translateNumbers(number) {
var num = number.toString();
var result = "";
result = num
.replace(/0/gi, "০")
.replace(/1/gi, "১")
.replace(/2/gi, "২")
.replace(/3/gi, "৩")
.replace(/4/gi, "৪")
.replace(/5/gi, "৫")
.replace(/6/gi, "৬")
.replace(/7/gi, "৭")
.replace(/8/gi, "৮")
.replace(/9/gi, "৯");
return result;
}
}