Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

wiki.go 6.5KB

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