2021-01-18 08:53:15 +00:00
|
|
|
'use strict';
|
|
|
|
|
2014-11-19 18:42:42 +00:00
|
|
|
/*
|
|
|
|
This is a repair tool. It rebuilds an old pad at a new pad location up to a
|
|
|
|
known "good" revision.
|
|
|
|
*/
|
|
|
|
|
2021-01-18 08:53:15 +00: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; });
|
|
|
|
|
|
|
|
if (process.argv.length !== 4 && process.argv.length !== 5) {
|
|
|
|
throw new Error('Use: node bin/repairPad.js $PADID $REV [$NEWPADID]');
|
2014-11-19 18:42:42 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 08:53:15 +00:00
|
|
|
const async = require('ep_etherpad-lite/node_modules/async');
|
|
|
|
const npm = require('ep_etherpad-lite/node_modules/npm');
|
|
|
|
const util = require('util');
|
2014-11-19 18:42:42 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
const padId = process.argv[2];
|
|
|
|
const newRevHead = process.argv[3];
|
|
|
|
const newPadId = process.argv[4] || `${padId}-rebuilt`;
|
2014-11-19 18:42:42 +00:00
|
|
|
|
2021-01-18 08:53:15 +00:00
|
|
|
let db, oldPad, newPad;
|
|
|
|
let Pad, PadManager;
|
2014-11-19 18:42:42 +00:00
|
|
|
|
|
|
|
async.series([
|
2021-01-18 08:53:15 +00:00
|
|
|
(callback) => npm.load({}, callback),
|
|
|
|
(callback) => {
|
2014-11-19 18:42:42 +00:00
|
|
|
// Get a handle into the database
|
2021-01-18 08:53:15 +00:00
|
|
|
db = require('ep_etherpad-lite/node/db/DB');
|
2014-11-19 18:42:42 +00:00
|
|
|
db.init(callback);
|
2020-11-23 18:21:51 +00:00
|
|
|
},
|
2021-01-18 08:53:15 +00:00
|
|
|
(callback) => {
|
|
|
|
Pad = require('ep_etherpad-lite/node/db/Pad').Pad;
|
|
|
|
PadManager = require('ep_etherpad-lite/node/db/PadManager');
|
2020-11-23 18:21:51 +00:00
|
|
|
// Get references to the original pad and to a newly created pad
|
|
|
|
// HACK: This is a standalone script, so we want to write everything
|
|
|
|
// out to the database immediately. The only problem with this is
|
|
|
|
// that a driver (like the mysql driver) can hardcode these values.
|
|
|
|
db.db.db.settings = {cache: 0, writeInterval: 0, json: true};
|
|
|
|
// Validate the newPadId if specified and that a pad with that ID does
|
|
|
|
// not already exist to avoid overwriting it.
|
|
|
|
if (!PadManager.isValidPadId(newPadId)) {
|
2021-01-18 08:53:15 +00:00
|
|
|
throw new Error('Cannot create a pad with that id as it is invalid');
|
2020-11-23 18:21:51 +00:00
|
|
|
}
|
|
|
|
PadManager.doesPadExists(newPadId, (err, exists) => {
|
2021-01-18 08:53:15 +00:00
|
|
|
if (exists) throw new Error('Cannot create a pad with that id as it already exists');
|
2020-11-23 18:21:51 +00:00
|
|
|
});
|
|
|
|
PadManager.getPad(padId, (err, pad) => {
|
|
|
|
oldPad = pad;
|
|
|
|
newPad = new Pad(newPadId);
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
},
|
2021-01-18 08:53:15 +00:00
|
|
|
(callback) => {
|
2014-11-19 18:42:42 +00:00
|
|
|
// Clone all Chat revisions
|
2020-11-23 18:21:51 +00:00
|
|
|
const chatHead = oldPad.chatHead;
|
2021-01-18 08:53:15 +00:00
|
|
|
for (let i = 0, curHeadNum = 0; i <= chatHead; i++) {
|
2020-11-23 18:21:51 +00:00
|
|
|
db.db.get(`pad:${padId}:chat:${i}`, (err, chat) => {
|
|
|
|
db.db.set(`pad:${newPadId}:chat:${curHeadNum++}`, chat);
|
|
|
|
console.log(`Created: Chat Revision: pad:${newPadId}:chat:${curHeadNum}`);
|
2014-11-19 18:42:42 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
callback();
|
2020-11-23 18:21:51 +00:00
|
|
|
},
|
2021-01-18 08:53:15 +00:00
|
|
|
(callback) => {
|
2014-11-19 18:42:42 +00:00
|
|
|
// Rebuild Pad from revisions up to and including the new revision head
|
2021-01-18 08:53:15 +00:00
|
|
|
const AuthorManager = require('ep_etherpad-lite/node/db/AuthorManager');
|
|
|
|
const Changeset = require('ep_etherpad-lite/static/js/Changeset');
|
2014-11-19 18:42:42 +00:00
|
|
|
// Author attributes are derived from changesets, but there can also be
|
|
|
|
// non-author attributes with specific mappings that changesets depend on
|
|
|
|
// and, AFAICT, cannot be recreated any other way
|
2014-12-04 02:11:39 +00:00
|
|
|
newPad.pool.numToAttrib = oldPad.pool.numToAttrib;
|
2020-11-23 18:21:51 +00:00
|
|
|
for (let curRevNum = 0; curRevNum <= newRevHead; curRevNum++) {
|
|
|
|
db.db.get(`pad:${padId}:revs:${curRevNum}`, (err, rev) => {
|
2015-10-07 10:42:19 +00:00
|
|
|
if (rev.meta) {
|
2021-01-18 08:53:15 +00:00
|
|
|
throw new Error('The specified revision number could not be found.');
|
2015-10-07 10:42:19 +00:00
|
|
|
}
|
2020-11-23 18:21:51 +00:00
|
|
|
const newRevNum = ++newPad.head;
|
|
|
|
const newRevId = `pad:${newPad.id}:revs:${newRevNum}`;
|
2014-11-21 03:31:03 +00:00
|
|
|
db.db.set(newRevId, rev);
|
2014-12-04 02:11:39 +00:00
|
|
|
AuthorManager.addPad(rev.meta.author, newPad.id);
|
|
|
|
newPad.atext = Changeset.applyToAText(rev.changeset, newPad.atext, newPad.pool);
|
2020-11-23 18:21:51 +00:00
|
|
|
console.log(`Created: Revision: pad:${newPad.id}:revs:${newRevNum}`);
|
2021-01-18 08:53:15 +00:00
|
|
|
if (newRevNum === newRevHead) {
|
2014-11-19 18:42:42 +00:00
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-11-23 18:21:51 +00:00
|
|
|
},
|
2021-01-18 08:53:15 +00:00
|
|
|
(callback) => {
|
2014-11-19 18:42:42 +00:00
|
|
|
// Add saved revisions up to the new revision head
|
|
|
|
console.log(newPad.head);
|
2020-11-23 18:21:51 +00:00
|
|
|
const newSavedRevisions = [];
|
2021-01-18 08:53:15 +00:00
|
|
|
for (const savedRev of oldPad.savedRevisions) {
|
2014-11-19 18:42:42 +00:00
|
|
|
if (savedRev.revNum <= newRevHead) {
|
|
|
|
newSavedRevisions.push(savedRev);
|
2020-11-23 18:21:51 +00:00
|
|
|
console.log(`Added: Saved Revision: ${savedRev.revNum}`);
|
2014-11-19 18:42:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
newPad.savedRevisions = newSavedRevisions;
|
|
|
|
callback();
|
2020-11-23 18:21:51 +00:00
|
|
|
},
|
2021-01-18 08:53:15 +00:00
|
|
|
(callback) => {
|
2014-11-19 18:42:42 +00:00
|
|
|
// Save the source pad
|
2020-11-23 18:21:51 +00:00
|
|
|
db.db.set(`pad:${newPadId}`, newPad, (err) => {
|
|
|
|
console.log(`Created: Source Pad: pad:${newPadId}`);
|
2021-01-18 08:53:15 +00:00
|
|
|
util.callbackify(newPad.saveToDatabase.bind(newPad))(callback);
|
2014-11-19 18:42:42 +00:00
|
|
|
});
|
2020-11-23 18:21:51 +00:00
|
|
|
},
|
|
|
|
], (err) => {
|
2021-01-18 08:53:15 +00:00
|
|
|
if (err) throw err;
|
|
|
|
console.info('finished');
|
2014-11-19 18:42:42 +00:00
|
|
|
});
|