Widget: DataExporter: Difference between revisions
From LINKS Community Center
Eschmidt (talk | contribs) No edit summary |
Eschmidt (talk | contribs) No edit summary |
||
Line 22: | Line 22: | ||
// Check user. | // Check user. | ||
const usrData = await fetch('/api.php?action=query&meta=userinfo&format=json').then(rsp => rsp.json()); | const usrData = await fetch( | ||
'/api.php?action=query&meta=userinfo&format=json' | |||
).then(rsp => rsp.json()); | |||
if (!permittedUsers.includes(usrData?.query?.userinfo?.name)) { | if (!permittedUsers.includes(usrData?.query?.userinfo?.name)) { | ||
output.innerHTML = '<span style="color:red; font-size:large">Access Denied</span>'; | output.innerHTML = '<span style="color:red; font-size:large">Access Denied</span>'; | ||
Line 31: | Line 33: | ||
// Fetch schema and parse it into an export query. | // Fetch schema and parse it into an export query. | ||
const | const schemaXml = await fetch( | ||
'/index.php?&action=raw&title=Category:' + formData.get('library') | |||
).then(rsp => rsp.text()); | |||
const schema = (new DOMParser).parseFromString(schemaXml, 'text/xml'); | const schema = (new DOMParser).parseFromString(schemaXml, 'text/xml'); | ||
let exportUrl = '[[Category:' + formData.get('library').replaceAll('_', ' ') + ']]'; | let exportUrl = encodeURIComponent( | ||
'[[Category:' + formData.get('library').replaceAll('_', ' ') + ']]' | |||
).replaceAll('%', '-'); | |||
for (const field of schema.querySelectorAll('Field')) { | for (const field of schema.querySelectorAll('Field')) { | ||
Line 43: | Line 46: | ||
|| field.getAttribute('name'); | || field.getAttribute('name'); | ||
const label = field.querySelector('Label')?.textContent; | const label = field.querySelector('Label')?.textContent; | ||
exportUrl += ' | exportUrl += encodeURIComponent('/?' + prop).replaceAll('%', '-'); | ||
if (!!label) exportUrl += ' | if (!!label) exportUrl += '%3D' + encodeURIComponent(label).replaceAll('%', '-'); | ||
} | } | ||
exportUrl += '/format%3Dspreadsheet/fileformat%3D' + formData.get('fileformat'); | |||
output.innerHTML = exportUrl; | output.innerHTML = exportUrl; | ||
const exportParams = new URLSearchParams(); | // const exportParams = new URLSearchParams(); | ||
exportParams.set('action', 'ask'); | // exportParams.set('action', 'ask'); | ||
exportParams.set('format', 'spreadsheet'); | // exportParams.set('format', 'spreadsheet'); | ||
exportParams.set('fileformat', formData.get('fileformat')); | // exportParams.set('fileformat', formData.get('fileformat')); | ||
exportParams.set('query', encodeURIComponent(exportUrl)); | // exportParams.set('query', encodeURIComponent(exportUrl)); | ||
// fetch('/api.php?' + exportParams.toString()); | // fetch('/api.php?' + exportParams.toString()); | ||
const dl = document.createElement('a'); | const dl = document.createElement('a'); | ||
dl.href = '/ | dl.href = '/index.php/Special:Ask/' + exportUrl; | ||
dl.download = 'export.' + formData.get('fileformat'); | dl.download = 'export.' + formData.get('fileformat'); | ||
document.body.appendChild(dl); | document.body.appendChild(dl); |