2016-02-07 16:45:42 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2016-02-08 16:00:07 +03:00
|
|
|
"strings"
|
2016-02-07 16:45:42 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/boltdb/bolt"
|
2016-02-08 02:31:47 +03:00
|
|
|
"github.com/sergi/go-diff/diffmatchpatch"
|
2016-02-07 16:45:42 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var db *bolt.DB
|
|
|
|
var open bool
|
|
|
|
|
|
|
|
// Open to create the database and open
|
2016-02-07 21:15:42 +03:00
|
|
|
func Open(filename string) error {
|
2016-02-07 16:45:42 +03:00
|
|
|
var err error
|
|
|
|
config := &bolt.Options{Timeout: 30 * time.Second}
|
2016-02-07 21:15:42 +03:00
|
|
|
db, err = bolt.Open(filename, 0600, config)
|
2016-02-07 16:45:42 +03:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Opening BoltDB timed out")
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
open = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close database
|
|
|
|
func Close() {
|
|
|
|
open = false
|
|
|
|
db.Close()
|
|
|
|
}
|
|
|
|
|
2016-02-10 17:13:21 +03:00
|
|
|
// WikiData is data for storing in DB
|
|
|
|
type WikiData struct {
|
2016-02-08 02:31:47 +03:00
|
|
|
Title string
|
|
|
|
CurrentText string
|
|
|
|
Diffs []string
|
|
|
|
Timestamps []string
|
2016-03-14 16:42:19 +03:00
|
|
|
Encrypted bool
|
2016-03-15 04:39:09 +03:00
|
|
|
Locked string
|
2016-02-07 16:45:42 +03:00
|
|
|
}
|
|
|
|
|
2016-02-08 16:00:07 +03:00
|
|
|
func hasPassword(title string) (bool, error) {
|
|
|
|
title = strings.ToLower(title)
|
|
|
|
if !open {
|
2016-03-14 22:36:01 +03:00
|
|
|
Open(RuntimeArgs.DatabaseLocation)
|
|
|
|
defer Close()
|
2016-02-08 16:00:07 +03:00
|
|
|
}
|
|
|
|
hasPassword := false
|
|
|
|
err := db.View(func(tx *bolt.Tx) error {
|
|
|
|
var err error
|
|
|
|
b := tx.Bucket([]byte("datas"))
|
|
|
|
if b == nil {
|
2016-02-10 02:38:25 +03:00
|
|
|
return fmt.Errorf("db must be opened before loading")
|
2016-02-08 16:00:07 +03:00
|
|
|
}
|
|
|
|
k := []byte(title)
|
|
|
|
val := b.Get(k)
|
|
|
|
if val == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-02-10 17:13:21 +03:00
|
|
|
var p WikiData
|
2016-02-08 16:00:07 +03:00
|
|
|
err = p.decode(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, line := range strings.Split(p.CurrentText, "\n") {
|
|
|
|
if strings.Contains(line, "<") == true && strings.Contains(line, ">") == true && strings.Contains(line, "user") == true && strings.Contains(line, "password") == true && strings.Contains(line, "public") == true {
|
|
|
|
hasPassword = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2016-02-10 17:13:21 +03:00
|
|
|
fmt.Printf("Could not get WikiData: %s", err)
|
2016-02-08 16:00:07 +03:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return hasPassword, nil
|
|
|
|
}
|
|
|
|
|
2016-03-15 04:39:09 +03:00
|
|
|
func getCurrentText(title string, version int) (string, []versionsInfo, bool, time.Duration, bool, string) {
|
2016-03-14 22:25:09 +03:00
|
|
|
Open(RuntimeArgs.DatabaseLocation)
|
|
|
|
defer Close()
|
2016-02-08 16:37:22 +03:00
|
|
|
title = strings.ToLower(title)
|
2016-02-10 02:38:25 +03:00
|
|
|
var vi []versionsInfo
|
2016-02-11 17:26:02 +03:00
|
|
|
totalTime := time.Now().Sub(time.Now())
|
2016-02-10 02:38:25 +03:00
|
|
|
isCurrent := true
|
2016-02-08 16:37:22 +03:00
|
|
|
currentText := ""
|
2016-03-14 16:42:19 +03:00
|
|
|
encrypted := false
|
2016-03-15 04:39:09 +03:00
|
|
|
locked := ""
|
2016-02-08 16:37:22 +03:00
|
|
|
if !open {
|
2016-03-15 04:39:09 +03:00
|
|
|
return currentText, vi, isCurrent, totalTime, encrypted, locked
|
2016-02-08 16:37:22 +03:00
|
|
|
}
|
|
|
|
err := db.View(func(tx *bolt.Tx) error {
|
|
|
|
var err error
|
|
|
|
b := tx.Bucket([]byte("datas"))
|
|
|
|
if b == nil {
|
2016-02-10 02:38:25 +03:00
|
|
|
return fmt.Errorf("db must be opened before loading")
|
2016-02-08 16:37:22 +03:00
|
|
|
}
|
|
|
|
k := []byte(title)
|
|
|
|
val := b.Get(k)
|
|
|
|
if val == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-02-10 17:13:21 +03:00
|
|
|
var p WikiData
|
2016-02-08 16:37:22 +03:00
|
|
|
err = p.decode(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
currentText = p.CurrentText
|
2016-03-14 16:42:19 +03:00
|
|
|
encrypted = p.Encrypted
|
2016-03-15 04:39:09 +03:00
|
|
|
locked = p.Locked
|
2016-02-10 02:38:25 +03:00
|
|
|
if version > -1 && version < len(p.Diffs) {
|
|
|
|
// get that version of text instead
|
|
|
|
currentText = rebuildTextsToDiffN(p, version)
|
|
|
|
isCurrent = false
|
|
|
|
}
|
2016-02-11 17:26:02 +03:00
|
|
|
vi, totalTime = getImportantVersions(p)
|
|
|
|
log.Println(totalTime)
|
2016-02-08 16:37:22 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2016-02-10 17:13:21 +03:00
|
|
|
fmt.Printf("Could not get WikiData: %s", err)
|
2016-02-08 16:37:22 +03:00
|
|
|
}
|
2016-03-15 04:39:09 +03:00
|
|
|
return currentText, vi, isCurrent, totalTime, encrypted, locked
|
2016-02-08 16:37:22 +03:00
|
|
|
}
|
|
|
|
|
2016-02-10 17:13:21 +03:00
|
|
|
func (p *WikiData) load(title string) error {
|
2016-02-08 16:00:07 +03:00
|
|
|
title = strings.ToLower(title)
|
2016-02-07 16:45:42 +03:00
|
|
|
if !open {
|
2016-03-14 22:36:01 +03:00
|
|
|
Open(RuntimeArgs.DatabaseLocation)
|
|
|
|
defer Close()
|
2016-02-07 16:45:42 +03:00
|
|
|
}
|
|
|
|
err := db.View(func(tx *bolt.Tx) error {
|
|
|
|
var err error
|
|
|
|
b := tx.Bucket([]byte("datas"))
|
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-02-08 02:31:47 +03:00
|
|
|
k := []byte(title)
|
2016-02-07 16:45:42 +03:00
|
|
|
val := b.Get(k)
|
|
|
|
if val == nil {
|
2016-02-08 02:31:47 +03:00
|
|
|
// make new one
|
|
|
|
p.Title = title
|
|
|
|
p.CurrentText = ""
|
|
|
|
p.Diffs = []string{}
|
|
|
|
p.Timestamps = []string{}
|
2016-02-07 16:45:42 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err = p.decode(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2016-02-10 17:13:21 +03:00
|
|
|
fmt.Printf("Could not get WikiData: %s", err)
|
2016-02-07 16:45:42 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-10 17:13:21 +03:00
|
|
|
func (p *WikiData) save(newText string) error {
|
2016-02-07 16:45:42 +03:00
|
|
|
if !open {
|
2016-03-14 22:36:01 +03:00
|
|
|
Open(RuntimeArgs.DatabaseLocation)
|
|
|
|
defer Close()
|
2016-02-07 16:45:42 +03:00
|
|
|
}
|
|
|
|
err := db.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket, err := tx.CreateBucketIfNotExists([]byte("datas"))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("create bucket: %s", err)
|
|
|
|
}
|
2016-02-08 02:31:47 +03:00
|
|
|
// find diffs
|
|
|
|
dmp := diffmatchpatch.New()
|
|
|
|
diffs := dmp.DiffMain(p.CurrentText, newText, true)
|
|
|
|
delta := dmp.DiffToDelta(diffs)
|
|
|
|
p.CurrentText = newText
|
2016-02-10 01:52:52 +03:00
|
|
|
p.Timestamps = append(p.Timestamps, time.Now().Format(time.ANSIC))
|
2016-02-08 02:31:47 +03:00
|
|
|
p.Diffs = append(p.Diffs, delta)
|
2016-02-07 16:45:42 +03:00
|
|
|
enc, err := p.encode()
|
|
|
|
if err != nil {
|
2016-02-10 17:13:21 +03:00
|
|
|
return fmt.Errorf("could not encode WikiData: %s", err)
|
2016-02-07 16:45:42 +03:00
|
|
|
}
|
2016-02-14 17:05:56 +03:00
|
|
|
p.Title = strings.ToLower(p.Title)
|
2016-02-07 16:45:42 +03:00
|
|
|
err = bucket.Put([]byte(p.Title), enc)
|
2016-02-14 17:05:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could add to bucket: %s", err)
|
|
|
|
}
|
2016-02-07 16:45:42 +03:00
|
|
|
return err
|
|
|
|
})
|
2016-02-14 17:31:30 +03:00
|
|
|
// // Add the new name to the programdata so its not randomly generated
|
|
|
|
// if err == nil && len(p.Timestamps) > 0 && len(p.CurrentText) > 0 {
|
|
|
|
// err2 := db.Update(func(tx *bolt.Tx) error {
|
|
|
|
// b := tx.Bucket([]byte("programdata"))
|
|
|
|
// id, _ := b.NextSequence()
|
|
|
|
// idInt := int(id)
|
|
|
|
// return b.Put(itob(idInt), []byte(p.Title))
|
|
|
|
// })
|
|
|
|
// if err2 != nil {
|
|
|
|
// return fmt.Errorf("could not add to programdata: %s", err)
|
|
|
|
// }
|
|
|
|
// }
|
2016-02-07 16:45:42 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-10 17:13:21 +03:00
|
|
|
func (p *WikiData) encode() ([]byte, error) {
|
2016-02-07 16:45:42 +03:00
|
|
|
enc, err := json.Marshal(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return enc, nil
|
|
|
|
}
|
|
|
|
|
2016-02-10 17:13:21 +03:00
|
|
|
func (p *WikiData) decode(data []byte) error {
|
2016-02-07 16:45:42 +03:00
|
|
|
err := json.Unmarshal(data, &p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|