summaryrefslogtreecommitdiff
path: root/internal/anki/sqlite_schemer.go
blob: 2c616738a1ba43234fe19254ddbc60956d3b1f98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package anki

import (
	"crypto/sha1"
	"database/sql"
	"encoding/binary"
	"encoding/json"
	"fmt"
	"math/rand"
	"path/filepath"
	"strings"
	"time"

	_ "github.com/mattn/go-sqlite3"
)

// SQLiteSchemer creates and populates the Anki SQLite database (collection.anki2).
type SQLiteSchemer struct{}

// NewSQLiteSchemer returns a SQLiteSchemer ready for use.
func NewSQLiteSchemer() *SQLiteSchemer {
	return &SQLiteSchemer{}
}

// CreateDatabase opens dbPath, applies schema, and inserts collection metadata plus notes/cards.
func (s *SQLiteSchemer) CreateDatabase(dbPath string, g *APKGGenerator, tmpl *CardTemplate) error {
	db, err := sql.Open("sqlite3", dbPath)
	if err != nil {
		return err
	}
	defer func() {
		_ = db.Close()
	}()

	if err := s.createTables(db); err != nil {
		return fmt.Errorf("failed to create tables: %w", err)
	}

	if err := s.insertCollection(db, g, tmpl); err != nil {
		return fmt.Errorf("failed to insert collection: %w", err)
	}

	if err := s.insertNotesAndCards(db, g); err != nil {
		return fmt.Errorf("failed to insert notes and cards: %w", err)
	}

	return nil
}

func (s *SQLiteSchemer) createTables(db *sql.DB) error {
	queries := []string{
		`CREATE TABLE col (
			id integer PRIMARY KEY,
			crt integer NOT NULL,
			mod integer NOT NULL,
			scm integer NOT NULL,
			ver integer NOT NULL,
			dty integer NOT NULL,
			usn integer NOT NULL,
			ls integer NOT NULL,
			conf text NOT NULL,
			models text NOT NULL,
			decks text NOT NULL,
			dconf text NOT NULL,
			tags text NOT NULL
		)`,
		`CREATE TABLE notes (
			id integer PRIMARY KEY,
			guid text NOT NULL,
			mid integer NOT NULL,
			mod integer NOT NULL,
			usn integer NOT NULL,
			tags text NOT NULL,
			flds text NOT NULL,
			sfld text NOT NULL,
			csum integer NOT NULL,
			flags integer NOT NULL,
			data text NOT NULL
		)`,
		`CREATE TABLE cards (
			id integer PRIMARY KEY,
			nid integer NOT NULL,
			did integer NOT NULL,
			ord integer NOT NULL,
			mod integer NOT NULL,
			usn integer NOT NULL,
			type integer NOT NULL,
			queue integer NOT NULL,
			due integer NOT NULL,
			ivl integer NOT NULL,
			factor integer NOT NULL,
			reps integer NOT NULL,
			lapses integer NOT NULL,
			left integer NOT NULL,
			odue integer NOT NULL,
			odid integer NOT NULL,
			flags integer NOT NULL,
			data text NOT NULL
		)`,
		`CREATE TABLE revlog (
			id integer PRIMARY KEY,
			cid integer NOT NULL,
			usn integer NOT NULL,
			ease integer NOT NULL,
			ivl integer NOT NULL,
			lastIvl integer NOT NULL,
			factor integer NOT NULL,
			time integer NOT NULL,
			type integer NOT NULL
		)`,
		`CREATE TABLE graves (
			usn integer NOT NULL,
			oid integer NOT NULL,
			type integer NOT NULL
		)`,
		`CREATE INDEX ix_notes_csum ON notes (csum)`,
		`CREATE INDEX ix_notes_usn ON notes (usn)`,
		`CREATE INDEX ix_cards_usn ON cards (usn)`,
		`CREATE INDEX ix_cards_nid ON cards (nid)`,
		`CREATE INDEX ix_cards_sched ON cards (did, queue, due)`,
		`CREATE INDEX ix_revlog_usn ON revlog (usn)`,
		`CREATE INDEX ix_revlog_cid ON revlog (cid)`,
	}

	for _, query := range queries {
		if _, err := db.Exec(query); err != nil {
			return fmt.Errorf("failed to execute query: %w", err)
		}
	}

	return nil
}

