1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-30 13:29:56 +00:00

Introduce $tw.mws for MWS globals

This commit is contained in:
Jeremy Ruston 2024-02-05 14:49:08 +00:00
parent 2c810faeeb
commit f925f036c9
11 changed files with 41 additions and 39 deletions

View File

@ -41,7 +41,7 @@ function loadBackupArchive(archivePath) {
const bagNames = fs.readdirSync(path.resolve(archivePath,"bags")).filter(filename => filename !== ".DS_Store");
for(const bagName of bagNames) {
console.log(`Reading bag ${bagName}`);
$tw.sqlTiddlerStore.createBag(decodeURIComponent(bagName),"No description for " + bagName);
$tw.mws.store.createBag(decodeURIComponent(bagName),"No description for " + bagName);
// Read the metadata
const jsonInfo = JSON.parse(fs.readFileSync(path.resolve(archivePath,"bags",bagName,"meta/info.json"),"utf8"));
if(fs.existsSync(path.resolve(archivePath,"bags",bagName,"tiddlers"))) {
@ -50,7 +50,7 @@ function loadBackupArchive(archivePath) {
for(const tiddlerFilename of tiddlerFilenames) {
const jsonTiddler = fs.readFileSync(path.resolve(archivePath,"bags",bagName,"tiddlers",tiddlerFilename),"utf8"),
tiddler = JSON.parse(jsonTiddler);
$tw.sqlTiddlerStore.saveBagTiddler(tiddler,decodeURIComponent(bagName));
$tw.mws.store.saveBagTiddler(tiddler,decodeURIComponent(bagName));
}
}
}
@ -58,7 +58,7 @@ function loadBackupArchive(archivePath) {
const recipeNames = fs.readdirSync(path.resolve(archivePath,"recipes")).filter(filename => filename !== ".DS_Store");
for(const recipeName of recipeNames) {
const jsonInfo = JSON.parse(fs.readFileSync(path.resolve(archivePath,"recipes",recipeName,"info.json"),"utf8"));
$tw.sqlTiddlerStore.createRecipe(decodeURIComponent(recipeName),jsonInfo.recipe.map(recipeLine => recipeLine[0]),"No description for " + recipeName);
$tw.mws.store.createRecipe(decodeURIComponent(recipeName),jsonInfo.recipe.map(recipeLine => recipeLine[0]),"No description for " + recipeName);
}
}

View File

@ -38,28 +38,30 @@ exports.startup = function() {
var databasePath = path.resolve($tw.boot.wikiPath,"store/database.sqlite");
// Create and initialise the tiddler store
var SqlTiddlerStore = require("$:/plugins/tiddlywiki/multiwikiserver/sql-tiddler-store.js").SqlTiddlerStore;
$tw.sqlTiddlerStore = new SqlTiddlerStore({
databasePath: databasePath
});
$tw.mws = {
store: new SqlTiddlerStore({
databasePath: databasePath
})
};
// Create docs bag and recipe
$tw.sqlTiddlerStore.createBag("docs","TiddlyWiki Documentation from https://tiddlywiki.com/");
$tw.sqlTiddlerStore.createRecipe("docs",["docs"],"TiddlyWiki Documentation from https://tiddlywiki.com/");
$tw.sqlTiddlerStore.saveTiddlersFromPath(path.resolve($tw.boot.corePath,$tw.config.editionsPath,"tw5.com/tiddlers"),"docs");
$tw.sqlTiddlerStore.createBag("dev-docs","TiddlyWiki Developer Documentation from https://tiddlywiki.com/dev/");
$tw.sqlTiddlerStore.createRecipe("dev-docs",["dev-docs"],"TiddlyWiki Developer Documentation from https://tiddlywiki.com/dev/");
$tw.sqlTiddlerStore.saveTiddlersFromPath(path.resolve($tw.boot.corePath,$tw.config.editionsPath,"dev/tiddlers"),"dev-docs");
$tw.mws.store.createBag("docs","TiddlyWiki Documentation from https://tiddlywiki.com/");
$tw.mws.store.createRecipe("docs",["docs"],"TiddlyWiki Documentation from https://tiddlywiki.com/");
$tw.mws.store.saveTiddlersFromPath(path.resolve($tw.boot.corePath,$tw.config.editionsPath,"tw5.com/tiddlers"),"docs");
$tw.mws.store.createBag("dev-docs","TiddlyWiki Developer Documentation from https://tiddlywiki.com/dev/");
$tw.mws.store.createRecipe("dev-docs",["dev-docs"],"TiddlyWiki Developer Documentation from https://tiddlywiki.com/dev/");
$tw.mws.store.saveTiddlersFromPath(path.resolve($tw.boot.corePath,$tw.config.editionsPath,"dev/tiddlers"),"dev-docs");
// Create bags and recipes
$tw.sqlTiddlerStore.createBag("bag-alpha","A test bag");
$tw.sqlTiddlerStore.createBag("bag-beta","Another test bag");
$tw.sqlTiddlerStore.createBag("bag-gamma","A further test bag");
$tw.sqlTiddlerStore.createRecipe("recipe-rho",["bag-alpha","bag-beta"],"First wiki");
$tw.sqlTiddlerStore.createRecipe("recipe-sigma",["bag-alpha","bag-gamma"],"Second Wiki");
$tw.sqlTiddlerStore.createRecipe("recipe-tau",["bag-alpha"],"Third Wiki");
$tw.sqlTiddlerStore.createRecipe("recipe-upsilon",["bag-alpha","bag-gamma","bag-beta"],"Fourth Wiki");
$tw.mws.store.createBag("bag-alpha","A test bag");
$tw.mws.store.createBag("bag-beta","Another test bag");
$tw.mws.store.createBag("bag-gamma","A further test bag");
$tw.mws.store.createRecipe("recipe-rho",["bag-alpha","bag-beta"],"First wiki");
$tw.mws.store.createRecipe("recipe-sigma",["bag-alpha","bag-gamma"],"Second Wiki");
$tw.mws.store.createRecipe("recipe-tau",["bag-alpha"],"Third Wiki");
$tw.mws.store.createRecipe("recipe-upsilon",["bag-alpha","bag-gamma","bag-beta"],"Fourth Wiki");
// Save tiddlers
$tw.sqlTiddlerStore.saveBagTiddler({title: "$:/SiteTitle",text: "Bag Alpha"},"bag-alpha");
$tw.sqlTiddlerStore.saveBagTiddler({title: "$:/SiteTitle",text: "Bag Beta"},"bag-beta");
$tw.sqlTiddlerStore.saveBagTiddler({title: "$:/SiteTitle",text: "Bag Gamma"},"bag-gamma");
$tw.mws.store.saveBagTiddler({title: "$:/SiteTitle",text: "Bag Alpha"},"bag-alpha");
$tw.mws.store.saveBagTiddler({title: "$:/SiteTitle",text: "Bag Beta"},"bag-beta");
$tw.mws.store.saveBagTiddler({title: "$:/SiteTitle",text: "Bag Gamma"},"bag-gamma");
};
})();

