2024-01-28 21:27:12 +00:00
|
|
|
/*\
|
2024-02-23 12:51:07 +00:00
|
|
|
title: $:/plugins/tiddlywiki/multiwikiserver/mws-load-archive.js
|
2024-01-28 21:27:12 +00:00
|
|
|
type: application/javascript
|
|
|
|
module-type: command
|
|
|
|
|
2024-02-23 12:51:07 +00:00
|
|
|
Command to load archive of recipes, bags and tiddlers from a directory
|
2024-01-28 21:27:12 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
exports.info = {
|
2024-02-23 12:51:07 +00:00
|
|
|
name: "mws-load-archive",
|
2024-01-28 21:27:12 +00:00
|
|
|
synchronous: true
|
|
|
|
};
|
|
|
|
|
|
|
|
var Command = function(params,commander,callback) {
|
|
|
|
this.params = params;
|
|
|
|
this.commander = commander;
|
|
|
|
this.callback = callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
Command.prototype.execute = function() {
|
|
|
|
var self = this;
|
|
|
|
// Check parameters
|
|
|
|
if(this.params.length < 1) {
|
2024-02-23 12:51:07 +00:00
|
|
|
return "Missing pathname";
|
2024-01-28 21:27:12 +00:00
|
|
|
}
|
|
|
|
var archivePath = this.params[0];
|
|
|
|
loadBackupArchive(archivePath);
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
function loadBackupArchive(archivePath) {
|
|
|
|
const fs = require("fs"),
|
|
|
|
path = require("path");
|
|
|
|
// Iterate the bags
|
|
|
|
const bagNames = fs.readdirSync(path.resolve(archivePath,"bags")).filter(filename => filename !== ".DS_Store");
|
2024-02-23 09:27:53 +00:00
|
|
|
for(const bagFilename of bagNames) {
|
|
|
|
const bagName = decodeURIComponent(bagFilename);
|
2024-01-28 21:27:12 +00:00
|
|
|
console.log(`Reading bag ${bagName}`);
|
2024-02-23 09:27:53 +00:00
|
|
|
const bagInfo = JSON.parse(fs.readFileSync(path.resolve(archivePath,"bags",bagFilename,"meta.json"),"utf8"));
|
|
|
|
$tw.mws.store.createBag(bagName,bagInfo.description,bagInfo.accesscontrol);
|
|
|
|
if(fs.existsSync(path.resolve(archivePath,"bags",bagFilename,"tiddlers"))) {
|
|
|
|
const tiddlerFilenames = fs.readdirSync(path.resolve(archivePath,"bags",bagFilename,"tiddlers"));
|
2024-01-28 21:27:12 +00:00
|
|
|
for(const tiddlerFilename of tiddlerFilenames) {
|
2024-02-23 09:27:53 +00:00
|
|
|
if(tiddlerFilename.endsWith(".json")) {
|
|
|
|
const tiddlerPath = path.resolve(archivePath,"bags",bagFilename,"tiddlers",tiddlerFilename),
|
|
|
|
jsonTiddler = fs.readFileSync(tiddlerPath,"utf8"),
|
|
|
|
tiddler = sanitiseTiddler(JSON.parse(jsonTiddler));
|
|
|
|
if(tiddler && tiddler.title) {
|
|
|
|
$tw.mws.store.saveBagTiddler(tiddler,bagName);
|
|
|
|
} else {
|
|
|
|
console.log(`Malformed JSON tiddler in file ${tiddlerPath}`);
|
2024-02-16 16:41:39 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-28 21:27:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Iterate the recipes
|
2024-02-23 09:27:53 +00:00
|
|
|
const recipeNames = fs.readdirSync(path.resolve(archivePath,"recipes"));
|
|
|
|
for(const recipeFilename of recipeNames) {
|
|
|
|
if(recipeFilename.endsWith(".json")) {
|
|
|
|
const recipeName = decodeURIComponent(recipeFilename.substring(0,recipeFilename.length - ".json".length));
|
|
|
|
const jsonInfo = JSON.parse(fs.readFileSync(path.resolve(archivePath,"recipes",recipeFilename),"utf8"));
|
|
|
|
$tw.mws.store.createRecipe(recipeName,jsonInfo.bag_names,jsonInfo.description,jsonInfo.accesscontrol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function sanitiseTiddler(tiddler) {
|
|
|
|
var sanitisedFields = Object.create(null);
|
|
|
|
for(const fieldName in tiddler) {
|
|
|
|
const fieldValue = tiddler[fieldName];
|
|
|
|
let sanitisedValue = "";
|
|
|
|
if(typeof fieldValue === "string") {
|
|
|
|
sanitisedValue = fieldValue;
|
|
|
|
} else if($tw.utils.isDate(fieldValue)) {
|
|
|
|
sanitisedValue = $tw.utils.stringifyDate(fieldValue);
|
|
|
|
} else if($tw.utils.isArray(fieldValue)) {
|
|
|
|
sanitisedValue = $tw.utils.stringifyList(fieldValue);
|
|
|
|
}
|
|
|
|
sanitisedFields[fieldName] = sanitisedValue;
|
2024-01-28 21:27:12 +00:00
|
|
|
}
|
2024-02-23 09:27:53 +00:00
|
|
|
return sanitisedFields;
|
2024-01-28 21:27:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.Command = Command;
|
|
|
|
|
|
|
|
})();
|