func (s *SQLiteSchemer) insertCollection(db *sql.DB, g *APKGGenerator, tmpl *CardTemplate) error {
	now := time.Now().Unix()

	decks := map[string]interface{}{
		"1": map[string]interface{}{
			"id":               1,
			"name":             "Default",
			"mod":              now,
			"desc":             "",
			"collapsed":        false,
			"dyn":              0,
			"conf":             1,
			"usn":              0,
			"newToday":         []int{0, 0},
			"revToday":         []int{0, 0},
			"lrnToday":         []int{0, 0},
			"timeToday":        []int{0, 0},
			"browserCollapsed": false,
			"extendNew":        10,
			"extendRev":        50,
		},
		fmt.Sprintf("%d", g.deckID): map[string]interface{}{
			"id":               g.deckID,
			"name":             g.deckName,
			"mod":              now,
			"desc":             "Bulgarian vocabulary cards created by TotalRecall",
			"collapsed":        false,
			"dyn":              0,
			"conf":             1,
			"usn":              0,
			"newToday":         []int{0, 0},
			"revToday":         []int{0, 0},
			"lrnToday":         []int{0, 0},
			"timeToday":        []int{0, 0},
			"browserCollapsed": false,
			"extendNew":        10,
			"extendRev":        50,
		},
	}
	decksJSON, err := marshalJSON("decks", decks)
	if err != nil {
		return err
	}

	models := map[string]interface{}{
		fmt.Sprintf("%d", g.modelID):     tmpl.EnBgNoteTypeConfig(g.modelID, g.deckID),
		fmt.Sprintf("%d", g.modelIDBgBg): tmpl.BgBgNoteTypeConfig(g.modelIDBgBg, g.deckID),
	}
	modelsJSON, err := marshalJSON("models", models)
	if err != nil {
		return err
	}

	conf := map[string]interface{}{
		"nextPos":       1,
		"estTimes":      true,
		"activeDecks":   []int64{1},
		"sortType":      "noteFld",
		"sortBackwards": false,
		"addToCur":      true,
		"curDeck":       1,
		"newSpread":     0,
		"dueCounts":     true,
		"collapseTime":  1200,
		"timeLim":       0,
		"schedVer":      1,
		"curModel":      fmt.Sprintf("%d", g.modelID),
		"dayLearnFirst": false,
	}
	confJSON, err := marshalJSON("conf", conf)
	if err != nil {
		return err
	}

	dconf := map[string]interface{}{
		"1": map[string]interface{}{
			"id":   1,
			"name": "Default",
			"dyn":  0,
			"new": map[string]interface{}{
				"delays":        []int{1, 10},
				"ints":          []int{1, 4, 7},
				"initialFactor": 2500,
				"perDay":        20,
				"order":         1,
				"bury":          true,
				"separate":      true,
			},
			"lapse": map[string]interface{}{
				"delays":      []int{10},
				"mult":        0,
				"minInt":      1,
				"leechFails":  8,
				"leechAction": 0,
			},
			"rev": map[string]interface{}{
				"perDay":   100,
				"ease4":    1.3,
				"fuzz":     0.05,
				"maxIvl":   36500,
				"ivlFct":   1,
				"bury":     true,
				"minSpace": 1,
			},
			"timer":    0,
			"maxTaken": 60,
			"usn":      0,
			"mod":      now,
			"autoplay": true,
			"replayq":  true,
		},
	}
	dconfJSON, err := marshalJSON("dconf", dconf)
	if err != nil {
		return err
	}

	query := `INSERT INTO col VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
	_, err = db.Exec(query,
		1,
		now,
		now*1000,
		now*1000,
		11,
		0,
		0,
		0,
		string(confJSON),
		string(modelsJSON),
		string(decksJSON),
		string(dconfJSON),
		"{}",
	)
	return err
}

func marshalJSON(name string, value any) ([]byte, error) {
	data, err := json.Marshal(value)
	if err != nil {
		return nil, fmt.Errorf("marshal %s: %w", name, err)
	}

	return data, nil
}

func (s *SQLiteSchemer) insertNotesAndCards(db *sql.DB, g *APKGGenerator) error {
	now := time.Now()

	for i, card := range g.cards {
		noteID := now.UnixMilli() + int64(i*3)
		cardID1 := noteID + 1
		cardID2 := noteID + 2

		isBgBg := card.CardType == "bg-bg"

		imageField := buildMediaField(card.ImageFile, g.mediaFiles, func(name string) string {
			return fmt.Sprintf(`<img src="%s">`, name)
		})
		audioField := buildMediaField(card.AudioFile, g.mediaFiles, func(name string) string {
			return fmt.Sprintf("[sound:%s]", name)
		})
		audioFieldBack := buildMediaField(card.AudioFileBack, g.mediaFiles, func(name string) string {
			return fmt.Sprintf("[sound:%s]", name)
		})

		var fields string
		var modelID int64
		var guid string

		if isBgBg {
			fields = strings.Join([]string{
				card.Bulgarian,
				card.Translation,
				imageField,
				audioField,
				audioFieldBack,
				card.Notes,
			}, "\x1f")
			modelID = g.modelIDBgBg
			guid = ankiGUID(fmt.Sprintf("tr_bgbg_%s", card.Bulgarian))
		} else {
			english := card.Translation
			if english == "" {
				english = "Translation needed"
			}
			fields = strings.Join([]string{
				english,
				card.Bulgarian,
				imageField,
				audioField,
				card.Notes,
			}, "\x1f")
			modelID = g.modelID
			guid = ankiGUID(fmt.Sprintf("tr_%s", card.Bulgarian))
		}

		csum := fieldChecksum(card.Bulgarian)

		noteQuery := `INSERT INTO notes VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
		_, err := db.Exec(noteQuery,
			noteID,
			guid,
			modelID,
			now.Unix(),
			-1,
			"",
			fields,
			card.Bulgarian,
			csum,
			0,
			"",
		)
		if err != nil {
			return fmt.Errorf("failed to insert note: %w", err)
		}

		// For new cards (type=0), due is the position in the new-card queue
		dueForward := i * 2
		dueReverse := i*2 + 1

		cardQuery := `INSERT INTO cards VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
		_, err = db.Exec(cardQuery,
			cardID1, noteID, g.deckID,
			0,            // ord
			now.Unix(),   // mod
			-1,           // usn
			0,            // type (new)
			0,            // queue (new)
			dueForward,   // due (position in new queue)
			0, 0, 0, 0, 0, 0, 0, 0, "",
		)
		if err != nil {
			return fmt.Errorf("failed to insert forward card: %w", err)
		}

		_, err = db.Exec(cardQuery,
			cardID2, noteID, g.deckID,
			1,            // ord
			now.Unix(),   // mod
			-1,           // usn
			0,            // type (new)
			0,            // queue (new)
			dueReverse,   // due (position in new queue)
			0, 0, 0, 0, 0, 0, 0, 0, "",
		)
		if err != nil {
			return fmt.Errorf("failed to insert reverse card: %w", err)
		}
	}

	return nil
}

// buildMediaField resolves a media file path to an Anki field string using the formatter.
func buildMediaField(filePath string, mediaFiles map[string]int, formatter func(string) string) string {
	if filePath == "" || !fileExists(filePath) {
		return ""
	}
	cardDirID := filepath.Base(filepath.Dir(filePath))
	originalFilename := filepath.Base(filePath)
	uniqueFilename := fmt.Sprintf("%s_%s", cardDirID, originalFilename)
	if _, ok := mediaFiles[uniqueFilename]; ok {
		return formatter(uniqueFilename)
	}
	return ""
}

// fieldChecksum computes Anki's csum: first 4 bytes of SHA-1 of the sort field, as uint32.
func fieldChecksum(sortField string) uint32 {
	h := sha1.Sum([]byte(sortField))
	return binary.BigEndian.Uint32(h[:4])
}

// ankiGUID generates a short, stable, base91-style GUID from a seed string.
// Anki expects GUIDs to be ~10 characters. We hash the seed for stability
// (same word always produces the same GUID) and encode as base91.
func ankiGUID(seed string) string {
	h := sha1.Sum([]byte(seed))
	return base91Encode(h[:8])
}

// base91Encode encodes bytes into Anki's base91 character set (same as Anki's Python implementation).
func base91Encode(data []byte) string {
	const table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" +
		"!#$%&()*+,-./:;<=>?@[]^_`{|}~"
	// Use a simple encoding: convert to a big random-ish number via the hash bytes
	// and repeatedly divide by 91
	var result []byte
	// Treat data as a big-endian unsigned integer
	val := uint64(0)
	for _, b := range data {
		val = val*256 + uint64(b)
	}
	for val > 0 {
		result = append(result, table[val%91])
		val /= 91
	}
	if len(result) == 0 {
		// Fallback: generate a random GUID
		r := rand.New(rand.NewSource(time.Now().UnixNano()))
		for i := 0; i < 10; i++ {
			result = append(result, table[r.Intn(91)])
		}
	}
	return string(result)
}