User:Sophivorus/TissueDB.js
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/**
* This script gets the data from the public TissueDB spreadsheet
* https://docs.google.com/spreadsheets/d/1uoFUGo4JrycAOOHapLzuwNIZ_oNYMDFCktBcIYBUHpU
* and turns them into wiki pages at [[TissueDB]]
*/
var TissueDB = {
async init() {
const prefix = 'SandboxDB/';
TissueDB.makeTissues( prefix );
TissueDB.makeMaterials( prefix );
TissueDB.makeSimulators( prefix );
},
async makeTissues( prefix ) {
const tissues = await TissueDB.get( 'Tissues' );
for ( const tissue of tissues ) {
if ( !tissue[ 'Lead text' ] ) {
continue;
}
// Add tissue data
let text = '{{SandboxDB Tissue Layout';
const tissueData = {
'lead_paragraph': tissue[ 'Lead text' ],
'hero_image': tissue[ 'Hero image' ],
'hero_image_caption': tissue[ 'Hero image caption' ],
'hero_image_alt': tissue[ 'Hero image alt' ],
'hero_image_license': tissue[ 'Hero image license' ],
'common_names': tissue[ 'Common names' ],
'anatomical_terms': tissue[ 'Anatomical terms' ],
'clinical_terms': tissue[ 'Clinical terms' ],
'regional_terms': tissue[ 'Regional terms' ],
};
const tissueDataClean = Object.fromEntries( Object.entries( tissueData ).filter( ([ _, v ]) => v ) );
for ( const param in tissueDataClean ) {
text += '\n| ' + param + ' = ' + tissueData[ param ];
}
text += '\n}}';
// Add page data
text += '\n\n{{Page data';
const pageData = {
'keywords': tissue[ 'Page keywords' ],
'description': tissue[ 'Page description' ]
}
const pageDataClean = Object.fromEntries( Object.entries( pageData ).filter( ( [ _, v ] ) => v ) );
for ( const param in pageDataClean ) {
text += '\n| ' + param + ' = ' + pageData[ param ];
}
text += '\n}}';
// Create or update the page
const title = prefix + 'Tissues/' + tissue[ 'Title' ];
new mw.Api().postWithEditToken( {
action: 'edit',
title: title,
text: text
} );
// Output progress
console.log( title );
}
},
async makeMaterials( prefix ) {
//const materials = await TissueDB.get( 'Materials' );
},
async makeSimulators( prefix ) {
//const simulators = await TissueDB.get( 'Simulators' );
},
/**
* Get a sheet from the spreadsheet
* Docs: https://developers.google.com/workspace/sheets/api/reference/rest
*/
async get( sheet ) {
const id = '1uoFUGo4JrycAOOHapLzuwNIZ_oNYMDFCktBcIYBUHpU';
const key = 'AIzaSyDmToZIyqGeAhBjhD-QB1AfdeKI68Jd8UM';
const url = 'https://sheets.googleapis.com/v4/spreadsheets/' + id + '/values/' + sheet + '?key=' + key;
const response = await fetch( url );
const json = await response.json();
// Convert the data into an array of objects
// each object being a map from column name to value
const values = json.values;
const headers = values.shift();
const data = [];
for ( const value of values ) {
const item = {}
headers.forEach( ( header, index ) => {
item[ header ] = value[ index ];
} );
data.push( item );
}
return data;
}
};
TissueDB.init();