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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2020 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package repo
  5. import (
  6. "fmt"
  7. "path/filepath"
  8. "strings"
  9. user_model "code.gitea.io/gitea/models/user"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/util"
  12. )
  13. // ErrWikiAlreadyExist represents a "WikiAlreadyExist" kind of error.
  14. type ErrWikiAlreadyExist struct {
  15. Title string
  16. }
  17. // IsErrWikiAlreadyExist checks if an error is an ErrWikiAlreadyExist.
  18. func IsErrWikiAlreadyExist(err error) bool {
  19. _, ok := err.(ErrWikiAlreadyExist)
  20. return ok
  21. }
  22. func (err ErrWikiAlreadyExist) Error() string {
  23. return fmt.Sprintf("wiki page already exists [title: %s]", err.Title)
  24. }
  25. func (err ErrWikiAlreadyExist) Unwrap() error {
  26. return util.ErrAlreadyExist
  27. }
  28. // ErrWikiReservedName represents a reserved name error.
  29. type ErrWikiReservedName struct {
  30. Title string
  31. }
  32. // IsErrWikiReservedName checks if an error is an ErrWikiReservedName.
  33. func IsErrWikiReservedName(err error) bool {
  34. _, ok := err.(ErrWikiReservedName)
  35. return ok
  36. }
  37. func (err ErrWikiReservedName) Error() string {
  38. return fmt.Sprintf("wiki title is reserved: %s", err.Title)
  39. }
  40. func (err ErrWikiReservedName) Unwrap() error {
  41. return util.ErrInvalidArgument
  42. }
  43. // ErrWikiInvalidFileName represents an invalid wiki file name.
  44. type ErrWikiInvalidFileName struct {
  45. FileName string
  46. }
  47. // IsErrWikiInvalidFileName checks if an error is an ErrWikiInvalidFileName.
  48. func IsErrWikiInvalidFileName(err error) bool {
  49. _, ok := err.(ErrWikiInvalidFileName)
  50. return ok
  51. }
  52. func (err ErrWikiInvalidFileName) Error() string {
  53. return fmt.Sprintf("Invalid wiki filename: %s", err.FileName)
  54. }
  55. func (err ErrWikiInvalidFileName) Unwrap() error {
  56. return util.ErrInvalidArgument
  57. }
  58. // WikiCloneLink returns clone URLs of repository wiki.
  59. func (repo *Repository) WikiCloneLink() *CloneLink {
  60. return repo.cloneLink(true)
  61. }
  62. // WikiPath returns wiki data path by given user and repository name.
  63. func WikiPath(userName, repoName string) string {
  64. return filepath.Join(user_model.UserPath(userName), strings.ToLower(repoName)+".wiki.git")
  65. }
  66. // WikiPath returns wiki data path for given repository.
  67. func (repo *Repository) WikiPath() string {
  68. return WikiPath(repo.OwnerName, repo.Name)
  69. }
  70. // HasWiki returns true if repository has wiki.
  71. func (repo *Repository) HasWiki() bool {
  72. isDir, err := util.IsDir(repo.WikiPath())
  73. if err != nil {
  74. log.Error("Unable to check if %s is a directory: %v", repo.WikiPath(), err)
  75. }
  76. return isDir
  77. }