mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-01-31 19:02:59 +01:00
Make changes based on code review
Simplified the cloning process, added validation checks to ensure the new pad ID is valid and that a pad doesn't already exist with that ID. Also fixed a bug in the chatHead cloning loop and added the ability to specify a pad ID on the command the line (defaulting to the original "-rebuilt" pad ID formula)
This commit is contained in:
parent
25ccb6cfc3
commit
c7b1aebfe8
1 changed files with 38 additions and 39 deletions
|
@ -3,8 +3,8 @@
|
||||||
known "good" revision.
|
known "good" revision.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if(process.argv.length != 4) {
|
if(process.argv.length != 4 && process.argv.length != 5) {
|
||||||
console.error("Use: node bin/repairPad.js $PADID $REV");
|
console.error("Use: node bin/repairPad.js $PADID $REV [$NEWPADID]");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,10 +14,10 @@ var ueberDB = require("../src/node_modules/ueberDB");
|
||||||
|
|
||||||
var padId = process.argv[2];
|
var padId = process.argv[2];
|
||||||
var newRevHead = process.argv[3];
|
var newRevHead = process.argv[3];
|
||||||
var newPadId = padId + "-rebuilt";
|
var newPadId = process.argv[4] || padId + "-rebuilt";
|
||||||
|
|
||||||
var db, pad, newPad, settings;
|
var db, oldPad, newPad, settings;
|
||||||
var AuthorManager, ChangeSet, PadManager;
|
var AuthorManager, ChangeSet, Pad, PadManager;
|
||||||
|
|
||||||
async.series([
|
async.series([
|
||||||
function(callback) {
|
function(callback) {
|
||||||
|
@ -35,26 +35,37 @@ async.series([
|
||||||
db = require('../src/node/db/DB');
|
db = require('../src/node/db/DB');
|
||||||
db.init(callback);
|
db.init(callback);
|
||||||
}, function(callback) {
|
}, function(callback) {
|
||||||
|
PadManager = require('../src/node/db/PadManager');
|
||||||
|
Pad = require('../src/node/db/Pad').Pad;
|
||||||
// Get references to the original pad and to a newly created pad
|
// Get references to the original pad and to a newly created pad
|
||||||
// HACK: This is a standalone script, so we want to write everything
|
// HACK: This is a standalone script, so we want to write everything
|
||||||
// out to the database immediately. The only problem with this is
|
// out to the database immediately. The only problem with this is
|
||||||
// that a driver (like the mysql driver) can hardcode these values.
|
// that a driver (like the mysql driver) can hardcode these values.
|
||||||
db.db.db.settings = {cache: 0, writeInterval: 0, json: true};
|
db.db.db.settings = {cache: 0, writeInterval: 0, json: true};
|
||||||
PadManager = require('../src/node/db/PadManager');
|
// Validate the newPadId if specified and that a pad with that ID does
|
||||||
PadManager.getPad(padId, function(err, _pad) {
|
// not already exist to avoid overwriting it.
|
||||||
pad = _pad;
|
if (!PadManager.isValidPadId(newPadId)) {
|
||||||
PadManager.getPad(newPadId, function(err, _newPad) {
|
console.error("Cannot create a pad with that id as it is invalid");
|
||||||
newPad = _newPad;
|
process.exit(1);
|
||||||
callback();
|
}
|
||||||
|
PadManager.doesPadExists(newPadId, function(err, exists) {
|
||||||
|
if (exists) {
|
||||||
|
console.error("Cannot create a pad with that id as it already exists");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
PadManager.getPad(padId, function(err, pad) {
|
||||||
|
oldPad = pad;
|
||||||
|
newPad = new Pad(newPadId);
|
||||||
|
callback();
|
||||||
});
|
});
|
||||||
}, function(callback) {
|
}, function(callback) {
|
||||||
// Clone all Chat revisions
|
// Clone all Chat revisions
|
||||||
var chatHead = pad.chatHead;
|
var chatHead = oldPad.chatHead;
|
||||||
for(var i = 0; i <= chatHead; i++) {
|
for(var i = 0, curHeadNum = 0; i <= chatHead; i++) {
|
||||||
db.db.get("pad:" + padId + ":chat:" + i, function (err, chat) {
|
db.db.get("pad:" + padId + ":chat:" + i, function (err, chat) {
|
||||||
db.db.set("pad:" + newPadId + ":chat:" + i, chat);
|
db.db.set("pad:" + newPadId + ":chat:" + curHeadNum++, chat);
|
||||||
console.log("Created: Chat Revision: pad:" + newPadId + ":chat:" + i)
|
console.log("Created: Chat Revision: pad:" + newPadId + ":chat:" + curHeadNum);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
callback();
|
callback();
|
||||||
|
@ -65,28 +76,16 @@ async.series([
|
||||||
// Author attributes are derived from changesets, but there can also be
|
// Author attributes are derived from changesets, but there can also be
|
||||||
// non-author attributes with specific mappings that changesets depend on
|
// non-author attributes with specific mappings that changesets depend on
|
||||||
// and, AFAICT, cannot be recreated any other way
|
// and, AFAICT, cannot be recreated any other way
|
||||||
newPad.pool.numToAttrib = pad.pool.numToAttrib;
|
newPad.pool.numToAttrib = oldPad.pool.numToAttrib;
|
||||||
for(var i = 1; i <= newRevHead; i++) {
|
for(var curRevNum = 0; curRevNum <= newRevHead; curRevNum++) {
|
||||||
db.db.get("pad:" + padId + ":revs:" + i, function(err, rev) {
|
db.db.get("pad:" + padId + ":revs:" + curRevNum, function(err, rev) {
|
||||||
var author = rev.meta.author;
|
var newRevNum = ++newPad.head;
|
||||||
var changeset = rev.changeset;
|
var newRevId = "pad:" + newPad.id + ":revs:" + newRevNum;
|
||||||
var newRev = ++newPad.head;
|
|
||||||
var newRevId = "pad:" + newPad.id + ":revs:" + newRev;
|
|
||||||
var newAtext = Changeset.applyToAText(changeset, newPad.atext, newPad.pool);
|
|
||||||
|
|
||||||
AuthorManager.addPad(author, newPad.id);
|
|
||||||
newPad.pool.putAttrib(['author', author || '']);
|
|
||||||
|
|
||||||
Changeset.copyAText(newAtext, newPad.atext);
|
|
||||||
|
|
||||||
db.db.set(newRevId, rev);
|
db.db.set(newRevId, rev);
|
||||||
if(newRev % 100 == 0) {
|
AuthorManager.addPad(rev.meta.author, newPad.id);
|
||||||
db.db.setSub(newRevId, ["meta", "atext"], newPad.atext)
|
newPad.atext = Changeset.applyToAText(rev.changeset, newPad.atext, newPad.pool);
|
||||||
}
|
console.log("Created: Revision: pad:" + newPad.id + ":revs:" + newRevNum);
|
||||||
|
if (newRevNum == newRevHead) {
|
||||||
console.log("Created: Revision: pad:" + newPad.id + ":revs:" + newRev);
|
|
||||||
|
|
||||||
if (newRev == newRevHead) {
|
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -95,8 +94,8 @@ async.series([
|
||||||
// Add saved revisions up to the new revision head
|
// Add saved revisions up to the new revision head
|
||||||
console.log(newPad.head);
|
console.log(newPad.head);
|
||||||
var newSavedRevisions = [];
|
var newSavedRevisions = [];
|
||||||
for(var i in pad.savedRevisions) {
|
for(var i in oldPad.savedRevisions) {
|
||||||
savedRev = pad.savedRevisions[i]
|
savedRev = oldPad.savedRevisions[i]
|
||||||
if (savedRev.revNum <= newRevHead) {
|
if (savedRev.revNum <= newRevHead) {
|
||||||
newSavedRevisions.push(savedRev);
|
newSavedRevisions.push(savedRev);
|
||||||
console.log("Added: Saved Revision: " + savedRev.revNum);
|
console.log("Added: Saved Revision: " + savedRev.revNum);
|
||||||
|
|
Loading…
Reference in a new issue