wiki.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 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. func (repo *Repository) WikiPath() string {
  41. return WikiPath(repo.MustOwner().Name, repo.Name)
  42. }
  43. // HasWiki returns true if repository has wiki.
  44. func (repo *Repository) HasWiki() bool {
  45. return com.IsDir(repo.WikiPath())
  46. }
  47. // InitWiki initializes a wiki for repository,
  48. // it does nothing when repository already has wiki.
  49. func (repo *Repository) InitWiki() error {
  50. if repo.HasWiki() {
  51. return nil
  52. }
  53. if err := git.InitRepository(repo.WikiPath(), true); err != nil {
  54. return fmt.Errorf("InitRepository: %v", err)
  55. } else if err = createDelegateHooks(repo.WikiPath()); err != nil {
  56. return fmt.Errorf("createDelegateHooks: %v", err)
  57. }
  58. return nil
  59. }
  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. return UpdateLocalCopyBranch(repo.WikiPath(), repo.LocalWikiPath(), "master", true)
  66. }
  67. func discardLocalWikiChanges(localPath string) error {
  68. return discardLocalRepoBranchChanges(localPath, "master")
  69. }
  70. // updateWikiPage adds new page to repository wiki.
  71. func (repo *Repository) updateWikiPage(doer *User, oldTitle, title, content, message string, isNew bool) (err error) {
  72. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  73. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  74. if err = repo.InitWiki(); err != nil {
  75. return fmt.Errorf("InitWiki: %v", err)
  76. }
  77. localPath := repo.LocalWikiPath()
  78. if err = discardLocalWikiChanges(localPath); err != nil {
  79. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  80. } else if err = repo.UpdateLocalWiki(); err != nil {
  81. return fmt.Errorf("UpdateLocalWiki: %v", err)
  82. }
  83. title = ToWikiPageName(title)
  84. filename := path.Join(localPath, title+".md")
  85. // If not a new file, show perform update not create.
  86. if isNew {
  87. if com.IsExist(filename) {
  88. return ErrWikiAlreadyExist{filename}
  89. }
  90. } else {
  91. os.Remove(path.Join(localPath, oldTitle+".md"))
  92. }
  93. // SECURITY: if new file is a symlink to non-exist critical file,
  94. // attack content can be written to the target file (e.g. authorized_keys2)
  95. // as a new page operation.
  96. // So we want to make sure the symlink is removed before write anything.
  97. // The new file we created will be in normal text format.
  98. os.Remove(filename)
  99. if err = ioutil.WriteFile(filename, []byte(content), 0666); err != nil {
  100. return fmt.Errorf("WriteFile: %v", err)
  101. }
  102. if len(message) == 0 {
  103. message = "Update page '" + title + "'"
  104. }
  105. if err = git.AddChanges(localPath, true); err != nil {
  106. return fmt.Errorf("AddChanges: %v", err)
  107. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  108. Committer: doer.NewGitSig(),
  109. Message: message,
  110. }); err != nil {
  111. return fmt.Errorf("CommitChanges: %v", err)
  112. } else if err = git.Push(localPath, "origin", "master"); err != nil {
  113. return fmt.Errorf("Push: %v", err)
  114. }
  115. return nil
  116. }
  117. func (repo *Repository) AddWikiPage(doer *User, title, content, message string) error {
  118. return repo.updateWikiPage(doer, "", title, content, message, true)
  119. }
  120. func (repo *Repository) EditWikiPage(doer *User, oldTitle, title, content, message string) error {
  121. return repo.updateWikiPage(doer, oldTitle, title, content, message, false)
  122. }
  123. func (repo *Repository) DeleteWikiPage(doer *User, title string) (err error) {
  124. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  125. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  126. localPath := repo.LocalWikiPath()
  127. if err = discardLocalWikiChanges(localPath); err != nil {
  128. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  129. } else if err = repo.UpdateLocalWiki(); err != nil {
  130. return fmt.Errorf("UpdateLocalWiki: %v", err)
  131. }
  132. title = ToWikiPageName(title)
  133. filename := path.Join(localPath, title+".md")
  134. os.Remove(filename)
  135. message := "Delete page '" + title + "'"
  136. if err = git.AddChanges(localPath, true); err != nil {
  137. return fmt.Errorf("AddChanges: %v", err)
  138. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  139. Committer: doer.NewGitSig(),
  140. Message: message,
  141. }); err != nil {
  142. return fmt.Errorf("CommitChanges: %v", err)
  143. } else if err = git.Push(localPath, "origin", "master"); err != nil {
  144. return fmt.Errorf("Push: %v", err)
  145. }
  146. return nil
  147. }