aboutsummaryrefslogtreecommitdiffstats
path: root/etherpad/src/etherpad/control/historycontrol.js
blob: a78cfadd462447af77d62dc50519cf2207af04f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
 * Copyright 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import("fastJSON");
import("etherpad.utils.render404");
import("etherpad.pad.model");
import("etherpad.collab.collab_server");
import("etherpad.collab.ace.easysync2.*");
import("jsutils.eachProperty");

function _urlCache() {
  if (!appjet.cache.historyUrlCache) {
    appjet.cache.historyUrlCache = {};
  }
  return appjet.cache.historyUrlCache;
}

function _replyWithJSONAndCache(obj) {
  obj.apiversion = _VERSION;
  var output = fastJSON.stringify(obj);
  _urlCache()[request.path] = output;
  response.write(output);
  response.stop();  
}

function _replyWithJSON(obj) {
  obj.apiversion = _VERSION;
  response.write(fastJSON.stringify(obj));
  response.stop();
}

function _error(msg, num) {
  _replyWithJSON({error: String(msg), errornum: num});
}

var _VERSION = 1;

var _ERROR_REVISION_NUMBER_TOO_LARGE = 14;

function _do_text(padId, r) {
  if (! padId) render404();
  model.accessPadGlobal(padId, function(pad) {
    if (! pad.exists()) {
      render404();
    }
    if (r > pad.getHeadRevisionNumber()) {
      _error("Revision number too large", _ERROR_REVISION_NUMBER_TOO_LARGE);
    }
    var text = pad.getInternalRevisionText(r);
    text = _censorText(text);
    _replyWithJSONAndCache({ text: text });
  });
}

function _do_stat(padId) {
  var obj = {};
  if (! padId) {
    obj.exists = false;
  }
  else {
    model.accessPadGlobal(padId, function(pad) {
      if (! pad.exists()) {
        obj.exists = false;
      }
      else {
        obj.exists = true;
        obj.latestRev = pad.getHeadRevisionNumber();
      }
    });
  }
  _replyWithJSON(obj);
}

function _censorText(text) {
  // may not change length of text
  return text.replace(/(http:\/\/pad.spline.inf.fu-berlin.de\/)(\w+)/g, function(url, u1, u2) {
    return u1 + u2.replace(/\w/g, '-');
  });
}

function _do_changes(padId, first, last) {
  if (! padId) render404();
  
  var charPool = [];
  var changeList = [];

  function charPoolText(txt) {
    charPool.push(txt);
    return _encodeVarInt(txt.length);
  }
  
  model.accessPadGlobal(padId, function(pad) {

    if (first > pad.getHeadRevisionNumber() || last > pad.getHeadRevisionNumber()) {
      _error("Revision number too large", _ERROR_REVISION_NUMBER_TOO_LARGE);      
    }
    
    var curAText = Changeset.makeAText("\n");
    if (first > 0) {
      curAText = pad.getInternalRevisionAText(first - 1);
    }
    curAText.text = _censorText(curAText.text);
    var lastTimestamp = null;
    for(var r=first;r<=last;r++) {
      var binRev = [];
      var timestamp = +pad.getRevisionDate(r);
      binRev.push(_encodeTimeStamp(timestamp, lastTimestamp));
      lastTimestamp = timestamp;
      binRev.push(_encodeVarInt(1)); // fake author
      
      var c = pad.getRevisionChangeset(r);
      var splices = Changeset.toSplices(c);
      splices.forEach(function (splice) {
        var startChar = splice[0];
        var endChar = splice[1];
        var newText = splice[2];
        oldText = curAText.text.substring(startChar, endChar);
        
        if (oldText.length == 0) {
          binRev.push('+');
          binRev.push(_encodeVarInt(startChar));
          binRev.push(charPoolText(newText));
        }
        else if (newText.length == 0) {
          binRev.push('-');
          binRev.push(_encodeVarInt(startChar));
          binRev.push(charPoolText(oldText));
        }
        else {
          binRev.push('*');
          binRev.push(_encodeVarInt(startChar));
          binRev.push(charPoolText(oldText));
          binRev.push(charPoolText(newText));
        }
      });
      changeList.push(binRev.join(''));

      curAText = Changeset.applyToAText(c, curAText, pad.pool());
    }
    
    _replyWithJSONAndCache({charPool: charPool.join(''), changes: changeList.join(',')});
    
  });
}

function render_history(padOpaqueRef, rest) {
  if (_urlCache()[request.path]) {
    response.write(_urlCache()[request.path]);
    response.stop();
    return true;
  }
  var padId;
  if (padOpaqueRef == "CSi1xgbFXl" || padOpaqueRef == "13sentences") {
    // made-up, hard-coded opaque ref, should be a table for these
    padId = "jbg5HwzUX8";
  }
  else if (padOpaqueRef == "dO1j7Zf34z" || padOpaqueRef == "foundervisa") {
    // made-up, hard-coded opaque ref, should be a table for these
    padId = "3hS7kQyDXG";
  }
  else {
    padId = null;
  }
  var regexResult;
  if ((regexResult = /^stat$/.exec(rest))) {
    _do_stat(padId);
  }
  else if ((regexResult = /^text\/(\d+)$/.exec(rest))) {
    var r = Number(regexResult[1]);
    _do_text(padId, r);
  }
  else if ((regexResult = /^changes\/(\d+)-(\d+)$/.exec(rest))) {
    _do_changes(padId, Number(regexResult[1]), Number(regexResult[2]));
  }
  else {
    return false;
  }
}

function _encodeVarInt(num) {
  var n = +num;
  if (isNaN(n)) {
    throw new Error("Can't encode non-number "+num);
  }
  var chars = [];
  var done = false;
  while (! done) {
    if (n < 32) done = true;
    var nd = (n % 32);
    if (chars.length > 0) {
      // non-first, will become non-last digit
      nd = (nd | 32);
    }
    chars.push(_BASE64_DIGITS[nd]);
    n = Math.floor(n / 32)
  }
  return chars.reverse().join('');
}
var _BASE64_DIGITS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._";

function _encodeTimeStamp(tMillis, baseMillis) {
  var t = Math.floor(tMillis/1000);
  var base = Math.floor(baseMillis/1000);
  var absolute = ["+", t];
  var resultPair = absolute;
  if (((typeof base) == "number") && base <= t) {
    var relative = ["", t - base];
    if (relative[1] < absolute[1]) {
      resultPair = relative;
    }
  }
  return resultPair[0] + _encodeVarInt(resultPair[1]);
}