MediaWiki:Common.js: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
Line 572:
fetchAndRender();
}, function (e) { mw.log('Datacheck: mediawiki.api failed to load', e); });
});
 
 
/* ── Datacheck term_map review gadget ────────────────────────────────────────
On a Datacheck: page that renders Module:TermMap in review mode, turn each row's
.tm-validate / .tm-redo controls into Validate / Redo buttons. Each click prompts
for a comment (REQUIRED) and writes review_status + comment + reviewed_by/at into the
matching Data:term_map.json entry — keyed by the row's hidden .tm-key (the JSON key),
under the REVIEWER'S OWN session (no bot creds). Reviews survive a redeploy via
deploy.py term_map_data merge-forward. Anonymous users see the badge + comment only. */
$(function () {
if (!(window.mw && mw.config) || mw.config.get('wgNamespaceNumber') !== 3006) { return; }
var vCells = document.querySelectorAll('.tm-validate');
if (!vCells.length) { return; } // not a term_map review page
if (!mw.config.get('wgUserName')) { mw.log('TermMap review: log in to enable controls.'); return; }
 
var marker = document.querySelector('.tm-data-title');
var dataTitle = marker ? (marker.textContent || '').trim() : 'Data:term_map.json';
 
function notify(msg) { if (mw.notify) { mw.notify(msg); } else { mw.log(msg); } }
function nowIso() { return new Date().toISOString().slice(0, 19) + 'Z'; }
 
mw.loader.using(['mediawiki.api']).then(function () {
var api = new mw.Api(), data = null, baseRevId = null, rows = [];
 
function collect() { // one entry per review row, mapped by .tm-key
rows = [];
Array.prototype.forEach.call(vCells, function (vCell) {
var tr = vCell.closest ? vCell.closest('tr') : null;
if (!tr) { return; }
var keyEl = tr.querySelector('.tm-key');
rows.push({
key: keyEl ? (keyEl.textContent || '').trim() : '',
vCell: vCell, rCell: tr.querySelector('.tm-redo'),
badge: tr.querySelector('.rt-status'), comment: tr.querySelector('.tm-comment')
});
});
}
 
function fetchAndRender() {
api.get({ action: 'query', prop: 'revisions', rvprop: 'content|ids',
rvslots: 'main', titles: dataTitle, formatversion: 2 }).done(function (res) {
var page = res.query && res.query.pages && res.query.pages[0];
var rev = page && page.revisions && page.revisions[0];
if (!rev) { notify('TermMap: data page not found — ' + dataTitle); return; }
baseRevId = rev.revid;
var content = rev.slots ? rev.slots.main.content : rev.content;
try { data = JSON.parse(content); }
catch (e) { notify('TermMap: bad JSON in ' + dataTitle); return; }
render();
}).fail(function (code) { notify('TermMap: load failed — ' + code); });
}
 
function save(okMsg) {
api.postWithEditToken({
action: 'edit', title: dataTitle, contentmodel: 'json',
text: JSON.stringify(data, null, 4),
summary: 'TermMap review: human verdict via review gadget', baserevid: baseRevId
}).done(function (res) {
if (res.edit && res.edit.newrevid) { baseRevId = res.edit.newrevid; }
render();
notify(okMsg || 'TermMap: saved.');
}).fail(function (code) {
if (code === 'editconflict') { notify('TermMap: edit conflict — reloading.'); fetchAndRender(); }
else { notify('TermMap: save failed — ' + code); }
});
}
 
function setReview(rec, status, comment) {
rec.review_status = status;
rec.comment = comment;
rec.reviewed_by = mw.config.get('wgUserName');
rec.reviewed_at = nowIso();
}
 
function act(rec, status) {
var verb = (status === 'validated') ? 'Validate' : 'Redo';
var entered = window.prompt(verb + ' — why? (stored as the review comment)', rec.comment || '');
if (entered === null) { return; } // cancelled
entered = entered.replace(/^\s+|\s+$/g, '');
if (!entered) { notify('TermMap: a comment is required.'); return; }
setReview(rec, status, entered);
save('TermMap: ' + verb.toLowerCase() + 'd.');
}
 
function btn(label, title, active, variant, handler) {
var b = document.createElement('button');
b.type = 'button'; b.textContent = label; b.title = title;
b.className = 'rt-btn rt-btn--' + variant + (active ? ' is-active' : '');
b.onclick = function (e) { e.preventDefault(); handler(); };
return b;
}
function fill(cell, node) { if (cell) { cell.innerHTML = ''; cell.appendChild(node); } }
 
function render() {
if (!data) { return; }
rows.forEach(function (r) {
var rec = data[r.key];
if (!rec) { return; }
var status = rec.review_status || 'unreviewed';
fill(r.vCell, btn('✓', status === 'validated' ? 'Validated — click to edit the comment' : 'Validate (with a reason)',
status === 'validated', 'confirm', function () { act(rec, 'validated'); }));
fill(r.rCell, btn('↻', status === 'redo' ? 'Marked redo — click to edit the comment' : 'Redo (with a reason)',
status === 'redo', 'override', function () { act(rec, 'redo'); }));
if (r.badge) {
r.badge.className = 'rt-status rt-status-' + status;
r.badge.textContent = (status === 'validated') ? '✓ validated' : (status === 'redo') ? '↻ redo' : 'unreviewed';
}
if (r.comment) { r.comment.textContent = rec.comment || ''; }
});
}
 
collect();
fetchAndRender();
}, function (e) { mw.log('TermMap review: mediawiki.api failed to load', e); });
});