Files
Gogs/internal/database/wiki.go

207 lines
5.8 KiB
Go
Raw Permalink Normal View History

package database
2015-11-26 17:33:45 -05:00
import (
2016-03-04 13:32:17 -05:00
"net/url"
2015-11-27 01:50:38 -05:00
"os"
2015-11-27 00:24:24 -05:00
"path"
2015-11-26 17:33:45 -05:00
"path/filepath"
"strconv"
2015-11-26 17:33:45 -05:00
"strings"
"time"
"github.com/cockroachdb/errors"
2015-11-26 17:33:45 -05:00
2018-05-27 08:53:48 +08:00
"github.com/gogs/git-module"
2015-11-27 00:24:24 -05:00
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/osutil"
"gogs.io/gogs/internal/pathutil"
"gogs.io/gogs/internal/repoutil"
"gogs.io/gogs/internal/sync"
2015-11-26 17:33:45 -05:00
)
var wikiWorkingPool = sync.NewExclusivePool()
2015-11-27 00:24:24 -05:00
// WikiBranch returns the branch name used by the wiki repository. It checks if
// "main" branch exists, otherwise falls back to "master".
func WikiBranch(repoPath string) string {
if git.RepoHasBranch(repoPath, "main") {
return "main"
}
return "master"
}
2015-11-27 00:24:24 -05:00
// ToWikiPageURL formats a string to corresponding wiki URL name.
func ToWikiPageURL(name string) string {
return url.QueryEscape(name)
2015-11-26 17:33:45 -05:00
}
// ToWikiPageName formats a URL back to corresponding wiki page name. It enforces
// single-level hierarchy by replacing all "/" with spaces.
2015-12-20 21:13:12 +01:00
func ToWikiPageName(urlString string) string {
name, _ := url.QueryUnescape(urlString)
name = pathutil.Clean(name)
return strings.ReplaceAll(name, "/", " ")
2015-11-27 00:24:24 -05:00
}
2015-11-30 20:45:55 -05:00
// WikiCloneLink returns clone URLs of repository wiki.
//
// Deprecated: Use repoutil.NewCloneLink instead.
func (r *Repository) WikiCloneLink() (cl *repoutil.CloneLink) {
return r.cloneLink(true)
2015-11-30 20:45:55 -05:00
}
2015-11-26 17:33:45 -05:00
// WikiPath returns wiki data path by given user and repository name.
func WikiPath(userName, repoName string) string {
return filepath.Join(repoutil.UserPath(userName), strings.ToLower(repoName)+".wiki.git")
2015-11-26 17:33:45 -05:00
}
func (r *Repository) WikiPath() string {
return WikiPath(r.MustOwner().Name, r.Name)
2015-11-26 17:33:45 -05:00
}
// HasWiki returns true if repository has wiki.
func (r *Repository) HasWiki() bool {
return osutil.IsDir(r.WikiPath())
2015-11-26 17:33:45 -05:00
}
// InitWiki initializes a wiki for repository,
// it does nothing when repository already has wiki.
func (r *Repository) InitWiki() error {
if r.HasWiki() {
2015-11-26 17:33:45 -05:00
return nil
}
if err := git.Init(r.WikiPath(), git.InitOptions{Bare: true}); err != nil {
return errors.Newf("init repository: %v", err)
} else if err = createDelegateHooks(r.WikiPath()); err != nil {
return errors.Newf("createDelegateHooks: %v", err)
2015-11-26 17:33:45 -05:00
}
return nil
}
func (r *Repository) LocalWikiPath() string {
return filepath.Join(conf.Server.AppDataPath, "tmp", "local-wiki", strconv.FormatInt(r.ID, 10))
2015-11-27 00:24:24 -05:00
}
// UpdateLocalWiki makes sure the local copy of repository wiki is up-to-date.
func (r *Repository) UpdateLocalWiki() error {
wikiPath := r.WikiPath()
return UpdateLocalCopyBranch(wikiPath, r.LocalWikiPath(), WikiBranch(wikiPath), true)
2015-11-27 00:24:24 -05:00
}
2016-03-04 13:32:17 -05:00
func discardLocalWikiChanges(localPath string) error {
return discardLocalRepoBranchChanges(localPath, WikiBranch(localPath))
2016-03-04 13:32:17 -05:00
}
2015-11-27 01:50:38 -05:00
// updateWikiPage adds new page to repository wiki.
func (r *Repository) updateWikiPage(doer *User, oldTitle, title, content, message string, isNew bool) error {
wikiWorkingPool.CheckIn(strconv.FormatInt(r.ID, 10))
defer wikiWorkingPool.CheckOut(strconv.FormatInt(r.ID, 10))
2015-11-27 00:24:24 -05:00
if err := r.InitWiki(); err != nil {
return errors.Newf("InitWiki: %v", err)
2015-11-26 17:33:45 -05:00
}
localPath := r.LocalWikiPath()
if err := discardLocalWikiChanges(localPath); err != nil {
return errors.Newf("discardLocalWikiChanges: %v", err)
} else if err = r.UpdateLocalWiki(); err != nil {
return errors.Newf("UpdateLocalWiki: %v", err)
2015-11-27 00:24:24 -05:00
}
title = ToWikiPageName(title)
2015-11-27 00:24:24 -05:00
filename := path.Join(localPath, title+".md")
2015-11-27 01:50:38 -05:00
// If not a new file, show perform update not create.
if isNew {
if osutil.Exist(filename) {
2015-11-27 01:50:38 -05:00
return ErrWikiAlreadyExist{filename}
}
} else {
oldTitle = ToWikiPageName(oldTitle)
_ = os.Remove(path.Join(localPath, oldTitle+".md"))
2015-11-27 01:50:38 -05:00
}
// SECURITY: if new file is a symlink to non-exist critical file,
// attack content can be written to the target file (e.g. authorized_keys2)
// as a new page operation.
// So we want to make sure the symlink is removed before write anything.
// The new file we created will be in normal text format.
_ = os.Remove(filename)
if err := os.WriteFile(filename, []byte(content), 0o666); err != nil {
return errors.Newf("WriteFile: %v", err)
2015-11-27 00:24:24 -05:00
}
if message == "" {
2015-11-27 00:24:24 -05:00
message = "Update page '" + title + "'"
}
if err := git.Add(localPath, git.AddOptions{All: true}); err != nil {
return errors.Newf("add all changes: %v", err)
}
err := git.CreateCommit(
localPath,
&git.Signature{
Name: doer.DisplayName(),
Email: doer.Email,
When: time.Now(),
},
message,
)
if err != nil {
return errors.Newf("commit changes: %v", err)
} else if err = git.Push(localPath, "origin", WikiBranch(localPath)); err != nil {
return errors.Newf("push: %v", err)
2015-11-27 00:24:24 -05:00
}
2015-11-26 17:33:45 -05:00
return nil
}
2015-11-27 01:50:38 -05:00
func (r *Repository) AddWikiPage(doer *User, title, content, message string) error {
return r.updateWikiPage(doer, "", title, content, message, true)
2015-11-27 01:50:38 -05:00
}
func (r *Repository) EditWikiPage(doer *User, oldTitle, title, content, message string) error {
return r.updateWikiPage(doer, oldTitle, title, content, message, false)
2015-11-27 01:50:38 -05:00
}
func (r *Repository) DeleteWikiPage(doer *User, title string) (err error) {
wikiWorkingPool.CheckIn(strconv.FormatInt(r.ID, 10))
defer wikiWorkingPool.CheckOut(strconv.FormatInt(r.ID, 10))
localPath := r.LocalWikiPath()
2016-03-04 13:32:17 -05:00
if err = discardLocalWikiChanges(localPath); err != nil {
return errors.Newf("discardLocalWikiChanges: %v", err)
} else if err = r.UpdateLocalWiki(); err != nil {
return errors.Newf("UpdateLocalWiki: %v", err)
}
title = ToWikiPageName(title)
_ = os.Remove(path.Join(localPath, title+".md"))
message := "Delete page '" + title + "'"
if err = git.Add(localPath, git.AddOptions{All: true}); err != nil {
return errors.Newf("add all changes: %v", err)
}
err = git.CreateCommit(
localPath,
&git.Signature{
Name: doer.DisplayName(),
Email: doer.Email,
When: time.Now(),
},
message,
)
if err != nil {
return errors.Newf("commit changes: %v", err)
} else if err = git.Push(localPath, "origin", WikiBranch(localPath)); err != nil {
return errors.Newf("push: %v", err)
}
return nil
}