Skip to content

Commit

Permalink
Add cli to fix collection tags
Browse files Browse the repository at this point in the history
  • Loading branch information
zincarla committed Aug 23, 2022
1 parent f6056be commit c52982d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ images/*
gib
.vs
go-image-board
*.log
*.log
testtools/*
testtools\*
32 changes: 32 additions & 0 deletions gib.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func main() {
missingOnly := flag.Bool("missingonly", false, "When used with dhashonly or thumbsonly, prevents deleting pre-existing entries.")
renameFilesOnly := flag.Bool("renameonly", false, "Renames all posts and corrects the names in the database. Use if changing naming convention of files.")
removeOrphanFiles := flag.Bool("removeorphanfiles", false, "Removes images and thumbnails that do not have an associated database entry.")
fixCollectionTags := flag.Bool("fixcollectiontags", false, "Validates and fixes tags applied to all collections")
flag.Parse()

//Load succeeded
Expand Down Expand Up @@ -233,6 +234,37 @@ func main() {

return //We do not want to start server if used in cli
}
if *fixCollectionTags {
//Loop through all collections
page := uint64(0)
processedCollections := uint64(0)
for true {
collections, maxCount, err := database.DBInterface.SearchCollections([]interfaces.TagInformation{}, page, config.Configuration.PageStride)
page += config.Configuration.PageStride
if err != nil {
logging.WriteLog(logging.LogLevelError, "main/main", "0", logging.ResultFailure, []string{"Error processing hashes.", err.Error()})
break
}
if len(collections) <= 0 {
logging.WriteLog(logging.LogLevelInfo, "main/main", "0", logging.ResultInfo, []string{"Finished queing collections"})
break
}
logging.WriteLog(logging.LogLevelInfo, "main/main", "0", logging.ResultInfo, []string{"Queing", strconv.FormatUint(page, 10), "of", strconv.FormatUint(maxCount, 10)})
for _, nextCollection := range collections {
//Fix missing tags
count, err := database.DBInterface.FixCollectionTags(nextCollection.ID)
if err != nil {
logging.WriteLog(logging.LogLevelError, "main/main", "0", logging.ResultInfo, []string{"Failed to fix collection images", err.Error()})
} else if err == nil && count > 0 {
logging.WriteLog(logging.LogLevelInfo, "main/main", "0", logging.ResultInfo, []string{"Fixed colllection tags", nextCollection.Name, "rows", strconv.FormatInt(count, 10)})
}
processedCollections++
}
}
logging.WriteLog(logging.LogLevelInfo, "main/main", "0", logging.ResultInfo, []string{"Completed collection tag correction"})

return //We do not want to start server if used in cli
}
//Verify TLS Settings
if config.Configuration.UseTLS {
if _, err := os.Stat(config.Configuration.TLSCertPath); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions interfaces/dbinterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,6 @@ type DBInterface interface {
SearchCollections(Tags []TagInformation, PageStart uint64, PageStride uint64) ([]CollectionInformation, uint64, error)
//GetCollectionTags returns a list of TagInformation for all tags that apply to the given collection
GetCollectionTags(CollectionID uint64) ([]TagInformation, error)
//FixCollectionTags verifies and fixes collection tags, returns row count and error
FixCollectionTags(CollectionID uint64) (int64, error)
}
11 changes: 11 additions & 0 deletions plugins/mariadbplugin/CollectionFunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,14 @@ func (DBConnection *MariaDBPlugin) GetCollectionTags(CollectionID uint64) ([]int
}
return ToReturn, nil
}

//GetCollectionTags returns a list of TagInformation for all tags that apply to the given collection
func (DBConnection *MariaDBPlugin) FixCollectionTags(CollectionID uint64) (int64, error) {
results, err := DBConnection.DBHandle.Exec("CALL LinkCollTags(?)", CollectionID)

if err != nil {
return 0, err
}

return results.RowsAffected()
}

0 comments on commit c52982d

Please sign in to comment.