forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollection.kt
More file actions
707 lines (612 loc) · 23.3 KB
/
Collection.kt
File metadata and controls
707 lines (612 loc) · 23.3 KB
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/****************************************************************************************
* Copyright (c) 2011 Norbert Nagold <norbert.nagold@gmail.com> *
* Copyright (c) 2012 Kostas Spyropoulos <inigo.aldana@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify it under *
* the terms of the GNU General private License as published by the Free Software *
* Foundation; either version 3 of the License, or (at your option) any later *
* version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT ANY *
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
* PARTICULAR PURPOSE. See the GNU General private License for more details. *
* *
* You should have received a copy of the GNU General private License along with *
* this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************************/
// "FunctionName": many libAnki functions used to have leading _s
@file:Suppress("FunctionName")
package com.ichi2.libanki
import androidx.annotation.CheckResult
import androidx.annotation.VisibleForTesting
import androidx.annotation.WorkerThread
import anki.card_rendering.EmptyCardsReport
import anki.collection.OpChanges
import anki.collection.OpChangesWithCount
import anki.config.ConfigKey
import anki.config.Preferences
import anki.config.copy
import anki.config.preferences
import anki.search.SearchNode
import anki.sync.SyncAuth
import anki.sync.SyncStatusResponse
import com.ichi2.libanki.Utils.ids2str
import com.ichi2.libanki.backend.model.toBackendNote
import com.ichi2.libanki.backend.model.toProtoBuf
import com.ichi2.libanki.exception.ConfirmModSchemaException
import com.ichi2.libanki.exception.InvalidSearchException
import com.ichi2.libanki.sched.DummyScheduler
import com.ichi2.libanki.sched.Scheduler
import com.ichi2.libanki.utils.NotInLibAnki
import com.ichi2.libanki.utils.TimeManager
import com.ichi2.utils.KotlinCleanup
import net.ankiweb.rsdroid.Backend
import net.ankiweb.rsdroid.RustCleanup
import net.ankiweb.rsdroid.exceptions.BackendInvalidInputException
import timber.log.Timber
import java.io.*
import java.util.*
// Anki maintains a cache of used tags so it can quickly present a list of tags
// for autocomplete and in the browser. For efficiency, deletions are not
// tracked, so unused tags can only be removed from the list with a DB check.
//
// This module manages the tag cache and tags for notes.
@KotlinCleanup("inline function in init { } so we don't need to init `crt` etc... at the definition")
@WorkerThread
open class Collection(
/**
* The path to the collection.anki2 database. Must be unicode and openable with [File].
*/
val path: String,
/**
* Outside of libanki, you should not access the backend directly for collection operations.
* Operations that work on a closed collection (eg importing), or do not require a collection
* at all (eg translations) are the exception.
*/
val backend: Backend
) {
/** Access backend translations */
val tr = backend.tr
@get:JvmName("isDbClosed")
val dbClosed: Boolean
get() {
return dbInternal == null
}
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
fun debugEnsureNoOpenPointers() {
val result = backend.getActiveSequenceNumbers()
if (result.isNotEmpty()) {
val numbers = result.toString()
throw IllegalStateException("Contained unclosed sequence numbers: $numbers")
}
}
// a lot of legacy code does not check for nullability
val db: DB
get() = dbInternal!!
var dbInternal: DB? = null
/**
* Getters/Setters ********************************************************** *************************************
*/
val media: Media
lateinit var decks: Decks
protected set
val tags: Tags
lateinit var config: Config
@KotlinCleanup("see if we can inline a function inside init {} and make this `val`")
lateinit var sched: Scheduler
protected set
private var startTime: Long
private var startReps: Int
val mod: Long
get() = db.queryLongScalar("select mod from col")
val crt: Long
get() = db.queryLongScalar("select crt from col")
val scm: Long
get() = db.queryLongScalar("select scm from col")
private val lastSync: Long
get() = db.queryLongScalar("select ls from col")
fun usn(): Int {
return -1
}
var ls: Long = 0
// END: SQL table columns
init {
media = Media(this)
tags = Tags(this)
val created = reopen()
startReps = 0
startTime = 0
_loadScheduler()
if (created) {
config.set("schedVer", 2)
// we need to reload the scheduler: this was previously loaded as V1
_loadScheduler()
}
}
fun name(): String {
// TODO:
return File(path).name.replace(".anki2", "")
}
/**
* Scheduler
* ***********************************************************
*/
fun schedVer(): Int {
// schedVer was not set on legacy v1 collections
val ver = config.get("schedVer") ?: 1
return if (listOf(1, 2).contains(ver)) {
ver
} else {
throw RuntimeException("Unsupported scheduler version")
}
}
fun _loadScheduler() {
val ver = schedVer()
if (ver == 1) {
sched = DummyScheduler(this)
} else if (ver == 2) {
if (!backend.getConfigBool(ConfigKey.Bool.SCHED_2021)) {
backend.setConfigBool(ConfigKey.Bool.SCHED_2021, true, undoable = false)
}
sched = Scheduler(this)
if (config.get<Int>("creationOffset") == null) {
val prefs = getPreferences().copy {
scheduling = scheduling.copy { newTimezone = true }
}
setPreferences(prefs)
}
}
}
/**
* Disconnect from DB.
* Python implementation has a save argument for legacy reasons;
* AnkiDroid always saves as changes are made.
*/
@Synchronized
fun close(downgrade: Boolean = false, forFullSync: Boolean = false) {
if (!dbClosed) {
if (!forFullSync) {
backend.closeCollection(downgrade)
}
dbInternal = null
Timber.i("Collection closed")
}
}
/** True if DB was created */
fun reopen(afterFullSync: Boolean = false): Boolean {
Timber.i("(Re)opening Database: %s", path)
return if (dbClosed) {
val (db_, created) = Storage.openDB(path, backend, afterFullSync)
dbInternal = db_
load()
if (afterFullSync) {
_loadScheduler()
}
created
} else {
false
}
}
fun load() {
notetypes = Notetypes(this)
decks = Decks(this)
config = Config(backend)
}
/** Note: not in libanki. Mark schema modified to force a full
* sync, but with the confirmation checking function disabled This
* is equivalent to `modSchema(False)` in Anki. A distinct method
* is used so that the type does not states that an exception is
* thrown when in fact it is never thrown.
*/
open fun modSchemaNoCheck() {
db.execute(
"update col set scm=?, mod=?",
TimeManager.time.intTimeMS(),
TimeManager.time.intTimeMS()
)
}
/** Mark schema modified to cause a one-way sync.
* ConfirmModSchemaException will be thrown if the user needs to be prompted to confirm the action.
* If the user chooses to confirm then modSchemaNoCheck should be called, after which the exception can
* be safely ignored, and the outer code called again.
*
* @throws ConfirmModSchemaException
*/
@Throws(ConfirmModSchemaException::class)
fun modSchema() {
if (!schemaChanged()) {
/* In Android we can't show a dialog which blocks the main UI thread
Therefore we can't wait for the user to confirm if they want to do
a one-way sync here, and we instead throw an exception asking the outer
code to handle the user's choice */
throw ConfirmModSchemaException()
}
modSchemaNoCheck()
}
/** True if schema changed since last sync. */
fun schemaChanged(): Boolean {
return scm > lastSync
}
/**
* Object creation helpers **************************************************
* *********************************************
*/
fun getCard(id: Long): Card {
return Card(this, id)
}
fun updateCards(cards: Iterable<Card>, skipUndoEntry: Boolean = false): OpChanges {
return backend.updateCards(cards.map { it.toBackendCard() }, skipUndoEntry)
}
fun updateCard(card: Card, skipUndoEntry: Boolean = false): OpChanges {
return updateCards(listOf(card), skipUndoEntry)
}
fun getNote(id: Long): Note {
return Note(this, id)
}
/**
* Notes ******************************************************************** ***************************
*/
fun noteCount(): Int {
return db.queryScalar("SELECT count() FROM notes")
}
/**
* Return a new note with the model derived from the deck or the configuration
* @param forDeck When true it uses the model specified in the deck (mid), otherwise it uses the model specified in
* the configuration (curModel)
* @return The new note
*/
fun newNote(forDeck: Boolean = true): Note {
return newNote(notetypes.current(forDeck))
}
/**
* Return a new note with a specific model
* @param m The model to use for the new note
* @return The new note
*/
fun newNote(m: NotetypeJson): Note {
return Note.fromNotetypeId(m.id)
}
/**
* Cards ******************************************************************** ***************************
*/
val isEmpty: Boolean
get() = db.queryScalar("SELECT 1 FROM cards LIMIT 1") == 0
fun cardCount(): Int {
return db.queryScalar("SELECT count() FROM cards")
}
// NOT IN LIBANKI //
fun cardCount(vararg dids: Long): Int {
return db.queryScalar("SELECT count() FROM cards WHERE did IN " + ids2str(dids))
}
fun isEmptyDeck(vararg dids: Long): Boolean {
return cardCount(*dids) == 0
}
/*
Finding cards ************************************************************ ***********************************
*/
/**
* Construct a search string from the provided search nodes. For example:
* */
/*
import anki.search.searchNode
import anki.search.SearchNode
import anki.search.SearchNodeKt.group
val node = searchNode {
group = SearchNodeKt.group {
joiner = SearchNode.Group.Joiner.AND
nodes += searchNode { deck = "a **test** deck" }
nodes += searchNode {
negated = searchNode {
tag = "foo"
}
}
nodes += searchNode { flag = SearchNode.Flag.FLAG_GREEN }
}
}
// yields "deck:a \*\*test\*\* deck" -tag:foo flag:3
val text = col.buildSearchString(node)
}
*/
@Suppress("unused")
fun buildSearchString(node: SearchNode): String {
return backend.buildSearchString(node)
}
/**
* Return a list of card ids
* @throws InvalidSearchException
*/
fun findCards(
search: String,
order: SortOrder = SortOrder.NoOrdering()
): List<CardId> {
val adjustedOrder = if (order is SortOrder.UseCollectionOrdering) {
SortOrder.BuiltinSortKind(
config.get("sortType") ?: "noteFld",
config.get("sortBackwards") ?: false
)
} else {
order
}
return try {
backend.searchCards(search, adjustedOrder.toProtoBuf())
} catch (e: BackendInvalidInputException) {
throw InvalidSearchException(e)
}
}
fun findNotes(
query: String,
order: SortOrder = SortOrder.NoOrdering()
): List<Long> {
val adjustedOrder = if (order is SortOrder.UseCollectionOrdering) {
SortOrder.BuiltinSortKind(
config.get("noteSortType") ?: "noteFld",
config.get("browserNoteSortBackwards") ?: false
)
} else {
order
}
val noteIDsList = try {
backend.searchNotes(query, adjustedOrder.toProtoBuf())
} catch (e: BackendInvalidInputException) {
throw InvalidSearchException(e)
}
return noteIDsList
}
data class CardIdToNoteId(val id: Long, val nid: Long)
/** Return a list of card ids */
@RustCleanup("Remove in V16.") // Not in libAnki
fun findOneCardByNote(query: String, order: SortOrder): List<CardId> {
// This function shouldn't exist and CardBrowser should be modified to use Notes,
// so not much effort was expended here
val noteIds = findNotes(query, order)
// select the card with the lowest `ord` to show
val cursor = db.query(
"""
SELECT c.id, card_with_min_ord.nid
FROM (
SELECT nid, MIN(ord) AS ord
FROM cards
WHERE nid IN ${ids2str(noteIds)}
GROUP BY nid
) AS card_with_min_ord
JOIN cards AS c ON card_with_min_ord.nid = c.nid AND card_with_min_ord.ord = c.ord
""".trimMargin()
)
val resultList = mutableListOf<CardIdToNoteId>()
cursor.use { cur ->
while (cur.moveToNext()) {
val id = cur.getLong(cur.getColumnIndex("id"))
val nid = cur.getLong(cur.getColumnIndex("nid"))
resultList.add(CardIdToNoteId(id, nid))
}
}
// sort resultList by nid
val noteIdMap = noteIds.mapIndexed { index, id -> id to index }.toMap()
val sortedResultList = resultList.sortedBy { noteIdMap[it.nid] }
// Extract ids from sortedResultList
return sortedResultList.map { it.id }
}
@RustCleanup("Calling code should handle returned OpChanges")
fun findReplace(nids: List<Long>, src: String, dst: String, regex: Boolean = false, field: String? = null, fold: Boolean = true): Int {
return backend.findAndReplace(nids, src, dst, regex, !fold, field ?: "").count
}
/*
Stats ******************************************************************** ***************************
*/
// card stats
// stats
/*
* Timeboxing *************************************************************** ********************************
*/
fun startTimebox() {
startTime = TimeManager.time.intTime()
startReps = sched.reps
}
data class TimeboxReached(val secs: Int, val reps: Int)
/* Return (elapsedTime, reps) if timebox reached, or null.
* Automatically restarts timebox if expired. */
fun timeboxReached(): TimeboxReached? {
if (sched.timeboxSecs() == 0) {
// timeboxing disabled
return null
}
val elapsed = TimeManager.time.intTime() - startTime
val limit = sched.timeboxSecs()
return if (elapsed > limit) {
TimeboxReached(
limit,
sched.reps - startReps
).also {
startTimebox()
}
} else {
null
}
}
/*
* Undo ********************************************************************* **************************
*/
/** eg "Undo suspend card" if undo available */
fun undoLabel(): String? {
val action = undoStatus().undo
return action?.let { tr.undoUndoAction(it) }
}
fun undoAvailable(): Boolean {
val status = undoStatus()
return status.undo != null
}
fun redoLabel(): String? {
val action = undoStatus().redo
return action?.let { tr.undoRedoAction(it) }
}
fun redoAvailable(): Boolean {
return undoStatus().redo != null
}
fun removeNotes(nids: Iterable<NoteId> = listOf(), cids: Iterable<CardId> = listOf()): OpChangesWithCount {
return backend.removeNotes(noteIds = nids, cardIds = cids)
}
fun removeCardsAndOrphanedNotes(cardIds: Iterable<Long>) {
backend.removeCards(cardIds)
}
fun addNote(note: Note, deckId: DeckId): OpChanges {
val resp = backend.addNote(note.toBackendNote(), deckId)
note.id = resp.noteId
return resp.changes
}
/** allowEmpty is ignored in the new schema */
@RustCleanup("Remove this in favour of addNote() above; call addNote() inside undoableOp()")
fun addNote(note: Note): Int {
addNote(note, note.notetype.did)
return note.numberOfCards(this)
}
/**
* DB maintenance *********************************************************** ************************************
*/
/*
* Basic integrity check. Only used by unit tests.
*/
@KotlinCleanup("have getIds() return a list of mids and define idsToStr over it")
fun basicCheck(): Boolean {
// cards without notes
if (db.queryScalar("select 1 from cards where nid not in (select id from notes) limit 1") > 0) {
return false
}
val badNotes = db.queryScalar(
"select 1 from notes where id not in (select distinct nid from cards) " +
"or mid not in " + ids2str(notetypes.ids()) + " limit 1"
) > 0
// notes without cards or models
if (badNotes) {
return false
}
// invalid ords
for (m in notetypes.all()) {
// ignore clozes
if (m.getInt("type") != Consts.MODEL_STD) {
continue
}
// Make a list of valid ords for this model
val tmpls = m.getJSONArray("tmpls")
val badOrd = db.queryScalar(
"select 1 from cards where (ord < 0 or ord >= ?) and nid in ( " +
"select id from notes where mid = ?) limit 1",
tmpls.length(),
m.getLong("id")
) > 0
if (badOrd) {
return false
}
}
return true
}
/**
* Card Flags *****************************************************************************************************
*/
fun setUserFlag(flag: Int, cids: List<Long>) {
assert(flag in (0..7))
db.execute(
"update cards set flags = (flags & ~?) | ?, usn=?, mod=? where id in " + ids2str(
cids
),
7,
flag,
usn(),
TimeManager.time.intTime()
)
}
lateinit var notetypes: Notetypes
protected set
//endregion
/** Not in libAnki */
@CheckResult
fun filterToValidCards(cards: LongArray?): List<Long> {
return db.queryLongList("select id from cards where id in " + ids2str(cards))
}
fun setDeck(cids: Iterable<CardId>, did: DeckId): OpChangesWithCount {
return backend.setDeck(cardIds = cids, deckId = did)
}
/** Save (flush) the note to the DB. Unlike note.flush(), this is undoable. This should
* not be used for adding new notes. */
fun updateNote(note: Note): OpChanges {
return backend.updateNotes(notes = listOf(note.toBackendNote()), skipUndoEntry = false)
}
fun updateNotes(notes: Iterable<Note>): OpChanges {
return backend.updateNotes(notes = notes.map { it.toBackendNote() }, skipUndoEntry = false)
}
@NotInLibAnki
fun emptyCids(): List<CardId> {
return getEmptyCards().notesList.flatMap { it.cardIdsList }
}
/** Fixes and optimizes the database. If any errors are encountered, a list of
* problems is returned. Throws if DB is unreadable. */
fun fixIntegrity(): List<String> {
return backend.checkDatabase()
}
/** Change the flag color of the specified cards. flag=0 removes flag. */
@CheckResult
fun setUserFlagForCards(cids: Iterable<Long>, flag: Int): OpChangesWithCount {
return backend.setFlag(cardIds = cids, flag = flag)
}
fun getEmptyCards(): EmptyCardsReport {
return backend.getEmptyCards()
}
@Suppress("unused")
fun syncStatus(auth: SyncAuth): SyncStatusResponse {
return backend.syncStatus(input = auth)
}
/** Takes raw input from TypeScript frontend and returns suitable translations. */
fun i18nResourcesRaw(input: ByteArray): ByteArray {
return backend.i18nResourcesRaw(input = input)
}
// Python code has a cardsOfNote, but not vice-versa yet
fun notesOfCards(cids: Iterable<CardId>): List<NoteId> {
return db.queryLongList("select distinct nid from cards where id in ${ids2str(cids)}")
}
fun cardIdsOfNote(nid: NoteId): List<CardId> {
return backend.cardsOfNote(nid = nid)
}
/**
* returns the list of cloze ordinals in a note
*
* `"{{c1::A}} {{c3::B}}" => [1, 3]`
*/
fun clozeNumbersInNote(n: Note): List<Int> {
// the call appears to be non-deterministic. Sort ascending
return backend.clozeNumbersInNote(n.toBackendNote())
.sorted()
}
fun getImageForOcclusionRaw(input: ByteArray): ByteArray {
return backend.getImageForOcclusionRaw(input = input)
}
fun getImageOcclusionNoteRaw(input: ByteArray): ByteArray {
return backend.getImageOcclusionNoteRaw(input = input)
}
fun getImageOcclusionFieldsRaw(input: ByteArray): ByteArray {
return backend.getImageOcclusionFieldsRaw(input = input)
}
fun addImageOcclusionNoteRaw(input: ByteArray): ByteArray {
return backend.addImageOcclusionNoteRaw(input = input)
}
fun updateImageOcclusionNoteRaw(input: ByteArray): ByteArray {
return backend.updateImageOcclusionNoteRaw(input = input)
}
fun congratsInfoRaw(input: ByteArray): ByteArray {
return backend.congratsInfoRaw(input = input)
}
fun compareAnswer(expected: String, provided: String): String {
return backend.compareAnswer(expected = expected, provided = provided)
}
fun extractClozeForTyping(text: String, ordinal: Int): String {
return backend.extractClozeForTyping(text = text, ordinal = ordinal)
}
fun defaultsForAdding(currentReviewCard: Card? = null): anki.notes.DeckAndNotetype {
val homeDeck = currentReviewCard?.currentDeckId()?.did ?: 0L
return backend.defaultsForAdding(homeDeckOfCurrentReviewCard = homeDeck)
}
fun getPreferences(): Preferences {
return backend.getPreferences()
}
fun setPreferences(preferences: Preferences): OpChanges {
return backend.setPreferences(preferences)
}
}