summaryrefslogtreecommitdiff
path: root/ext/js/language/dictionary-importer.js
blob: f3e8665423ff1e53a821444f75a1081a518392a9 (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
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
/*
 * Copyright (C) 2020-2021  Yomichan Authors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public 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 Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

/* global
 * JSZip
 * JsonSchema
 * MediaUtil
 */

class DictionaryImporter {
    constructor(mediaLoader, onProgress) {
        this._mediaLoader = mediaLoader;
        this._onProgress = onProgress;
    }

    async importDictionary(dictionaryDatabase, archiveContent, details) {
        if (!dictionaryDatabase) {
            throw new Error('Invalid database');
        }
        if (!dictionaryDatabase.isPrepared()) {
            throw new Error('Database is not ready');
        }

        const hasOnProgress = (typeof this._onProgress === 'function');

        // Read archive
        const archive = await JSZip.loadAsync(archiveContent);

        // Read and validate index
        const indexFileName = 'index.json';
        const indexFile = archive.file(indexFileName);
        if (!indexFile) {
            throw new Error('No dictionary index found in archive');
        }

        const index = JSON.parse(await indexFile.async('string'));

        const indexSchema = await this._getSchema('/data/schemas/dictionary-index-schema.json');
        this._validateJsonSchema(index, indexSchema, indexFileName);

        const dictionaryTitle = index.title;
        const version = index.format || index.version;

        if (!dictionaryTitle || !index.revision) {
            throw new Error('Unrecognized dictionary format');
        }

        // Verify database is not already imported
        if (await dictionaryDatabase.dictionaryExists(dictionaryTitle)) {
            throw new Error('Dictionary is already imported');
        }

        // Data format converters
        const convertTermBankEntry = (version === 1 ? this._convertTermBankEntryV1.bind(this) : this._convertTermBankEntryV3.bind(this));
        const convertTermMetaBankEntry = this._convertTermMetaBankEntry.bind(this);
        const convertKanjiBankEntry = (version === 1 ? this._convertKanjiBankEntryV1.bind(this) : this._convertKanjiBankEntryV3.bind(this));
        const convertKanjiMetaBankEntry = this._convertKanjiMetaBankEntry.bind(this);
        const convertTagBankEntry = this._convertTagBankEntry.bind(this);

        // Load schemas
        const dataBankSchemaPaths = this._getDataBankSchemaPaths(version);
        const dataBankSchemas = await Promise.all(dataBankSchemaPaths.map((path) => this._getSchema(path)));

        // Files
        const termFiles      = this._getArchiveFiles(archive, 'term_bank_?.json');
        const termMetaFiles  = this._getArchiveFiles(archive, 'term_meta_bank_?.json');
        const kanjiFiles     = this._getArchiveFiles(archive, 'kanji_bank_?.json');
        const kanjiMetaFiles = this._getArchiveFiles(archive, 'kanji_meta_bank_?.json');
        const tagFiles       = this._getArchiveFiles(archive, 'tag_bank_?.json');

        // Load data
        const termList      = await this._readFileSequence(termFiles,      convertTermBankEntry,      dataBankSchemas[0], dictionaryTitle);
        const termMetaList  = await this._readFileSequence(termMetaFiles,  convertTermMetaBankEntry,  dataBankSchemas[1], dictionaryTitle);
        const kanjiList     = await this._readFileSequence(kanjiFiles,     convertKanjiBankEntry,     dataBankSchemas[2], dictionaryTitle);
        const kanjiMetaList = await this._readFileSequence(kanjiMetaFiles, convertKanjiMetaBankEntry, dataBankSchemas[3], dictionaryTitle);
        const tagList       = await this._readFileSequence(tagFiles,       convertTagBankEntry,       dataBankSchemas[4], dictionaryTitle);
        this._addOldIndexTags(index, tagList, dictionaryTitle);

        // Prefix wildcard support
        const prefixWildcardsSupported = !!details.prefixWildcardsSupported;
        if (prefixWildcardsSupported) {
            for (const entry of termList) {
                entry.expressionReverse = stringReverse(entry.expression);
                entry.readingReverse = stringReverse(entry.reading);
            }
        }

        // Extended data support
        const requirements = [];
        for (const entry of termList) {
            const glossaryList = entry.glossary;
            for (let i = 0, ii = glossaryList.length; i < ii; ++i) {
                const glossary = glossaryList[i];
                if (typeof glossary !== 'object' || glossary === null) { continue; }
                glossaryList[i] = this._formatDictionaryTermGlossaryObject(glossary, entry, requirements);
            }
        }

        // Async requirements
        const {media} = await this._resolveAsyncRequirements(requirements, archive);

        // Add dictionary
        const summary = this._createSummary(dictionaryTitle, version, index, {prefixWildcardsSupported});

        dictionaryDatabase.bulkAdd('dictionaries', [summary], 0, 1);

        // Add data
        const errors = [];
        const total = (
            termList.length +
            termMetaList.length +
            kanjiList.length +
            kanjiMetaList.length +
            tagList.length
        );
        let loadedCount = 0;
        const maxTransactionLength = 1000;

        const bulkAdd = async (objectStoreName, entries) => {
            const ii = entries.length;
            for (let i = 0; i < ii; i += maxTransactionLength) {
                const count = Math.min(maxTransactionLength, ii - i);

                try {
                    await dictionaryDatabase.bulkAdd(objectStoreName, entries, i, count);
                } catch (e) {
                    errors.push(e);
                }

                loadedCount += count;
                if (hasOnProgress) {
                    this._onProgress(total, loadedCount);
                }
            }
        };

        await bulkAdd('terms', termList);
        await bulkAdd('termMeta', termMetaList);
        await bulkAdd('kanji', kanjiList);
        await bulkAdd('kanjiMeta', kanjiMetaList);
        await bulkAdd('tagMeta', tagList);
        await bulkAdd('media', media);

        return {result: summary, errors};
    }

    _createSummary(dictionaryTitle, version, index, details) {
        const summary = {
            title: dictionaryTitle,
            revision: index.revision,
            sequenced: index.sequenced,
            version
        };

        const {author, url, description, attribution} = index;
        if (typeof author === 'string') { summary.author = author; }
        if (typeof url === 'string') { summary.url = url; }
        if (typeof description === 'string') { summary.description = description; }
        if (typeof attribution === 'string') { summary.attribution = attribution; }

        Object.assign(summary, details);

        return summary;
    }

    async _getSchema(fileName) {
        const schema = await this._fetchJsonAsset(fileName);
        return new JsonSchema(schema);
    }

    _validateJsonSchema(value, schema, fileName) {
        try {
            schema.validate(value);
        } catch (e) {
            throw this._formatSchemaError(e, fileName);
        }
    }

    _formatSchemaError(e, fileName) {
        const valuePathString = this._getSchemaErrorPathString(e.valueStack, 'dictionary');
        const schemaPathString = this._getSchemaErrorPathString(e.schemaStack, 'schema');

        const e2 = new Error(`Dictionary has invalid data in '${fileName}' for value '${valuePathString}', validated against '${schemaPathString}': ${e.message}`);
        e2.data = e;

        return e2;
    }

    _getSchemaErrorPathString(infoList, base='') {
        let result = base;
        for (const {path} of infoList) {
            const pathArray = Array.isArray(path) ? path : [path];
            for (const pathPart of pathArray) {
                if (pathPart === null) {
                    result = base;
                } else {
                    switch (typeof pathPart) {
                        case 'string':
                            if (result.length > 0) {
                                result += '.';
                            }
                            result += pathPart;
                            break;
                        case 'number':
                            result += `[${pathPart}]`;
                            break;
                    }
                }
            }
        }
        return result;
    }

    _getDataBankSchemaPaths(version) {
        const termBank = (
            version === 1 ?
            '/data/schemas/dictionary-term-bank-v1-schema.json' :
            '/data/schemas/dictionary-term-bank-v3-schema.json'
        );
        const termMetaBank = '/data/schemas/dictionary-term-meta-bank-v3-schema.json';
        const kanjiBank = (
            version === 1 ?
            '/data/schemas/dictionary-kanji-bank-v1-schema.json' :
            '/data/schemas/dictionary-kanji-bank-v3-schema.json'
        );
        const kanjiMetaBank = '/data/schemas/dictionary-kanji-meta-bank-v3-schema.json';
        const tagBank = '/data/schemas/dictionary-tag-bank-v3-schema.json';

        return [termBank, termMetaBank, kanjiBank, kanjiMetaBank, tagBank];
    }

    _formatDictionaryTermGlossaryObject(data, entry, requirements) {
        switch (data.type) {
            case 'text':
                return data.text;
            case 'image':
                return this._formatDictionaryTermGlossaryImage(data, entry, requirements);
            case 'structured-content':
                return this._formatStructuredContent(data, entry, requirements);
            default:
                throw new Error(`Unhandled data type: ${data.type}`);
        }
    }

    _formatDictionaryTermGlossaryImage(data, entry, requirements) {
        const target = {};
        requirements.push({type: 'image', target, args: [data, entry]});
        return target;
    }

    _formatStructuredContent(data, entry, requirements) {
        const content = this._prepareStructuredContent(data.content, entry, requirements);
        return {
            type: 'structured-content',
            content
        };
    }

    _prepareStructuredContent(content, entry, requirements) {
        if (typeof content === 'string' || !(typeof content === 'object' && content !== null)) {
            return content;
        }
        if (Array.isArray(content)) {
            for (let i = 0, ii = content.length; i < ii; ++i) {
                content[i] = this._prepareStructuredContent(content[i], entry, requirements);
            }
            return content;
        }
        const {tag} = content;
        switch (tag) {
            case 'img':
                return this._prepareStructuredContentImage(content, entry, requirements);
        }
        const childContent = content.content;
        if (typeof childContent !== 'undefined') {
            content.content = this._prepareStructuredContent(childContent, entry, requirements);
        }
        return content;
    }

    _prepareStructuredContentImage(content, entry, requirements) {
        const target = {};
        requirements.push({type: 'structured-content-image', target, args: [content, entry]});
        return target;
    }

    async _resolveAsyncRequirements(requirements, archive) {
        const media = new Map();
        const context = {archive, media};

        const promises = [];
        for (const requirement of requirements) {
            promises.push(this._resolveAsyncRequirement(context, requirement));
        }

        await Promise.all(promises);

        return {
            media: [...media.values()]
        };
    }

    async _resolveAsyncRequirement(context, requirement) {
        const {type, target, args} = requirement;
        let result;
        switch (type) {
            case 'image':
                result = await this._resolveDictionaryTermGlossaryImage(context, ...args);
                break;
            case 'structured-content-image':
                result = await this._resolveStructuredContentImage(context, ...args);
                break;
            default:
                return;
        }
        Object.assign(target, result);
    }

    async _resolveDictionaryTermGlossaryImage(context, data, entry) {
        return await this._createImageData(context, data, entry, {type: 'image'});
    }

    async _resolveStructuredContentImage(context, content, entry) {
        const {verticalAlign, sizeUnits} = content;
        const result = await this._createImageData(context, content, entry, {tag: 'img'});
        if (typeof verticalAlign === 'string') { result.verticalAlign = verticalAlign; }
        if (typeof sizeUnits === 'string') { result.sizeUnits = sizeUnits; }
        return result;
    }

    async _createImageData(context, data, entry, attributes) {
        const {
            path,
            width: preferredWidth,
            height: preferredHeight,
            title,
            description,
            pixelated,
            imageRendering,
            appearance,
            background,
            collapsed,
            collapsible
        } = data;
        const {width, height} = await this._getImageMedia(context, path, entry);
        const newData = Object.assign({}, attributes, {path, width, height});
        if (typeof preferredWidth === 'number') { newData.preferredWidth = preferredWidth; }
        if (typeof preferredHeight === 'number') { newData.preferredHeight = preferredHeight; }
        if (typeof title === 'string') { newData.title = title; }
        if (typeof description === 'string') { newData.description = description; }
        if (typeof pixelated === 'boolean') { newData.pixelated = pixelated; }
        if (typeof imageRendering === 'string') { newData.imageRendering = imageRendering; }
        if (typeof appearance === 'string') { newData.appearance = appearance; }
        if (typeof background === 'boolean') { newData.background = background; }
        if (typeof collapsed === 'boolean') { newData.collapsed = collapsed; }
        if (typeof collapsible === 'boolean') { newData.collapsible = collapsible; }
        return newData;
    }

    async _getImageMedia(context, path, entry) {
        const {media} = context;
        const {dictionary} = entry;

        const createError = (message) => {
            const {expression, reading} = entry;
            const readingSource = reading.length > 0 ? ` (${reading})`: '';
            return new Error(`${message} at path ${JSON.stringify(path)} for ${expression}${readingSource} in ${dictionary}`);
        };

        // Check if already added
        let mediaData = media.get(path);
        if (typeof mediaData !== 'undefined') {
            if (MediaUtil.getFileExtensionFromImageMediaType(mediaData.mediaType) === null) {
                throw createError('Media file is not a valid image');
            }
            return mediaData;
        }

        // Find file in archive
        const file = context.archive.file(path);
        if (file === null) {
            throw createError('Could not find image');
        }

        // Load file content
        const content = await file.async('base64');
        const mediaType = MediaUtil.getImageMediaTypeFromFileName(path);
        if (mediaType === null) {
            throw createError('Could not determine media type for image');
        }

        // Load image data
        let width;
        let height;
        try {
            ({width, height} = await this._mediaLoader.getImageResolution(mediaType, content));
        } catch (e) {
            throw createError('Could not load image');
        }

        // Create image data
        mediaData = {
            dictionary,
            path,
            mediaType,
            width,
            height,
            content
        };
        media.set(path, mediaData);

        return mediaData;
    }

    async _fetchJsonAsset(url) {
        const response = await fetch(url, {
            method: 'GET',
            mode: 'no-cors',
            cache: 'default',
            credentials: 'omit',
            redirect: 'follow',
            referrerPolicy: 'no-referrer'
        });
        if (!response.ok) {
            throw new Error(`Failed to fetch ${url}: ${response.status}`);
        }
        return await response.json();
    }

    _convertTermBankEntryV1(entry, dictionary) {
        const [expression, reading, definitionTags, rules, score, ...glossary] = entry;
        return {expression, reading, definitionTags, rules, score, glossary, dictionary};
    }

    _convertTermBankEntryV3(entry, dictionary) {
        const [expression, reading, definitionTags, rules, score, glossary, sequence, termTags] = entry;
        return {expression, reading, definitionTags, rules, score, glossary, sequence, termTags, dictionary};
    }

    _convertTermMetaBankEntry(entry, dictionary) {
        const [expression, mode, data] = entry;
        return {expression, mode, data, dictionary};
    }

    _convertKanjiBankEntryV1(entry, dictionary) {
        const [character, onyomi, kunyomi, tags, ...meanings] = entry;
        return {character, onyomi, kunyomi, tags, meanings, dictionary};
    }

    _convertKanjiBankEntryV3(entry, dictionary) {
        const [character, onyomi, kunyomi, tags, meanings, stats] = entry;
        return {character, onyomi, kunyomi, tags, meanings, stats, dictionary};
    }

    _convertKanjiMetaBankEntry(entry, dictionary) {
        const [character, mode, data] = entry;
        return {character, mode, data, dictionary};
    }

    _convertTagBankEntry(entry, dictionary) {
        const [name, category, order, notes, score] = entry;
        return {name, category, order, notes, score, dictionary};
    }

    _addOldIndexTags(index, results, dictionary) {
        const {tagMeta} = index;
        if (typeof tagMeta !== 'object' || tagMeta === null) { return; }
        for (const name of Object.keys(tagMeta)) {
            const {category, order, notes, score} = tagMeta[name];
            results.push({name, category, order, notes, score, dictionary});
        }
    }

    _getArchiveFiles(archive, fileNameFormat) {
        const indexPosition = fileNameFormat.indexOf('?');
        const prefix = fileNameFormat.substring(0, indexPosition);
        const suffix = fileNameFormat.substring(indexPosition + 1);
        const results = [];
        for (let i = 1; true; ++i) {
            const fileName = `${prefix}${i}${suffix}`;
            const file = archive.file(fileName);
            if (!file) { break; }
            results.push(file);
        }
        return results;
    }

    async _readFileSequence(files, convertEntry, schema, dictionaryTitle) {
        const results = [];
        for (const file of files) {
            const entries = JSON.parse(await file.async('string'));
            this._validateJsonSchema(entries, schema, file.name);
            for (const entry of entries) {
                results.push(convertEntry(entry, dictionaryTitle));
            }
        }
        return results;
    }
}