No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
/** | /** | ||
* reFill.js — | * reFill.js — batch-fill bare URL refs via WMF Citoid | ||
* | * Works with BharatpediaNeo + WikiEditor + CodeMirror. | ||
* Source editor only. Preview and check every citation before saving. | |||
*/ | */ | ||
(function () { | (function () { | ||
'use strict'; | 'use strict'; | ||
var CITOID = 'https://en.wikipedia.org/api/rest_v1/data/citation/mediawiki/'; | |||
var TYPE_MAP = { | |||
artwork: 'Citation', | |||
audioRecording: 'Citation', | |||
bill: 'Citation', | |||
blogPost: 'Cite web', | |||
book: 'Cite book', | |||
bookSection: 'Citation', | |||
case: 'Citation', | |||
computerProgram: 'Citation', | |||
conferencePaper: 'Cite journal', | |||
dataset: 'Citation', | |||
dictionaryEntry: 'Citation', | |||
document: 'Citation', | |||
email: 'Cite web', | |||
encyclopediaArticle: 'Citation', | |||
film: 'Citation', | |||
forumPost: 'Cite web', | |||
hearing: 'Citation', | |||
instantMessage: 'Citation', | |||
interview: 'Citation', | |||
journalArticle: 'Cite journal', | |||
letter: 'Citation', | |||
magazineArticle: 'Cite news', | |||
manuscript: 'Cite book', | |||
map: 'Citation', | |||
newspaperArticle: 'Cite news', | |||
patent: 'Cite patent', | |||
podcast: 'Citation', | |||
preprint: 'Citation', | |||
presentation: 'Cite journal', | |||
radioBroadcast: 'Citation', | |||
report: 'Cite report', | |||
standard: 'Citation', | |||
statute: 'Citation', | |||
thesis: 'Cite thesis', | |||
tvBroadcast: 'Citation', | |||
videoRecording: 'Cite AV media', | |||
webpage: 'Cite web', | |||
_default: 'Cite web' | |||
}; | |||
function ready(fn) { | function ready(fn) { | ||
| Line 14: | Line 58: | ||
} | } | ||
function | function $box() { | ||
return | return $('#wpTextbox1'); | ||
} | } | ||
var | function getWikitext() { | ||
var $t = $box(); | |||
if ($t.length && $.fn.textSelection) { | |||
return $t.textSelection('getContents'); | |||
} | |||
if ($t.length) { | |||
return $t.val(); | |||
} | |||
var el = document.querySelector('#editform textarea, #mw-content-text textarea, textarea'); | |||
return el ? el.value : ''; | |||
} | |||
} | |||
function setWikitext(text) { | |||
var $t = $box(); | |||
if ($t.length && $.fn.textSelection) { | |||
$t.textSelection('setContents', text); | |||
return; | |||
} | |||
if ($t.length) { | |||
$t.val(text); | |||
$t.trigger('change').trigger('input'); | |||
return; | |||
} | |||
var el = document.querySelector('#editform textarea, #mw-content-text textarea, textarea'); | |||
if (el) { | |||
el.value = text; | |||
el.dispatchEvent(new Event('input', { bubbles: true })); | |||
} | |||
} | |||
function esc(s) { | function esc(s) { | ||
| Line 40: | Line 96: | ||
} | } | ||
function templateFromCitoid(item, | function templateFromCitoid(item, fallbackUrl) { | ||
var name = TYPE_MAP[item && item.itemType] || TYPE_MAP._default; | var name = TYPE_MAP[item && item.itemType] || TYPE_MAP._default; | ||
var p = [ | var p = []; | ||
var url = (item && item.url) || fallbackUrl; | |||
if (url) { | |||
p.push('url=' + esc(url)); | |||
} | |||
if (item && item.title) { | if (item && item.title) { | ||
p.push('title=' + esc(item.title)); | p.push('title=' + esc(item.title)); | ||
| Line 48: | Line 108: | ||
var site = item && (item.websiteTitle || item.publicationTitle || item.publisher); | var site = item && (item.websiteTitle || item.publicationTitle || item.publisher); | ||
if (site) { | if (site) { | ||
if (name === 'Cite news' || name === 'Cite journal') { | |||
p.push('work=' + esc(site)); | |||
} else if (name === 'Cite book') { | |||
p.push('publisher=' + esc(site)); | |||
} else { | |||
p.push('website=' + esc(site)); | |||
} | |||
} | } | ||
if (item && item.date) { | if (item && item.date) { | ||
p.push('date=' + esc(item.date)); | p.push('date=' + esc(item.date)); | ||
} | |||
if (item && item.author && item.author[0]) { | |||
var a = item.author[0]; | |||
if (a[1]) { | |||
p.push('last=' + esc(a[1])); | |||
} | |||
if (a[0]) { | |||
p.push('first=' + esc(a[0])); | |||
} | |||
} | } | ||
p.push('access-date=' + new Date().toISOString().slice(0, 10)); | p.push('access-date=' + new Date().toISOString().slice(0, 10)); | ||
| Line 57: | Line 132: | ||
} | } | ||
function findBare( | function findBare(wikitext) { | ||
var re = /<ref(\s[^>]*)?>\s*(https?:\/\/[^\s<>[\]|]+)\s*<\/ref>/gi; | var re = /<ref(\s[^>]*)?>\s*(https?:\/\/[^\s<>[\]|]+)\s*<\/ref>/gi; | ||
var out = [] | var out = []; | ||
while ((m = re.exec( | var m; | ||
out.push({ full: m[0], attrs: m[1] || '', url: m[2] }); | while ((m = re.exec(wikitext)) !== null) { | ||
out.push({ | |||
full: m[0], | |||
attrs: m[1] || '', | |||
url: m[2] | |||
}); | |||
} | } | ||
return out; | return out; | ||
| Line 67: | Line 147: | ||
function fetchOne(url) { | function fetchOne(url) { | ||
return fetch(CITOID + encodeURIComponent(url), { headers: { Accept: 'application/json' } }) | return fetch(CITOID + encodeURIComponent(url), { | ||
headers: { Accept: 'application/json' } | |||
}).then(function (r) { | |||
if (!r.ok) { | |||
throw new Error('HTTP ' + r.status); | |||
} | |||
return r.json(); | |||
}).then(function (data) { | |||
return Array.isArray(data) ? data[0] : data; | |||
}); | |||
} | } | ||
function run() { | function run() { | ||
var | var text = getWikitext(); | ||
if (! | if (!text) { | ||
window.alert('reFill: no editor | window.alert('reFill: no editor text. Use Edit source (?action=edit).'); | ||
return; | return; | ||
} | } | ||
var bare = findBare(text); | var bare = findBare(text); | ||
if (!bare.length) { | if (!bare.length) { | ||
| Line 91: | Line 171: | ||
return; | return; | ||
} | } | ||
var btn = document.getElementById('refill-bare-btn'); | var btn = document.getElementById('refill-bare-btn'); | ||
if (btn) { | if (btn) { | ||
| Line 96: | Line 177: | ||
btn.textContent = 'Filling ' + bare.length + '…'; | btn.textContent = 'Filling ' + bare.length + '…'; | ||
} | } | ||
var chain = Promise.resolve(); | var chain = Promise.resolve(); | ||
var ok = 0; | var ok = 0; | ||
var fail = 0; | |||
bare.forEach(function (item) { | bare.forEach(function (item) { | ||
chain = chain.then(function () { | chain = chain.then(function () { | ||
return fetchOne(item.url).then(function (meta) { | return fetchOne(item.url).then(function (meta) { | ||
var next = '<ref' + item.attrs + '>' + templateFromCitoid(meta || {}, item.url) + '</ref>'; | |||
text = text.replace(item.full, next); | |||
ok++; | ok++; | ||
}).catch(function () { | }).catch(function (err) { | ||
return new Promise(function ( | fail++; | ||
if (window.console) { | |||
console.warn('reFill failed', item.url, err); | |||
} | |||
}).then(function () { | |||
return new Promise(function (resolve) { | |||
setTimeout(resolve, 350); | |||
}); | |||
}); | }); | ||
}); | }); | ||
}); | }); | ||
chain.then(function () { | chain.then(function () { | ||
setWikitext(text); | |||
if (btn) { | if (btn) { | ||
btn.disabled = false; | btn.disabled = false; | ||
btn.textContent = 'Fill bare URLs'; | btn.textContent = 'Fill bare URLs'; | ||
} | } | ||
window.alert('reFill finished: ' + ok + ' | window.alert('reFill finished: ' + ok + ' filled, ' + fail + ' failed. Check the text box, then Preview.'); | ||
}); | }); | ||
} | } | ||
| Line 150: | Line 242: | ||
ready(function () { | ready(function () { | ||
addButton(); | var start = function () { | ||
addButton(); | |||
setTimeout(addButton, 500); | |||
setTimeout(addButton, 2000); | |||
setTimeout(addButton, 5000); | |||
}; | |||
if (mw.loader && mw.loader.using) { | |||
mw.loader.using(['jquery', 'jquery.textSelection']).then(start).catch(start); | |||
} else { | |||
start(); | |||
} | |||
}); | }); | ||
})(); | })(); | ||
Revision as of 10:53, 25 September 2026
/**
* reFill.js — batch-fill bare URL refs via WMF Citoid
* Works with BharatpediaNeo + WikiEditor + CodeMirror.
* Source editor only. Preview and check every citation before saving.
*/
(function () {
'use strict';
var CITOID = 'https://en.wikipedia.org/api/rest_v1/data/citation/mediawiki/';
var TYPE_MAP = {
artwork: 'Citation',
audioRecording: 'Citation',
bill: 'Citation',
blogPost: 'Cite web',
book: 'Cite book',
bookSection: 'Citation',
case: 'Citation',
computerProgram: 'Citation',
conferencePaper: 'Cite journal',
dataset: 'Citation',
dictionaryEntry: 'Citation',
document: 'Citation',
email: 'Cite web',
encyclopediaArticle: 'Citation',
film: 'Citation',
forumPost: 'Cite web',
hearing: 'Citation',
instantMessage: 'Citation',
interview: 'Citation',
journalArticle: 'Cite journal',
letter: 'Citation',
magazineArticle: 'Cite news',
manuscript: 'Cite book',
map: 'Citation',
newspaperArticle: 'Cite news',
patent: 'Cite patent',
podcast: 'Citation',
preprint: 'Citation',
presentation: 'Cite journal',
radioBroadcast: 'Citation',
report: 'Cite report',
standard: 'Citation',
statute: 'Citation',
thesis: 'Cite thesis',
tvBroadcast: 'Citation',
videoRecording: 'Cite AV media',
webpage: 'Cite web',
_default: 'Cite web'
};
function ready(fn) {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', fn);
} else {
fn();
}
}
function $box() {
return $('#wpTextbox1');
}
function getWikitext() {
var $t = $box();
if ($t.length && $.fn.textSelection) {
return $t.textSelection('getContents');
}
if ($t.length) {
return $t.val();
}
var el = document.querySelector('#editform textarea, #mw-content-text textarea, textarea');
return el ? el.value : '';
}
function setWikitext(text) {
var $t = $box();
if ($t.length && $.fn.textSelection) {
$t.textSelection('setContents', text);
return;
}
if ($t.length) {
$t.val(text);
$t.trigger('change').trigger('input');
return;
}
var el = document.querySelector('#editform textarea, #mw-content-text textarea, textarea');
if (el) {
el.value = text;
el.dispatchEvent(new Event('input', { bubbles: true }));
}
}
function esc(s) {
return String(s || '').replace(/\|/g, '{{!}}').replace(/\n/g, ' ').trim();
}
function templateFromCitoid(item, fallbackUrl) {
var name = TYPE_MAP[item && item.itemType] || TYPE_MAP._default;
var p = [];
var url = (item && item.url) || fallbackUrl;
if (url) {
p.push('url=' + esc(url));
}
if (item && item.title) {
p.push('title=' + esc(item.title));
}
var site = item && (item.websiteTitle || item.publicationTitle || item.publisher);
if (site) {
if (name === 'Cite news' || name === 'Cite journal') {
p.push('work=' + esc(site));
} else if (name === 'Cite book') {
p.push('publisher=' + esc(site));
} else {
p.push('website=' + esc(site));
}
}
if (item && item.date) {
p.push('date=' + esc(item.date));
}
if (item && item.author && item.author[0]) {
var a = item.author[0];
if (a[1]) {
p.push('last=' + esc(a[1]));
}
if (a[0]) {
p.push('first=' + esc(a[0]));
}
}
p.push('access-date=' + new Date().toISOString().slice(0, 10));
return '{{' + name + '|' + p.join('|') + '}}';
}
function findBare(wikitext) {
var re = /<ref(\s[^>]*)?>\s*(https?:\/\/[^\s<>[\]|]+)\s*<\/ref>/gi;
var out = [];
var m;
while ((m = re.exec(wikitext)) !== null) {
out.push({
full: m[0],
attrs: m[1] || '',
url: m[2]
});
}
return out;
}
function fetchOne(url) {
return fetch(CITOID + encodeURIComponent(url), {
headers: { Accept: 'application/json' }
}).then(function (r) {
if (!r.ok) {
throw new Error('HTTP ' + r.status);
}
return r.json();
}).then(function (data) {
return Array.isArray(data) ? data[0] : data;
});
}
function run() {
var text = getWikitext();
if (!text) {
window.alert('reFill: no editor text. Use Edit source (?action=edit).');
return;
}
var bare = findBare(text);
if (!bare.length) {
window.alert('reFill: no <ref>https://…</ref> found.');
return;
}
var btn = document.getElementById('refill-bare-btn');
if (btn) {
btn.disabled = true;
btn.textContent = 'Filling ' + bare.length + '…';
}
var chain = Promise.resolve();
var ok = 0;
var fail = 0;
bare.forEach(function (item) {
chain = chain.then(function () {
return fetchOne(item.url).then(function (meta) {
var next = '<ref' + item.attrs + '>' + templateFromCitoid(meta || {}, item.url) + '</ref>';
text = text.replace(item.full, next);
ok++;
}).catch(function (err) {
fail++;
if (window.console) {
console.warn('reFill failed', item.url, err);
}
}).then(function () {
return new Promise(function (resolve) {
setTimeout(resolve, 350);
});
});
});
});
chain.then(function () {
setWikitext(text);
if (btn) {
btn.disabled = false;
btn.textContent = 'Fill bare URLs';
}
window.alert('reFill finished: ' + ok + ' filled, ' + fail + ' failed. Check the text box, then Preview.');
});
}
function addButton() {
if (document.getElementById('refill-bare-btn')) {
return;
}
var btn = document.createElement('button');
btn.id = 'refill-bare-btn';
btn.type = 'button';
btn.textContent = 'Fill bare URLs';
btn.style.cssText = [
'position:fixed',
'right:16px',
'bottom:16px',
'z-index:2147483647',
'padding:10px 14px',
'font:600 14px/1.2 sans-serif',
'color:#fff',
'background:#b32424',
'border:0',
'border-radius:6px',
'cursor:pointer',
'box-shadow:0 2px 8px rgba(0,0,0,.35)'
].join(';');
btn.addEventListener('click', function (e) {
e.preventDefault();
e.stopPropagation();
run();
});
document.body.appendChild(btn);
}
ready(function () {
var start = function () {
addButton();
setTimeout(addButton, 500);
setTimeout(addButton, 2000);
setTimeout(addButton, 5000);
};
if (mw.loader && mw.loader.using) {
mw.loader.using(['jquery', 'jquery.textSelection']).then(start).catch(start);
} else {
start();
}
});
})();