From d527c340c68ac79e6f3615eb5eaf2f24baed15b6 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Wed, 26 Apr 2017 02:44:01 -0400 Subject: [PATCH] Fix "text is not defined" error during full-text content sync --- chrome/content/zotero/xpcom/fulltext.js | 44 +++++++++--------- test/tests/fulltextTest.js | 60 +++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 20 deletions(-) diff --git a/chrome/content/zotero/xpcom/fulltext.js b/chrome/content/zotero/xpcom/fulltext.js index 2179a5a0b..0d90317c1 100644 --- a/chrome/content/zotero/xpcom/fulltext.js +++ b/chrome/content/zotero/xpcom/fulltext.js @@ -960,36 +960,40 @@ Zotero.Fulltext = Zotero.FullText = new function(){ var itemID = item.id; var currentVersion = yield this.getItemVersion(itemID) - var processorCacheFile = this.getItemProcessorCacheFile(item); - var itemCacheFile = this.getItemCacheFile(item); + var processorCacheFile = this.getItemProcessorCacheFile(item); // .zotero-ft-unprocessed + var itemCacheFile = this.getItemCacheFile(item); // .zotero-ft-cache // If a storage directory doesn't exist, create it if (!processorCacheFile.parent.exists()) { yield Zotero.Attachments.createDirectoryForItem(item); } - // If the local version is 0 but the text matches, just update the version - if (currentVersion == 0 && itemCacheFile.exists() - && (yield Zotero.File.getContentsAsync(itemCacheFile)) == text) { + // If indexed previously and the existing extracted text matches the new text, + // just update the version + if (currentVersion !== false + && itemCacheFile.exists() + && (yield Zotero.File.getContentsAsync(itemCacheFile)) == data.content) { Zotero.debug("Current full-text content matches remote for item " + libraryKey + " -- updating version"); - var synced = SYNC_STATE_IN_SYNC; - yield Zotero.DB.queryAsync("UPDATE fulltextItems SET version=? WHERE itemID=?", [version, itemID]); - } - else { - Zotero.debug("Writing full-text content and data for item " + libraryKey - + " to " + processorCacheFile.path); - yield Zotero.File.putContentsAsync(processorCacheFile, JSON.stringify({ - indexedChars: data.indexedChars, - totalChars: data.totalChars, - indexedPages: data.indexedPages, - totalPages: data.totalPages, - version: version, - text: data.content - })); - var synced = SYNC_STATE_TO_PROCESS; + return Zotero.DB.queryAsync( + "REPLACE INTO fulltextItems (itemID, version, synced) VALUES (?, ?, ?)", + [itemID, version, SYNC_STATE_IN_SYNC] + ); } + // Otherwise save data to -unprocessed file + Zotero.debug("Writing full-text content and data for item " + libraryKey + + " to " + processorCacheFile.path); + yield Zotero.File.putContentsAsync(processorCacheFile, JSON.stringify({ + indexedChars: data.indexedChars, + totalChars: data.totalChars, + indexedPages: data.indexedPages, + totalPages: data.totalPages, + version, + text: data.content + })); + var synced = SYNC_STATE_TO_PROCESS; + // If indexed previously, update the sync state if (currentVersion !== false) { yield Zotero.DB.queryAsync("UPDATE fulltextItems SET synced=? WHERE itemID=?", [synced, itemID]); diff --git a/test/tests/fulltextTest.js b/test/tests/fulltextTest.js index 9c62d1caa..29fc07e83 100644 --- a/test/tests/fulltextTest.js +++ b/test/tests/fulltextTest.js @@ -231,4 +231,64 @@ describe("Zotero.Fulltext", function () { } }) }) + + describe("#setItemContent()", function () { + it("should store data in .zotero-ft-unprocessed file", function* () { + var item = yield importFileAttachment('test.pdf'); + + var processorCacheFile = Zotero.Fulltext.getItemProcessorCacheFile(item).path; + var itemCacheFile = Zotero.Fulltext.getItemCacheFile(item).path; + yield Zotero.File.putContentsAsync(itemCacheFile, "Test"); + + yield Zotero.Fulltext.setItemContent( + item.libraryID, + item.key, + { + content: "Test", + indexedChars: 4, + totalChars: 4 + }, + 5 + ); + + assert.equal((yield Zotero.Fulltext.getItemVersion(item.id)), 0); + assert.equal( + yield Zotero.DB. valueQueryAsync("SELECT synced FROM fulltextItems WHERE itemID=?", item.id), + 2 // to process + ); + assert.isTrue(yield OS.File.exists(processorCacheFile)); + }); + + + it("should update the version if the local version is 0 but the text matches", function* () { + var item = yield importFileAttachment('test.pdf'); + + yield Zotero.DB.queryAsync( + "REPLACE INTO fulltextItems (itemID, version, synced) VALUES (?, 0, ?)", + [item.id, 0] // to process + ); + + var processorCacheFile = Zotero.Fulltext.getItemProcessorCacheFile(item).path; + var itemCacheFile = Zotero.Fulltext.getItemCacheFile(item).path; + yield Zotero.File.putContentsAsync(itemCacheFile, "Test"); + + yield Zotero.Fulltext.setItemContent( + item.libraryID, + item.key, + { + content: "Test", + indexedChars: 4, + totalChars: 4 + }, + 5 + ); + + assert.equal((yield Zotero.Fulltext.getItemVersion(item.id)), 5); + assert.equal( + yield Zotero.DB. valueQueryAsync("SELECT synced FROM fulltextItems WHERE itemID=?", item.id), + 1 // in sync + ); + assert.isFalse(yield OS.File.exists(processorCacheFile)); + }); + }); })