1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-20 13:49:42 +02:00
documize/domain/search/mysql/store.go

320 lines
8.8 KiB
Go
Raw Normal View History

// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package mysql
import (
2017-08-15 14:15:31 +01:00
"database/sql"
"fmt"
"strings"
"github.com/documize/community/core/env"
"github.com/documize/community/core/streamutil"
"github.com/documize/community/core/stringutil"
"github.com/documize/community/domain"
2017-08-15 19:41:44 +01:00
"github.com/documize/community/model/attachment"
"github.com/documize/community/model/doc"
"github.com/documize/community/model/page"
"github.com/documize/community/model/search"
2018-01-10 16:07:17 +00:00
"github.com/documize/community/model/workflow"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
)
// Scope provides data access to MySQL.
type Scope struct {
Runtime *env.Runtime
}
2017-08-15 19:41:44 +01:00
// IndexDocument adds search index entries for document inserting title, tags and attachments as
// searchable items. Any existing document entries are removed.
func (s Scope) IndexDocument(ctx domain.RequestContext, doc doc.Document, a []attachment.Attachment) (err error) {
// remove previous search entries
2017-09-25 14:37:11 +01:00
_, err = ctx.Transaction.Exec("DELETE FROM search WHERE orgid=? AND documentid=? AND (itemtype='doc' OR itemtype='file' OR itemtype='tag')",
ctx.OrgID, doc.RefID)
if err != nil {
2017-08-15 19:41:44 +01:00
err = errors.Wrap(err, "execute delete document index entries")
}
2017-08-15 19:41:44 +01:00
// insert doc title
2017-09-25 14:37:11 +01:00
_, err = ctx.Transaction.Exec("INSERT INTO search (orgid, documentid, itemid, itemtype, content) VALUES (?, ?, ?, ?, ?)",
ctx.OrgID, doc.RefID, "", "doc", doc.Title)
if err != nil {
2017-08-15 19:41:44 +01:00
err = errors.Wrap(err, "execute insert document title entry")
}
2017-08-15 19:41:44 +01:00
// insert doc tags
tags := strings.Split(doc.Tags, "#")
for _, t := range tags {
if len(t) == 0 {
continue
}
2017-09-25 14:37:11 +01:00
_, err = ctx.Transaction.Exec("INSERT INTO search (orgid, documentid, itemid, itemtype, content) VALUES (?, ?, ?, ?, ?)",
ctx.OrgID, doc.RefID, "", "tag", t)
2017-08-15 19:41:44 +01:00
if err != nil {
err = errors.Wrap(err, "execute insert document tag entry")
return
}
}
2017-08-15 19:41:44 +01:00
for _, file := range a {
2017-09-25 14:37:11 +01:00
_, err = ctx.Transaction.Exec("INSERT INTO search (orgid, documentid, itemid, itemtype, content) VALUES (?, ?, ?, ?, ?)",
ctx.OrgID, doc.RefID, file.RefID, "file", file.Filename)
2017-08-15 19:41:44 +01:00
if err != nil {
err = errors.Wrap(err, "execute insert document file entry")
}
}
return nil
}
2017-08-15 19:41:44 +01:00
// DeleteDocument removes all search entries for document.
func (s Scope) DeleteDocument(ctx domain.RequestContext, ID string) (err error) {
2017-09-25 14:37:11 +01:00
_, err = ctx.Transaction.Exec("DELETE FROM search WHERE orgid=? AND documentid=?", ctx.OrgID, ID)
if err != nil {
2017-08-15 19:41:44 +01:00
err = errors.Wrap(err, "execute delete document entries")
}
2017-08-15 19:41:44 +01:00
return
}
2017-08-15 19:41:44 +01:00
// IndexContent adds search index entry for document context.
// Any existing document entries are removed.
func (s Scope) IndexContent(ctx domain.RequestContext, p page.Page) (err error) {
2018-01-10 16:07:17 +00:00
// we do not index pending pages
if p.Status == workflow.ChangePending || p.Status == workflow.ChangePendingNew {
return
}
2017-08-15 19:41:44 +01:00
// remove previous search entries
2017-09-25 14:37:11 +01:00
_, err = ctx.Transaction.Exec("DELETE FROM search WHERE orgid=? AND documentid=? AND itemid=? AND itemtype='page'",
ctx.OrgID, p.DocumentID, p.RefID)
if err != nil {
2017-08-15 19:41:44 +01:00
err = errors.Wrap(err, "execute delete document content entry")
}
2017-08-15 19:41:44 +01:00
// prepare content
content, err := stringutil.HTML(p.Body).Text(false)
if err != nil {
2017-08-15 20:29:35 +01:00
err = errors.Wrap(err, "search strip HTML failed")
2017-08-15 19:41:44 +01:00
return
}
2017-08-15 19:41:44 +01:00
content = strings.TrimSpace(content)
2017-09-25 14:37:11 +01:00
_, err = ctx.Transaction.Exec("INSERT INTO search (orgid, documentid, itemid, itemtype, content) VALUES (?, ?, ?, ?, ?)",
ctx.OrgID, p.DocumentID, p.RefID, "page", content)
if err != nil {
2017-08-15 19:41:44 +01:00
err = errors.Wrap(err, "execute insert document content entry")
}
2018-03-19 15:04:02 +00:00
_, err = ctx.Transaction.Exec("INSERT INTO search (orgid, documentid, itemid, itemtype, content) VALUES (?, ?, ?, ?, ?)",
ctx.OrgID, p.DocumentID, p.RefID, "page", p.Title)
if err != nil {
err = errors.Wrap(err, "execute insert document page title entry")
}
2017-08-15 19:41:44 +01:00
return nil
}
2017-08-15 19:41:44 +01:00
// DeleteContent removes all search entries for specific document content.
func (s Scope) DeleteContent(ctx domain.RequestContext, pageID string) (err error) {
// remove all search entries
var stmt1 *sqlx.Stmt
2017-08-15 20:29:35 +01:00
stmt1, err = ctx.Transaction.Preparex("DELETE FROM search WHERE orgid=? AND itemid=? AND itemtype=?")
2017-08-15 19:41:44 +01:00
defer streamutil.Close(stmt1)
if err != nil {
2017-08-15 19:41:44 +01:00
err = errors.Wrap(err, "prepare delete document content entry")
return
}
2017-08-15 20:29:35 +01:00
_, err = stmt1.Exec(ctx.OrgID, pageID, "page")
if err != nil {
2017-08-15 19:41:44 +01:00
err = errors.Wrap(err, "execute delete document content entry")
return
}
return
}
// Documents searches the documents that the client is allowed to see, using the keywords search string, then audits that search.
// Visible documents include both those in the client's own organisation and those that are public, or whose visibility includes the client.
2017-08-15 14:15:31 +01:00
func (s Scope) Documents(ctx domain.RequestContext, q search.QueryOptions) (results []search.QueryResult, err error) {
q.Keywords = strings.TrimSpace(q.Keywords)
if len(q.Keywords) == 0 {
return
}
2017-08-15 14:15:31 +01:00
results = []search.QueryResult{}
2017-08-15 14:15:31 +01:00
// Match doc names
if q.Doc {
r1, err1 := s.matchFullText(ctx, q.Keywords, "doc")
if err1 != nil {
err = errors.Wrap(err1, "search document names")
return
}
2017-08-15 14:15:31 +01:00
results = append(results, r1...)
}
2017-08-15 14:15:31 +01:00
// Match doc content
if q.Content {
r2, err2 := s.matchFullText(ctx, q.Keywords, "page")
if err2 != nil {
err = errors.Wrap(err2, "search document content")
return
}
2017-08-15 14:15:31 +01:00
results = append(results, r2...)
}
2017-08-15 14:15:31 +01:00
// Match doc tags
if q.Tag {
r3, err3 := s.matchFullText(ctx, q.Keywords, "tag")
if err3 != nil {
err = errors.Wrap(err3, "search document tag")
return
}
2017-08-15 14:15:31 +01:00
results = append(results, r3...)
}
2017-08-15 14:15:31 +01:00
// Match doc attachments
if q.Attachment {
r4, err4 := s.matchLike(ctx, q.Keywords, "file")
if err4 != nil {
err = errors.Wrap(err4, "search document attachments")
return
}
2017-08-15 14:15:31 +01:00
results = append(results, r4...)
}
2018-03-30 17:03:18 +01:00
if len(results) == 0 {
results = []search.QueryResult{}
}
2017-08-15 14:15:31 +01:00
return
}
func (s Scope) matchFullText(ctx domain.RequestContext, keywords, itemType string) (r []search.QueryResult, err error) {
sql1 := `
2018-03-19 15:04:02 +00:00
SELECT
s.id, s.orgid, s.documentid, s.itemid, s.itemtype,
d.labelid as spaceid, COALESCE(d.title,'Unknown') AS document, d.tags,
d.excerpt, d.template, d.versionid,
2017-08-15 14:15:31 +01:00
COALESCE(l.label,'Unknown') AS space
FROM
search s,
document d
2018-03-19 15:04:02 +00:00
LEFT JOIN
2017-08-15 14:15:31 +01:00
label l ON l.orgid=d.orgid AND l.refid = d.labelid
WHERE
s.orgid = ?
AND s.itemtype = ?
2018-03-19 15:04:02 +00:00
AND s.documentid = d.refid
AND d.labelid IN
(
2018-03-19 15:04:02 +00:00
SELECT refid FROM label WHERE orgid=? AND refid IN
(
2018-03-30 17:03:18 +01:00
SELECT refid from permission WHERE orgid=? AND who='user' AND (whoid=? OR whoid='0') AND location='space'
UNION ALL
2018-03-30 17:03:18 +01:00
SELECT p.refid from permission p LEFT JOIN rolemember r ON p.whoid=r.roleid WHERE p.orgid=? AND p.who='role'
AND p.location='space' AND (r.userid=? OR r.userid='0')
2018-03-19 15:04:02 +00:00
)
)
AND MATCH(s.content) AGAINST(? IN BOOLEAN MODE)`
2017-08-15 14:15:31 +01:00
err = s.Runtime.Db.Select(&r,
sql1,
ctx.OrgID,
2017-08-15 14:15:31 +01:00
itemType,
ctx.OrgID,
ctx.OrgID,
ctx.UserID,
ctx.OrgID,
2017-08-15 14:15:31 +01:00
ctx.UserID,
keywords)
if err == sql.ErrNoRows {
err = nil
r = []search.QueryResult{}
}
if err != nil {
err = errors.Wrap(err, "search document "+itemType)
}
return
}
func (s Scope) matchLike(ctx domain.RequestContext, keywords, itemType string) (r []search.QueryResult, err error) {
// LIKE clause does not like quotes!
keywords = strings.Replace(keywords, "'", "", -1)
keywords = strings.Replace(keywords, "\"", "", -1)
keywords = strings.Replace(keywords, "%", "", -1)
keywords = fmt.Sprintf("%%%s%%", keywords)
sql1 := `
2018-03-19 15:04:02 +00:00
SELECT
s.id, s.orgid, s.documentid, s.itemid, s.itemtype,
d.labelid as spaceid, COALESCE(d.title,'Unknown') AS document, d.tags, d.excerpt,
2017-08-15 14:15:31 +01:00
COALESCE(l.label,'Unknown') AS space
FROM
search s,
document d
2018-03-19 15:04:02 +00:00
LEFT JOIN
2017-08-15 14:15:31 +01:00
label l ON l.orgid=d.orgid AND l.refid = d.labelid
WHERE
s.orgid = ?
AND s.itemtype = ?
2018-03-19 15:04:02 +00:00
AND s.documentid = d.refid
2017-08-15 14:15:31 +01:00
-- AND d.template = 0
2018-03-19 15:04:02 +00:00
AND d.labelid IN
(
2018-03-30 17:03:18 +01:00
SELECT refid FROM label WHERE orgid=? AND refid IN
(
SELECT refid from permission WHERE orgid=? AND who='user' AND (whoid=? OR whoid='0') AND location='space'
UNION ALL
SELECT p.refid from permission p LEFT JOIN rolemember r ON p.whoid=r.roleid WHERE p.orgid=? AND p.who='role'
2018-03-30 17:03:18 +01:00
AND p.location='space' AND (r.userid=? OR r.userid='0')
)
)
2017-08-15 14:15:31 +01:00
AND s.content LIKE ?`
err = s.Runtime.Db.Select(&r,
sql1,
ctx.OrgID,
itemType,
ctx.OrgID,
ctx.OrgID,
ctx.UserID,
2017-08-15 14:15:31 +01:00
ctx.OrgID,
ctx.UserID,
keywords)
if err == sql.ErrNoRows {
err = nil
r = []search.QueryResult{}
}
if err != nil {
2017-08-15 14:15:31 +01:00
err = errors.Wrap(err, "search document "+itemType)
}
return
}