Created
September 25, 2010 15:54
-
-
Save aribn/596981 to your computer and use it in GitHub Desktop.
Cloning an etherpad
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To create a clone of pad CWrp961Hnw as of revision 3: | |
// http://sketchpad.cc/sp/pad/newsketch?clonePadId=CWrp961Hnw&cloneRevNum=3 | |
// -------------------------------------------------------------------------------- | |
// in etherpad/src/etherpad/control/pad/pad_control.js | |
// -------------------------------------------------------------------------------- | |
import("etherpad.collab.collab_server.buildHistoricalAuthorDataMapForPadHistory"); | |
function render_newsketch() { | |
var session = getSession(); | |
var padId; | |
padId = randomUniquePadId(); | |
session.instantCreate = padId; | |
var query = ""; | |
if (request.query && request.query != "") { | |
query = "?"+request.query; | |
} | |
response.redirect("/"+padId+query); | |
} | |
function _createIfNecessary(localPadId, pad) { | |
if (pad.exists()) { | |
delete getSession().instantCreate; | |
return; | |
} | |
var validPadId = padutils.makeValidLocalPadId(localPadId); | |
if (localPadId != validPadId) { | |
response.redirect('/'+validPadId); | |
} | |
if (request.params.createImmediately || getSession().instantCreate == localPadId) { | |
var initialContent; | |
if (request.query) { | |
var clonePadId = getQueryVariable("clonePadId"); | |
var cloneRevNum = getQueryVariable("cloneRevNum"); | |
var usePadId; | |
if (isReadOnlyId(clonePadId)) { | |
usePadId = readonlyToPadId(clonePadId); | |
if (!pro_utils.isProDomainRequest()) { | |
usePadId = padutils.getGlobalPadId(usePadId); | |
} | |
} else { | |
usePadId = padutils.getGlobalPadId(clonePadId); | |
} | |
var padGlobal = usePadId; | |
var padText = model.accessPadGlobal(usePadId, function(pad) { | |
return pad.getRevisionText(cloneRevNum); | |
}, 'r'); | |
var historicalAuthorData = model.accessPadGlobal(usePadId, function(pad) { | |
return buildHistoricalAuthorDataMapForPadHistory(pad); | |
}, 'r'); | |
var list = formatAuthorData(historicalAuthorData); | |
var header = "// This sketch builds on a prior work created by " + list.join(" & ") + "\n"; | |
var priorUrl = "// http://" + request.host + "/sp/pad/view/"+clonePadId+"/rev."+cloneRevNum + "\n\n"; | |
pad.create(header + priorUrl + padText); | |
return; | |
} else { | |
initialContent = getDefaultPadText(); | |
} | |
pad.create(initialContent); | |
delete getSession().instantCreate; | |
return; | |
} | |
response.redirect("/sp/pad/create?padId="+encodeURIComponent(localPadId)); | |
} | |
function formatAuthorData(historicalAuthorData) { | |
var authors_all = []; | |
for (var author in historicalAuthorData) { | |
var n = historicalAuthorData[author].name; | |
authors_all[n] = (authors_all[n]) ? 1+authors_all[n] : 1; | |
} | |
var authors = []; | |
for (var n in authors_all) { | |
if (n == "undefined") { | |
authors.push("[unnamed author]"); | |
} else { | |
authors.push(n); | |
} | |
} | |
return authors; | |
} | |
function getQueryVariable(variable) { | |
var query = request.query; | |
var vars = query.split("&"); | |
for (var i = 0; i < vars.length; i++) { | |
var pair = vars[i].split("="); | |
if (pair[0] == variable) { | |
return pair[1]; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to unify handling of local vs global and r.o. vs r.w. variations.