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
 
(28 intermediate revisions by the same user not shown)
Line 4: Line 4:


     <head>
     <head>
        <style>
            #form_wrapper {
                display: flex;
                flex-flow: row wrap;
                justify-content: flex-start;
                align-items: flex-start;
            }
        </style>
         <script>
         <script>
             'use strict';
             'use strict';


             const base = 'https://links.communitycenter.eu';
             const permittedUsers = ['Eschmidt'];
            const url = '/index.php?title=Category:Disaster_Community_Technology&action=raw';
 
            async function exportData() {
                const output = document.getElementById('output');
 
                // Check user.
                const usrData = await fetch(
                    '/api.php?action=query&meta=userinfo&format=json'
                ).then(rsp => rsp.json());
                if (!permittedUsers.includes(usrData?.query?.userinfo?.name)) {
                    output.innerHTML = '<span style="color:red; font-size:large">Access Denied</span>';
                    return;
                }
 
                const formData = new FormData(document.getElementById('export_form'));
 
                // Fetch schema and parse it into an export query.
                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');
 
                let exportUrl = encodeURIComponent(
                    '[[Category:' + formData.get('library').replaceAll('_', ' ') + ']]'
                ).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 += '/' + encodeURIComponent('?' + prop).replaceAll('%', '-');
                    if (!!label) exportUrl += '%3D' + encodeURIComponent(label).replaceAll('%', '-');
                }
 
                exportUrl += '/format%3Dspreadsheet/fileformat%3D' + formData.get('fileformat');
                output.innerHTML = exportUrl;
 
                // const exportParams = new URLSearchParams();
                // exportParams.set('action', 'ask');
                // exportParams.set('format', 'spreadsheet');
                // exportParams.set('fileformat', formData.get('fileformat'));
                // exportParams.set('query', encodeURIComponent(exportUrl));


            const usr = '/api.php?action=query&meta=userinfo&format=json';
                // fetch('/api.php?' + exportParams.toString());


            async function exportData() {
                // const dl = document.createElement('a');
                 const res = await fetch(usr);
                // dl.href = 'https://links.communitycenter.eu/index.php/Special:Ask/' + exportUrl;
                 console.log(res)
                 // dl.download = 'export.' + formData.get('fileformat');
                 document.getElementById('output').innerText = JSON.stringify(res);
                // document.body.appendChild(dl);
                 // dl.click();
                 // document.body.removeChild(dl);
 
                fetch('https://links.communitycenter.eu/index.php/Special:Ask/' + exportUrl)
                    .then(res => res.blob())
                    .then(blob => {
                        const file = window.URL.createObjectURL(blob);
                        window.location.assign(file);
                    })
 
                // exportUrl = '/api.php?action=ask&format=spreadsheet&query=[[Category:Disaster Community Technology]]';
                // exportUrl = '/api.php?action=ask&format=spreadsheet&query=[[Category:Use Cases]]';
                // exportUrl = '/api.php?action=ask&format=spreadsheet&query=[[Category:Guideline]]';
 
                // exportUrl += ''; // &fileformat=csv  &fileformat=ods  &fileformat=xlsx
                // exportUrl += ''; // &filename=MyFilename <- LIB+DATE
             }
             }
         </script>
         </script>
Line 22: Line 87:
     <body>
     <body>
         <button type="button" onclick="exportData()">Export Data</button>
         <button type="button" onclick="exportData()">Export Data</button>
         <pre id="output">
         <form id="export_form">
            <div id="form_wrapper">
                <fieldset>
                    <legend>Library</legend>
 
                    <input type="radio" id="lib_TL" name="library" value="Disaster_Community_Technology" checked>
                    <label for="lib_TL">Technologies</label><br>
 
                    <input type="radio" id="lib_UCL" name="library" value="Use_Cases">
                    <label for="lib_UCL">Use Cases</label><br>
 
                    <input type="radio" id="lib_GL" name="library" value="Guideline">
                    <label for="lib_GL">Guidelines</label>
                </fieldset>
                <fieldset>
                    <legend>Format</legend>
 
                    <input type="radio" id="file_XLSX" name="fileformat" value="xlsx" checked>
                    <label for="file_XLSX">Excel (.xlsx)</label><br>
 
                    <input type="radio" id="file_ODS" name="fileformat" value="ods">
                    <label for="file_ODS">OpenOffice (.ods)</label><br>


         </pre>
                    <input type="radio" id="file_CSV" name="fileformat" value="csv">
                    <label for="file_CSV">CSV (.csv)</label>
                </fieldset>
            </div>
        </form>
         <div id="output"></div>
     </body>
     </body>


     </html>
     </html>
</includeonly>
</includeonly>

Latest revision as of 13:34, 17 August 2023