You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wiki.go 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "io/ioutil"
  8. "net/url"
  9. "os"
  10. "path"
  11. "path/filepath"
  12. "strings"
  13. "github.com/Unknwon/com"
  14. "code.gitea.io/git"
  15. "code.gitea.io/gitea/modules/setting"
  16. "code.gitea.io/gitea/modules/sync"
  17. )
  18. var wikiWorkingPool = sync.NewExclusivePool()
  19. // ToWikiPageURL formats a string to corresponding wiki URL name.
  20. func ToWikiPageURL(name string) string {
  21. return url.QueryEscape(strings.Replace(name, " ", "-", -1))
  22. }
  23. // ToWikiPageName formats a URL back to corresponding wiki page name,
  24. // and removes leading characters './' to prevent changing files
  25. // that are not belong to wiki repository.
  26. func ToWikiPageName(urlString string) string {
  27. name, _ := url.QueryUnescape(strings.Replace(urlString, "-", " ", -1))
  28. return strings.Replace(strings.TrimLeft(name, "./"), "/", " ", -1)
  29. }
  30. // WikiCloneLink returns clone URLs of repository wiki.
  31. func (repo *Repository) WikiCloneLink() (cl *CloneLink) {
  32. return repo.cloneLink(true)
  33. }
  34. // WikiPath returns wiki data path by given user and repository name.
  35. func WikiPath(userName, repoName string) string {
  36. return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".wiki.git")
  37. }
  38. // WikiPath returns wiki data path for given repository.
  39. func (repo *Repository) WikiPath() string {
  40. return WikiPath(repo.MustOwner().Name, repo.Name)
  41. }
  42. // HasWiki returns true if repository has wiki.
  43. func (repo *Repository) HasWiki() bool {
  44. return com.IsDir(repo.WikiPath())
  45. }
  46. // InitWiki initializes a wiki for repository,
  47. // it does nothing when repository already has wiki.
  48. func (repo *Repository) InitWiki() error {
  49. if repo.HasWiki() {
  50. return nil
  51. }
  52. if err := git.InitRepository(repo.WikiPath(), true); err != nil {
  53. return fmt.Errorf("InitRepository: %v", err)
  54. } else if err = createUpdateHook(repo.WikiPath()); err != nil {
  55. return fmt.Errorf("createUpdateHook: %v", err)
  56. }
  57. return nil
  58. }
  59. // LocalWikiPath returns the path to the local wiki repository (?).
  60. func (repo *Repository) LocalWikiPath() string {
  61. return path.Join(setting.AppDataPath, "tmp/local-wiki", com.ToStr(repo.ID))
  62. }
  63. // UpdateLocalWiki makes sure the local copy of repository wiki is up-to-date.
  64. func (repo *Repository) UpdateLocalWiki() error {
  65. // Don't pass branch name here because it fails to clone and
  66. // checkout to a specific branch when wiki is an empty repository.
  67. return UpdateLocalCopyBranch(repo.WikiPath(), repo.LocalWikiPath(), "")
  68. }
  69. func discardLocalWikiChanges(localPath string) error {
  70. return discardLocalRepoBranchChanges(localPath, "master")
  71. }
  72. // updateWikiPage adds new page to repository wiki.
  73. func (repo *Repository) updateWikiPage(doer *User, oldTitle, title, content, message string, isNew bool) (err error) {
  74. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  75. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  76. if err = repo.InitWiki(); err != nil {
  77. return fmt.Errorf("InitWiki: %v", err)
  78. }
  79. localPath := repo.LocalWikiPath()
  80. if err = discardLocalWikiChanges(localPath); err != nil {
  81. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  82. } else if err = repo.UpdateLocalWiki(); err != nil {
  83. return fmt.Errorf("UpdateLocalWiki: %v", err)
  84. }
  85. title = ToWikiPageName(title)
  86. filename := path.Join(localPath, title+".md")
  87. // If not a new file, show perform update not create.
  88. if isNew {
  89. if com.IsExist(filename) {
  90. return ErrWikiAlreadyExist{filename}
  91. }
  92. } else {
  93. file := path.Join(localPath, oldTitle+".md")
  94. if err := os.Remove(file); err != nil {
  95. return fmt.Errorf("Fail to remove %s: %v", file, err)
  96. }
  97. }
  98. // SECURITY: if new file is a symlink to non-exist critical file,
  99. // attack content can be written to the target file (e.g. authorized_keys2)
  100. // as a new page operation.
  101. // So we want to make sure the symlink is removed before write anything.
  102. // The new file we created will be in normal text format.
  103. _ = os.Remove(filename)
  104. if err = ioutil.WriteFile(filename, []byte(content), 0666); err != nil {
  105. return fmt.Errorf("WriteFile: %v", err)
  106. }
  107. if len(message) == 0 {
  108. message = "Update page '" + title + "'"
  109. }
  110. if err = git.AddChanges(localPath, true); err != nil {
  111. return fmt.Errorf("AddChanges: %v", err)
  112. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  113. Committer: doer.NewGitSig(),
  114. Message: message,
  115. }); err != nil {
  116. return fmt.Errorf("CommitChanges: %v", err)
  117. } else if err = git.Push(localPath, "origin", "master"); err != nil {
  118. return fmt.Errorf("Push: %v", err)
  119. }
  120. return nil
  121. }
  122. // AddWikiPage adds a new wiki page with a given title.
  123. func (repo *Repository) AddWikiPage(doer *User, title, content, message string) error {
  124. return repo.updateWikiPage(doer, "", title, content, message, true)
  125. }
  126. // EditWikiPage updates a wiki page identified by its title,
  127. // optionally also changing title.
  128. func (repo *Repository) EditWikiPage(doer *User, oldTitle, title, content, message string) error {
  129. return repo.updateWikiPage(doer, oldTitle, title, content, message, false)
  130. }
  131. // DeleteWikiPage deletes a wiki page identified by its title.
  132. func (repo *Repository) DeleteWikiPage(doer *User, title string) (err error) {
  133. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  134. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  135. localPath := repo.LocalWikiPath()
  136. if err = discardLocalWikiChanges(localPath); err != nil {
  137. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  138. } else if err = repo.UpdateLocalWiki(); err != nil {
  139. return fmt.Errorf("UpdateLocalWiki: %v", err)
  140. }
  141. title = ToWikiPageName(title)
  142. filename := path.Join(localPath, title+".md")
  143. if err := os.Remove(filename); err != nil {
  144. return fmt.Errorf("Fail to remove %s: %v", filename, err)
  145. }
  146. message := "Delete page '" + title + "'"
  147. if err = git.AddChanges(localPath, true); err != nil {
  148. return fmt.Errorf("AddChanges: %v", err)
  149. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  150. Committer: doer.NewGitSig(),
  151. Message: message,
  152. }); err != nil {
  153. return fmt.Errorf("CommitChanges: %v", err)
  154. } else if err = git.Push(localPath, "origin", "master"); err != nil {
  155. return fmt.Errorf("Push: %v", err)
  156. }
  157. return nil
  158. }