You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

wiki.go 5.9 kB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
Squashed commit of the following: commit 0afcb843d7ffd596991c4885cab768273a6eb42c Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 17:13:29 2016 -0600 Removed Upload stats as the upload table is just a temporary table commit 7ecd73ff5535612d79d471409173ee7f1fcfa157 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 08:42:41 2016 -0600 Fix for CodeMirror mode commit c29b9ab531e2e7af0fb5db24dc17e51027dd1174 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 08:03:33 2016 -0600 Made tabbing in editor use spaces commit 23af384c53206a8a40e11e45bf49d7a149c4adcd Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:56:46 2016 -0600 Fix for data-url commit cfb8a97591cb6fc0a92e49563b7b764c524db0e9 Merge: 7fc8a89 991ce42 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:42:53 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go public/js/gogs.js commit 7fc8a89cb495478225b02d613e647f99a1489634 Merge: fd3d86c c03d040 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:40:00 2016 -0600 Merge branch 'feature-create-and-edit-repo-file' of github.com:richmahn/gogs into feature-create-and-edit-repo-file commit fd3d86ca6bbc02cfda566a504ffd6b03db4f75ef Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:39:44 2016 -0600 Code cleanup commit c03d0401c1049eeeccc32ab1f9c3303c130be5ee Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 29 15:38:23 2016 -0600 Code cleanup commit 98e1206ccf9f9a4503c020e3a7830cf9f861dfae Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 18:36:01 2016 -0600 Code cleanup and fixes commit c2895dc742f25f8412879c9fa15e18f27f42f194 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 18:24:04 2016 -0600 Fixes per Unknwon's requests commit 6aa7e46b21ad4c96e562daa2eac26a8fb408f8ef Merge: 889e9fa ad7ea88 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 17:13:43 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go modules/setting/setting.go commit 889e9faf1bd8559a4979c8f46005d488c1a234d4 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:09:18 2016 -0600 Fix in gogs.js commit 47603edf223f147b114be65f3bd27bc1e88827a5 Merge: bb57912 cf85e9e Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:07:36 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go public/js/gogs.js commit bb5791255867a71c11a77b639db050ad09c597a4 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:02:18 2016 -0600 Update for using CodeMirror mode addon commit d10d128c51039be19e2af9c66c63db66a9f2ec6d Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Tue Jul 19 16:12:57 2016 -0600 Update for Edit commit 34a34982025144e3225e389f7849eb6273c1d576 Merge: fa1b752 1c7dcdd Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Tue Jul 19 11:52:02 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go commit fa1b752be29cd455c5184ddac2ffe80b3489763e Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 15 18:35:42 2016 -0600 Feature for editing, creating, uploading and deleting files
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
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. }