View File

@ -23,9 +23,9 @@ exports.handler = function(request,response,state) {
var recipe_name = $tw.utils.decodeURIComponentSafe(state.params[0]),
bag_name = $tw.utils.decodeURIComponentSafe(state.params[1]),
title = $tw.utils.decodeURIComponentSafe(state.params[2]);
var recipeBags = $tw.sqlTiddlerStore.getRecipeBags(recipe_name);
var recipeBags = $tw.mws.store.getRecipeBags(recipe_name);
if(recipeBags.indexOf(bag_name) !== -1) {
$tw.sqlTiddlerStore.deleteTiddler(title,bag_name);
$tw.mws.store.deleteTiddler(title,bag_name);
response.writeHead(204, "OK", {
"Content-Type": "text/plain"
});

View File

@ -23,7 +23,7 @@ exports.handler = function(request,response,state) {
var bag_name = $tw.utils.decodeURIComponentSafe(state.params[0]),
bag_name_2 = $tw.utils.decodeURIComponentSafe(state.params[1]),
title = $tw.utils.decodeURIComponentSafe(state.params[2]),
result = bag_name === bag_name_2 && $tw.sqlTiddlerStore.getBagTiddler(title,bag_name);
result = bag_name === bag_name_2 && $tw.mws.store.getBagTiddler(title,bag_name);
if(bag_name === bag_name_2 && result) {
// If application/json is requested then this is an API request, and gets the response in JSON
if(request.headers.accept && request.headers.accept.indexOf("application/json") !== -1) {
@ -40,7 +40,7 @@ exports.handler = function(request,response,state) {
}
});
tiddlerFields.type = tiddlerFields.type || "text/vnd.tiddlywiki";
tiddlerFields = $tw.sqlTiddlerStore.processCanonicalUriTiddler(tiddlerFields,bag_name,null);
tiddlerFields = $tw.mws.store.processCanonicalUriTiddler(tiddlerFields,bag_name,null);
state.sendResponse(200,{"Content-Type": "application/json"},JSON.stringify(tiddlerFields),"utf8");
} else {
// This is not a JSON API request, we should return the raw tiddler content

View File

@ -22,7 +22,7 @@ exports.handler = function(request,response,state) {
// Get the parameters
var bag_name = $tw.utils.decodeURIComponentSafe(state.params[0]),
bag_name_2 = $tw.utils.decodeURIComponentSafe(state.params[1]),
titles = bag_name === bag_name_2 && $tw.sqlTiddlerStore.getBagTiddlers(bag_name);
titles = bag_name === bag_name_2 && $tw.mws.store.getBagTiddlers(bag_name);
if(bag_name === bag_name_2 && titles) {
// If application/json is requested then this is an API request, and gets the response in JSON
if(request.headers.accept && request.headers.accept.indexOf("application/json") !== -1) {
@ -33,7 +33,7 @@ exports.handler = function(request,response,state) {
"Content-Type": "text/html"
});
// Render the html
var html = $tw.sqlTiddlerStore.adminWiki.renderTiddler("text/html","$:/plugins/tiddlywiki/multiwikiserver/templates/get-bags",{
var html = $tw.mws.store.adminWiki.renderTiddler("text/html","$:/plugins/tiddlywiki/multiwikiserver/templates/get-bags",{
variables: {
"bag-name": bag_name,
"bag-titles": JSON.stringify(titles)

View File

@ -23,7 +23,7 @@ exports.handler = function(request,response,state) {
var recipe_name = $tw.utils.decodeURIComponentSafe(state.params[0]),
recipe_name_2 = $tw.utils.decodeURIComponentSafe(state.params[1]),
title = $tw.utils.decodeURIComponentSafe(state.params[2]),
tiddlerInfo = recipe_name === recipe_name_2 && $tw.sqlTiddlerStore.getRecipeTiddler(title,recipe_name);
tiddlerInfo = recipe_name === recipe_name_2 && $tw.mws.store.getRecipeTiddler(title,recipe_name);
if(recipe_name === recipe_name_2 && tiddlerInfo && tiddlerInfo.tiddler) {
// If application/json is requested then this is an API request, and gets the response in JSON
if(request.headers.accept && request.headers.accept.indexOf("application/json") !== -1) {
@ -40,7 +40,7 @@ exports.handler = function(request,response,state) {
}
});
tiddlerFields.type = tiddlerFields.type || "text/vnd.tiddlywiki";
tiddlerFields = $tw.sqlTiddlerStore.processCanonicalUriTiddler(tiddlerFields,null,recipe_name);
tiddlerFields = $tw.mws.store.processCanonicalUriTiddler(tiddlerFields,null,recipe_name);
state.sendResponse(200,{"Content-Type": "application/json"},JSON.stringify(tiddlerFields),"utf8");
} else {
// This is not a JSON API request, we should return the raw tiddler content

View File

@ -24,11 +24,11 @@ exports.handler = function(request,response,state) {
recipe_name_2 = $tw.utils.decodeURIComponentSafe(state.params[1]);
if(recipe_name === recipe_name_2) {
// Get the tiddlers in the recipe
var recipeTiddlers = $tw.sqlTiddlerStore.getRecipeTiddlers(recipe_name);
var recipeTiddlers = $tw.mws.store.getRecipeTiddlers(recipe_name);
// Get a skinny version of each tiddler
var tiddlers = [];
$tw.utils.each(recipeTiddlers,function(recipeTiddlerInfo) {
var tiddlerInfo = $tw.sqlTiddlerStore.getRecipeTiddler(recipeTiddlerInfo.title,recipe_name);
var tiddlerInfo = $tw.mws.store.getRecipeTiddler(recipeTiddlerInfo.title,recipe_name);
tiddlers.push(Object.assign({},tiddlerInfo.tiddler,{text: undefined}));
});
var text = JSON.stringify(tiddlers);

View File

@ -26,9 +26,9 @@ exports.handler = function(request,response,state) {
"Content-Type": "text/html"
});
// Get the tiddlers in the recipe
var recipeTiddlers = $tw.sqlTiddlerStore.getRecipeTiddlers(recipe_name);
var recipeTiddlers = $tw.mws.store.getRecipeTiddlers(recipe_name);
// Render the template
var template = $tw.sqlTiddlerStore.adminWiki.renderTiddler("text/plain","$:/core/templates/tiddlywiki5.html",{
var template = $tw.mws.store.adminWiki.renderTiddler("text/plain","$:/core/templates/tiddlywiki5.html",{
variables: {
saveTiddlerFilter: `
$:/boot/boot.css
@ -50,10 +50,10 @@ exports.handler = function(request,response,state) {
}
response.write(template.substring(0,markerPos + marker.length));
$tw.utils.each(recipeTiddlers,function(recipeTiddlerInfo) {
var result = $tw.sqlTiddlerStore.getRecipeTiddler(recipeTiddlerInfo.title,recipe_name);
var result = $tw.mws.store.getRecipeTiddler(recipeTiddlerInfo.title,recipe_name);
if(result) {
var tiddlerFields = result.tiddler;
tiddlerFields = $tw.sqlTiddlerStore.processCanonicalUriTiddler(tiddlerFields,null,recipe_name);
tiddlerFields = $tw.mws.store.processCanonicalUriTiddler(tiddlerFields,null,recipe_name);
response.write(JSON.stringify(tiddlerFields).replace(/</g,"\\u003c"));
response.write(",\n")
}

View File

@ -24,7 +24,7 @@ exports.handler = function(request,response,state) {
bag_name_2 = $tw.utils.decodeURIComponentSafe(state.params[1]),
data = $tw.utils.parseJSONSafe(state.data);
if(bag_name === bag_name_2 && data) {
const result = $tw.sqlTiddlerStore.createBag(bag_name,data.description);
const result = $tw.mws.store.createBag(bag_name,data.description);
if(!result) {
state.sendResponse(204,{
"Content-Type": "text/plain"

View File

@ -39,7 +39,7 @@ exports.handler = function(request,response,state) {
});
// Require the recipe names to match
if(recipe_name === recipe_name_2) {
var result = $tw.sqlTiddlerStore.saveRecipeTiddler(fields,recipe_name);
var result = $tw.mws.store.saveRecipeTiddler(fields,recipe_name);
if(result) {
response.writeHead(204, "OK",{
Etag: "\"" + result.bag_name + "/" + encodeURIComponent(title) + "/" + result.tiddler_id + ":\"",

View File

@ -24,7 +24,7 @@ exports.handler = function(request,response,state) {
recipe_name_2 = $tw.utils.decodeURIComponentSafe(state.params[1]),
data = $tw.utils.parseJSONSafe(state.data);
if(recipe_name === recipe_name_2 && data) {
const result = $tw.sqlTiddlerStore.createRecipe(recipe_name,data.bag_names,data.description);
const result = $tw.mws.store.createRecipe(recipe_name,data.bag_names,data.description);
console.log(`create recipe route handler for ${recipe_name} with ${JSON.stringify(data)} got result ${JSON.stringify(result)}`)
if(!result) {
state.sendResponse(204,{