<commit_before>package app

import service._
import util.{WritableRepositoryAuthenticator, ReadableRepositoryAuthenticator, JGitUtil}
import util.Directory._
import jp.sf.amateras.scalatra.forms._

class WikiController extends WikiControllerBase 
  with WikiService with RepositoryService with AccountService with WritableRepositoryAuthenticator with ReadableRepositoryAuthenticator

trait WikiControllerBase extends ControllerBase {
  self: WikiService with RepositoryService with WritableRepositoryAuthenticator with ReadableRepositoryAuthenticator =>

  // TODO ユーザ名の先頭に_は使えないようにする
  case class WikiPageEditForm(pageName: String, content: String, message: Option[String], currentPageName: String)
  
  val newForm = mapping(
    "pageName"        -> trim(label("Page name"          , text(required, maxlength(40), pageName, unique))), 
    "content"         -> trim(label("Content"            , text(required))),
    "message"         -> trim(label("Message"            , optional(text()))),
    "currentPageName" -> trim(label("Current page name"  , text()))
  )(WikiPageEditForm.apply)
  
  val editForm = mapping(
    "pageName"        -> trim(label("Page name"          , text(required, maxlength(40), pageName))), 
    "content"         -> trim(label("Content"            , text(required))),
    "message"         -> trim(label("Message"            , optional(text()))),
    "currentPageName" -> trim(label("Current page name"  , text(required)))
  )(WikiPageEditForm.apply)
  
  get("/:owner/:repository/wiki")(readableRepository {
    val owner      = params("owner")
    val repository = params("repository")
    
    getWikiPage(owner, repository, "Home") match {
      case Some(page) => wiki.html.wiki("Home", page, getRepository(owner, repository, servletContext).get, isWritable(owner, repository))
      case None => redirect("/%s/%s/wiki/Home/_edit".format(owner, repository))
    }
  })
  
  get("/:owner/:repository/wiki/:page")(readableRepository {
    val owner      = params("owner")
    val repository = params("repository")
    val pageName   = params("page")
    
    getWikiPage(owner, repository, pageName) match {
      case Some(page) => wiki.html.wiki(pageName, page, getRepository(owner, repository, servletContext).get, isWritable(owner, repository))
      case None => redirect("/%s/%s/wiki/%s/_edit".format(owner, repository, pageName)) // TODO URLEncode
    }
  })
  
  get("/:owner/:repository/wiki/:page/_history")(readableRepository {
    val owner      = params("owner")
    val repository = params("repository")
    val page       = params("page")
    
    JGitUtil.withGit(getWikiRepositoryDir(owner, repository)){ git =>
      wiki.html.wikihistory(Some(page),
        JGitUtil.getCommitLog(git, "master", path = page + ".md")._1, getRepository(owner, repository, servletContext).get)
    }
  })
  
  get("/:owner/:repository/wiki/:page/_compare/:commitId")(readableRepository {
    val owner      = params("owner")
    val repository = params("repository")
    val page       = params("page")
    val commitId   = params("commitId").split("\\.\\.\\.")
    
    JGitUtil.withGit(getWikiRepositoryDir(owner, repository)){ git =>
      wiki.html.wikicompare(Some(page),
        getWikiDiffs(git, commitId(0), commitId(1)), getRepository(owner, repository, servletContext).get)
    }
  })
  
  get("/:owner/:repository/wiki/_compare/:commitId")(readableRepository {
    val owner      = params("owner")
    val repository = params("repository")
    val commitId   = params("commitId").split("\\.\\.\\.")
    
    JGitUtil.withGit(getWikiRepositoryDir(owner, repository)){ git =>
      wiki.html.wikicompare(None,
        getWikiDiffs(git, commitId(0), commitId(1)), getRepository(owner, repository, servletContext).get)
    }
  })
  
  get("/:owner/:repository/wiki/:page/_edit")(writableRepository {
    val owner      = params("owner")
    val repository = params("repository")
    val page       = params("page")
    
    wiki.html.wikiedit(page, 
        getWikiPage(owner, repository, page), getRepository(owner, repository, servletContext).get)
  })
  
  post("/:owner/:repository/wiki/_edit", editForm)(writableRepository { form =>
    val owner      = params("owner")
    val repository = params("repository")
    
    saveWikiPage(owner, repository, form.currentPageName, form.pageName, 
        form.content, context.loginAccount.get, form.message.getOrElse(""))
    
    redirect("%s/%s/wiki/%s".format(owner, repository, form.pageName))
  })
  
  get("/:owner/:repository/wiki/_new")(writableRepository {
    val owner      = params("owner")
    val repository = params("repository")
    
    wiki.html.wikiedit("", None, getRepository(owner, repository, servletContext).get)
  })
  
