<commit_before>/**
 * 2011 Peter 'Pita' Martischka
 *
 * 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.
 */

/*
The Pad Module trys to simulate the pad object from EtherPad. You can find the original code in /etherpad/src/etherpad/pad/model.js
see https://github.com/ether/pad/blob/master/etherpad/src/etherpad/pad/model.js
*/

var Changeset = require("./Changeset");
var AttributePoolFactory = require("./AttributePoolFactory");

/**
 * The initial Text of a Pad
 */
exports.startText = "Hello World\nGoodbye Etherpad";

/**
 * A Array with all known Pads
 */
globalPads = [];

/**
 * Return a Function Wrapper to work with the Pad
 * @param id A String with the id of the pad
 * @param createIfNotExist A Boolean which says the function if it should create the Pad if it not exist
 */
exports.getPad = function(id, createIfNotExist)
{  
  if(!globalPads[id] && createIfNotExist == true)
  {
    createPad(id);
  }
  
  if(!globalPads[id])
    return null;
  
  globalPads[id].timestamp = new Date().getTime();
  
  var functionWrapper = {};
  
  functionWrapper.id = id;
  functionWrapper.appendRevision = function (theChangeset, author) {return appendRevision(id, theChangeset, author)};
  functionWrapper.text = function () {return text(id)};
  functionWrapper.atext = function () {return atext(id)};
  functionWrapper.pool = function () {return pool(id)};
  functionWrapper.getHeadRevisionNumber = function () {return getHeadRevisionNumber(id)};
  functionWrapper.getRevisionChangeset = function (revNum) {return getRevisionChangeset(id, revNum)};
  functionWrapper.getRevisionAuthor = function (revNum) {return getRevisionAuthor(id, revNum)};
  functionWrapper.getAllAuthors = function () {return getAllAuthors(id)};
  
  return functionWrapper;
}

/**
 * Ensures that the Pad exists
 * @param id The Pad id
 */
exports.ensurePadExists = function(id)
{
  if(!globalPads[id])
  {
    createPad(id);
  }
}

/**
 * Creates an empty pad
 * @param id The Pad id
 */
function createPad(id)
{
  var pad = {};
  globalPads[id] = pad;
  
  pad.id = id;
  pad.rev = [];
  pad.head = -1;
  pad.atext = Changeset.makeAText("\n");
  pad.apool = AttributePoolFactory.createAttributePool();
  pad.authors = [];
  
  var firstChangeset = Changeset.makeSplice("\n", 0, 0,
                        exports.cleanText(exports.startText));                      
  appendRevision(id, firstChangeset, '');
}

/**
 * Append a changeset to a pad
 * @param id The Pad id
 * @param theChangeset the changeset which should apply to the text
 * @param The author of the revision, can be null
 */
function appendRevision(id, theChangeset, author)
{
  throwExceptionIfPadDontExist(id);

  if(!author)
    author = '';

  var atext = globalPads[id].atext;
  var apool = globalPads[id].apool;
  var newAText = Changeset.applyToAText(theChangeset, atext, apool);
  Changeset.copyAText(newAText, atext);
  
  var newRev = ++globalPads[id].head;
  globalPads[id].rev[newRev] = {};
  globalPads[id].rev[newRev].changeset = theChangeset;
  globalPads[id].rev[newRev].meta = {};
  globalPads[id].rev[newRev].meta.author = author;
  globalPads[id].rev[newRev].meta.timestamp = new Date().getTime();
  
  //ex. getNumForAuthor
  apool.putAttrib(['author',author||'']);
  
  if(newRev%100==0)
  {
    globalPads[id].rev[newRev].meta.atext=atext;
  }
}

/**
 * Returns all Authors of a Pad
 * @param id The Pad id<commit_msg>Don't add a anonymous author to the attribute pool, this prevents some wired bugs<commit_after>/**
 * 2011 Peter 'Pita' Martischka
 *
 * 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.
 */

