2021-01-18 09:53:15 +01:00
|
|
|
'use strict';
|
|
|
|
|
2024-03-15 20:59:28 +01:00
|
|
|
import process from "node:process";
|
2024-03-13 20:31:29 +01:00
|
|
|
|
2014-05-19 18:18:01 +02:00
|
|
|
/*
|
2019-01-30 11:47:50 +01:00
|
|
|
* This is a repair tool. It extracts all datas of a pad, removes and inserts them again.
|
|
|
|
*/
|
2014-05-19 18:18:01 +02:00
|
|
|
|
2021-01-18 09:53:15 +01:00
|
|
|
// As of v14, Node.js does not exit when there is an unhandled Promise rejection. Convert an
|
|
|
|
// unhandled rejection into an uncaught exception, which does cause Node.js to exit.
|
|
|
|
process.on('unhandledRejection', (err) => { throw err; });
|
|
|
|
|
2020-11-23 19:21:51 +01:00
|
|
|
console.warn('WARNING: This script must not be used while etherpad is running!');
|
2014-05-19 18:18:01 +02:00
|
|
|
|
2024-02-21 21:50:11 +01:00
|
|
|
if (process.argv.length !== 3) throw new Error('Use: node ./src/bin/repairPad.js $PADID');
|
2019-01-30 11:47:50 +01:00
|
|
|
|
|
|
|
// get the padID
|
2020-11-23 19:21:51 +01:00
|
|
|
const padId = process.argv[2];
|
2014-05-19 18:18:01 +02:00
|
|
|
|
2021-01-18 09:53:15 +01:00
|
|
|
let valueCount = 0;
|
2014-05-19 18:18:01 +02:00
|
|
|
|
2021-01-09 08:44:59 +01:00
|
|
|
(async () => {
|
2021-02-03 00:30:07 +01:00
|
|
|
// initialize database
|
2024-03-13 20:31:29 +01:00
|
|
|
require('ep_etherpad-lite/node/utils/Settings');
|
|
|
|
const db = require('ep_etherpad-lite/node/db/DB');
|
2021-01-18 09:53:15 +01:00
|
|
|
await db.init();
|
2019-01-30 11:47:50 +01:00
|
|
|
|
2021-01-18 09:53:15 +01:00
|
|
|
// get the pad
|
2024-03-13 20:31:29 +01:00
|
|
|
const padManager = require('ep_etherpad-lite/node/db/PadManager');
|
2021-01-18 09:53:15 +01:00
|
|
|
const pad = await padManager.getPad(padId);
|
2019-01-30 11:47:50 +01:00
|
|
|
|
2021-01-18 09:53:15 +01:00
|
|
|
// accumulate the required keys
|
|
|
|
const neededDBValues = [`pad:${padId}`];
|
2019-01-30 11:47:50 +01:00
|
|
|
|
2021-01-18 09:53:15 +01:00
|
|
|
// add all authors
|
2024-03-13 20:31:29 +01:00
|
|
|
neededDBValues.push(...pad.getAllAuthors().map((author: string) => `globalAuthor:${author}`));
|
2019-01-30 11:47:50 +01:00
|
|
|
|
2021-01-18 09:53:15 +01:00
|
|
|
// add all revisions
|
|
|
|
for (let rev = 0; rev <= pad.head; ++rev) {
|
|
|
|
neededDBValues.push(`pad:${padId}:revs:${rev}`);
|
|
|
|
}
|
2019-01-30 11:47:50 +01:00
|
|
|
|
2021-01-18 09:53:15 +01:00
|
|
|
// add all chat values
|
|
|
|
for (let chat = 0; chat <= pad.chatHead; ++chat) {
|
|
|
|
neededDBValues.push(`pad:${padId}:chat:${chat}`);
|
2014-05-19 18:18:01 +02:00
|
|
|
}
|
2021-01-18 09:53:15 +01:00
|
|
|
// now fetch and reinsert every key
|
2024-01-24 19:42:43 +01:00
|
|
|
console.log('Fetch and reinsert every key');
|
2021-01-18 09:53:15 +01:00
|
|
|
for (const key of neededDBValues) {
|
2024-01-24 19:42:43 +01:00
|
|
|
if (valueCount % 100 === 0) console.log(valueCount + "/" + neededDBValues.length);
|
2021-01-18 09:53:15 +01:00
|
|
|
const value = await db.get(key);
|
|
|
|
// if it isn't a globalAuthor value which we want to ignore..
|
|
|
|
// console.log(`Key: ${key}, value: ${JSON.stringify(value)}`);
|
|
|
|
await db.remove(key);
|
|
|
|
await db.set(key, value);
|
|
|
|
valueCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.info(`Finished: Replaced ${valueCount} values in the database`);
|
2021-01-09 08:44:59 +01:00
|
|
|
})();
|