$.when(mw.loader.using(['mediawiki.api', 'mediawiki.util']), $.ready)
.then(function() {
if (mw.config.get('wgCanonicalSpecialPageName') !== "Newpages") return;
mw.loader.load('https://fa.wikipedia.org/w/index.php?title=User:Jeeputer/newPagesFeed.css&action=raw&ctype=text/css', 'text/css');
var api = new mw.Api();
function parsePage(id) {
return api.get({
action: 'parse',
oldid: id,
format: 'json'
}).done(function(data) {
var cats = data.parse.categories;
for (var c = 0; c < cats.length; c++) {
var catName = cats[c]['*'];
if (catName === 'مقالههای_نامزد_حذف_سریع') {
var $csdElement = $('<span>')
.attr('title', 'این صفحه نامزد حذف سریع شده است')
.css({
'display': 'none',
'width': '150px'
})
.addClass('npf-row-content-left npf-csd')
.text('نامزد حذف سریع');
$('div#npf-comment-' + id).append($csdElement);
$csdElement.toggle(2000);
} else if (catName === 'صفحههای_حذف_زماندار') {
var $prodElement = $('<span>')
.attr('title', 'این صفحه نامزد حذف زماندار شده است')
.css('display', 'none')
.addClass('npf-row-content-left npf-csd')
.text('نامزد حذف زماندار');
$('div#npf-comment-' + id).append($prodElement);
$prodElement.toggle(2000);
} else if (catName === 'مقالههای_نامزد_حذف') {
var afdUrl = $(data.parse.text['*']).find('a:contains("نظرخواهی مربوط به این صفحه")')[0].href;
var $afdElement = $('<a>')
.attr({
'title': 'این صفحه در نظرخواهی برای حذف نامزد است',
'href': afdUrl
})
.css('display', 'none')
.addClass('npf-row-content-left npf-afd')
.text('نظرخواهی برای حذف');
$('div#npf-comment-' + id).append($afdElement);
$afdElement.toggle(2000);
}
}
var contentCats = cats.filter(function(cat) {
return typeof(cat.hidden) !== "string";
});
var langlinks = data.parse.langlinks || 0;
var enLangLink = false;
if (langlinks) {
for (var li = 0; li < langlinks.length; li++) {
if (langlinks[li].lang === 'en') {
// [ url, English title ]
enLangLink = [langlinks[li].url, langlinks[li]['*']];
break;
}
}
}
var categoriesCount = contentCats.length ? mw.language.convertNumber(contentCats.length) : 'فاقد';
var langLinksCount = langlinks.length ? mw.language.convertNumber(langlinks.length) : 'فاقد';
// Replace "در حال تجزیهٔ صفحه…" with categories and langlinks count
$('#cat-iw-container-' + id).fadeOut(
1000,
function() {
$('#cat-iw-container-' + id).empty();
$('#cat-iw-container-' + id)
.attr('title', 'اطلاعات ردهها و میانویکیها')
.append(
'<span id="npf-cat-count-' + id + '">' + categoriesCount + ' رده</span> • ' +
langLinksCount + ' میانویکی' +
// Append English language link if exists
(enLangLink
? ' (<a href="' + enLangLink[0] + '" title="' + enLangLink[1] + '">en</a>)'
: ''
)
);
// Font color = red if we have no categories
if (contentCats.length < 1) $('#npf-cat-count-' + id).addClass('npf-nocat');
// Show updated content
$('#cat-iw-container-' + id).fadeIn(1000);
});
});
}
function liftwing(id) {
var inferenceUrl = "https://api.wikimedia.org/service/lw/inference/v1/models/fawiki-damaging:predict";
let headers = new Headers({
"Content-Type": "application/json"
});
let data = {"rev_id": Number(id) };
fetch(inferenceUrl, {
method: "POST",
headers: headers,
body: JSON.stringify(data)
})
.then(response => response.json())
.then(inferenceData =>{
var DamagingScore = inferenceData.fawiki.scores[id].damaging.score.probability.true;
if (DamagingScore <= 1 && DamagingScore > 0.5) {
$('#npf-ores-' + id).show();
$('#npf-ores-' + id).css('color', 'red');
$('#npf-ores-' + id).attr('title', 'احتمال خرابکاری زیاد')
}
else if(DamagingScore <= 0.5 && DamagingScore >= 0.11)
{
$('#npf-ores-' + id).show();
$('#npf-ores-' + id).css('color', 'orange');
$('#npf-ores-' + id).attr('title', 'احتمال خرابکاری متوسط')
}
else if(DamagingScore < 0.11)
{
$('#npf-ores-' + id).show();
$('#npf-ores-' + id).css('color', 'green');
$('#npf-ores-' + id).attr('title', 'احتمال خرابکاری کم')
}
});
}
// Utility function to create container div for each row
function createRow(content) {
var $row = $('<div>').addClass('npf-row').append(content);
return $row;
}
function createBox(ingredients) {
var htmlID = 'npf-box-' + ingredients.revid; // Unique ID (e.g. npf-header-39298943) for each article box
var $box = $('<div>')
.attr('id', htmlID)
.addClass('npf-article-box');
if (ingredients.notPatrolled) { // Gray background for non-patrolled pages
$box.addClass('npf-notpatrolled')
.attr('title', 'مقالهٔ گشت نخورده');
}
var $header = $('<div>')
.attr('id', 'npf-header-' + ingredients.revid)
.addClass('npf-flex-row')
.append($('<span>')
.addClass('npf-row-content-right')
.css('display', 'inline-flex')
.append($('<span>')
.css('display', 'inline-flex')
.append(
$('<a>')
.attr({
'href': mw.util.getUrl(ingredients.title),
'title': ingredients.title
})
.addClass('npf-header-title')
.text(ingredients.title)
)
.append($('<span>')
.addClass('npf-ores-indicator')
.attr({
'id': 'npf-ores-' + ingredients.firstrevid
})
.css({
'display': 'none' // Adding the color yellow
})
.text('⚠')
)
)
.append(
$('<span>')
.addClass('npf-hist-link')
.append('(')
.append(
$('<a>')
.attr({
'href': mw.util.getUrl('Special:PageHistory/' + ingredients.title),
'title': 'تاریخچهٔ صفحهٔ ' + ingredients.title
})
.text('تاریخچه')
)
.append(')')
)
.append($('<span>')
.text(' · ' + ingredients.bytes + ' بایت')
.attr('title', 'حجم صفحه')
)
)
.append($('<span>')
.addClass('npf-row-content-left')
.append($('<a>')
.attr({
'href': mw.util.getUrl('Special:Permalink/' + ingredients.firstrevid),
'title': 'پیوند دائمی به نخستین نسخهٔ صفحه'
})
.text(ingredients.date)
)
);
var $details = $('<div>')
.attr('id', 'npf-details-' + ingredients.revid)
.addClass('npf-flex-row')
.append($('<span>')
.attr('title', 'ایجاد کنندهٔ صفحه')
.addClass('npf-row-content-right')
.append($('<span>')
.append('ایجاد شده توسط ')
.append($('<a>')
.addClass('npf-creator' + (ingredients.newUser ? ' new' : ''))
.attr('href', mw.util.getUrl('کاربر:' + ingredients.creator))
.text(ingredients.creator)
)
)
.append($(ingredients.userTools))
)
.append($('<span>')
.attr({
'id': 'cat-iw-container-' + ingredients.revid,
'title': 'لطفاً صبر کنید…'
})
.addClass('npf-row-content-left')
.text('در حال تجزیهٔ صفحه…')
);
var $comment = $('<div>')
.attr('id', 'npf-comment-' + ingredients.revid)
.addClass('npf-flex-row')
.append($('<span>')
.addClass('npf-row-content-right')
.attr('title', 'خلاصهٔ ویرایش در زمان ایجاد صفحه')
.append('خلاصهٔ ویرایش: ')
.append($('<span>')
.addClass('npf-italic')
.append($(ingredients.comment))
)
);
$box.append(createRow($header));
$box.append(createRow($details));
$box.append(createRow($comment));
// Append tag container only if we have some tags
if (ingredients.tags && ingredients.tags.length > 0) {
var $tagElements = $('<span>');
for (var t = 0; t < ingredients.tags.length; t++) {
$tagElements.append($(ingredients.tags[t]));
}
var $tags = $('<div>')
.attr('id', 'npf-tags-' + ingredients.revid)
.addClass('npf-tags-container')
.append($('<span>')
.attr('title', 'برچسبهای نصب شده بر نخستین ویرایش')
.append('برچسبها: ')
.append($tagElements)
);
$box.append(createRow($tags));
}
$('#npf-parent-container').append($box);
$('.mw-pager-navigation-bar').eq(1).insertAfter($box); // Move navigation bar to its initial location
}
var listItems = $('#mw-content-text > section.mw-pager-body > ul > li'),
boxIngredients = [], // Will be populated by information from list items without any API calls
i;
for (i = 0; i < listItems.length; i++) {
var hasTags = $(listItems[i])[0].children[9]; // ...children[8] is always for edit tags
boxIngredients[i] = {
notPatrolled: !!$(listItems[i]).hasClass('not-patrolled'),
title: $(listItems[i])[0].children[1].title,
url: $(listItems[i])[0].children[1].href,
date: $(listItems[i])[0].children[1].innerText,
revid: $($(listItems[i])[0]).attr('data-mw-revid'), // Latest revision's ID is available as "data-mw-revid" attribute for each list item
firstrevid: $(listItems[i])[0].children[1].href.match(/\oldid\=(\d*)/)[1], // First revision's ID
bytes: ($(listItems[i])[0].children[5].innerText).replace(/\[(.*) بایت\]/, '$1'),
creator: $(listItems[i])[0].children[6].innerText,
userTools: $(listItems[i])[0].children[7],
comment: ($(listItems[i])[0].children[8])
};
if (hasTags) { // We have some tags
boxIngredients[i].tags = [];
for (var ei = 0; ei < $(listItems[i])[0].children[9].children.length; ei++) {
var $element = $($(listItems[i])[0].children[9].children[ei]);
if ($element.hasClass('mw-tag-marker')) { // Prevent pushing "برچسبها" to array
// Store tags as jquery object so we can add
// them to the box with without modifications
boxIngredients[i].tags.push($element);
}
}
}
if ($($(listItems[i])[0].children[6]).hasClass('new')) {
boxIngredients[i].newUser = true;
}
}
$('#mw-content-text section.mw-pager-body > ul').remove();
$('#mw-content-text section.mw-pager-body').append('<div id="npf-parent-container"></div>'); // Append main parent container
for (i = 0; i < boxIngredients.length; i++) {
createBox(boxIngredients[i]); // Create boxes
parsePage(boxIngredients[i].revid); // Update categories and langlinks after we have the api response
liftwing(boxIngredients[i].firstrevid);
}
});