<commit_before>/*jslint continue:true */
/*global require, exports*/

////////////////////////////////////////////////////////////////////////////////
/// @brief Foxx application store
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2015 triagens GmbH, Cologne, Germany
///
/// 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.
///
/// Copyright holder is triAGENS GmbH, Cologne, Germany
///
/// @author Dr. <NAME>, <NAME>
/// @author Copyright 2015, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////

(function() {
  "use strict";


// -----------------------------------------------------------------------------
// --SECTION--                                                  global variables
// -----------------------------------------------------------------------------

  var checkedFishBowl = false;

// -----------------------------------------------------------------------------
// --SECTION--                                                           imports
// -----------------------------------------------------------------------------

  var arangodb = require("org/arangodb");
  var db = arangodb.db;
  var download = require("internal").download;
  var fs = require("fs");
  var throwDownloadError = arangodb.throwDownloadError;
  var utils = require("org/arangodb/foxx/manager-utils");

// -----------------------------------------------------------------------------
// --SECTION--                                                 private functions
// -----------------------------------------------------------------------------

////////////////////////////////////////////////////////////////////////////////
/// @brief returns the fishbowl repository
////////////////////////////////////////////////////////////////////////////////

  function getFishbowlUrl () {
    return "arangodb/foxx-apps";
  }

////////////////////////////////////////////////////////////////////////////////
/// @brief returns the fishbowl collection
///
/// this will create the collection if it does not exist. this is better than
/// needlessly creating the collection for each database in case it is not
/// used in context of the database.
////////////////////////////////////////////////////////////////////////////////

  var getFishbowlStorage = function() {

    var c = db._collection('_fishbowl');
    if (c ===  null) {
      c = db._create('_fishbowl', { isSystem : true });
    }

    if (c !== null && ! checkedFishBowl) {
      // ensure indexes
      c.ensureFulltextIndex("description");
      c.ensureFulltextIndex("name");
      checkedFishBowl = true;
    }

    return c;
  };

////////////////////////////////////////////////////////////////////////////////
/// @brief comparator for applications
////////////////////////////////////////////////////////////////////////////////

  var compareApps =  function(l, r) {
    var left = l.name.toLowerCase();
    var right = r.name.toLowerCase();

    if (left < right) {
      return -1;
    }

    if (right < left) {
      return 1;
    }

    return 0;
  };

////////////////////////////////////////////////////////////////////////////////
/// @brief comparator for versions
////////////////////////////////////////////////////////////////////////////////

  var compareVersions = function (a, b) {
    var i;

    if (a === b) {
      return 0;
    }

    // error handling
    if (typeof a !== "string") {
      return -1;
    }
    if (typeof b !== "string") {
      return 1;
    }

    var aComponents = a.split(".");
    var bComponents = b.split(".");
    var len = Math.min(aComponents.length, bComponents.length);

    // loop while the components are equal
    for (i = 0; i < len; i++) {

      // A bigger than B
      if (parseInt(aComponents[i], 10) > parseInt(bComponents[i], 10)) {
        return 1;
      }

      // B bigger than A
      if (parseInt(aComponents[i], 10) < parseInt(bComponents[i], 10)) {
        return -1;
      }
    }

    // If one's a prefix of the other, the longer one is bigger one.
    if (aComponents.length > bComponents.length) {
      return 1;
    }

    if (aComponents.length < bComponents.length) {
      return -1;
    }

    // Otherwise they are the same.
    return 0;
  };


////////////////////////////////////////////////////////////////////////////////
/// @brief updates the fishbowl from a zip archive
////////////////////////////////////////////////////////////////////////////////

  var updateFishbowlFromZip = function(filename) {
    var i;
    var tempPath = fs.getTempPath();
    var toSave = [ ];

    try {
      fs.makeDirectoryRecursive(tempPath);
      var root = fs.join(tempPath, "foxx-apps-master/applications");

      // remove any previous files in the directory
      fs.listTree(root).forEach(function (file) {
        if (file.match(/\.json$/)) {
          try {
            fs.remove(fs.join(root, file));
          }
          catch (ignore) {
          }
        }
      });

      fs.unzipFile(filename, tempPath, false, true);

      if (! fs.exists(root)) {
        throw new Error("'applications' directory is missing in foxx-apps-master, giving up");
      }

      var m = fs.listTree(root);
      var reSub = /(.*)\.json$/;
      var f, match, app, desc;

      for (i = 0;  i < m.length;  ++i) {
        f = m[i];
        match = reSub.exec(f);

        if (match !== null) {
          continue;
        }

        app = fs.join(root, f);

        try {
          desc = JSON.parse(fs.read(app));
        }
        catch (err1) {
          arangodb.printf("Cannot parse description for app '" + f + "': %s\n", String(err1));<commit_msg>Removed foxx store and client foxx manager dependency on common/bootstrap/module<commit_after>/*jslint continue:true */
/*global require, exports*/

////////////////////////////////////////////////////////////////////////////////
/// @brief Foxx application store
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2015 triagens GmbH, Cologne, Germany
///
/// 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.
///
/// Copyright holder is triAGENS GmbH, Cologne, Germany
///
/// @author Dr. <NAME>, <NAME>
/// @author Copyright 2015, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////

(function() {
  "use strict";


// -----------------------------------------------------------------------------
// --SECTION--                                                  global variables
// -----------------------------------------------------------------------------

  var checkedFishBowl = false;

// -----------------------------------------------------------------------------
// --SECTION--                                                           imports
// -----------------------------------------------------------------------------

  var arangodb = require("org/arangodb");
  var db = arangodb.db;
  var download = require("internal").download;
  var fs = require("fs");
  var throwDownloadError = arangodb.throwDownloadError;
  var utils = require("org/arangodb/foxx/manager-utils");

// -----------------------------------------------------------------------------
// --SECTION--                                                 private functions
// -----------------------------------------------------------------------------

////////////////////////////////////////////////////////////////////////////////
/// @brief returns the fishbowl repository
////////////////////////////////////////////////////////////////////////////////

  function getFishbowlUrl () {
    return "arangodb/foxx-apps";
  }

////////////////////////////////////////////////////////////////////////////////
/// @brief returns the fishbowl collection
///
/// this will create the collection if it does not exist. this is better than
/// needlessly creating the collection for each database in case it is not
/// used in context of the database.
////////////////////////////////////////////////////////////////////////////////

  var getFishbowlStorage = function() {

    var c = db._collection('_fishbowl');
    if (c ===  null) {
      c = db._create('_fishbowl', { isSystem : true });
    }

    if (c !== null && ! checkedFishBowl) {
      // ensure indexes
      c.ensureFulltextIndex("description");
      c.ensureFulltextIndex("name");
      checkedFishBowl = true;
    }

    return c;
  };

////////////////////////////////////////////////////////////////////////////////
/// @brief comparator for applications
////////////////////////////////////////////////////////////////////////////////

  var compareApps =  function(l, r) {
    var left = l.name.toLowerCase();
    var right = r.name.toLowerCase();

    if (left < right) {
      return -1;
    }

    if (right < left) {
      return 1;
    }

    return 0;
  };

////////////////////////////////////////////////////////////////////////////////
/// @brief comparator for versions
////////////////////////////////////////////////////////////////////////////////

  var compareVersions = function (a, b) {
    var i;

    if (a === b) {
      return 0;
    }

    // error handling
    if (typeof a !== "string") {
      return -1;
    }
    if (typeof b !== "string") {
      return 1;
    }

    var aComponents = a.split(".");
    var bComponents = b.split(".");
    var len = Math.min(aComponents.length, bComponents.length);

    // loop while the components are equal
    for (i = 0; i < len; i++) {

      // A bigger than B
      if (parseInt(aComponents[i], 10) > parseInt(bComponents[i], 10)) {
        return 1;
      }

      // B bigger than A
      if (parseInt(aComponents[i], 10) < parseInt(bComponents[i], 10)) {
        return -1;
      }
    }

    // If one's a prefix of the other, the longer one is bigger one.
    if (aComponents.length > bComponents.length) {
      return 1;
    }

    if (aComponents.length < bComponents.length) {
      return -1;
    }

    // Otherwise they are the same.
    return 0;
  };


////////////////////////////////////////////////////////////////////////////////
/// @brief updates the fishbowl from a zip archive
////////////////////////////////////////////////////////////////////////////////

  var updateFishbowlFromZip = function(filename) {
    var i;
    var tempPath = fs.getTempPath();
    var toSave = [ ];

    try {
      fs.makeDirectoryRecursive(tempPath);
      var root = fs.join(tempPath, "foxx-apps-master/applications");

      // remove any previous files in the directory
      fs.listTree(root).forEach(function (file) {
        if (file.match(/\.json$/)) {
          try {
            fs.remove(fs.join(root, file));
          }
          catch (ignore) {
          }
        }
      });

      fs.unzipFile(filename, tempPath, false, true);

      if (! fs.exists(root)) {
        throw new Error("'applications' directory is missing in foxx-apps-master, giving up");
      }

      var m = fs.listTree(root);
      var reSub = /(.*)\.json$/;
      var f, match, app, desc;

      for (i = 0;  i < m.length;  ++i) {
        f = m[i];
        match = reSub.exec(f);
        if (match !== null) {
          continue;
        }

        app = fs.join(root, f);

        try {
          desc = JSON.parse(fs.read(app));
        }
        catch (err1) {
          arangodb.printf("Cannot parse description for app '" + f + "': %s\n", String(err1));
{'fraction_non_alphanumeric': 0.33381619185578626, 'fraction_numerical': 0.005633349428617415, 'mean_word_length': 1.0442506991281462, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5207077', 'n_tokens_mistral': 3034, 'n_tokens_neox': 2864, 'n_words': 894}