From 79a89184d2009fea91d71df08e7b27c081acf477 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Wed, 12 Nov 2008 09:58:20 +0000 Subject: [PATCH] Upload objects earlier than earliest object mod time on server -- with any luck, this will fix the majority of "Invalid response" sync errors --- .../content/zotero/xpcom/data/collections.js | 11 ---- chrome/content/zotero/xpcom/data/creators.js | 11 ---- .../content/zotero/xpcom/data/dataObjects.js | 27 ++++++++ chrome/content/zotero/xpcom/data/items.js | 10 --- chrome/content/zotero/xpcom/data/tags.js | 11 ---- chrome/content/zotero/xpcom/search.js | 11 ---- chrome/content/zotero/xpcom/sync.js | 63 ++++++++++++++++--- 7 files changed, 81 insertions(+), 63 deletions(-) diff --git a/chrome/content/zotero/xpcom/data/collections.js b/chrome/content/zotero/xpcom/data/collections.js index f16698c5e7..c7f92f51e6 100644 --- a/chrome/content/zotero/xpcom/data/collections.js +++ b/chrome/content/zotero/xpcom/data/collections.js @@ -30,7 +30,6 @@ Zotero.Collections = new function() { this.get = get; this.add = add; - this.getUpdated = getUpdated; this.getCollectionsContainingItems = getCollectionsContainingItems; this.erase = erase; @@ -62,16 +61,6 @@ Zotero.Collections = new function() { } - function getUpdated(date) { - var sql = "SELECT collectionID FROM collections"; - if (date) { - sql += " WHERE dateModified>?"; - return Zotero.DB.columnQuery(sql, Zotero.Date.dateToSQL(date, true)); - } - return Zotero.DB.columnQuery(sql); - } - - function getCollectionsContainingItems(itemIDs, asIDs) { var sql = "SELECT collectionID FROM collections WHERE "; var sqlParams = []; diff --git a/chrome/content/zotero/xpcom/data/creators.js b/chrome/content/zotero/xpcom/data/creators.js index 0373a10ba6..0ac0f24f0d 100644 --- a/chrome/content/zotero/xpcom/data/creators.js +++ b/chrome/content/zotero/xpcom/data/creators.js @@ -26,7 +26,6 @@ Zotero.Creators = new function() { this.constructor.prototype = new Zotero.DataObjects(); this.get = get; - this.getUpdated = getUpdated; this.getDataID = getDataID; this.getCreatorsWithData = getCreatorsWithData; this.countCreatorsWithData = countCreatorsWithData; @@ -59,16 +58,6 @@ Zotero.Creators = new function() { } - function getUpdated(date) { - var sql = "SELECT creatorID FROM creators"; - if (date) { - sql += " WHERE dateModified>?"; - return Zotero.DB.columnQuery(sql, Zotero.Date.dateToSQL(date, true)); - } - return Zotero.DB.columnQuery(sql); - } - - /** * Returns the creatorDataID matching given fields * diff --git a/chrome/content/zotero/xpcom/data/dataObjects.js b/chrome/content/zotero/xpcom/data/dataObjects.js index 2f12b8f501..dbc472ea5d 100644 --- a/chrome/content/zotero/xpcom/data/dataObjects.js +++ b/chrome/content/zotero/xpcom/data/dataObjects.js @@ -35,6 +35,33 @@ Zotero.DataObjects = function (object, objectPlural, id, table) { } + this.getOlder = function (date) { + if (!date || date.constructor.name != 'Date') { + throw ("date must be a JS Date in " + + "Zotero." + this._ZDO_Objects + ".getOlder()") + } + + var sql = "SELECT " + this._ZDO_id + " FROM " + this._ZDO_table + + " WHERE dateModified?"; + return Zotero.DB.columnQuery(sql, Zotero.Date.dateToSQL(date, true)); + } + return Zotero.DB.columnQuery(sql); + } + + /* * Reloads data for specified items into internal array * diff --git a/chrome/content/zotero/xpcom/data/items.js b/chrome/content/zotero/xpcom/data/items.js index 363073d548..e332a44ba7 100644 --- a/chrome/content/zotero/xpcom/data/items.js +++ b/chrome/content/zotero/xpcom/data/items.js @@ -32,7 +32,6 @@ Zotero.Items = new function() { this.get = get; this.exist = exist; this.getAll = getAll; - this.getUpdated = getUpdated; this.add = add; this.cacheFields = cacheFields; this.erase = erase; @@ -125,15 +124,6 @@ Zotero.Items = new function() { } - function getUpdated(date) { - var s = new Zotero.Search(); - if (date) { - s.addCondition('dateModified', 'isAfter', Zotero.Date.dateToSQL(date, true)); - } - return s.search(); - } - - /* * Create a new item with optional metadata and pass back the primary reference * diff --git a/chrome/content/zotero/xpcom/data/tags.js b/chrome/content/zotero/xpcom/data/tags.js index 9a0b929ebd..13b3a705b9 100644 --- a/chrome/content/zotero/xpcom/data/tags.js +++ b/chrome/content/zotero/xpcom/data/tags.js @@ -35,7 +35,6 @@ Zotero.Tags = new function() { this.getID = getID; this.getIDs = getIDs; this.getTypes = getTypes; - this.getUpdated = getUpdated; this.getAll = getAll; this.getAllWithinSearch = getAllWithinSearch; this.getTagItems = getTagItems; @@ -115,16 +114,6 @@ Zotero.Tags = new function() { } - function getUpdated(date) { - var sql = "SELECT tagID FROM tags"; - if (date) { - sql += " WHERE dateModified>?"; - return Zotero.DB.columnQuery(sql, Zotero.Date.dateToSQL(date, true)); - } - return Zotero.DB.columnQuery(sql); - } - - /** * Get all tags indexed by tagID * diff --git a/chrome/content/zotero/xpcom/search.js b/chrome/content/zotero/xpcom/search.js index ada8ede86c..302a696fba 100644 --- a/chrome/content/zotero/xpcom/search.js +++ b/chrome/content/zotero/xpcom/search.js @@ -1495,7 +1495,6 @@ Zotero.Searches = new function(){ this.get = get; this.getAll = getAll; - this.getUpdated = getUpdated; this.erase = erase; @@ -1525,16 +1524,6 @@ Zotero.Searches = new function(){ } - function getUpdated(date) { - var sql = "SELECT savedSearchID FROM savedSearches"; - if (date) { - sql += " WHERE dateModified>?"; - return Zotero.DB.columnQuery(sql, Zotero.Date.dateToSQL(date, true)); - } - return Zotero.DB.columnQuery(sql); - } - - /* * Delete a given saved search from the DB */ diff --git a/chrome/content/zotero/xpcom/sync.js b/chrome/content/zotero/xpcom/sync.js index 4e465ef3b4..40dadaa837 100644 --- a/chrome/content/zotero/xpcom/sync.js +++ b/chrome/content/zotero/xpcom/sync.js @@ -2,7 +2,6 @@ Zotero.Sync = new function() { this.init = init; this.getObjectTypeID = getObjectTypeID; this.getObjectTypeName = getObjectTypeName; - this.getUpdatedObjects = getUpdatedObjects; this.getDeletedObjects = getDeletedObjects; this.purgeDeletedObjects = purgeDeletedObjects; @@ -78,13 +77,27 @@ Zotero.Sync = new function() { /** - * @param object lastSyncDate JS Date object - * @return object { items: [123, 234, ...], creators: [321, 432, ...], ... } + * @param {Date} olderThanDate Retrieve objects last updated before this date + * @param {Date} newerThanDate Retrieve objects last updated after this date + * @return {Object} { items: [123, 234, ...], creators: [321, 432, ...], ... } */ - function getUpdatedObjects(lastSyncDate) { - if (lastSyncDate && lastSyncDate.constructor.name != 'Date') { - throw ('lastSyncDate must be a Date or FALSE in ' - + 'Zotero.Sync.getDeletedObjects()') + this.getObjectsByDate = function (olderThanDate, newerThanDate) { + var funcName = "Zotero.Sync.getObjectsByDate()"; + if (olderThanDate && olderThanDate.constructor.name != 'Date') { + throw ("olderThanDate must be a Date or FALSE in " + funcName) + } + if (newerThanDate && newerThanDate.constructor.name != 'Date') { + throw ("newerThanDate must be a Date or FALSE in " + funcName) + } + + // If dates overlap, retrieve all objects + if (!olderThanDate && !newerThanDate) { + var all = true; + } + else if (olderThanDate && newerThanDate && olderThanDate > newerThanDate) { + olderThanDate = null; + newerThanDate = null; + var all = true; } var updatedIDs = {}; @@ -94,7 +107,25 @@ Zotero.Sync = new function() { Zotero.debug("Getting updated local " + types); - updatedIDs[types] = Zotero[Types].getUpdated(lastSyncDate); + if (olderThanDate) { + var earlierIDs = Zotero[Types].getOlder(olderThanDate); + if (earlierIDs) { + updatedIDs[types] = earlierIDs; + } + } + + if (newerThanDate || all) { + var laterIDs = Zotero[Types].getNewer(newerThanDate); + if (laterIDs) { + if (updatedIDs[types]) { + updatedIDs[types].concat(laterIDs); + } + else { + updatedIDs[types] = laterIDs; + } + } + } + if (!updatedIDs[types]) { updatedIDs[types] = []; } @@ -745,12 +776,26 @@ Zotero.Sync.Server = new function () { try { Zotero.UnresponsiveScriptIndicator.disable(); + var earliestRemoteDate = parseInt(xml.@earliest) ? + new Date((xml.@earliest + 43200) * 1000) : false; + var lastLocalSyncTime = Zotero.Sync.Server.lastLocalSyncTime; var lastLocalSyncDate = lastLocalSyncTime ? new Date(lastLocalSyncTime * 1000) : false; var syncSession = new Zotero.Sync.Server.Session; - syncSession.uploadIDs.updated = Zotero.Sync.getUpdatedObjects(lastLocalSyncDate); + // Fetch old objects not on server (due to a clear) and new + // objects added since last sync + if (earliestRemoteDate && lastLocalSyncDate) { + syncSession.uploadIDs.updated = Zotero.Sync.getObjectsByDate( + earliestRemoteDate, lastLocalSyncDate + ); + } + // Fetch all local objects + else { + syncSession.uploadIDs.updated = Zotero.Sync.getObjectsByDate(); + } + var deleted = Zotero.Sync.getDeletedObjects(lastLocalSyncDate); if (deleted == -1) { _error('Sync delete log starts after last sync date in Zotero.Sync.Server.sync()');