1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-23 07:09:43 +02:00

detect broken links

This commit is contained in:
Harvey Kandola 2016-10-27 15:44:40 -07:00
parent ad716a23ba
commit 16b7fd45d7
11 changed files with 148 additions and 108 deletions

View file

@ -107,6 +107,13 @@ func (p *Persister) GetDocumentMeta(id string) (meta entity.DocumentMeta, err er
return
}
meta.OutboundLinks, err = p.GetDocumentOutboundLinks(id)
if err != nil {
log.Error(fmt.Sprintf("Unable to execute GetDocumentOutboundLinks for document %s", id), err)
return
}
return
}
@ -400,6 +407,18 @@ func (p *Persister) DeleteDocument(documentID string) (rows int64, err error) {
return
}
// Mark references to this document as orphaned
err = p.MarkOrphanDocumentLink(documentID)
if err != nil {
return
}
// Remove all references from this document
_, err = p.DeleteSourceDocumentLinks(documentID)
if err != nil {
return
}
p.Base.Audit(p.Context, "delete-document", documentID, "")
return p.Base.DeleteConstrained(p.Context.Transaction, "document", p.Context.OrgID, documentID)

View file

@ -24,11 +24,10 @@ import (
// AddContentLink inserts wiki-link into the store.
// These links exist when content references another document or content.
func (p *Persister) AddContentLink(l entity.Link) (err error) {
l.UserID = p.Context.UserID
l.Created = time.Now().UTC()
l.Revised = time.Now().UTC()
stmt, err := p.Context.Transaction.Preparex("INSERT INTO link (refid, orgid, folderid, userid, sourceid, documentid, targetid, linktype, created, revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
stmt, err := p.Context.Transaction.Preparex("INSERT INTO link (refid, orgid, folderid, userid, sourcedocumentid, sourcepageid, targetdocumentid, targetpageid, linktype, created, revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
defer utility.Close(stmt)
if err != nil {
@ -36,7 +35,7 @@ func (p *Persister) AddContentLink(l entity.Link) (err error) {
return
}
_, err = stmt.Exec(l.RefID, l.OrgID, l.FolderID, l.UserID, l.SourceID, l.DocumentID, l.TargetID, l.LinkType, l.Created, l.Revised)
_, err = stmt.Exec(l.RefID, l.OrgID, l.FolderID, l.UserID, l.SourceDocumentID, l.SourcePageID, l.TargetDocumentID, l.TargetPageID, l.LinkType, l.Created, l.Revised)
if err != nil {
log.Error("Unable to execute insert for link", err)
@ -181,80 +180,78 @@ func (p *Persister) SearchLinkCandidates(keywords string) (docs []entity.LinkCan
return
}
// GetReferencedLinks returns all links that the specified section is referencing.
// func (p *Persister) GetReferencedLinks(sectionID string) (links []entity.Link, err error) {
// err = nil
//
// sql := "SELECT id,refid,orgid,folderid,userid,sourceid,documentid,targetid,linktype,orphan,created,revised from link WHERE orgid=? AND sourceid=?"
//
// err = Db.Select(&links, sql, p.Context.OrgID, sectionID)
//
// if err != nil {
// log.Error(fmt.Sprintf("Unable to execute select links for org %s", p.Context.OrgID), err)
// return
// }
//
// return
// }
//
// // GetContentLinksForSection returns all links that are linking to the specified section.
// func (p *Persister) GetContentLinksForSection(sectionID string) (links []entity.Link, err error) {
// err = nil
//
// sql := "SELECT id,refid,orgid,folderid,userid,sourceid,documentid,targetid,linktype,orphan,created,revised from link WHERE orgid=? AND sectionid=?"
//
// err = Db.Select(&links, sql, p.Context.OrgID, sectionID)
//
// if err != nil {
// log.Error(fmt.Sprintf("Unable to execute select links for org %s", p.Context.OrgID), err)
// return
// }
//
// return
// }
//
// // GetContentLinksForDocument returns all links that are linking to the specified document.
// func (p *Persister) GetContentLinksForDocument(documentID string) (links []entity.Link, err error) {
// err = nil
//
// sql := "SELECT id,refid,orgid,folderid,userid,sourceid,documentid,targetid,linktype,orphan,created,revised from link WHERE orgid=? AND documentid=?"
//
// err = Db.Select(&links, sql, p.Context.OrgID, documentID)
//
// if err != nil {
// log.Error(fmt.Sprintf("Unable to execute select links for org %s", p.Context.OrgID), err)
// return
// }
//
// return
// }
// GetDocumentOutboundLinks returns outbound links for specified document.
func (p *Persister) GetDocumentOutboundLinks(documentID string) (links []entity.Link, err error) {
err = nil
// MarkOrphanContentLink marks the link record as being invalid.
func (p *Persister) MarkOrphanContentLink(l entity.Link) (err error) {
l.Orphan = true
l.Revised = time.Now().UTC()
stmt, err := p.Context.Transaction.PrepareNamed("UPDATE link SET orphan=1 revised=:revised WHERE orgid=:orgid AND refid=:refid")
defer utility.Close(stmt)
err = Db.Select(&links,
`select l.refid, l.orgid, l.folderid, l.userid, l.sourcedocumentid, l.sourcepageid, l.targetdocumentid, l.targetpageid, l.linktype, l.orphan, l.created, l.revised
FROM link l
WHERE l.orgid=? AND l.sourcedocumentid=?`,
p.Context.OrgID,
documentID)
if err != nil {
log.Error(fmt.Sprintf("Unable to prepare update for link %s", l.RefID), err)
return
}
_, err = stmt.Exec(&l)
if len(links) == 0 {
links = []entity.Link{}
}
return
}
// MarkOrphanDocumentLink marks all link records referencing specified document.
func (p *Persister) MarkOrphanDocumentLink(documentID string) (err error) {
revised := time.Now().UTC()
stmt, err := p.Context.Transaction.Preparex("UPDATE link SET orphan=1, revised=? WHERE linktype='document' AND orgid=? AND targetdocumentid=?")
if err != nil {
return
}
defer utility.Close(stmt)
_, err = stmt.Exec(revised, p.Context.OrgID, documentID)
if err != nil {
log.Error(fmt.Sprintf("Unable to execute update for link %s", l.RefID), err)
return
}
return
}
// DeleteSourceLinks removes saved links for given source.
func (p *Persister) DeleteSourceLinks(sourceID string) (rows int64, err error) {
return p.Base.DeleteWhere(p.Context.Transaction, fmt.Sprintf("DELETE FROM link WHERE orgid=\"%s\" AND sourceid=\"%s\"", p.Context.OrgID, sourceID))
// MarkOrphanPageLink marks all link records referencing specified page.
func (p *Persister) MarkOrphanPageLink(pageID string) (err error) {
revised := time.Now().UTC()
stmt, err := p.Context.Transaction.Preparex("UPDATE link SET orphan=1, revised=? WHERE linktype='section' AND orgid=? AND targetpageid=?")
if err != nil {
return
}
defer utility.Close(stmt)
_, err = stmt.Exec(revised, p.Context.OrgID, pageID)
if err != nil {
return
}
return
}
// DeleteSourcePageLinks removes saved links for given source.
func (p *Persister) DeleteSourcePageLinks(pageID string) (rows int64, err error) {
return p.Base.DeleteWhere(p.Context.Transaction, fmt.Sprintf("DELETE FROM link WHERE orgid=\"%s\" AND sourcepageid=\"%s\"", p.Context.OrgID, pageID))
}
// DeleteSourceDocumentLinks removes saved links for given document.
func (p *Persister) DeleteSourceDocumentLinks(documentID string) (rows int64, err error) {
return p.Base.DeleteWhere(p.Context.Transaction, fmt.Sprintf("DELETE FROM link WHERE orgid=\"%s\" AND sourcedocumentid=\"%s\"", p.Context.OrgID, documentID))
}
// DeleteLink removes saved link from the store.

View file

@ -291,14 +291,18 @@ func (p *Persister) UpdatePage(page entity.Page, refID, userID string, skipRevis
links := util.GetContentLinks(page.Body)
// delete previous content links for this page
_, _ = p.DeleteSourceLinks(page.RefID)
_, _ = p.DeleteSourcePageLinks(page.RefID)
// save latest content links for this page
for _, link := range links {
link.Orphan = false
link.OrgID = p.Context.OrgID
link.UserID = p.Context.UserID
link.SourceID = page.RefID
link.Orphan = false
link.SourceDocumentID = page.DocumentID
if link.LinkType == "section" {
link.SourcePageID = page.RefID
}
err := p.AddContentLink(link)
@ -399,6 +403,12 @@ func (p *Persister) DeletePage(documentID, pageID string) (rows int64, err error
_, err = p.Base.DeleteWhere(p.Context.Transaction, fmt.Sprintf("DELETE FROM pagemeta WHERE orgid='%s' AND pageid='%s'", p.Context.OrgID, pageID))
_, err = searches.Delete(&databaseRequest{OrgID: p.Context.OrgID}, documentID, pageID)
// delete content links from this page
_, err = p.DeleteSourcePageLinks(pageID)
// mark as orphan links to this page
err = p.MarkOrphanPageLink(pageID)
p.Base.Audit(p.Context, "remove-page", documentID, pageID)
}