MediaWiki:Gadget-DPL-daty.js
Uwaga: aby zobaczyć zmiany po opublikowaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.
- Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5, lub Ctrl+R (⌘-R na komputerze Mac)
- Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
- Internet Explorer / Edge: Przytrzymaj Ctrl, jednocześnie klikając Odśwież, lub naciśnij klawisze Ctrl+F5
- Opera: Naciśnij klawisze Ctrl+F5.
$(function () {
function parseOptions(raw){
var tokens = raw.split(' ');
return {
noYear: tokens.indexOf('noYear') != -1
};
}
/**
* Applies the date fetched from API to the DPL
* @param {HTMLAnchorElement[]} links Array of links inside the DPL
* @param {{[title: string]: string}} dates Map that transforms the title into a date
*/
function applyDatesFromApi(links, dates) {
links.forEach(function (link) {
if(!dates[link.title]) return;
var textNode = link.previousSibling;
if(textNode) {
textNode.remove();
}
textNode = document.createTextNode(dates[link.title] + ': ');
link.parentElement.insertBefore(textNode, link);
});
}
/**
* Fetches the date categories for articles and updates the DPL
* @param {HTMLAnchorElement[]} links Links inside the DPL
* @param {string[]} titles Titles of pages to fetch categories for
* @param {any} options Options for the script
* @param {string?} continueToken Token from the API to load the next portion of data
*/
function getDateCategories(links, titles, options, continueToken) {
if(titles.length > 50) {
titles = titles.slice(0, 50);
}
var params = {
action: 'query',
format: 'json',
prop: 'categories',
titles: titles,
cllimit: 500,
clcontinue: continueToken
};
var api = new mw.Api();
api.get(params).done(function (data) {
var pages = data.query.pages;
var articleDates = {};
for(var p in pages) {
if(!pages[p].categories) continue;
pages[p].categories.forEach(function (cat) {
var match = cat.title.match(/Kategoria:([123]?[0-9]) (stycznia|lutego|marca|kwietnia|maja|czerwca|lipca|sierpnia|września|października|listopada|grudnia) ([0-9]{4})/);
if(!match) return;
var date = match[1] + ' ' + match[2].substr(0, 3);
if(!options.noYear) date += ' ' + match[3];
articleDates[pages[p].title] = date;
});
}
applyDatesFromApi(links, articleDates);
if(data.continue && data.continue.clcontinue) {
getDateCategories(links, titles, options, data.continue.clcontinue);
}
});
}
// Get all the DPLs on the page and then get links inside them
// Also parses any options that could be passed
var dpls = document.querySelectorAll('.dpl, .lista-najnowszych');
dpls.forEach(function(dpl){
var links = dpl.querySelectorAll('a');
var options = parseOptions(dpl.dataset.dpl || '');
if(links.length > 0) {
titles = [];
links.forEach(function (link) { titles.push(link.title); });
getDateCategories(links, titles, options);
}
});
});