MediaWiki:Common.js: Perbedaan antara revisi
Tidak ada ringkasan suntingan Tanda: Suntingan perangkat seluler Suntingan peramban seluler |
Tidak ada ringkasan suntingan Tanda: Suntingan perangkat seluler Suntingan peramban seluler |
||
| Baris 1: | Baris 1: | ||
/* ========================================================== | /* ========================================================== | ||
🧠 MIPPEDIA DATA - DYNAMIC CONTEXT v9 (FINAL FIX) | |||
Features: Auto-Thumbnail, Bold, Portal Sync, | |||
NEW: Dynamic Anniversary & Data Quality Guard | |||
========================================================== */ | |||
(function() { | (function() { | ||
$(document).ready(function() { | |||
var $descSection = $('#mip-desc-section'), $descBox = $('#mip-auto-description'), | |||
$sourceInfo = $('#mip-source-info'), $portalLinks = $('#mip-portal-links'), | |||
$projectPortal = $('#mip-project-portal'); | |||
if (!$descBox.length) return; | |||
var pageTitle = mw.config.get('wgPageName').replace(/_/g, ' '), | |||
cacheKey = 'mip_dynamic_v9_' + pageTitle, | |||
currentYear = 2026; | |||
var projects = [ | |||
{ id: 'id', name: 'Mippedia bahasa Indonesia', url: 'https://id.mippedia.org/api.php', base: 'https://id.mippedia.org/wiki/', label: 'Bahasa Indonesia' }, | |||
{ id: 'en', name: 'Mippedia bahasa Inggris', url: 'https://en.mippedia.org/api.php', base: 'https://en.mippedia.org/wiki/', label: 'Bahasa Inggris' }, | |||
{ id: 'concise', name: 'Mippedia bahasa Indonesia ringkas', url: 'https://concise.mippedia.org/api.php', base: 'https://concise.mippedia.org/wiki/', label: 'Versi Ringkas' } | |||
]; | |||
// --- 1. FITUR PENANGGALAN DINAMIS (PINTAR) --- | |||
function applyDynamicContext(text) { | |||
var date = new Date(); | |||
var bulan = ["Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember"]; | |||
var tglSkrg = date.getDate() + " " + bulan[date.getMonth()] + " " + date.getFullYear(); | |||
// Cek apakah ini organisasi/proyek (kata kunci: Community, Proyek, Organisasi, Yayasan) | |||
var isOrg = /(Community|Proyek|Organization|Yayasan|Perusahaan|Grup)/i.test(pageTitle) || /(didirikan|berdiri|diluncurkan)/i.test(text); | |||
var ageLabel = isOrg ? "berusia" : "usia"; | |||
var dateLabel = isOrg ? "Berdiri sejak" : "Lahir"; | |||
return text | |||
.replace(/(hari ini|saat ini|sekarang)/gi, '$1 (' + tglSkrg + ')') | |||
.replace(/(\d{1,2}\s(?:Januari|Februari|Maret|April|Mei|Juni|Juli|Agustus|September|Oktober|November|Desember)\s(\d{4}))/gi, function(m, f, y) { | |||
var gap = currentYear - parseInt(y); | |||
return f + " <span style='color: #444; font-weight: bold;'>– " + ageLabel + " " + gap + " tahun</span>"; | |||
}) | |||
.replace(/(sejak|tahun)\s(\d{4})/gi, function(m, k, y) { | |||
var gap = currentYear - parseInt(y); | |||
if (gap > 0 && gap < 100) return k + " " + y + " <small style='color: #888;'>(" + gap + " thn lalu)</small>"; | |||
return m; | |||
}); | |||
} | |||
// --- 5. DATA QUALITY GUARD (PERINGATAN MINIM) --- | |||
function applyQualityGuard(text) { | |||
var wordCount = text.split(/\s+/).length; | |||
if (wordCount < 20) { // Jika kurang dari 20 kata | |||
return text + ' <br><p style="margin-top:10px; padding:8px; background:#f9f9f9; border-left:4px solid #6a5acd; font-style:italic; font-size:0.9em; color:#555;">⚠️ Halaman ini sedang dalam pengembangan oleh <strong>Mippedia Community</strong>.</p>'; | |||
} | |||
return text; | |||
} | |||
function applyAutoBold(text) { return text.replace(new RegExp('(' + pageTitle + ')', 'gi'), '<strong>$1</strong>'); } | |||
function cleanExtract(text) { return text.replace(/\[\d+\]/g, '').replace(/\{\{[^}]+\}\}/g, '').replace(/\(\s*\)/g, '').replace(/\s\s+/g, ' ').trim(); } | |||
function applySummary(text) { | |||
var limit = 250; | |||
if (text.length <= limit) return '<span>' + text + '</span>'; | |||
return '<span>' + text.substring(0, limit) + '</span><span class="mip-dots">... </span><span class="mip-more" style="display:none;">' + text.substring(limit) + '</span><span class="mip-read-btn" style="color: #6a5acd; cursor: pointer; font-weight: bold; margin-left: 5px;">Baca selengkapnya</span>'; | |||
} | |||
function buildSmartLinks(currentId) { | |||
$portalLinks.empty(); | |||
var validLinks = []; | |||
var checkRequests = projects.filter(p => p.id !== currentId).map(function(p) { | |||
return $.ajax({ url: p.url, data: { action: 'query', titles: pageTitle, format: 'json', origin: '*' }, dataType: 'json' }) | |||
.then(function(data) { if (data.query.pages["-1"] === undefined) { validLinks.push('<a href="' + p.base + encodeURIComponent(pageTitle) + '" target="_blank" style="color: #6a5acd; text-decoration: underline; font-weight: bold;">' + p.label + '</a>'); } }); | |||
}); | |||
$.when.apply($, checkRequests).done(function() { | |||
if (validLinks.length > 0) { $portalLinks.html(validLinks.join(' <span style="color:#ccc; margin: 0 5px;">•</span> ')); $projectPortal.fadeIn(); } | |||
}); | |||
} | |||
var cached = JSON.parse(localStorage.getItem(cacheKey) || "{}"); | |||
function init() { | |||
var pCheck = projects[0]; // Prioritas bahasa Indonesia | |||
$.ajax({ | |||
url: pCheck.url, | |||
data: { action: 'query', prop: 'revisions|pageimages', rvprop: 'timestamp', piprop: 'thumbnail', pithumbsize: 150, titles: pageTitle, format: 'json', origin: '*' }, | |||
success: function(res) { | |||
var pg = res.query.pages, id = Object.keys(pg)[0]; | |||
var latestTS = (id != "-1") ? pg[id].revisions[0].timestamp : "0"; | |||
var thumbUrl = (id != "-1" && pg[id].thumbnail) ? pg[id].thumbnail.source : ""; | |||
if (cached.content && cached.ts === latestTS) { | |||
renderAll(cached.p, cached.content, cached.isTrans, cached.orig, cached.img); | |||
} else { fetchFreshData(latestTS, thumbUrl); } | |||
} | |||
}); | |||
} | |||
function fetchFreshData(newTS, img) { | |||
$.ajax({ | |||
url: projects[0].url, | |||
data: { action: 'query', prop: 'extracts', exintro: 1, explaintext: 1, titles: pageTitle, format: 'json', origin: '*' }, | |||
success: function(data) { | |||
var pages = data.query.pages, id = Object.keys(pages)[0]; | |||
if (id != "-1") { | |||
var extract = cleanExtract(pages[id].extract); | |||
localStorage.setItem(cacheKey, JSON.stringify({p:projects[0], content:extract, isTrans:false, orig:"", ts:newTS, img:img})); | |||
renderAll(projects[0], extract, false, "", img); | |||
} | |||
} | |||
}); | |||
} | |||
function renderAll(p, currentText, isTranslated, originalText, img) { | |||
// Gabungkan semua sistem otomatis | |||
var processedText = applyQualityGuard(applyAutoBold(applyDynamicContext(currentText))); | |||
var thumbHtml = img ? '<div style="float: right; margin-left: 15px; margin-bottom: 10px; border: 1px solid #ddd; padding: 3px; background: #fff; border-radius: 4px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);"><img src="'+img+'" style="max-width: 100px; display: block; height: auto;"></div>' : ''; | |||
$descSection.show(); | |||
$descBox.show().html(thumbHtml + applySummary(processedText) + '<div style="clear:both;"></div>'); | |||
$sourceInfo.show().html('<div style="font-size: 0.9em; color: #777;">Sumber Dari : <a href="' + p.base + encodeURIComponent(pageTitle) + '" target="_blank" style="color: #6a5acd; font-weight: bold; text-decoration: none;">' + p.name + '.</a></div>'); | |||
buildSmartLinks(p.id); | |||
$(document).off('click', '.mip-read-btn').on('click', '.mip-read-btn', function() { $(this).hide(); $('.mip-dots').hide(); $('.mip-more').fadeIn(); }); | |||
} | |||
init(); | |||
}); | |||
})(); | })(); | ||