From 4c5e77dcab06cb3fe5dda115e4599f187bf29529 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Tue, 29 Jun 2021 18:11:42 +0200 Subject: [PATCH] Display count of file uses --- database.go | 2 +- database_test.go | 4 ++- postsDb.go | 13 +++++++++ postsDb_test.go | 50 +++++++++++++++++++++++++++++++++- render.go | 31 +++++++++++---------- templates/editorfiles.gohtml | 2 +- templates/strings/de.yaml | 1 + templates/strings/default.yaml | 1 + 8 files changed, 85 insertions(+), 19 deletions(-) diff --git a/database.go b/database.go index 1db4eb7..98e444b 100644 --- a/database.go +++ b/database.go @@ -70,7 +70,7 @@ func (a *goBlog) openDatabase(file string, logging bool) (*database, error) { return nil }, } - if a.cfg.Db.Debug { + if c := a.cfg.Db; c != nil && c.Debug { dr = sqlhooks.Wrap(dr, &dbHooks{}) } sql.Register("goblog_db_"+dbDriverName, dr) diff --git a/database_test.go b/database_test.go index 980ac82..570355c 100644 --- a/database_test.go +++ b/database_test.go @@ -10,7 +10,9 @@ func (a *goBlog) setInMemoryDatabase() { func Test_database(t *testing.T) { t.Run("Basic Database Test", func(t *testing.T) { - app := &goBlog{} + app := &goBlog{ + cfg: &config{}, + } db, err := app.openDatabase(":memory:", false) if err != nil { diff --git a/postsDb.go b/postsDb.go index f229eaf..4269610 100644 --- a/postsDb.go +++ b/postsDb.go @@ -428,3 +428,16 @@ func (d *database) allPublishedDates(blog string) (dates []publishedDate, err er } return } + +func (db *database) usesOfMediaFile(name string) (count int, err error) { + query := "select count(distinct path) from (select path from posts where instr(content, @name) > 0 union all select path from post_parameters where instr(value, @name) > 0)" + row, err := db.queryRow(query, sql.Named("name", name)) + if err != nil { + return 0, err + } + err = row.Scan(&count) + if err != nil { + return 0, err + } + return count, nil +} diff --git a/postsDb_test.go b/postsDb_test.go index cefb297..b9490c9 100644 --- a/postsDb_test.go +++ b/postsDb_test.go @@ -155,7 +155,9 @@ func Test_postsDb(t *testing.T) { func Test_ftsWithoutTitle(t *testing.T) { // Added because there was a bug where there were no search results without title - app := &goBlog{} + app := &goBlog{ + cfg: &config{}, + } app.setInMemoryDatabase() err := app.db.savePost(&post{ @@ -175,3 +177,49 @@ func Test_ftsWithoutTitle(t *testing.T) { assert.NoError(t, err) assert.Len(t, ps, 1) } + +func Test_usesOfMediaFile(t *testing.T) { + app := &goBlog{ + cfg: &config{}, + } + app.setInMemoryDatabase() + + err := app.db.savePost(&post{ + Path: "/test/abc", + Content: "ABC test.jpg DEF", + Published: toLocalSafe(time.Now().String()), + Blog: "en", + Section: "test", + Status: statusDraft, + }, &postCreationOptions{new: true}) + require.NoError(t, err) + + err = app.db.savePost(&post{ + Path: "/test/def", + Content: "ABCDEF", + Published: toLocalSafe(time.Now().String()), + Blog: "en", + Section: "test", + Status: statusDraft, + Parameters: map[string][]string{ + "test": { + "https://example.com/test.jpg", + }, + }, + }, &postCreationOptions{new: true}) + require.NoError(t, err) + + err = app.db.savePost(&post{ + Path: "/test/hij", + Content: "ABCDEF", + Published: toLocalSafe(time.Now().String()), + Blog: "en", + Section: "test", + Status: statusDraft, + }, &postCreationOptions{new: true}) + require.NoError(t, err) + + count, err := app.db.usesOfMediaFile("test.jpg") + require.NoError(t, err) + assert.Equal(t, 2, count) +} diff --git a/render.go b/render.go index 4d47235..8a374dc 100644 --- a/render.go +++ b/render.go @@ -55,21 +55,22 @@ func (a *goBlog) initRendering() error { "translations": a.postTranslations, "shorturl": a.shortPostURL, // Others - "dateformat": dateFormat, - "isodate": isoDateFormat, - "unixtodate": unixToLocalDateString, - "now": localNowString, - "asset": a.assetFileName, - "string": a.ts.GetTemplateStringVariantFunc(), - "include": a.includeRenderedTemplate, - "urlize": urlize, - "sort": sortedStrings, - "absolute": a.getFullAddress, - "mentions": a.db.getWebmentionsByAddress, - "geotitle": a.geoTitle, - "geolink": geoOSMLink, - "opensearch": openSearchUrl, - "mbytes": mBytesString, + "dateformat": dateFormat, + "isodate": isoDateFormat, + "unixtodate": unixToLocalDateString, + "now": localNowString, + "asset": a.assetFileName, + "string": a.ts.GetTemplateStringVariantFunc(), + "include": a.includeRenderedTemplate, + "urlize": urlize, + "sort": sortedStrings, + "absolute": a.getFullAddress, + "mentions": a.db.getWebmentionsByAddress, + "geotitle": a.geoTitle, + "geolink": geoOSMLink, + "opensearch": openSearchUrl, + "mbytes": mBytesString, + "mediafileuses": a.db.usesOfMediaFile, } baseTemplate, err := template.New("base").Funcs(templateFunctions).ParseFiles(path.Join(templatesDir, templateBase+templatesExt)) if err != nil { diff --git a/templates/editorfiles.gohtml b/templates/editorfiles.gohtml index c6538ee..3d864ab 100644 --- a/templates/editorfiles.gohtml +++ b/templates/editorfiles.gohtml @@ -10,7 +10,7 @@
diff --git a/templates/strings/de.yaml b/templates/strings/de.yaml index 1b3ca3b..0e27e4a 100644 --- a/templates/strings/de.yaml +++ b/templates/strings/de.yaml @@ -11,6 +11,7 @@ docomment: "Kommentieren" download: "Herunterladen" drafts: "Entwürfe" editor: "Editor" +fileuses: "Datei-Verwendungen" interactions: "Interaktionen & Kommentare" interactionslabel: "Hast du eine Antwort hierzu veröffentlicht? Füge hier die URL ein." likeof: "Gefällt mir von" diff --git a/templates/strings/default.yaml b/templates/strings/default.yaml index 651de92..0c3da33 100644 --- a/templates/strings/default.yaml +++ b/templates/strings/default.yaml @@ -17,6 +17,7 @@ download: "Download" drafts: "Drafts" editor: "Editor" feed: "Feed" +fileuses: "file uses" indieauth: "IndieAuth" interactions: "Interactions & Comments" interactionslabel: "Have you published a response to this? Paste the URL here."