Widget: DataExporter: Difference between revisions

From LINKS Community Center
Jump to: navigation, search
Eschmidt (talk | contribs)
No edit summary
Eschmidt (talk | contribs)
No edit summary
Line 30: Line 30:
                 const formData = new FormData(document.getElementById('export_form'));
                 const formData = new FormData(document.getElementById('export_form'));


                // Fetch schema and parse it into an export query.
                 const schemaParams = new URLSearchParams();
                 const schemaParams = new URLSearchParams();
                 schemaParams.set('action', 'raw');
                 schemaParams.set('action', 'raw');
                 schemaParams.set('title', 'Category:' + formData.get('library'));
                 schemaParams.set('title', 'Category:' + formData.get('library'));
                const schemaXml = await fetch('/index.php?' + schemaParams.toString()).then(rsp => rsp.text());
                const schema = (new DOMParser).parseFromString(schemaXml, 'text/xml');
                let exportUrl = '[[Category:' + formData.get('library').replaceAll('_', ' ') + ']]';
                for (const field of schema.querySelectorAll('Field')) {
                    const prop = field.querySelector('semanticmediawiki_Property')?.getAttribute('name')
                        || field.getAttribute('name');
                    const label = field.querySelector('Label')?.textContent;
                    exportUrl += '|?' + prop;
                    if (!!label) exportUrl += '=' + label;
                    exportUrl += '<br>';
                    // output.innerHTML += prop + ' | ' + label + '<br>';
                }
                output.innerHTML = exportUrl;


                 const exportParams = new URLSearchParams();
                 const exportParams = new URLSearchParams();
Line 42: Line 58:
                 // exportUrl = '/api.php?action=ask&format=spreadsheet&query=[[Category:Use Cases]]';
                 // exportUrl = '/api.php?action=ask&format=spreadsheet&query=[[Category:Use Cases]]';
                 // exportUrl = '/api.php?action=ask&format=spreadsheet&query=[[Category:Guideline]]';
                 // exportUrl = '/api.php?action=ask&format=spreadsheet&query=[[Category:Guideline]]';
                const schemaXml = await fetch('/index.php?' + schemaParams.toString()).then(rsp => rsp.text());
                const schema = (new DOMParser).parseFromString(schemaXml, 'text/xml');
                for (const field of schema.querySelectorAll('Field')) {
                    const prop = field.querySelector('semanticmediawiki_Property')?.getAttribute('name')
                        || field.getAttribute('name');
                    const label = field.querySelector('Label')?.textContent;
                    output.innerHTML += prop + ' | ' + label + '<br>';
                }


                 // exportUrl += ''; // &fileformat=csv  &fileformat=ods  &fileformat=xlsx
                 // exportUrl += ''; // &fileformat=csv  &fileformat=ods  &fileformat=xlsx

Revision as of 09:33, 17 August 2023