MediaWiki:Common.js: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
Line 450:
$(function () {
if (!(window.mw && mw.config) || mw.config.get('wgNamespaceNumber') !== 3006) { return; }
var marker = document.querySelector('.rt-data-title');
var spans = document.querySelectorAll('.rt-confirm');
if (!marker || !spans.length || !mw.config.get('wgUserName')) { return; }
if (!mw.config.get('wgUserName')) { mw.log('Datacheck: log in to enable confirm controls.'); return; }
var dataTitle = (marker.textContent || '').trim();
 
if (!dataTitle) { return; }
// Data page title from the hidden marker, else derived from the page name (Datacheck:X -> Data:X.json).
var marker = document.querySelector('.rt-data-title');
var dataTitle = marker ? (marker.textContent || '').trim() : '';
if (!dataTitle) { return; }
var pn = mw.config.get('wgPageName') || '', rest = pn.replace(/^Datacheck:/, '');
if (rest && rest !== pn) { dataTitle = 'Data:' + rest + '.json'; }
});
if (!dataTitle) { mw.log('Datacheck: could not resolve the Data page title.'); return; }
 
function notify(msg) { if (mw.notify) { mw.notify(msg); } else { mw.log(msg); } }
 
mw.loader.using(['mediawiki.api', 'mediawiki.notify']).donethen(function () { // mw.notify is global; only the API needs loading
var api = new mw.Api(), data = null, baseRevId = null;
 
Line 464 ⟶ 473:
var page = res.query && res.query.pages && res.query.pages[0];
var rev = page && page.revisions && page.revisions[0];
if (!rev) { mw.notify('Datacheck: 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) { mw.notify('Datacheck: bad JSON in ' + dataTitle); return; }
render();
}).fail(function (code) { mw.notify('Datacheck: load failed — ' + code); });
}
 
Line 481 ⟶ 490:
if (res.edit && res.edit.newrevid) { baseRevId = res.edit.newrevid; }
render();
mw.notify(okMsg || 'Datacheck: saved.');
}).fail(function (code) {
if (code === 'editconflict') { mw.notify('Datacheck: edit conflict — reloading.'); fetchAndRender(); }
else { mw.notify('Datacheck: save failed — ' + code); }
});
}
Line 509 ⟶ 518:
if (entered === null) { return; }
var num = parseFloat(String(entered).replace(/[,\s]/g, ''));
if (isNaN(num)) { mw.notify('Datacheck: not a number — ' + entered); return; }
var reason = window.prompt('Reason (optional):', rec.override_reason || 'manual correction') || 'manual correction';
setStatus(rec, 'override', { value: num, corrected_rev: data.source_revision || null, override_reason: reason });
Line 518 ⟶ 527:
if (!data || !data.records) { return; }
if (spans.length !== data.records.length) {
mw.notify('Datacheck: table out of sync with the data page — reload the page.'); return;
}
Array.prototype.forEach.call(spans, function (span, i) {
Line 544 ⟶ 553:
 
fetchAndRender();
}, function (e) { mw.log('Datacheck: mediawiki.api failed to load', e); });
});
});