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