wiki.go 5.8 KB

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