1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 03:36:16 +00:00

Implement redirection hyphae #152

This commit is contained in:
Timur Ismagilov 2022-07-01 20:51:22 +05:00
parent 5b829f1d82
commit cff7dafcea
7 changed files with 59 additions and 20 deletions

View File

@ -57,8 +57,8 @@ func hyphaeInCategory(catName string) (hyphaList []string) {
var mutex sync.RWMutex
// addHyphaToCategory adds the hypha to the category and updates the records on the disk. If the hypha is already in the category, nothing happens. Pass canonical names.
func addHyphaToCategory(hyphaName, catName string) {
// AddHyphaToCategory adds the hypha to the category and updates the records on the disk. If the hypha is already in the category, nothing happens. Pass canonical names.
func AddHyphaToCategory(hyphaName, catName string) {
mutex.Lock()
if node, ok := hyphaToCategories[hyphaName]; ok {
node.storeCategory(catName)
@ -109,4 +109,5 @@ func RenameHyphaInAllCategories(oldName, newName string) {
}
}
}
go saveToDisk()
}

View File

@ -74,6 +74,6 @@ func handlerAddToCategory(w http.ResponseWriter, rq *http.Request) {
return
}
log.Println(user.FromRequest(rq).Name, "added", hyphaName, "to", catName)
addHyphaToCategory(hyphaName, catName)
AddHyphaToCategory(hyphaName, catName)
http.Redirect(w, rq, redirectTo, http.StatusSeeOther)
}

View File

@ -21,9 +21,10 @@ var (
NaviTitleIcon string
UseSiblingHyphaeSidebar bool
HomeHypha string
UserHypha string
HeaderLinksHypha string
HomeHypha string
UserHypha string
HeaderLinksHypha string
RedirectionCategory string
ListenAddr string
URL string
@ -64,9 +65,10 @@ type Config struct {
// Hyphae is a section of Config which has fields related to special hyphae.
type Hyphae struct {
HomeHypha string `comment:"This hypha will be the main (index) page of your wiki, served on /."`
UserHypha string `comment:"This hypha is used as a prefix for user hyphae."`
HeaderLinksHypha string `comment:"You can also specify a hypha to populate your own custom header links from."`
HomeHypha string `comment:"This hypha will be the main (index) page of your wiki, served on /."`
UserHypha string `comment:"This hypha is used as a prefix for user hyphae."`
HeaderLinksHypha string `comment:"You can also specify a hypha to populate your own custom header links from."`
RedirectionCategory string `comment:"Redirection hyphae will be added to this category. Default: redirection."`
}
// Network is a section of Config that has fields related to network stuff.
@ -113,9 +115,10 @@ func ReadConfigFile(path string) error {
NaviTitleIcon: "🍄",
UseSiblingHyphaeSidebar: false,
Hyphae: Hyphae{
HomeHypha: "home",
UserHypha: "u",
HeaderLinksHypha: "",
HomeHypha: "home",
UserHypha: "u",
HeaderLinksHypha: "",
RedirectionCategory: "redirection",
},
Network: Network{
ListenAddr: "127.0.0.1:1737",
@ -175,6 +178,7 @@ func ReadConfigFile(path string) error {
HomeHypha = cfg.HomeHypha
UserHypha = cfg.UserHypha
HeaderLinksHypha = cfg.HeaderLinksHypha
RedirectionCategory = cfg.RedirectionCategory
if ListenAddr == "" {
ListenAddr = cfg.ListenAddr
}

View File

@ -33,7 +33,8 @@ var (
{{define "rename [[hypha]]?"}}Переименовать <a href="/hypha/{{.}}">{{beautifulName .}}</a>?{{end}}
{{define "new name"}}Новое название:{{end}}
{{define "rename recursively"}}Также переименовать подгифы{{end}}
{{define "rename tip"}}Если вы переименуете эту гифу, сломаются все ссылки, ведущие на неё, а также исходящие относительные ссылки. Также вы потеряете всю текущую историю для нового названия. Переименовывайте аккуратно.{{end}}
{{define "rename tip"}}Переименовывайте аккуратно. <a href="/help/en/rename">Документация на английском.</a>{{end}}
{{define "leave redirections"}}Оставить перенаправления{{end}}
`
chainNaviTitle viewutil.Chain
chainEmptyHypha viewutil.Chain

View File

@ -12,8 +12,11 @@
<input type="checkbox" id="recursive" name="recursive" value="true" checked/>
<label for="recursive">{{block "rename recursively" .}}Rename subhyphae too{{end}}</label>
<br>
<input type="checkbox" id="redirection" name="redirection" value="true" checked/>
<label for="redirection">{{block "leave redirections" .}}Leave redirections{{end}}</label>
<p>{{block "rename tip" .}}If you rename this hypha, all incoming links and all relative outcoming links will break. You will also lose all history for the new name. Rename carefully.{{end}}</p>
<p>{{block "rename tip" .}}Rename carefully. <a href="/help/en/rename">Documentation.</a>{{end}}</p>
<button type="submit" value="Confirm" class="btn">
{{template "confirm"}}
</button>

View File

@ -5,6 +5,9 @@ import (
"fmt"
"github.com/bouncepaw/mycorrhiza/backlinks"
"github.com/bouncepaw/mycorrhiza/categories"
"github.com/bouncepaw/mycorrhiza/cfg"
"github.com/bouncepaw/mycorrhiza/files"
"path/filepath"
"regexp"
"github.com/bouncepaw/mycorrhiza/history"
@ -14,7 +17,8 @@ import (
)
// Rename renames the old hypha to the new name and makes a history record about that. Call if and only if the user has the permission to rename.
func Rename(oldHypha hyphae.ExistingHypha, newName string, recursive bool, u *user.User) error {
func Rename(oldHypha hyphae.ExistingHypha, newName string, recursive bool, leaveRedirections bool, u *user.User) error {
// * bouncepaw hates this function and related renaming functions
if newName == "" {
rejectRenameLog(oldHypha, u, "no new name given")
return errors.New("ui.rename_noname_tip")
@ -62,7 +66,7 @@ func Rename(oldHypha hyphae.ExistingHypha, newName string, recursive bool, u *us
newName))
}
hop.WithFilesRenamed(renameMap).Apply()
hop.WithFilesRenamed(renameMap)
if len(hop.Errs) != 0 {
return hop.Errs[0]
@ -76,11 +80,36 @@ func Rename(oldHypha hyphae.ExistingHypha, newName string, recursive bool, u *us
hyphae.RenameHyphaTo(h, newName, replaceName)
backlinks.UpdateBacklinksAfterRename(h, oldName)
categories.RenameHyphaInAllCategories(oldName, newName)
if leaveRedirections {
if err := leaveRedirection(oldName, newName, hop); err != nil {
hop.WithErrAbort(err)
return err
}
}
}
hop.Apply()
return nil
}
func leaveRedirection(oldName, newName string, hop *history.Op) error {
var (
text = fmt.Sprintf("=> %s | ✏️ %s\n", newName, util.BeautifulName(newName))
emptyHypha = hyphae.ByName(oldName)
)
switch emptyHypha := emptyHypha.(type) {
case *hyphae.EmptyHypha:
h := hyphae.ExtendEmptyToTextual(emptyHypha, filepath.Join(files.HyphaeDir(), oldName+".myco"))
hyphae.Insert(h)
categories.AddHyphaToCategory(oldName, cfg.RedirectionCategory)
defer backlinks.UpdateBacklinksAfterEdit(h, "")
return writeTextToDisk(h, []byte(text), hop)
default:
return errors.New("invalid state for hypha " + oldName + " renamed to " + newName)
}
}
func findHyphaeToRename(superhypha hyphae.ExistingHypha, recursive bool) []hyphae.ExistingHypha {
hyphaList := []hyphae.ExistingHypha{superhypha}
if recursive {

View File

@ -124,9 +124,10 @@ func handlerRename(w http.ResponseWriter, rq *http.Request) {
}
var (
oldHypha = h.(hyphae.ExistingHypha)
newName = util.CanonicalName(rq.PostFormValue("new-name"))
recursive = rq.PostFormValue("recursive") == "true"
oldHypha = h.(hyphae.ExistingHypha)
newName = util.CanonicalName(rq.PostFormValue("new-name"))
recursive = rq.PostFormValue("recursive") == "true"
leaveRedirections = rq.PostFormValue("redirection") == "true"
)
if rq.Method == "GET" {
@ -134,7 +135,7 @@ func handlerRename(w http.ResponseWriter, rq *http.Request) {
return
}
if err := shroom.Rename(oldHypha, newName, recursive, u); err != nil {
if err := shroom.Rename(oldHypha, newName, recursive, leaveRedirections, u); err != nil {
log.Printf("%s tries to rename %s: %s", u.Name, oldHypha.CanonicalName(), err.Error())
viewutil.HttpErr(meta, http.StatusForbidden, oldHypha.CanonicalName(), lc.Get(err.Error())) // TODO: localize
return