Fix "text is not defined" error during full-text content sync
This commit is contained in:
parent
696e828a02
commit
d527c340c6
2 changed files with 84 additions and 20 deletions
|
@ -960,36 +960,40 @@ Zotero.Fulltext = Zotero.FullText = new function(){
|
||||||
var itemID = item.id;
|
var itemID = item.id;
|
||||||
var currentVersion = yield this.getItemVersion(itemID)
|
var currentVersion = yield this.getItemVersion(itemID)
|
||||||
|
|
||||||
var processorCacheFile = this.getItemProcessorCacheFile(item);
|
var processorCacheFile = this.getItemProcessorCacheFile(item); // .zotero-ft-unprocessed
|
||||||
var itemCacheFile = this.getItemCacheFile(item);
|
var itemCacheFile = this.getItemCacheFile(item); // .zotero-ft-cache
|
||||||
|
|
||||||
// If a storage directory doesn't exist, create it
|
// If a storage directory doesn't exist, create it
|
||||||
if (!processorCacheFile.parent.exists()) {
|
if (!processorCacheFile.parent.exists()) {
|
||||||
yield Zotero.Attachments.createDirectoryForItem(item);
|
yield Zotero.Attachments.createDirectoryForItem(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the local version is 0 but the text matches, just update the version
|
// If indexed previously and the existing extracted text matches the new text,
|
||||||
if (currentVersion == 0 && itemCacheFile.exists()
|
// just update the version
|
||||||
&& (yield Zotero.File.getContentsAsync(itemCacheFile)) == text) {
|
if (currentVersion !== false
|
||||||
|
&& itemCacheFile.exists()
|
||||||
|
&& (yield Zotero.File.getContentsAsync(itemCacheFile)) == data.content) {
|
||||||
Zotero.debug("Current full-text content matches remote for item "
|
Zotero.debug("Current full-text content matches remote for item "
|
||||||
+ libraryKey + " -- updating version");
|
+ libraryKey + " -- updating version");
|
||||||
var synced = SYNC_STATE_IN_SYNC;
|
return Zotero.DB.queryAsync(
|
||||||
yield Zotero.DB.queryAsync("UPDATE fulltextItems SET version=? WHERE itemID=?", [version, itemID]);
|
"REPLACE INTO fulltextItems (itemID, version, synced) VALUES (?, ?, ?)",
|
||||||
}
|
[itemID, version, SYNC_STATE_IN_SYNC]
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 indexed previously, update the sync state
|
||||||
if (currentVersion !== false) {
|
if (currentVersion !== false) {
|
||||||
yield Zotero.DB.queryAsync("UPDATE fulltextItems SET synced=? WHERE itemID=?", [synced, itemID]);
|
yield Zotero.DB.queryAsync("UPDATE fulltextItems SET synced=? WHERE itemID=?", [synced, itemID]);
|
||||||
|
|
|
@ -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));
|
||||||
|
});
|
||||||
|
});
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue