<commit_before>/*! @license ©2015 <NAME> - Multimedia Lab / iMinds / Ghent University */

/** A TriplePatternFragmentsController responds to requests for fragments */

var Controller = require('./Controller'),
    url = require('url'),
    _ = require('lodash'),
    N3Util = require('n3').Util,
    Util = require('../Util');

// Creates a new TriplePatternFragmentsController
function TriplePatternFragmentsController(options) {
  if (!(this instanceof TriplePatternFragmentsController))
    return new TriplePatternFragmentsController(options);
  options = options || {};
  Controller.call(this, options);
  this._routers = options.routers || [];

  var timegates = options.timegates || {};
  this._timegateSettings = timegates.mementos || {};
  this._timegateBaseUrl = timegates.baseURL || '/timegate/';
}
Controller.extend(TriplePatternFragmentsController);

// Try to serve the requested fragment
TriplePatternFragmentsController.prototype._handleRequest = function (request, response, next) {
  // Create the query from the request by calling the fragment routers
  var requestParams = { url: request.parsedUrl },
      query = this._routers.reduce(function (query, router) {
    try { router.extractQueryParams(requestParams, query); }
    catch (e) { /* ignore routing errors */ }
    return query;
  }, { features: [] });

  // Execute the query on the data source
  var datasourceSettings = query.features.datasource && this._datasources[query.datasource];
  delete query.features.datasource;
  if (!datasourceSettings || !datasourceSettings.datasource.supportsQuery(query))
    return next();

  if (datasourceSettings.memento) {
    var mementodatetime = new Date(datasourceSettings.memento.interval[1]).toUTCString(),
        mementomap = {},
        origmap = {};

     _.forIn(this._timegateSettings, function (setting, key) {
       setting.versions.forEach(function (entry) {
         mementomap[entry] = key;
         origmap[entry] = setting.originalBaseURL;
       });
     });

     var origQuery = request.url.replace(/[^?]+/, ''),
         timegate = url.format(_.assign(request.parsedUrl, {pathname: this._timegateBaseUrl + mementomap[query.datasource]})),
         origurl = origmap[query.datasource] ?
                          origmap[query.datasource] + origQuery : url.format(_.assign(request.parsedUrl, {pathname: mementomap[query.datasource]}));

     response.setHeader('Link', '<' + origurl + '>;rel=original, <' + timegate + '>;rel=timegate');
     response.setHeader('Memento-Datetime', mementodatetime);
   } else {
     var tg = datasourceSettings.timegate,
         ogQuery = request.url.replace(/[^?]+/, '');

     if (tg === true) {
       tg = url.format(_.assign(request.parsedUrl, {pathname: this._timegateBaseUrl + query.datasource})),
       response.setHeader('Link', '<' + tg + '>;rel=timegate');
     } else if (tg !== null)
       response.setHeader('Link', '<' + tg + ogQuery + '>;rel=timegate');

   }
  // Write the query result
  var view = this._negotiateView('TriplePatternFragments', request, response),
      settings = this._createFragmentMetadata(request, query, datasourceSettings);
  settings.resultStream = datasourceSettings.datasource.select(query,
                          function (error) { error && next(error); });
  view.render(settings, request, response);
};

