<commit_before>var router = require("express").Router();
var renderer = require('../lib/renderer');
var xidb = require('../lib/xidb');
var assets = require('../lib/assets');
var fs = require("fs");
var moment = require("moment");
var path = require('path');

router.get("/api/v1/asset/:xid/:cid*", _apiv1GetAsset);
router.get("/api/v1/meta/:xid/:cid*", _apiv1GetMetadata);
router.get("/api/v1/versions/:xid*", _apiv1GetVersions);

// wiki pages
router.get("/vpage/:xid/:cid", _viewPage);
router.get("/viki/:wiki/*", _getPage);
router.get("/v/:wiki/:version/*", _getVPage);

// metadata
router.get("/view/:xid/:cid", _viewAsset);
router.get("/edit/:xid/:cid", _editAsset);
router.post("/save/:xid/:cid", _saveAsset);
router.put("/save/new/markdown", _saveNewMarkdown);
router.put("/save/:xid/:cid/markdown", _saveMarkdown);

router.get("/meta", _getMeta);
router.get("/meta/:xid", _getAssetVersions);
router.get("/meta/:xid/:cid", _getMetaPage);
router.get("/meta/:xid/:cid/as/:format", _getAsFormat);
router.get("/meta/:xid/:cid/branch", _getBranch);

router.post("/comment/:xid/:cid", _newComment);
router.post("/vote/:xid/:cid", _newVote);

function _viewPage(req, res) {
  var xid = req.params.xid;
  var cid = req.params.cid;
  var xlink = xidb.createLink(xid, cid);
  var metadata = xidb.getMetadataFromLink(xlink);
  var url = "/v/wiki/" + cid + "/" + metadata.markdown.page;

  console.log(">>> _viewPage", xlink, metadata);
  console.log(">>> redirecting to", url);

  res.redirect(url);
}

function _getPage(req, res) {
  var wiki = req.params.wiki;
  var repoDir = xidb.getRepoGitDir(wiki);
  var cid = xidb.getHeadCommit(repoDir);
  var page = req.params['0'];
  var url = path.join("/v", wiki, cid.slice(0,8), page);

  console.log(">>> redirecting to", url);

  res.redirect(url);
}

function _getVPage(req, res) {
  var wiki = req.params.wiki;
  var cid = req.params.version;
  var page = req.params['0'];
  var file = page.replace(/ /g, "-") + '.md';
  var snapshot = xidb.getWikiSnapshot(wiki, cid);
  var xlink = xidb.getMetalink(snapshot, file, true);

  if (xlink == null) {
    // check for legacy versioned URL
    console.log(">>> check for legacy versioned URL");

    cid = path.basename(page);
    snapshot = xidb.getWikiSnapshot(wiki, cid);

    if (snapshot == null) {
      var pageTitle = page.replace("-", " ");

      res.render("page-new.jade", {
	'title': "New page",
	'pageTitle': pageTitle,
      });

      return;
    }

    page = path.dirname(page);
    file = page.replace(/ /g, "-") + '.md';
    xlink = xidb.getMetalink(snapshot, file, true);

    if (xlink != null) {
      var ver = cid.slice(0,8);
      var url = path.join("/v", wiki, ver, page);
      res.redirect(url);
    }
    else {
      console.log(">>> 404 not found");

      res.locals.title = "404 - Not found";
      res.statusCode = 404;
      res.render('404.jade');
    }

    return;
  }

  var metadata = xidb.getMetadataFromLink(xlink);
  var branch = xidb.getMetadataFromLink(metadata.base.branch);
  var repo = xidb.getBranchRepo(metadata.base.branch);
  var content = metadata.as.html;
  var snapshot = xidb.getWikiSnapshot(wiki, cid);
  var latestSnapshot = xidb.getLatestWikiSnapshot(wiki);
  var latestXlink = xidb.getMetalink(latestSnapshot, file, true);
  var comments = xidb.getComments(xlink);
  var votes = xidb.getVotes(xlink);
  var voteResults = xidb.getVoteResults(metadata, votes);
  var age;

  if (xlink != latestXlink) {
    age = moment(snapshot.commit.timestamp).fromNow();
  }
  else {
    age = "current";
  }

  //console.log(">>> _getVPage", metadata);

  res.render("page", {
    'title': metadata.asset.title,
    'page': metadata,
    'repo': repo,
    'commit': branch.commit,
    'age': age,
    'content': content,
    'comments': comments,
    'commentLink': "/comment/" + xlink,
    'votes': votes,
    'voteResults': voteResults,
    'voteLink': "/vote/" + xlink,
  });
}

function _apiv1GetAsset(req, res) {
  var xid = req.params.xid;
  var cid = req.params.cid;

  var metadata = xidb.getMetadata(xid, cid);

  xidb.getBlob(metadata.base.branch, metadata.asset.sha, function(err, content) {
    res.writeHead(200, {'Content-Type': metadata.type });
    res.end(content);
    return;
  });
}

