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 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. "sync"
  14. "github.com/Unknwon/com"
  15. "github.com/gogits/git-module"
  16. "github.com/gogits/gogs/modules/setting"
  17. )
  18. var wikiWorkingPool = &workingPool{
  19. pool: make(map[string]*sync.Mutex),
  20. count: make(map[string]int),
  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. return strings.Replace(strings.TrimLeft(name, "./"), "/", " ", -1)
  32. }
  33. // WikiCloneLink returns clone URLs of repository wiki.
  34. func (repo *Repository) WikiCloneLink() (cl *CloneLink) {
  35. return repo.cloneLink(true)
  36. }
  37. // WikiPath returns wiki data path by given user and repository name.
  38. func WikiPath(userName, repoName string) string {
  39. return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".wiki.git")
  40. }
  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 = createUpdateHook(repo.WikiPath()); err != nil {
  57. return fmt.Errorf("createUpdateHook: %v", err)
  58. }
  59. return nil
  60. }
  61. func (repo *Repository) LocalWikiPath() string {
  62. return path.Join(setting.AppDataPath, "tmp/local-wiki", com.ToStr(repo.ID))
  63. }
  64. // UpdateLocalWiki makes sure the local copy of repository wiki is up-to-date.
  65. func (repo *Repository) UpdateLocalWiki() error {
  66. return updateLocalCopy(repo.WikiPath(), repo.LocalWikiPath(), "")
  67. }
  68. // discardLocalWikiChanges discards local commits make sure
  69. // it is even to remote branch when local copy exists.
  70. func discardLocalWikiChanges(localPath string) error {
  71. if !com.IsExist(localPath) {
  72. return nil
  73. }
  74. // No need to check if nothing in the repository.
  75. if !git.IsBranchExist(localPath, "master") {
  76. return nil
  77. }
  78. if err := git.ResetHEAD(localPath, true, "origin/master"); err != nil {
  79. return fmt.Errorf("ResetHEAD: %v", err)
  80. }
  81. return nil
  82. }
  83. // updateWikiPage adds new page to repository wiki.
  84. func (repo *Repository) updateWikiPage(doer *User, oldTitle, title, content, message string, isNew bool) (err error) {
  85. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  86. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  87. if err = repo.InitWiki(); err != nil {
  88. return fmt.Errorf("InitWiki: %v", err)
  89. }
  90. localPath := repo.LocalWikiPath()
  91. if err = discardLocalWikiChanges(localPath); err != nil {
  92. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  93. } else if err = repo.UpdateLocalWiki(); err != nil {
  94. return fmt.Errorf("UpdateLocalWiki: %v", err)
  95. }
  96. title = ToWikiPageName(title)
  97. filename := path.Join(localPath, title+".md")
  98. // If not a new file, show perform update not create.
  99. if isNew {
  100. if com.IsExist(filename) {
  101. return ErrWikiAlreadyExist{filename}
  102. }
  103. } else {
  104. os.Remove(path.Join(localPath, oldTitle+".md"))
  105. }
  106. // SECURITY: if new file is a symlink to non-exist critical file,
  107. // attack content can be written to the target file (e.g. authorized_keys2)
  108. // as a new page operation.
  109. // So we want to make sure the symlink is removed before write anything.
  110. // The new file we created will be in normal text format.
  111. os.Remove(filename)
  112. if err = ioutil.WriteFile(filename, []byte(content), 0666); err != nil {
  113. return fmt.Errorf("WriteFile: %v", err)
  114. }
  115. if len(message) == 0 {
  116. message = "Update page '" + title + "'"
  117. }
  118. if err = git.AddChanges(localPath, true); err != nil {
  119. return fmt.Errorf("AddChanges: %v", err)
  120. } else if err = git.CommitChanges(localPath, message, doer.NewGitSig()); err != nil {
  121. return fmt.Errorf("CommitChanges: %v", err)
  122. } else if err = git.Push(localPath, "origin", "master"); err != nil {
  123. return fmt.Errorf("Push: %v", err)
  124. }
  125. return nil
  126. }
  127. func (repo *Repository) AddWikiPage(doer *User, title, content, message string) error {
  128. return repo.updateWikiPage(doer, "", title, content, message, true)
  129. }
  130. func (repo *Repository) EditWikiPage(doer *User, oldTitle, title, content, message string) error {
  131. return repo.updateWikiPage(doer, oldTitle, title, content, message, false)
  132. }
  133. func (repo *Repository) DeleteWikiPage(doer *User, title string) (err error) {
  134. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  135. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  136. localPath := repo.LocalWikiPath()
  137. if err = discardLocalWikiChanges(localPath); err != nil {
  138. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  139. } else if err = repo.UpdateLocalWiki(); err != nil {
  140. return fmt.Errorf("UpdateLocalWiki: %v", err)
  141. }
  142. title = ToWikiPageName(title)
  143. filename := path.Join(localPath, title+".md")
  144. os.Remove(filename)
  145. message := "Delete page '" + title + "'"
  146. if err = git.AddChanges(localPath, true); err != nil {
  147. return fmt.Errorf("AddChanges: %v", err)
  148. } else if err = git.CommitChanges(localPath, message, doer.NewGitSig()); err != nil {
  149. return fmt.Errorf("CommitChanges: %v", err)
  150. } else if err = git.Push(localPath, "origin", "master"); err != nil {
  151. return fmt.Errorf("Push: %v", err)
  152. }
  153. return nil
  154. }