  post("/:owner/:repository/wiki/_new", newForm)(writableRepository { form =>
    val owner      = params("owner")
    val repository = params("repository")
    
    saveWikiPage(owner, repository, form.currentPageName, form.pageName, 
        form.content, context.loginAccount.get, form.message.getOrElse(""))
    
    redirect("%s/%s/wiki/%s".format(owner, repository, form.pageName))
  })
  
  get("/:owner/:repository/wiki/:page/_delete")(writableRepository {
    val owner      = params("owner")
    val repository = params("repository")
    val page       = params("page")
    
    deleteWikiPage(owner, repository, page, context.loginAccount.get.userName, "Delete %s".format(page))
    
    redirect("%s/%s/wiki".format(owner, repository))
  })
  
  get("/:owner/:repository/wiki/_pages")(readableRepository {
    val owner      = params("owner")
    val repository = params("repository")
    
    wiki.html.wikipages(getWikiPageList(owner, repository), getRepository(owner, repository, servletContext).get, isWritable(owner, repository))
  })
  
  get("/:owner/:repository/wiki/_history")(readableRepository {
    val owner      = params("owner")
    val repository = params("repository")
    
    JGitUtil.withGit(getWikiRepositoryDir(owner, repository)){ git =>
      wiki.html.wikihistory(None,
        JGitUtil.getCommitLog(git, "master")._1, getRepository(owner, repository, servletContext).get)
    }
  })

  /**
   * Constraint for the wiki page name.
   */
  def pageName: Constraint = new Constraint(){
    def validate(name: String, value: String): Option[String] = {
      if(!value.matches("^[a-zA-Z0-9\\-_]+$")){
        Some("Page name contains invalid character.")
      } else {
        None
      }
    }
  }
  
  def isWritable(owner: String, repository: String): Boolean = {
    context.loginAccount match {
      case Some(a) if(a.userType == AccountService.Administrator) => true
      case Some(a) if(a.userName == owner) => true
      case Some(a) if(getCollaborators(owner, repository).contains(a.userName)) => true
      case _ => false
    }
  }
  
  def unique: Constraint = new Constraint(){
    def validate(name: String, value: String): Option[String] = {
      if(getWikiPageList(params("owner"), params("repository")).contains(value)){
        Some("Page already exists.")
      } else {
        None
      }
    }
  }<commit_msg>Add action which returns the file content in the wiki repository.<commit_after>package app

import service._
import util.{WritableRepositoryAuthenticator, ReadableRepositoryAuthenticator, JGitUtil}
import util.Directory._
import jp.sf.amateras.scalatra.forms._

class WikiController extends WikiControllerBase 
  with WikiService with RepositoryService with AccountService with WritableRepositoryAuthenticator with ReadableRepositoryAuthenticator

trait WikiControllerBase extends ControllerBase {
  self: WikiService with RepositoryService with WritableRepositoryAuthenticator with ReadableRepositoryAuthenticator =>

  // TODO ユーザ名の先頭に_は使えないようにする
  case class WikiPageEditForm(pageName: String, content: String, message: Option[String], currentPageName: String)
  
  val newForm = mapping(
    "pageName"        -> trim(label("Page name"          , text(required, maxlength(40), pageName, unique))), 
    "content"         -> trim(label("Content"            , text(required))),
    "message"         -> trim(label("Message"            , optional(text()))),
    "currentPageName" -> trim(label("Current page name"  , text()))
  )(WikiPageEditForm.apply)
  
  val editForm = mapping(
    "pageName"        -> trim(label("Page name"          , text(required, maxlength(40), pageName))), 
    "content"         -> trim(label("Content"            , text(required))),
    "message"         -> trim(label("Message"            , optional(text()))),
    "currentPageName" -> trim(label("Current page name"  , text(required)))
  )(WikiPageEditForm.apply)
  
  get("/:owner/:repository/wiki")(readableRepository {
    val owner      = params("owner")
    val repository = params("repository")
    
    getWikiPage(owner, repository, "Home") match {
      case Some(page) => wiki.html.wiki("Home", page, getRepository(owner, repository, servletContext).get, isWritable(owner, repository))
      case None => redirect("/%s/%s/wiki/Home/_edit".format(owner, repository))
    }
  })
  
  get("/:owner/:repository/wiki/:page")(readableRepository {
    val owner      = params("owner")
    val repository = params("repository")
    val pageName   = params("page")
    
    getWikiPage(owner, repository, pageName) match {
      case Some(page) => wiki.html.wiki(pageName, page, getRepository(owner, repository, servletContext).get, isWritable(owner, repository))
      case None => redirect("/%s/%s/wiki/%s/_edit".format(owner, repository, pageName)) // TODO URLEncode
    }
  })
  
  get("/:owner/:repository/wiki/:page/_history")(readableRepository {
    val owner      = params("owner")
    val repository = params("repository")
    val page       = params("page")
    
    JGitUtil.withGit(getWikiRepositoryDir(owner, repository)){ git =>
      wiki.html.wikihistory(Some(page),
        JGitUtil.getCommitLog(git, "master", path = page + ".md")._1, getRepository(owner, repository, servletContext).get)
    }
  })
  