function _apiv1GetMetadata(req, res) {
  var xid = req.params.xid;<commit_msg>Enabled edit menu on page view<commit_after>var router = require("express").Router();
var renderer = require('../lib/renderer');
var xidb = require('../lib/xidb');
var assets = require('../lib/assets');
var fs = require("fs");
var moment = require("moment");
var path = require('path');

router.get("/api/v1/asset/:xid/:cid*", _apiv1GetAsset);
router.get("/api/v1/meta/:xid/:cid*", _apiv1GetMetadata);
router.get("/api/v1/versions/:xid*", _apiv1GetVersions);

// wiki pages
router.get("/vpage/:xid/:cid", _viewPage);
router.get("/viki/:wiki/*", _getPage);
router.get("/v/:wiki/:version/*", _getVPage);

// metadata
router.get("/view/:xid/:cid", _viewAsset);
router.get("/edit/:xid/:cid", _editAsset);
router.post("/save/:xid/:cid", _saveAsset);
router.put("/save/new/markdown", _saveNewMarkdown);
router.put("/save/:xid/:cid/markdown", _saveMarkdown);

router.get("/meta", _getMeta);
router.get("/meta/:xid", _getAssetVersions);
router.get("/meta/:xid/:cid", _getMetaPage);
router.get("/meta/:xid/:cid/as/:format", _getAsFormat);
router.get("/meta/:xid/:cid/branch", _getBranch);

router.post("/comment/:xid/:cid", _newComment);
router.post("/vote/:xid/:cid", _newVote);

function _viewPage(req, res) {
  var xid = req.params.xid;
  var cid = req.params.cid;
  var xlink = xidb.createLink(xid, cid);
  var metadata = xidb.getMetadataFromLink(xlink);
  var url = "/v/wiki/" + cid + "/" + metadata.markdown.page;

  console.log(">>> _viewPage", xlink, metadata);
  console.log(">>> redirecting to", url);

  res.redirect(url);
}

function _getPage(req, res) {
  var wiki = req.params.wiki;
  var repoDir = xidb.getRepoGitDir(wiki);
  var cid = xidb.getHeadCommit(repoDir);
  var page = req.params['0'];
  var url = path.join("/v", wiki, cid.slice(0,8), page);

  console.log(">>> redirecting to", url);

  res.redirect(url);
}

function _getVPage(req, res) {
  var wiki = req.params.wiki;
  var cid = req.params.version;
  var page = req.params['0'];
  var file = page.replace(/ /g, "-") + '.md';
  var snapshot = xidb.getWikiSnapshot(wiki, cid);
  var xlink = xidb.getMetalink(snapshot, file, true);

  if (xlink == null) {
    // check for legacy versioned URL
    console.log(">>> check for legacy versioned URL");

    cid = path.basename(page);
    snapshot = xidb.getWikiSnapshot(wiki, cid);

    if (snapshot == null) {
      var pageTitle = page.replace("-", " ");

      res.render("page-new.jade", {
	'title': "New page",
	'pageTitle': pageTitle,
      });

      return;
    }

    page = path.dirname(page);
    file = page.replace(/ /g, "-") + '.md';
    xlink = xidb.getMetalink(snapshot, file, true);

    if (xlink != null) {
      var ver = cid.slice(0,8);
      var url = path.join("/v", wiki, ver, page);
      res.redirect(url);
    }
    else {
      console.log(">>> 404 not found");

      res.locals.title = "404 - Not found";
      res.statusCode = 404;
      res.render('404.jade');
    }

    return;
  }

  var metadata = xidb.getMetadataFromLink(xlink);
  var branch = xidb.getMetadataFromLink(metadata.base.branch);
  var repo = xidb.getBranchRepo(metadata.base.branch);
  var content = metadata.as.html;
  var snapshot = xidb.getWikiSnapshot(wiki, cid);
  var latestSnapshot = xidb.getLatestWikiSnapshot(wiki);
  var latestXlink = xidb.getMetalink(latestSnapshot, file, true);
  var comments = xidb.getComments(xlink);
  var votes = xidb.getVotes(xlink);
  var voteResults = xidb.getVoteResults(metadata, votes);
  var age;

  if (xlink != latestXlink) {
    age = moment(snapshot.commit.timestamp).fromNow();
  }
  else {
    age = "current";
  }

  //console.log(">>> _getVPage", metadata);

  res.locals.canEdit = true;

  res.render("page", {
    'title': metadata.asset.title,
    'page': metadata,
    'repo': repo,
    'commit': branch.commit,
    'age': age,
    'content': content,
    'comments': comments,
    'commentLink': "/comment/" + xlink,
    'votes': votes,
    'voteResults': voteResults,
    'voteLink': "/vote/" + xlink,
  });
}

function _apiv1GetAsset(req, res) {
  var xid = req.params.xid;
  var cid = req.params.cid;

  var metadata = xidb.getMetadata(xid, cid);

  xidb.getBlob(metadata.base.branch, metadata.asset.sha, function(err, content) {
    res.writeHead(200, {'Content-Type': metadata.type });
    res.end(content);
    return;
  });
}

function _apiv1GetMetadata(req, res) {
  var xid = req.params.xid;
{'fraction_non_alphanumeric': 0.18893107812679782, 'fraction_numerical': 0.006673570360142676, 'mean_word_length': 3.6731182795698927, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, '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': '1249768', 'n_tokens_mistral': 3217, 'n_tokens_neox': 3256, 'n_words': 626}