// Creates metadata about the requested fragment
TriplePatternFragmentsController.prototype._createFragmentMetadata =
function (request, query, datasourceSettings) {
  // TODO: these URLs should be generated by the routers
  var requestUrl = request.parsedUrl,
      // maintain the originally requested query string to avoid encoding differences
      origQuery = request.url.replace(/[^?]+/, ''),
      pageUrl = url.format(requestUrl).replace(/\?.*/, origQuery),
      paramsNoPage = _.omit(requestUrl.query, 'page'),
      currentPage = parseInt(requestUrl.query.page, 10) || 1,
      datasourceUrl = url.format(_.omit(requestUrl, 'query')),
      fragmentUrl = url.format(_.defaults({ query: paramsNoPage }, requestUrl)),
      fragmentPageUrlBase = fragmentUrl + (/\?/.test(fragmentUrl) ? '&' : '?') + 'page=',<commit_msg>Fixed bug rewriting request Url<commit_after>/*! @license ©2015 <NAME> - Multimedia Lab / iMinds / Ghent University */

/** A TriplePatternFragmentsController responds to requests for fragments */

var Controller = require('./Controller'),
    url = require('url'),
    _ = require('lodash'),
    N3Util = require('n3').Util,
    Util = require('../Util');

// Creates a new TriplePatternFragmentsController
function TriplePatternFragmentsController(options) {
  if (!(this instanceof TriplePatternFragmentsController))
    return new TriplePatternFragmentsController(options);
  options = options || {};
  Controller.call(this, options);
  this._routers = options.routers || [];

  var timegates = options.timegates || {};
  this._timegateSettings = timegates.mementos || {};
  this._timegateBaseUrl = timegates.baseURL || '/timegate/';
}
Controller.extend(TriplePatternFragmentsController);

// Try to serve the requested fragment
TriplePatternFragmentsController.prototype._handleRequest = function (request, response, next) {
  // Create the query from the request by calling the fragment routers
  var requestParams = { url: request.parsedUrl },
      query = this._routers.reduce(function (query, router) {
    try { router.extractQueryParams(requestParams, query); }
    catch (e) { /* ignore routing errors */ }
    return query;
  }, { features: [] });

  // Execute the query on the data source
  var datasourceSettings = query.features.datasource && this._datasources[query.datasource];
  delete query.features.datasource;
  if (!datasourceSettings || !datasourceSettings.datasource.supportsQuery(query))
    return next();

  if (datasourceSettings.memento) {
    var mementodatetime = new Date(datasourceSettings.memento.interval[1]).toUTCString(),
        map = {};

     _.forIn(this._timegateSettings, function (setting, key) {
       setting.versions.forEach(function (entry) {
         map[entry] = { memento: key, original: setting.originalBaseURL };
       });
     });

     var origQuery = request.url.replace(/[^?]+/, ''),
         timegate = url.format(_.defaults({pathname: this._timegateBaseUrl + map[query.datasource].memento}, request.parsedUrl)),
         origurl = map[query.datasource].original ?
                          map[query.datasource].original + origQuery : url.format(_.defaults({pathname: map[query.datasource].memento}, request.parsedUrl));

     response.setHeader('Link', '<' + origurl + '>;rel=original, <' + timegate + '>;rel=timegate');
     response.setHeader('Memento-Datetime', mementodatetime);
   } else {
     var tg = datasourceSettings.timegate,
         ogQuery = request.url.replace(/[^?]+/, '');

     if (tg === true) {
       tg = url.format(_.defaults({pathname: this._timegateBaseUrl + query.datasource}, request.parsedUrl));
       response.setHeader('Link', '<' + tg + '>;rel=timegate');
     } else if (tg !== null)
       response.setHeader('Link', '<' + tg + ogQuery + '>;rel=timegate');
   }
  // Write the query result
  var view = this._negotiateView('TriplePatternFragments', request, response),
      settings = this._createFragmentMetadata(request, query, datasourceSettings);
  settings.resultStream = datasourceSettings.datasource.select(query,
                          function (error) { error && next(error); });
  view.render(settings, request, response);
};

// Creates metadata about the requested fragment
TriplePatternFragmentsController.prototype._createFragmentMetadata =
function (request, query, datasourceSettings) {
  // TODO: these URLs should be generated by the routers
  var requestUrl = request.parsedUrl,
      // maintain the originally requested query string to avoid encoding differences
      origQuery = request.url.replace(/[^?]+/, ''),
      pageUrl = url.format(requestUrl).replace(/\?.*/, origQuery),
      paramsNoPage = _.omit(requestUrl.query, 'page'),
      currentPage = parseInt(requestUrl.query.page, 10) || 1,
      datasourceUrl = url.format(_.omit(requestUrl, 'query')),
      fragmentUrl = url.format(_.defaults({ query: paramsNoPage }, requestUrl)),
      fragmentPageUrlBase = fragmentUrl + (/\?/.test(fragmentUrl) ? '&' : '?') + 'page=',
{'fraction_non_alphanumeric': 0.14053859100745372, 'fraction_numerical': 0.0024044241404183697, 'mean_word_length': 3.876318874560375, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6535299', 'n_tokens_mistral': 2440, 'n_tokens_neox': 2349, 'n_words': 517}