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.

223 lines
6.7 KiB

9 years ago
9 years ago
  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. var branch = ""
  72. if com.IsExist(repo.LocalWikiPath()) {
  73. branch = "master"
  74. }
  75. return UpdateLocalCopyBranch(repo.WikiPath(), repo.LocalWikiPath(), branch)
  76. }
  77. func discardLocalWikiChanges(localPath string) error {
  78. return discardLocalRepoBranchChanges(localPath, "master")
  79. }
  80. // pathAllowed checks if a wiki path is allowed
  81. func pathAllowed(path string) error {
  82. for i := range reservedWikiPaths {
  83. if path == reservedWikiPaths[i] {
  84. return ErrWikiAlreadyExist{path}
  85. }
  86. }
  87. return nil
  88. }
  89. // updateWikiPage adds new page to repository wiki.
  90. func (repo *Repository) updateWikiPage(doer *User, oldWikiPath, wikiPath, content, message string, isNew bool) (err error) {
  91. if err = pathAllowed(wikiPath); err != nil {
  92. return err
  93. }
  94. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  95. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  96. if err = repo.InitWiki(); err != nil {
  97. return fmt.Errorf("InitWiki: %v", err)
  98. }
  99. localPath := repo.LocalWikiPath()
  100. if err = discardLocalWikiChanges(localPath); err != nil {
  101. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  102. } else if err = repo.UpdateLocalWiki(); err != nil {
  103. return fmt.Errorf("UpdateLocalWiki: %v", err)
  104. }
  105. title := ToWikiPageName(wikiPath)
  106. filename := path.Join(localPath, wikiPath+".md")
  107. // If not a new file, show perform update not create.
  108. if isNew {
  109. if com.IsExist(filename) {
  110. return ErrWikiAlreadyExist{filename}
  111. }
  112. } else {
  113. file := path.Join(localPath, oldWikiPath+".md")
  114. if err := os.Remove(file); err != nil {
  115. return fmt.Errorf("Failed to remove %s: %v", file, err)
  116. }
  117. }
  118. // SECURITY: if new file is a symlink to non-exist critical file,
  119. // attack content can be written to the target file (e.g. authorized_keys2)
  120. // as a new page operation.
  121. // So we want to make sure the symlink is removed before write anything.
  122. // The new file we created will be in normal text format.
  123. _ = os.Remove(filename)
  124. if err = ioutil.WriteFile(filename, []byte(content), 0666); err != nil {
  125. return fmt.Errorf("WriteFile: %v", err)
  126. }
  127. if len(message) == 0 {
  128. message = "Update page '" + title + "'"
  129. }
  130. if err = git.AddChanges(localPath, true); err != nil {
  131. return fmt.Errorf("AddChanges: %v", err)
  132. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  133. Committer: doer.NewGitSig(),
  134. Message: message,
  135. }); err != nil {
  136. return fmt.Errorf("CommitChanges: %v", err)
  137. } else if err = git.Push(localPath, git.PushOptions{
  138. Remote: "origin",
  139. Branch: "master",
  140. }); err != nil {
  141. return fmt.Errorf("Push: %v", err)
  142. }
  143. return nil
  144. }
  145. // AddWikiPage adds a new wiki page with a given wikiPath.
  146. func (repo *Repository) AddWikiPage(doer *User, wikiPath, content, message string) error {
  147. return repo.updateWikiPage(doer, "", wikiPath, content, message, true)
  148. }
  149. // EditWikiPage updates a wiki page identified by its wikiPath,
  150. // optionally also changing wikiPath.
  151. func (repo *Repository) EditWikiPage(doer *User, oldWikiPath, wikiPath, content, message string) error {
  152. return repo.updateWikiPage(doer, oldWikiPath, wikiPath, content, message, false)
  153. }
  154. // DeleteWikiPage deletes a wiki page identified by its wikiPath.
  155. func (repo *Repository) DeleteWikiPage(doer *User, wikiPath string) (err error) {
  156. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  157. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  158. localPath := repo.LocalWikiPath()
  159. if err = discardLocalWikiChanges(localPath); err != nil {
  160. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  161. } else if err = repo.UpdateLocalWiki(); err != nil {
  162. return fmt.Errorf("UpdateLocalWiki: %v", err)
  163. }
  164. filename := path.Join(localPath, wikiPath+".md")
  165. if err := os.Remove(filename); err != nil {
  166. return fmt.Errorf("Failed to remove %s: %v", filename, err)
  167. }
  168. title := ToWikiPageName(wikiPath)
  169. message := "Delete page '" + title + "'"
  170. if err = git.AddChanges(localPath, true); err != nil {
  171. return fmt.Errorf("AddChanges: %v", err)
  172. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  173. Committer: doer.NewGitSig(),
  174. Message: message,
  175. }); err != nil {
  176. return fmt.Errorf("CommitChanges: %v", err)
  177. } else if err = git.Push(localPath, git.PushOptions{
  178. Remote: "origin",
  179. Branch: "master",
  180. }); err != nil {
  181. return fmt.Errorf("Push: %v", err)
  182. }
  183. return nil
  184. }