Don't reindex downloaded full-text content if already up to date

This commit is contained in:
Dan Stillman 2013-11-05 17:20:29 -05:00
parent 6e9b491e82
commit 342e631beb

View file

@ -724,25 +724,47 @@ Zotero.Fulltext = new function(){
* Save full-text content and stats to a cache file * Save full-text content and stats to a cache file
*/ */
this.setItemContent = function (libraryID, key, text, stats, version) { this.setItemContent = function (libraryID, key, text, stats, version) {
var libraryKey = libraryID + "/" + key;
var item = Zotero.Items.getByLibraryAndKey(libraryID, key); var item = Zotero.Items.getByLibraryAndKey(libraryID, key);
if (!item) { if (!item) {
let msg = "Item not found setting full-text content"; let msg = "Item " + libraryKey + " not found setting full-text content";
Zotero.debug(msg, 1); Zotero.debug(msg, 1);
Components.utils.reportError(msg); Components.utils.reportError(msg);
return; return;
} }
var itemID = item.id; var itemID = item.id;
var currentVersion = Zotero.DB.valueQuery(
"SELECT version FROM fulltextItems WHERE itemID=?", itemID
);
if (text !== '') { if (text !== '') {
var cacheFile = this.getItemProcessorCacheFile(itemID); var processorCacheFile = this.getItemProcessorCacheFile(itemID);
var itemCacheFile = this.getItemCacheFile(itemID);
// If a storage directory doesn't exist, create it // If a storage directory doesn't exist, create it
if (!cacheFile.parent.exists()) { if (!processorCacheFile.parent.exists()) {
Zotero.Attachments.createDirectoryForItem(itemID); Zotero.Attachments.createDirectoryForItem(itemID);
} }
Zotero.debug("Writing full-text content and data to " + cacheFile.path); // If the local version of the content is already up to date and cached, skip
Zotero.File.putContents(cacheFile, JSON.stringify({ if (currentVersion && currentVersion == version && itemCacheFile.exists()) {
Zotero.debug("Current full-text content version matches remote for item "
+ libraryKey + " -- skipping");
var synced = SYNC_STATE_IN_SYNC;
}
// If the local version is 0 but the text matches, just update the version
else if (currentVersion == 0 && itemCacheFile.exists()
&& Zotero.File.getContents(itemCacheFile) == text) {
Zotero.debug("Current full-text content matches remote for item "
+ libraryKey + " -- updating version");
var synced = SYNC_STATE_IN_SYNC;
Zotero.DB.query("UPDATE fulltextItems SET version=? WHERE itemID=?", [version, itemID]);
}
else {
Zotero.debug("Writing full-text content and data for item " + libraryKey
+ " to " + processorCacheFile.path);
Zotero.File.putContents(processorCacheFile, JSON.stringify({
indexedChars: stats.indexedChars, indexedChars: stats.indexedChars,
totalChars: stats.totalChars, totalChars: stats.totalChars,
indexedPages: stats.indexedPages, indexedPages: stats.indexedPages,
@ -752,13 +774,14 @@ Zotero.Fulltext = new function(){
})); }));
var synced = SYNC_STATE_TO_PROCESS; var synced = SYNC_STATE_TO_PROCESS;
} }
}
else { else {
Zotero.debug("Marking full-text content for download"); Zotero.debug("Marking full-text content for download for item " + libraryKey);
var synced = SYNC_STATE_TO_DOWNLOAD; var synced = SYNC_STATE_TO_DOWNLOAD;
} }
// Mark the item as unprocessed // If indexed previously, update the sync state
if (Zotero.DB.valueQuery("SELECT COUNT(*) FROM fulltextItems WHERE itemID=?", itemID)) { if (currentVersion !== false) {
Zotero.DB.query("UPDATE fulltextItems SET synced=? WHERE itemID=?", [synced, itemID]); Zotero.DB.query("UPDATE fulltextItems SET synced=? WHERE itemID=?", [synced, itemID]);
} }
// If not yet indexed, add an empty row // If not yet indexed, add an empty row