2024-02-18 19:39:41 -05:00
|
|
|
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"
|
2026-02-05 22:08:54 -05:00
|
|
|
"strconv"
|
2015-11-26 17:33:45 -05:00
|
|
|
"strings"
|
2022-10-23 19:15:14 +08:00
|
|
|
"time"
|
2015-12-20 18:02:54 +01:00
|
|
|
|
2026-01-22 08:20:53 -05:00
|
|
|
"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
|
|
|
|
2020-02-22 09:05:26 +08:00
|
|
|
"gogs.io/gogs/internal/conf"
|
2026-02-05 22:08:54 -05:00
|
|
|
"gogs.io/gogs/internal/osutil"
|
2026-01-22 11:00:27 -05:00
|
|
|
"gogs.io/gogs/internal/pathutil"
|
2022-06-25 18:07:39 +08:00
|
|
|
"gogs.io/gogs/internal/repoutil"
|
2019-10-24 01:51:46 -07:00
|
|
|
"gogs.io/gogs/internal/sync"
|
2015-11-26 17:33:45 -05:00
|
|
|
)
|
|
|
|
|
|
2016-08-30 15:19:53 -07:00
|
|
|
var wikiWorkingPool = sync.NewExclusivePool()
|
2015-11-27 00:24:24 -05:00
|
|
|
|
2026-01-20 23:38:10 -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 {
|
2017-02-15 18:18:33 -05:00
|
|
|
return url.QueryEscape(name)
|
2015-11-26 17:33:45 -05:00
|
|
|
}
|
|
|
|
|
|
2026-01-22 11:00:27 -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 {
|
2017-02-15 18:18:33 -05:00
|
|
|
name, _ := url.QueryUnescape(urlString)
|
2026-01-22 11:00:27 -05:00
|
|
|
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.
|
2022-06-25 18:07:39 +08:00
|
|
|
//
|
|
|
|
|
// Deprecated: Use repoutil.NewCloneLink instead.
|
2025-04-14 18:00:07 -04:00
|
|
|
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 {
|
2022-11-05 23:33:05 +08:00
|
|
|
return filepath.Join(repoutil.UserPath(userName), strings.ToLower(repoName)+".wiki.git")
|
2015-11-26 17:33:45 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-14 18:00:07 -04: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.
|
2025-04-14 18:00:07 -04:00
|
|
|
func (r *Repository) HasWiki() bool {
|
2026-02-05 22:08:54 -05:00
|
|
|
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.
|
2025-04-14 18:00:07 -04:00
|
|
|
func (r *Repository) InitWiki() error {
|
|
|
|
|
if r.HasWiki() {
|
2015-11-26 17:33:45 -05:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-14 18:00:07 -04:00
|
|
|
if err := git.Init(r.WikiPath(), git.InitOptions{Bare: true}); err != nil {
|
2026-01-22 08:20:53 -05:00
|
|
|
return errors.Newf("init repository: %v", err)
|
2025-04-14 18:00:07 -04:00
|
|
|
} else if err = createDelegateHooks(r.WikiPath()); err != nil {
|
2026-01-22 08:20:53 -05:00
|
|
|
return errors.Newf("createDelegateHooks: %v", err)
|
2015-11-26 17:33:45 -05:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-14 18:00:07 -04:00
|
|
|
func (r *Repository) LocalWikiPath() string {
|
2026-02-05 22:08:54 -05:00
|
|
|
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.
|
2025-04-14 18:00:07 -04:00
|
|
|
func (r *Repository) UpdateLocalWiki() error {
|
2026-01-20 23:38:10 -05:00
|
|
|
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 {
|
2026-01-20 23:38:10 -05:00
|
|
|
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.
|
2026-01-22 11:00:27 -05:00
|
|
|
func (r *Repository) updateWikiPage(doer *User, oldTitle, title, content, message string, isNew bool) error {
|
2026-02-05 22:08:54 -05:00
|
|
|
wikiWorkingPool.CheckIn(strconv.FormatInt(r.ID, 10))
|
|
|
|
|
defer wikiWorkingPool.CheckOut(strconv.FormatInt(r.ID, 10))
|
2015-11-27 00:24:24 -05:00
|
|
|
|
2026-01-22 11:00:27 -05:00
|
|
|
if err := r.InitWiki(); err != nil {
|
2026-01-22 08:20:53 -05:00
|
|
|
return errors.Newf("InitWiki: %v", err)
|
2015-11-26 17:33:45 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-14 18:00:07 -04:00
|
|
|
localPath := r.LocalWikiPath()
|
2026-01-22 11:00:27 -05:00
|
|
|
if err := discardLocalWikiChanges(localPath); err != nil {
|
2026-01-22 08:20:53 -05:00
|
|
|
return errors.Newf("discardLocalWikiChanges: %v", err)
|
2025-04-14 18:00:07 -04:00
|
|
|
} else if err = r.UpdateLocalWiki(); err != nil {
|
2026-01-22 08:20:53 -05:00
|
|
|
return errors.Newf("UpdateLocalWiki: %v", err)
|
2015-11-27 00:24:24 -05:00
|
|
|
}
|
|
|
|
|
|
2016-07-01 15:33:35 +08: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 {
|
2026-02-05 22:08:54 -05:00
|
|
|
if osutil.Exist(filename) {
|
2015-11-27 01:50:38 -05:00
|
|
|
return ErrWikiAlreadyExist{filename}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2026-01-22 11:00:27 -05:00
|
|
|
oldTitle = ToWikiPageName(oldTitle)
|
|
|
|
|
_ = os.Remove(path.Join(localPath, oldTitle+".md"))
|
2015-11-27 01:50:38 -05:00
|
|
|
}
|
|
|
|
|
|
2016-07-01 15:33:35 +08: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.
|
2026-01-22 11:00:27 -05:00
|
|
|
_ = os.Remove(filename)
|
2016-07-01 15:33:35 +08:00
|
|
|
|
2026-01-22 11:00:27 -05:00
|
|
|
if err := os.WriteFile(filename, []byte(content), 0o666); err != nil {
|
2026-01-22 08:20:53 -05:00
|
|
|
return errors.Newf("WriteFile: %v", err)
|
2015-11-27 00:24:24 -05:00
|
|
|
}
|
|
|
|
|
|
2022-03-06 16:33:45 +08:00
|
|
|
if message == "" {
|
2015-11-27 00:24:24 -05:00
|
|
|
message = "Update page '" + title + "'"
|
|
|
|
|
}
|
2026-01-22 11:00:27 -05:00
|
|
|
if err := git.Add(localPath, git.AddOptions{All: true}); err != nil {
|
2026-01-22 08:20:53 -05:00
|
|
|
return errors.Newf("add all changes: %v", err)
|
2022-10-23 19:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
2026-01-22 11:00:27 -05:00
|
|
|
err := git.CreateCommit(
|
2022-10-23 19:15:14 +08:00
|
|
|
localPath,
|
|
|
|
|
&git.Signature{
|
|
|
|
|
Name: doer.DisplayName(),
|
|
|
|
|
Email: doer.Email,
|
|
|
|
|
When: time.Now(),
|
|
|
|
|
},
|
|
|
|
|
message,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
2026-01-22 08:20:53 -05:00
|
|
|
return errors.Newf("commit changes: %v", err)
|
2026-01-20 23:38:10 -05:00
|
|
|
} else if err = git.Push(localPath, "origin", WikiBranch(localPath)); err != nil {
|
2026-01-22 08:20:53 -05:00
|
|
|
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
|
|
|
|
2025-04-14 18:00:07 -04: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
|
|
|
}
|
|
|
|
|
|
2025-04-14 18:00:07 -04: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
|
|
|
}
|
2016-03-03 17:06:50 -05:00
|
|
|
|
2025-04-14 18:00:07 -04:00
|
|
|
func (r *Repository) DeleteWikiPage(doer *User, title string) (err error) {
|
2026-02-05 22:08:54 -05:00
|
|
|
wikiWorkingPool.CheckIn(strconv.FormatInt(r.ID, 10))
|
|
|
|
|
defer wikiWorkingPool.CheckOut(strconv.FormatInt(r.ID, 10))
|
2016-03-03 17:06:50 -05:00
|
|
|
|
2025-04-14 18:00:07 -04:00
|
|
|
localPath := r.LocalWikiPath()
|
2016-03-04 13:32:17 -05:00
|
|
|
if err = discardLocalWikiChanges(localPath); err != nil {
|
2026-01-22 08:20:53 -05:00
|
|
|
return errors.Newf("discardLocalWikiChanges: %v", err)
|
2025-04-14 18:00:07 -04:00
|
|
|
} else if err = r.UpdateLocalWiki(); err != nil {
|
2026-01-22 08:20:53 -05:00
|
|
|
return errors.Newf("UpdateLocalWiki: %v", err)
|
2016-03-03 17:06:50 -05:00
|
|
|
}
|
|
|
|
|
|
2016-07-01 15:33:35 +08:00
|
|
|
title = ToWikiPageName(title)
|
2026-01-22 11:00:27 -05:00
|
|
|
_ = os.Remove(path.Join(localPath, title+".md"))
|
2016-03-03 17:06:50 -05:00
|
|
|
|
|
|
|
|
message := "Delete page '" + title + "'"
|
|
|
|
|
|
2022-03-22 00:55:36 +08:00
|
|
|
if err = git.Add(localPath, git.AddOptions{All: true}); err != nil {
|
2026-01-22 08:20:53 -05:00
|
|
|
return errors.Newf("add all changes: %v", err)
|
2022-10-23 19:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = git.CreateCommit(
|
|
|
|
|
localPath,
|
|
|
|
|
&git.Signature{
|
|
|
|
|
Name: doer.DisplayName(),
|
|
|
|
|
Email: doer.Email,
|
|
|
|
|
When: time.Now(),
|
|
|
|
|
},
|
|
|
|
|
message,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
2026-01-22 08:20:53 -05:00
|
|
|
return errors.Newf("commit changes: %v", err)
|
2026-01-20 23:38:10 -05:00
|
|
|
} else if err = git.Push(localPath, "origin", WikiBranch(localPath)); err != nil {
|
2026-01-22 08:20:53 -05:00
|
|
|
return errors.Newf("push: %v", err)
|
2016-03-03 17:06:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|