enforce schema version; allow importing+exporting v2 scheduler

To avoid all sorts of problems, we need to ensure cards scheduled with
the V2 scheduler are not studied in older clients. Unfortunately we
can't just bump the file's schema version, as the existing clients will
freely import files created with newer versions. This patch changes
that, so things should be a bit easier in the future.

In the mean time, we need a way to prevent older clients from importing
files created with the V2 scheduler. To do this, we switch to using a
'collection.anki21' file in the archive, and include a dummy collection
.anki2 file.

The code has been tested with both deck and collection packages, but
exporting deck packages w/ scheduling info will remain disabled until
the V2 scheduler has had more testing.
This commit is contained in:
Damien Elmes 2018-01-29 14:12:04 +10:00
parent ea3ad655a7
commit 0cce540d83
5 changed files with 58 additions and 10 deletions

View file

@ -4,7 +4,7 @@
import re, os, zipfile, shutil import re, os, zipfile, shutil
from anki.lang import _ from anki.lang import _
from anki.utils import ids2str, splitFields, json from anki.utils import ids2str, splitFields, json, namedtmp
from anki.hooks import runHook from anki.hooks import runHook
from anki import Collection from anki import Collection
@ -113,6 +113,9 @@ class AnkiExporter(Exporter):
Exporter.__init__(self, col) Exporter.__init__(self, col)
def exportInto(self, path): def exportInto(self, path):
# sched info+v2 scheduler not compatible w/ older clients
self._v2sched = self.col.schedVer() != 1 and self.includeSched
# create a new collection at the target # create a new collection at the target
try: try:
os.unlink(path) os.unlink(path)
@ -263,13 +266,19 @@ class AnkiPackageExporter(AnkiExporter):
z.close() z.close()
def doExport(self, z, path): def doExport(self, z, path):
if self.col.schedVer() != 1:
raise Exception("Experimental scheduler currently doesn't support deck exports.")
# export into the anki2 file # export into the anki2 file
colfile = path.replace(".apkg", ".anki2") colfile = path.replace(".apkg", ".anki2")
AnkiExporter.exportInto(self, colfile) AnkiExporter.exportInto(self, colfile)
z.write(colfile, "collection.anki2") if not self._v2sched:
z.write(colfile, "collection.anki2")
else:
# fixme: remove in the future
raise Exception("Please switch to the normal scheduler before exporting a single deck with scheduling information.")
# prevent older clients from accessing
self._addDummyCollection(z)
z.write(colfile, "collection.anki21")
# and media # and media
self.prepareMedia() self.prepareMedia()
media = self._exportMedia(z, self.mediaFiles, self.mediaDir) media = self._exportMedia(z, self.mediaFiles, self.mediaDir)
@ -304,6 +313,20 @@ class AnkiPackageExporter(AnkiExporter):
# is zipped up # is zipped up
pass pass
# create a dummy collection to ensure older clients don't try to read
# data they don't understand
def _addDummyCollection(self, zip):
path = namedtmp("dummy.anki2")
c = Collection(path)
n = c.newNote()
n['Front'] = "This file requires a newer version of Anki."
c.addNote(n)
c.save()
c.close()
zip.write(path, "collection.anki2")
os.unlink(path)
# Collection package # Collection package
###################################################################### ######################################################################
@ -320,8 +343,13 @@ class AnkiCollectionPackageExporter(AnkiPackageExporter):
def doExport(self, z, path): def doExport(self, z, path):
# close our deck & write it into the zip file, and reopen # close our deck & write it into the zip file, and reopen
self.count = self.col.cardCount() self.count = self.col.cardCount()
v2 = self.col.schedVer() != 1
self.col.close() self.col.close()
z.write(self.col.path, "collection.anki2") if not v2:
z.write(self.col.path, "collection.anki2")
else:
self._addDummyCollection(z)
z.write(self.col.path, "collection.anki21")
self.col.reopen() self.col.reopen()
# copy all media # copy all media
if not self.includeMedia: if not self.includeMedia:

View file

@ -31,6 +31,9 @@ class Anki2Importer(Importer):
self.src.close(save=False) self.src.close(save=False)
def _prepareFiles(self): def _prepareFiles(self):
if self.file.endswith(".anki21") and self.col.schedVer() == 1:
raise Exception("V2 scheduler must be enabled to import this file.")
self.dst = self.col self.dst = self.col
self.src = Collection(self.file) self.src = Collection(self.file)

View file

@ -12,8 +12,15 @@ class AnkiPackageImporter(Anki2Importer):
def run(self): def run(self):
# extract the deck from the zip file # extract the deck from the zip file
self.zip = z = zipfile.ZipFile(self.file) self.zip = z = zipfile.ZipFile(self.file)
col = z.read("collection.anki2") # v2 scheduler?
colpath = tmpfile(suffix=".anki2") try:
z.getinfo("collection.anki21")
suffix = ".anki21"
except KeyError:
suffix = ".anki2"
col = z.read("collection"+suffix)
colpath = tmpfile(suffix=suffix)
with open(colpath, "wb") as f: with open(colpath, "wb") as f:
f.write(col) f.write(col)
self.file = colpath self.file = colpath

View file

@ -40,6 +40,8 @@ def Collection(path, lock=True, server=False, sync=True, log=False):
col = _Collection(db, server, log) col = _Collection(db, server, log)
if ver < SCHEMA_VERSION: if ver < SCHEMA_VERSION:
_upgrade(col, ver) _upgrade(col, ver)
elif ver > SCHEMA_VERSION:
raise Exception("This file requires a newer version of Anki.")
elif create: elif create:
# add in reverse order so basic is default # add in reverse order so basic is default
addClozeModel(col) addClozeModel(col)

View file

@ -388,10 +388,18 @@ def replaceWithApkg(mw, file, backup):
def _replaceWithApkg(mw, file, backup): def _replaceWithApkg(mw, file, backup):
mw.progress.start(immediate=True) mw.progress.start(immediate=True)
# overwrite collection
z = zipfile.ZipFile(file) z = zipfile.ZipFile(file)
# v2 scheduler?
colname = "collection.anki21"
try: try:
z.extract("collection.anki2", mw.pm.profileFolder()) z.getinfo(colname)
except KeyError:
colname = "collection.anki2"
try:
z.extract(colname, mw.pm.profileFolder())
except: except:
mw.progress.finish() mw.progress.finish()
showWarning(_("The provided file is not a valid .apkg file.")) showWarning(_("The provided file is not a valid .apkg file."))