diff --git a/moonsource-weread.koplugin/moon_source.lua b/moonsource-weread.koplugin/moon_source.lua index 4e9d9d3..083a7b7 100644 --- a/moonsource-weread.koplugin/moon_source.lua +++ b/moonsource-weread.koplugin/moon_source.lua @@ -13,6 +13,6 @@ return { api = 1, provides = { source = true }, meta = function() return require("moonsource_weread.source").meta() end, - new = function() return require("moonsource_weread.source").new() end, + new = function() require("moonsource_weread.legacy").run() return require("moonsource_weread.source").new() end, setting = lazy("moonsource_weread.setting"), } diff --git a/moonsource-weread.koplugin/moonsource_weread/legacy.lua b/moonsource-weread.koplugin/moonsource_weread/legacy.lua new file mode 100644 index 0000000..b4c7d1a --- /dev/null +++ b/moonsource-weread.koplugin/moonsource_weread/legacy.lua @@ -0,0 +1,114 @@ +--[[-- +旧版月读内置微信读书的 id 是 `wechat`,插件版是 `weread`。首次加载时把旧数据就地改名,用户不用重新登录、书架和进度都在: +设置、数据库里按源分区的记录、缓存目录及库里记录的路径、源启用状态。 + +只在发现旧数据时动手,改完旧设置清空,之后每次启动只是两次空判断。 +--]] + +local lfs = require("libs/libkoreader-lfs") + +local Legacy = {} + +local OLD, NEW = "wechat", "weread" + +-- 带 source_id 列的表;本表集合与月读主库一致。 +local TABLES = { + "books", "chapters", "notes", "pending_progress", "reading_stats", "xray_entities", "chapter_summaries", + "xray_scan", "xray_rare_scan", "review_cache", "xray_removed", "xray_chapter", "toc_snapshots", "toc_entries", + "chapter_summary_pending", +} + +local done = false + +local function exists(path) + return lfs.attributes(path, "mode") ~= nil +end + +local function moveSettings() + local Settings = require("utils.settings") + local old, new = Settings.getSource(OLD), Settings.getSource(NEW) + if next(old) == nil then return false end + if next(new) == nil then + for key, value in pairs(old) do new[key] = value end + Settings.saveSource(NEW) + end + for key in pairs(old) do old[key] = nil end + Settings.saveSource(OLD) + return true +end + +--- 启用状态、当前源跟着改名。 +local function moveCommon() + local Settings = require("utils.settings") + local common = Settings.get("source") + local changed = false + if type(common.enabled_sources) == "table" and common.enabled_sources[OLD] ~= nil then + if common.enabled_sources[NEW] == nil then common.enabled_sources[NEW] = common.enabled_sources[OLD] end + common.enabled_sources[OLD] = nil + changed = true + end + if common.active_source == OLD then + common.active_source = NEW + changed = true + end + if changed then Settings.save("source") end +end + +--- 把旧目录并进新目录:新目录没有的整项改名,两边都是目录的往下合并,同名文件留旧的不覆盖。 +--- 旧目录搬空并删除才返回 true(此时库里的路径才能整体改前缀)。 +local function mergeDir(old_dir, new_dir) + if not exists(new_dir) then return os.rename(old_dir, new_dir) == true end + for name in lfs.dir(old_dir) do + if name ~= "." and name ~= ".." then + local from, to = old_dir .. "/" .. name, new_dir .. "/" .. name + if not exists(to) then + os.rename(from, to) + elseif lfs.attributes(from, "mode") == "directory" and lfs.attributes(to, "mode") == "directory" then + mergeDir(from, to) + end + end + end + return lfs.rmdir(old_dir) == true +end + +---@return boolean moved +local function moveCache() + local Paths = require("utils.paths") + local old_dir = Paths.sourceCacheDir(OLD) + if not exists(old_dir) then return false end + return mergeDir(old_dir, Paths.sourceCacheDir(NEW)) +end + +local function moveDatabase(paths_moved) + local DB = require("utils.db") + DB.transaction(function() + for _idx, name in ipairs(TABLES) do + -- 同主键的新记录已存在就保留新的,旧的留在原处不再用 + DB.exec(("UPDATE OR IGNORE %s SET source_id=? WHERE source_id=?;"):format(name), NEW, OLD) + end + if paths_moved then + local old_part, new_part = "/cache/" .. OLD .. "/", "/cache/" .. NEW .. "/" + for _idx, name in ipairs({ "books", "chapters", "local_scan" }) do + DB.exec(("UPDATE %s SET path=REPLACE(path, ?, ?) WHERE path LIKE ?;"):format(name), + old_part, new_part, "%" .. old_part .. "%") + end + end + return true + end) +end + +--- 就地迁移旧 id 的数据;每个进程只跑一次。 +function Legacy.run() + if done then return end + done = true + local Settings = require("utils.settings") + local has_settings = next(Settings.getSource(OLD)) ~= nil + local has_cache = exists(require("utils.paths").sourceCacheDir(OLD)) + if not has_settings and not has_cache then return end + pcall(moveSettings) + pcall(moveCommon) + local ok, paths_moved = pcall(moveCache) + pcall(moveDatabase, ok and paths_moved) +end + +return Legacy diff --git a/tests/weread/legacy_spec.lua b/tests/weread/legacy_spec.lua new file mode 100644 index 0000000..578d893 --- /dev/null +++ b/tests/weread/legacy_spec.lua @@ -0,0 +1,59 @@ +--[[-- +moonsource_weread.legacy:旧内置版 id `wechat` 的设置、库记录、缓存目录就地改成 `weread`。 + +@module tests.weread.legacy_spec +--]] +---@diagnostic disable: undefined-field + +local Assert = require("support.assert") +local Config = require("support.config") +local lfs = require("libs/libkoreader-lfs") + +local root = Config.dir() .. "/weread_legacy" +os.execute(("rm -rf '%s' && mkdir -p '%s/cache/wechat/book'"):format(root, root)) +local f = assert(io.open(root .. "/cache/wechat/book/1.html", "w")) +f:write("x") +f:close() + +local sources = { wechat = { cookie = "c" }, weread = {} } +local common = { enabled_sources = { wechat = true }, active_source = "wechat" } +local saved = {} +package.loaded["utils.settings"] = { + getSource = function(id) sources[id] = sources[id] or {}; return sources[id] end, + saveSource = function(id) saved[#saved + 1] = id end, + get = function() return common end, + save = function() end, +} +package.loaded["utils.paths"] = { sourceCacheDir = function(id) return root .. "/cache/" .. id end } +local sql = {} +package.loaded["utils.db"] = { + transaction = function(fn) return fn() end, + exec = function(statement, ...) sql[#sql + 1] = { statement, ... }; return true end, +} +package.loaded["moonsource_weread.legacy"] = nil +local Legacy = require("moonsource_weread.legacy") + +Legacy.run() + +Assert.eq(sources.weread.cookie, "c", "设置搬到新 id") +Assert.is_nil(next(sources.wechat), "旧设置清空") +Assert.eq(common.enabled_sources.weread, true) +Assert.is_nil(common.enabled_sources.wechat) +Assert.eq(common.active_source, "weread") +Assert.is_nil(lfs.attributes(root .. "/cache/wechat", "mode")) +Assert.eq(lfs.attributes(root .. "/cache/weread/book/1.html", "mode"), "file", "缓存目录改名") + +local tables, paths = 0, 0 +for _idx, entry in ipairs(sql) do + if entry[1]:find("SET source_id=%?") and entry[2] == "weread" and entry[3] == "wechat" then tables = tables + 1 end + if entry[1]:find("REPLACE%(path") then paths = paths + 1 end +end +Assert.eq(tables, 15) +Assert.eq(paths, 3, "库里记录的路径跟着改") + +-- 同一进程不重复执行 +local count = #sql +Legacy.run() +Assert.eq(#sql, count) + +os.execute(("rm -rf '%s'"):format(root))