  get("/:owner/:repository/wiki/:page/_compare/:commitId")(readableRepository {
    val owner      = params("owner")
    val repository = params("repository")
    val page       = params("page")
    val commitId   = params("commitId").split("\\.\\.\\.")
    
    JGitUtil.withGit(getWikiRepositoryDir(owner, repository)){ git =>
      wiki.html.wikicompare(Some(page),
        getWikiDiffs(git, commitId(0), commitId(1)), getRepository(owner, repository, servletContext).get)
    }
  })
  
  get("/:owner/:repository/wiki/_compare/:commitId")(readableRepository {
    val owner      = params("owner")
    val repository = params("repository")
    val commitId   = params("commitId").split("\\.\\.\\.")
    
    JGitUtil.withGit(getWikiRepositoryDir(owner, repository)){ git =>
      wiki.html.wikicompare(None,
        getWikiDiffs(git, commitId(0), commitId(1)), getRepository(owner, repository, servletContext).get)
    }
  })
  
  get("/:owner/:repository/wiki/:page/_edit")(writableRepository {
    val owner      = params("owner")
    val repository = params("repository")
    val page       = params("page")
    
    wiki.html.wikiedit(page, 
        getWikiPage(owner, repository, page), getRepository(owner, repository, servletContext).get)
  })
  
  post("/:owner/:repository/wiki/_edit", editForm)(writableRepository { form =>
    val owner      = params("owner")
    val repository = params("repository")
    
    saveWikiPage(owner, repository, form.currentPageName, form.pageName, 
        form.content, context.loginAccount.get, form.message.getOrElse(""))
    
    redirect("%s/%s/wiki/%s".format(owner, repository, form.pageName))
  })
  
  get("/:owner/:repository/wiki/_new")(writableRepository {
    val owner      = params("owner")
    val repository = params("repository")
    
    wiki.html.wikiedit("", None, getRepository(owner, repository, servletContext).get)
  })
  
  post("/:owner/:repository/wiki/_new", newForm)(writableRepository { form =>
    val owner      = params("owner")
    val repository = params("repository")
    
    saveWikiPage(owner, repository, form.currentPageName, form.pageName, 
        form.content, context.loginAccount.get, form.message.getOrElse(""))
    
    redirect("%s/%s/wiki/%s".format(owner, repository, form.pageName))
  })
  
  get("/:owner/:repository/wiki/:page/_delete")(writableRepository {
    val owner      = params("owner")
    val repository = params("repository")
    val page       = params("page")
    
    deleteWikiPage(owner, repository, page, context.loginAccount.get.userName, "Delete %s".format(page))
    
    redirect("%s/%s/wiki".format(owner, repository))
  })
  
  get("/:owner/:repository/wiki/_pages")(readableRepository {
    val owner      = params("owner")
    val repository = params("repository")
    
    wiki.html.wikipages(getWikiPageList(owner, repository), getRepository(owner, repository, servletContext).get, isWritable(owner, repository))
  })
  
  get("/:owner/:repository/wiki/_history")(readableRepository {
    val owner      = params("owner")
    val repository = params("repository")
    
    JGitUtil.withGit(getWikiRepositoryDir(owner, repository)){ git =>
      wiki.html.wikihistory(None,
        JGitUtil.getCommitLog(git, "master")._1, getRepository(owner, repository, servletContext).get)
    }
  })

  get("/:owner/:repository/wiki/_blob/*")(readableRepository {
    val owner      = params("owner")
    val repository = params("repository")
    val path       = multiParams("splat").head

    contentType = "application/octet-stream"
    getFileContent(owner, repository, path).get
  })

  /**
   * Constraint for the wiki page name.
   */
  def pageName: Constraint = new Constraint(){
    def validate(name: String, value: String): Option[String] = {
      if(!value.matches("^[a-zA-Z0-9\\-_]+$")){
        Some("Page name contains invalid character.")
      } else {
        None
      }
    }
  }
  
  def isWritable(owner: String, repository: String): Boolean = {
    context.loginAccount match {
      case Some(a) if(a.userType == AccountService.Administrator) => true
      case Some(a) if(a.userName == owner) => true
      case Some(a) if(getCollaborators(owner, repository).contains(a.userName)) => true
      case _ => false
    }
  }
  
  def unique: Constraint = new Constraint(){
    def validate(name: String, value: String): Option[String] = {
      if(getWikiPageList(params("owner"), params("repository")).contains(value)){
        Some("Page already exists.")
      } else {
        None
      }
    }
  }
{'fraction_non_alphanumeric': 0.14517347990381313, 'fraction_numerical': 0.0016489178976296806, 'mean_word_length': 3.455463728191001, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 49, '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': '2386991', 'n_tokens_mistral': 4366, 'n_tokens_neox': 4297, 'n_words': 815}