Користувач:Olexa Riznyk/translations status.js
Перейти до навігації
Перейти до пошуку
Увага: Після публікування слід очистити кеш браузера, щоб побачити зміни.
- Firefox / Safari: тримайте Shift, коли натискаєте Оновити, або натисніть Ctrl-F5 чи Ctrl-Shift-R (⌘-R на Apple Mac)
- Google Chrome: натисніть Ctrl-Shift-R (⌘-Shift-R на Apple Mac)
- Internet Explorer / Edge: тримайте Ctrl, коли натискаєте Оновити, або натисніть Ctrl-F5
- Opera: натисніть Ctrl-F5
// Translations Status: On "/Переклади" subpages of private users' pages,
// displays lists of links to translated articles as tables with status of their tralslations,
// based on data from "Шаблон:Перекладена стаття" or "Шаблон:Запозичений код" on their talk pages.
const translTemplateNames = ['translated page',
'translated',
'перекладена стаття',
'пкс',
'перекладено',
'перекладена сторінка',
'пс',
'частково перекладена стаття',
'запозичений код',
'запозичений шаблон',
'перекладений шаблон'
];
const importances = ['', 'низька', 'середня', 'висока', 'найвища'];
var api = new mw.Api();
var sourceApis = {};
function getSourceApi(sourceLang) {
if (!sourceApis[sourceLang]) {
sourceApis[sourceLang] = new mw.Api({
ajax: {
url: 'https://' + sourceLang + '.wikipedia.org/w/api.php',
dataType: 'jsonp'
}
});
}
return sourceApis[sourceLang];
}
function updateFromTranslVersion(translVersion) {
api.get({
action: 'query',
revids: translVersion,
prop: 'revisions',
rvprop: 'ids|timestamp',
format: 'json'
})
.done(function(data){
const currentTimestamp = new Date();
const translRevision = data.query.pages[Object.keys(data.query.pages)[0]].revisions[0];
const translAge = (currentTimestamp - new Date(translRevision.timestamp)) / (1000 * 60 * 60 * 24);
$('#oldid' + translRevision.revid).text(Math.floor(translAge));
});
}
function updateFromSourceVersion(sourceLang, sourceVersion) {
sourceApi = getSourceApi(sourceLang);
sourceApi.get({
action: 'query',
revids: sourceVersion,
format: 'json'
}).done(function(data) {
const sourcePageId = data.query.pages[Object.keys(data.query.pages)[0]].pageid;
sourceApi.get({
action: 'query',
prop: 'revisions',
pageids: sourcePageId,
rvprop: 'timestamp|size',
rvlimit: 'max',
rvstartid: sourceVersion,
rvdir: 'newer',
format: 'json'
}).done(function(data) {
const sourceRevisions = data.query.pages[sourcePageId].revisions;
const sourceNewEditsCount = sourceRevisions.length - 1;
var sourceChangedAge, sourceSizeChange;
if (sourceNewEditsCount > 0) {
sourceChangedAge = ((new Date()) - new Date(sourceRevisions[1].timestamp)) / (1000 * 60 * 60 * 24);
sourceSizeChange = sourceRevisions[sourceNewEditsCount].size - sourceRevisions[0].size;
} else {
sourceChangedAge = 0;
sourceSizeChange = 0;
}
const sourceChangedAgeElement = $('#oldid' + sourceVersion + sourceLang);
sourceChangedAgeElement.text(Math.floor(sourceChangedAge));
const sourceNewEditsCountElement = sourceChangedAgeElement.next();
sourceNewEditsCountElement.text(sourceNewEditsCount);
const sourceSizeChangeElement = sourceNewEditsCountElement.next();
sourceSizeChangeElement.text(sourceSizeChange);
}).fail(function(error) {
console.error('Error fetching ' + sourceLang + ' ' + sourceVersion + ' revisions info from API: ' + error);
});
}).fail(function(error) {
console.error('Error fetching ' + sourceLang + ' ' + sourceVersion + ' page info from API: ' + error);
});
}
function updateFromTalkTitle(talkTitle) {
api.get({
action: 'parse',
page: talkTitle.getPrefixedText(),
section: 0,
prop: 'parsetree',
format: 'json'
})
.done(function(data){
const talkName = data.parse.title;
// Process importances
var translImportance = 0;
$($.parseXML(data.parse.parsetree['*'])).find('template').each(function() {
const curImportanceText = $(this).find('part > name:contains("важливість")').siblings('value').text();
if (importances.includes(curImportanceText)) {
translImportance = Math.max(translImportance, importances.indexOf(curImportanceText));
}
});
// Process translation templates
$($.parseXML(data.parse.parsetree['*'])).find('root').children('template').each(function() {
if (translTemplateNames.includes($(this).children('title').first().text().toLowerCase())) {
var importanceElement = $('[title="' + talkName + '"]');
importanceElement.attr('data-sort-value', translImportance);
if (translImportance > 0)
importanceElement.text(importances[translImportance]);
const sourceLang = $(this).children('part').first().text();
langElement = importanceElement.next();
langElement.text(sourceLang);
var translAgeElement = langElement.next();
const translVersion = $(this).find('part > name:contains("insertversion")').siblings('value').text();
if (translVersion) {
translAgeElement.attr('id', 'oldid' + translVersion);
updateFromTranslVersion(translVersion);
}
var sourceAgeElement = translAgeElement.next();
var sourceVersion = $(this).find('part > name:contains("версія")').siblings('value').text();
if (!sourceVersion)
sourceVersion = $(this).find('part > name:contains("version")').siblings('value').text();
if (sourceVersion) {
sourceAgeElement.attr('id', 'oldid' + sourceVersion + sourceLang);
updateFromSourceVersion(sourceLang, sourceVersion);
}
return false;
}
});
});
}
mw.loader.using(['mediawiki.api', 'jquery.ui', 'jquery.tablesorter'], function () {
$(document).ready( function () {
if (!mw.config.get('wgIsArticle'))
return;
var title = new mw.Title(mw.config.get('wgPageName'));
if (title.getNamespaceId() != 2 // Not user page
|| !title.getName().endsWith('/Переклади')) // Not "Переклади" sub-page
return;
$('#bodyContent').find('ul').each(function () {
var table = $('<table class="wikitable sortable"/>');
thead = $('<thead/>');
thead.append('<tr>'
+ '<th>№</th>'
+ '<th>Сторінка</th>'
+ '<th>Важливість</th>'
+ '<th>Мова</th>'
+ '<th>Днів<br/>перекладові</th>'
+ '<th>Днів<br/>неактуальності</th>'
+ '<th>Нових<br/>змін</th>'
+ '<th>Зміна<br/>розміру</th>'
+ '</tr>');
table.append(thead);
tbody = $('<tbody/>');
num = 0;
var talkTitles = [];
$(this).find('li').each(function () {
var articleTitle = new mw.Title($(this).find('a:first').attr('title'));
talkTitles.push(articleTitle.getTalkPage());
tbody.append('<tr id="' + articleTitle.getPrefixedText() + '">'
+ '<td align="right">' + (++num) + '</td>'
+ '<td>' + $(this).html() + '</td>'
+ '<td title="' + articleTitle.getTalkPage().getPrefixedText() + '"> </td>' // TODO: Use jQuery instead of constructing HTML to address escaping
+ '<td align="right"> </td>'
+ '<td align="right"> </td>'
+ '<td align="right"> </td>'
+ '<td align="right"> </td>'
+ '<td align="right"> </td>'
+ '</tr>');
});
table.append(tbody);
$(this).replaceWith(table);
talkTitles.forEach((element) => {updateFromTalkTitle(element)});
});
$('#bodyContent').find('table').each(function () {$(this).tablesorter();});
});
});