bin/rebuildPad.js: Asyncify
parent
72c2abab8d
commit
5b519b9a9c
|
@ -13,7 +13,6 @@ if (process.argv.length !== 4 && process.argv.length !== 5) {
|
||||||
throw new Error('Use: node bin/repairPad.js $PADID $REV [$NEWPADID]');
|
throw new Error('Use: node bin/repairPad.js $PADID $REV [$NEWPADID]');
|
||||||
}
|
}
|
||||||
|
|
||||||
const async = require('ep_etherpad-lite/node_modules/async');
|
|
||||||
const npm = require('ep_etherpad-lite/node_modules/npm');
|
const npm = require('ep_etherpad-lite/node_modules/npm');
|
||||||
const util = require('util');
|
const util = require('util');
|
||||||
|
|
||||||
|
@ -21,99 +20,76 @@ const padId = process.argv[2];
|
||||||
const newRevHead = process.argv[3];
|
const newRevHead = process.argv[3];
|
||||||
const newPadId = process.argv[4] || `${padId}-rebuilt`;
|
const newPadId = process.argv[4] || `${padId}-rebuilt`;
|
||||||
|
|
||||||
let db, oldPad, newPad;
|
(async () => {
|
||||||
let Pad, PadManager;
|
await util.promisify(npm.load)({});
|
||||||
|
|
||||||
async.series([
|
const db = require('ep_etherpad-lite/node/db/DB');
|
||||||
(callback) => npm.load({}, callback),
|
await db.init();
|
||||||
(callback) => {
|
|
||||||
// Get a handle into the database
|
const Pad = require('ep_etherpad-lite/node/db/Pad').Pad;
|
||||||
db = require('ep_etherpad-lite/node/db/DB');
|
const PadManager = require('ep_etherpad-lite/node/db/PadManager');
|
||||||
util.callbackify(db.init)(callback);
|
// Get references to the original pad and to a newly created pad
|
||||||
},
|
// HACK: This is a standalone script, so we want to write everything
|
||||||
(callback) => {
|
// out to the database immediately. The only problem with this is
|
||||||
Pad = require('ep_etherpad-lite/node/db/Pad').Pad;
|
// that a driver (like the mysql driver) can hardcode these values.
|
||||||
PadManager = require('ep_etherpad-lite/node/db/PadManager');
|
db.db.db.settings = {cache: 0, writeInterval: 0, json: true};
|
||||||
// Get references to the original pad and to a newly created pad
|
// Validate the newPadId if specified and that a pad with that ID does
|
||||||
// HACK: This is a standalone script, so we want to write everything
|
// not already exist to avoid overwriting it.
|
||||||
// out to the database immediately. The only problem with this is
|
if (!PadManager.isValidPadId(newPadId)) {
|
||||||
// that a driver (like the mysql driver) can hardcode these values.
|
throw new Error('Cannot create a pad with that id as it is invalid');
|
||||||
db.db.db.settings = {cache: 0, writeInterval: 0, json: true};
|
}
|
||||||
// Validate the newPadId if specified and that a pad with that ID does
|
const exists = await PadManager.doesPadExist(newPadId);
|
||||||
// not already exist to avoid overwriting it.
|
if (exists) throw new Error('Cannot create a pad with that id as it already exists');
|
||||||
if (!PadManager.isValidPadId(newPadId)) {
|
|
||||||
throw new Error('Cannot create a pad with that id as it is invalid');
|
const oldPad = await PadManager.getPad(padId);
|
||||||
|
const newPad = new Pad(newPadId);
|
||||||
|
|
||||||
|
// Clone all Chat revisions
|
||||||
|
const chatHead = oldPad.chatHead;
|
||||||
|
await Promise.all([...Array(chatHead + 1).keys()].map(async (i) => {
|
||||||
|
const chat = await db.get(`pad:${padId}:chat:${i}`);
|
||||||
|
await db.set(`pad:${newPadId}:chat:${i}`, chat);
|
||||||
|
console.log(`Created: Chat Revision: pad:${newPadId}:chat:${i}`);
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Rebuild Pad from revisions up to and including the new revision head
|
||||||
|
const AuthorManager = require('ep_etherpad-lite/node/db/AuthorManager');
|
||||||
|
const Changeset = require('ep_etherpad-lite/static/js/Changeset');
|
||||||
|
// 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
|
||||||
|
newPad.pool.numToAttrib = oldPad.pool.numToAttrib;
|
||||||
|
for (let curRevNum = 0; curRevNum <= newRevHead; curRevNum++) {
|
||||||
|
const rev = await db.get(`pad:${padId}:revs:${curRevNum}`);
|
||||||
|
if (rev.meta) {
|
||||||
|
throw new Error('The specified revision number could not be found.');
|
||||||
}
|
}
|
||||||
util.callbackify(PadManager.doesPadExist)(newPadId, (err, exists) => {
|
const newRevNum = ++newPad.head;
|
||||||
if (err != null) return callback(err);
|
const newRevId = `pad:${newPad.id}:revs:${newRevNum}`;
|
||||||
if (exists) throw new Error('Cannot create a pad with that id as it already exists');
|
await Promise.all([
|
||||||
callback();
|
db.set(newRevId, rev),
|
||||||
});
|
AuthorManager.addPad(rev.meta.author, newPad.id),
|
||||||
},
|
]);
|
||||||
(callback) => {
|
newPad.atext = Changeset.applyToAText(rev.changeset, newPad.atext, newPad.pool);
|
||||||
util.callbackify(PadManager.getPad)(padId, (err, pad) => {
|
console.log(`Created: Revision: pad:${newPad.id}:revs:${newRevNum}`);
|
||||||
if (err) return callback(err);
|
}
|
||||||
oldPad = pad;
|
|
||||||
newPad = new Pad(newPadId);
|
// Add saved revisions up to the new revision head
|
||||||
callback();
|
console.log(newPad.head);
|
||||||
});
|
const newSavedRevisions = [];
|
||||||
},
|
for (const savedRev of oldPad.savedRevisions) {
|
||||||
(callback) => {
|
if (savedRev.revNum <= newRevHead) {
|
||||||
// Clone all Chat revisions
|
newSavedRevisions.push(savedRev);
|
||||||
const chatHead = oldPad.chatHead;
|
console.log(`Added: Saved Revision: ${savedRev.revNum}`);
|
||||||
for (let i = 0, curHeadNum = 0; i <= chatHead; i++) {
|
|
||||||
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}`);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
callback();
|
}
|
||||||
},
|
newPad.savedRevisions = newSavedRevisions;
|
||||||
(callback) => {
|
|
||||||
// Rebuild Pad from revisions up to and including the new revision head
|
|
||||||
const AuthorManager = require('ep_etherpad-lite/node/db/AuthorManager');
|
|
||||||
const Changeset = require('ep_etherpad-lite/static/js/Changeset');
|
|
||||||
// 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
|
|
||||||
newPad.pool.numToAttrib = oldPad.pool.numToAttrib;
|
|
||||||
for (let curRevNum = 0; curRevNum <= newRevHead; curRevNum++) {
|
|
||||||
db.db.get(`pad:${padId}:revs:${curRevNum}`, (err, rev) => {
|
|
||||||
if (rev.meta) {
|
|
||||||
throw new Error('The specified revision number could not be found.');
|
|
||||||
}
|
|
||||||
const newRevNum = ++newPad.head;
|
|
||||||
const newRevId = `pad:${newPad.id}:revs:${newRevNum}`;
|
|
||||||
db.db.set(newRevId, rev);
|
|
||||||
AuthorManager.addPad(rev.meta.author, newPad.id);
|
|
||||||
newPad.atext = Changeset.applyToAText(rev.changeset, newPad.atext, newPad.pool);
|
|
||||||
console.log(`Created: Revision: pad:${newPad.id}:revs:${newRevNum}`);
|
|
||||||
if (newRevNum === newRevHead) {
|
|
||||||
callback();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
|
||||||
(callback) => {
|
|
||||||
// Add saved revisions up to the new revision head
|
|
||||||
console.log(newPad.head);
|
|
||||||
const newSavedRevisions = [];
|
|
||||||
for (const savedRev of oldPad.savedRevisions) {
|
|
||||||
if (savedRev.revNum <= newRevHead) {
|
|
||||||
newSavedRevisions.push(savedRev);
|
|
||||||
console.log(`Added: Saved Revision: ${savedRev.revNum}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
newPad.savedRevisions = newSavedRevisions;
|
|
||||||
callback();
|
|
||||||
},
|
|
||||||
// Save the source pad
|
// Save the source pad
|
||||||
(callback) => db.db.set(`pad:${newPadId}`, newPad, callback),
|
await db.set(`pad:${newPadId}`, newPad);
|
||||||
(callback) => {
|
|
||||||
console.log(`Created: Source Pad: pad:${newPadId}`);
|
console.log(`Created: Source Pad: pad:${newPadId}`);
|
||||||
util.callbackify(newPad.saveToDatabase.bind(newPad))(callback);
|
await newPad.saveToDatabase();
|
||||||
},
|
|
||||||
], (err) => {
|
|
||||||
if (err) throw err;
|
|
||||||
console.info('finished');
|
console.info('finished');
|
||||||
});
|
})();
|
||||||
|
|
Loading…
Reference in New Issue