/*
The Pad Module trys to simulate the pad object from EtherPad. You can find the original code in /etherpad/src/etherpad/pad/model.js
see https://github.com/ether/pad/blob/master/etherpad/src/etherpad/pad/model.js
*/

var Changeset = require("./Changeset");
var AttributePoolFactory = require("./AttributePoolFactory");

/**
 * The initial Text of a Pad
 */
exports.startText = "Hello World\nGoodbye Etherpad";

/**
 * A Array with all known Pads
 */
globalPads = [];

/**
 * Return a Function Wrapper to work with the Pad
 * @param id A String with the id of the pad
 * @param createIfNotExist A Boolean which says the function if it should create the Pad if it not exist
 */
exports.getPad = function(id, createIfNotExist)
{  
  if(!globalPads[id] && createIfNotExist == true)
  {
    createPad(id);
  }
  
  if(!globalPads[id])
    return null;
  
  globalPads[id].timestamp = new Date().getTime();
  
  var functionWrapper = {};
  
  functionWrapper.id = id;
  functionWrapper.appendRevision = function (theChangeset, author) {return appendRevision(id, theChangeset, author)};
  functionWrapper.text = function () {return text(id)};
  functionWrapper.atext = function () {return atext(id)};
  functionWrapper.pool = function () {return pool(id)};
  functionWrapper.getHeadRevisionNumber = function () {return getHeadRevisionNumber(id)};
  functionWrapper.getRevisionChangeset = function (revNum) {return getRevisionChangeset(id, revNum)};
  functionWrapper.getRevisionAuthor = function (revNum) {return getRevisionAuthor(id, revNum)};
  functionWrapper.getAllAuthors = function () {return getAllAuthors(id)};
  
  return functionWrapper;
}

/**
 * Ensures that the Pad exists
 * @param id The Pad id
 */
exports.ensurePadExists = function(id)
{
  if(!globalPads[id])
  {
    createPad(id);
  }
}

/**
 * Creates an empty pad
 * @param id The Pad id
 */
function createPad(id)
{
  var pad = {};
  globalPads[id] = pad;
  
  pad.id = id;
  pad.rev = [];
  pad.head = -1;
  pad.atext = Changeset.makeAText("\n");
  pad.apool = AttributePoolFactory.createAttributePool();
  pad.authors = [];
  
  var firstChangeset = Changeset.makeSplice("\n", 0, 0,
                        exports.cleanText(exports.startText));                      
  appendRevision(id, firstChangeset, '');
}

/**
 * Append a changeset to a pad
 * @param id The Pad id
 * @param theChangeset the changeset which should apply to the text
 * @param The author of the revision, can be null
 */
function appendRevision(id, theChangeset, author)
{
  throwExceptionIfPadDontExist(id);

  if(!author)
    author = '';

  var atext = globalPads[id].atext;
  var apool = globalPads[id].apool;
  var newAText = Changeset.applyToAText(theChangeset, atext, apool);
  Changeset.copyAText(newAText, atext);
  
  var newRev = ++globalPads[id].head;
  globalPads[id].rev[newRev] = {};
  globalPads[id].rev[newRev].changeset = theChangeset;
  globalPads[id].rev[newRev].meta = {};
  globalPads[id].rev[newRev].meta.author = author;
  globalPads[id].rev[newRev].meta.timestamp = new Date().getTime();
  
  //ex. getNumForAuthor
  if(author != '')
    apool.putAttrib(['author',author||'']);
  
  if(newRev%100==0)
  {
    globalPads[id].rev[newRev].meta.atext=atext;
  }
}

/**
 * Returns all Authors of a Pad
 * @param id The Pad id
{'fraction_non_alphanumeric': 0.1246984126984127, 'fraction_numerical': 0.0038095238095238095, 'mean_word_length': 3.8348680171884593, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3664852', 'n_tokens_mistral': 2527, 'n_tokens_neox': 2466, 'n_words': 707}