- Fix some collection sync issues
This commit is contained in:
parent
40443c6b91
commit
882074e847
2 changed files with 73 additions and 18 deletions
|
@ -293,9 +293,6 @@ Zotero.Item.prototype.loadPrimaryData = function(allowFail) {
|
||||||
where.push(whereSQL);
|
where.push(whereSQL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
Zotero.debug("skipping " + field);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!columns.length) {
|
if (!columns.length) {
|
||||||
|
@ -1750,9 +1747,7 @@ Zotero.Item.prototype.save = function() {
|
||||||
// TODO: clear caches
|
// TODO: clear caches
|
||||||
throw ("Cannot set source to invalid item " + this._sourceItem);
|
throw ("Cannot set source to invalid item " + this._sourceItem);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (newSourceItem) {
|
|
||||||
var newSourceItemNotifierData = {};
|
var newSourceItemNotifierData = {};
|
||||||
newSourceItemNotifierData[newSourceItem.id] = {
|
newSourceItemNotifierData[newSourceItem.id] = {
|
||||||
old: newSourceItem.serialize()
|
old: newSourceItem.serialize()
|
||||||
|
@ -1788,6 +1783,10 @@ Zotero.Item.prototype.save = function() {
|
||||||
+ "WHERE itemID=?";
|
+ "WHERE itemID=?";
|
||||||
var changedCollections = Zotero.DB.columnQuery(sql, this.id);
|
var changedCollections = Zotero.DB.columnQuery(sql, this.id);
|
||||||
if (changedCollections) {
|
if (changedCollections) {
|
||||||
|
sql = "UPDATE collections SET dateModified=CURRENT_TIMESTAMP, clientDateModified=CURRENT_TIMESTAMP "
|
||||||
|
+ "WHERE collectionID IN (SELECT collectionID FROM collectionItems WHERE itemID=?)";
|
||||||
|
Zotero.DB.query(sql, this.id);
|
||||||
|
|
||||||
if (newSourceItem) {
|
if (newSourceItem) {
|
||||||
sql = "UPDATE OR REPLACE collectionItems "
|
sql = "UPDATE OR REPLACE collectionItems "
|
||||||
+ "SET itemID=? WHERE itemID=?";
|
+ "SET itemID=? WHERE itemID=?";
|
||||||
|
@ -1990,10 +1989,13 @@ Zotero.Item.prototype.numChildren = function(includeTrashed) {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the itemID of the source item for a note or file
|
* @return {Integer|FALSE} itemID of the parent item for an attachment or note, or FALSE if none
|
||||||
* @return {Integer}
|
|
||||||
*/
|
*/
|
||||||
Zotero.Item.prototype.getSource = function() {
|
Zotero.Item.prototype.getSource = function() {
|
||||||
|
if (this._sourceItem === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (this._sourceItem !== null) {
|
if (this._sourceItem !== null) {
|
||||||
if (typeof this._sourceItem == 'number') {
|
if (typeof this._sourceItem == 'number') {
|
||||||
return this._sourceItem;
|
return this._sourceItem;
|
||||||
|
@ -2034,10 +2036,13 @@ Zotero.Item.prototype.getSource = function() {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the key of the source item for a note or file
|
* @return {String|FALSE} Key of the parent item for an attachment or note, or FALSE if none
|
||||||
* @return {String}
|
|
||||||
*/
|
*/
|
||||||
Zotero.Item.prototype.getSourceKey = function() {
|
Zotero.Item.prototype.getSourceKey = function() {
|
||||||
|
if (this._sourceItem === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (this._sourceItem !== null) {
|
if (this._sourceItem !== null) {
|
||||||
if (typeof this._sourceItem == 'string') {
|
if (typeof this._sourceItem == 'string') {
|
||||||
return this._sourceItem;
|
return this._sourceItem;
|
||||||
|
@ -2094,7 +2099,7 @@ Zotero.Item.prototype.setSource = function(sourceItemID) {
|
||||||
this._previousData = this.serialize();
|
this._previousData = this.serialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
this._sourceItem = sourceItemID ? parseInt(sourceItemID) : null;
|
this._sourceItem = sourceItemID ? parseInt(sourceItemID) : false;
|
||||||
this._changedSource = true;
|
this._changedSource = true;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -2120,7 +2125,7 @@ Zotero.Item.prototype.setSourceKey = function(sourceItemKey) {
|
||||||
var oldSourceItemKey = sourceItem.key;
|
var oldSourceItemKey = sourceItem.key;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
var oldSourceItemKey = null;
|
var oldSourceItemKey = false;
|
||||||
}
|
}
|
||||||
if (oldSourceItemKey == sourceItemKey) {
|
if (oldSourceItemKey == sourceItemKey) {
|
||||||
Zotero.debug("Source item has not changed in Zotero.Item.setSourceKey()");
|
Zotero.debug("Source item has not changed in Zotero.Item.setSourceKey()");
|
||||||
|
@ -2131,7 +2136,7 @@ Zotero.Item.prototype.setSourceKey = function(sourceItemKey) {
|
||||||
this._previousData = this.serialize();
|
this._previousData = this.serialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
this._sourceItem = sourceItemKey ? sourceItemKey : null;
|
this._sourceItem = sourceItemKey ? sourceItemKey : false;
|
||||||
this._changedSource = true;
|
this._changedSource = true;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -3321,6 +3326,16 @@ Zotero.Item.prototype.diff = function (item, includeMatches, ignoreFields) {
|
||||||
|
|
||||||
var changed = false;
|
var changed = false;
|
||||||
|
|
||||||
|
changed = thisData.sourceItemKey != otherData.sourceItemKey;
|
||||||
|
if (includeMatches || changed) {
|
||||||
|
diff[0].sourceItemKey = thisData.sourceItemKey;
|
||||||
|
diff[1].sourceItemKey = otherData.sourceItemKey;
|
||||||
|
|
||||||
|
if (changed) {
|
||||||
|
numDiffs++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (thisData.attachment) {
|
if (thisData.attachment) {
|
||||||
for (var field in thisData.attachment) {
|
for (var field in thisData.attachment) {
|
||||||
changed = thisData.attachment[field] != otherData.attachment[field];
|
changed = thisData.attachment[field] != otherData.attachment[field];
|
||||||
|
|
|
@ -1893,6 +1893,7 @@ Zotero.Sync.Server.Data = new function() {
|
||||||
var remoteCreatorStore = {};
|
var remoteCreatorStore = {};
|
||||||
var relatedItemsStore = {};
|
var relatedItemsStore = {};
|
||||||
var itemStorageModTimes = {};
|
var itemStorageModTimes = {};
|
||||||
|
var childItemStore = [];
|
||||||
|
|
||||||
if (xml.groups.length()) {
|
if (xml.groups.length()) {
|
||||||
Zotero.debug("Processing remotely changed groups");
|
Zotero.debug("Processing remotely changed groups");
|
||||||
|
@ -1992,12 +1993,11 @@ Zotero.Sync.Server.Data = new function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
var remoteObj = Zotero.Sync.Server.Data['xmlTo' + Type](xmlNode);
|
var remoteObj = Zotero.Sync.Server.Data['xmlTo' + Type](xmlNode);
|
||||||
|
|
||||||
// Some types we don't bother to reconcile
|
// Some types we don't bother to reconcile
|
||||||
if (_noMergeTypes.indexOf(type) != -1) {
|
if (_noMergeTypes.indexOf(type) != -1) {
|
||||||
// If local is newer, send to server
|
// If local is newer, send to server
|
||||||
if (obj.dateModified > remoteObj.dateModified) {
|
if (obj.dateModified > remoteObj.dateModified) {
|
||||||
Zotero.debug('c');
|
|
||||||
syncSession.addToUpdated(obj);
|
syncSession.addToUpdated(obj);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -2059,7 +2059,7 @@ Zotero.Sync.Server.Data = new function() {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'collection':
|
case 'collection':
|
||||||
var changed = _mergeCollection(obj, remoteObj);
|
var changed = _mergeCollection(obj, remoteObj, childItemStore);
|
||||||
if (!changed) {
|
if (!changed) {
|
||||||
syncSession.removeFromUpdated(obj);
|
syncSession.removeFromUpdated(obj);
|
||||||
}
|
}
|
||||||
|
@ -2134,6 +2134,7 @@ Zotero.Sync.Server.Data = new function() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Temporarily remove and store related items that don't yet exist
|
// Temporarily remove and store related items that don't yet exist
|
||||||
if (type == 'item') {
|
if (type == 'item') {
|
||||||
var missing = Zotero.Sync.Server.Data.removeMissingRelatedItems(xmlNode);
|
var missing = Zotero.Sync.Server.Data.removeMissingRelatedItems(xmlNode);
|
||||||
|
@ -2303,6 +2304,13 @@ Zotero.Sync.Server.Data = new function() {
|
||||||
Zotero.debug('Saving merged ' + types);
|
Zotero.debug('Saving merged ' + types);
|
||||||
|
|
||||||
if (type == 'collection') {
|
if (type == 'collection') {
|
||||||
|
for each(var col in toSave) {
|
||||||
|
var changed = _removeChildItemsFromCollection(col, childItemStore);
|
||||||
|
if (changed) {
|
||||||
|
syncSession.addToUpdated(col);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Save collections recursively from the top down
|
// Save collections recursively from the top down
|
||||||
_saveCollections(toSave);
|
_saveCollections(toSave);
|
||||||
}
|
}
|
||||||
|
@ -2318,7 +2326,17 @@ Zotero.Sync.Server.Data = new function() {
|
||||||
|
|
||||||
// Save the rest
|
// Save the rest
|
||||||
for each(var obj in toSave) {
|
for each(var obj in toSave) {
|
||||||
|
// Keep list of all child items being saved
|
||||||
|
var store = false;
|
||||||
|
if (!obj.isTopLevelItem()) {
|
||||||
|
store = true;
|
||||||
|
}
|
||||||
|
|
||||||
obj.save();
|
obj.save();
|
||||||
|
|
||||||
|
if (store) {
|
||||||
|
childItemStore.push(obj.id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add back related items (which now exist)
|
// Add back related items (which now exist)
|
||||||
|
@ -2534,7 +2552,27 @@ Zotero.Sync.Server.Data = new function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function _mergeCollection(localObj, remoteObj) {
|
// Remove any child items from collection, which might exist if an attachment in a collection was
|
||||||
|
// remotely changed from a top-level item to a child item
|
||||||
|
function _removeChildItemsFromCollection(collection, childItems) {
|
||||||
|
if (!childItems.length) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
var itemIDs = collection.getChildItems(true);
|
||||||
|
// TODO: fix to always return array
|
||||||
|
if (!itemIDs) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
var newItemIDs = Zotero.Utilities.prototype.arrayDiff(childItems, itemIDs);
|
||||||
|
if (itemIDs.length == newItemIDs.length) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
collection.childItems = newItemIDs;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function _mergeCollection(localObj, remoteObj, childItems) {
|
||||||
var diff = localObj.diff(remoteObj, false, true);
|
var diff = localObj.diff(remoteObj, false, true);
|
||||||
if (!diff) {
|
if (!diff) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -2594,6 +2632,8 @@ Zotero.Sync.Server.Data = new function() {
|
||||||
alert(msg);
|
alert(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_removeChildItemsFromCollection(targetObj, childItems);
|
||||||
|
|
||||||
targetObj.save();
|
targetObj.save();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -3061,7 +3101,7 @@ Zotero.Sync.Server.Data = new function() {
|
||||||
// Both notes and attachments might have parents and notes
|
// Both notes and attachments might have parents and notes
|
||||||
if (item.isNote() || item.isAttachment()) {
|
if (item.isNote() || item.isAttachment()) {
|
||||||
var sourceItemKey = xmlItem.@sourceItem.toString();
|
var sourceItemKey = xmlItem.@sourceItem.toString();
|
||||||
item.setSourceKey(sourceItemKey ? sourceItemKey : null);
|
item.setSourceKey(sourceItemKey ? sourceItemKey : false);
|
||||||
item.setNote(xmlItem.note.toString());
|
item.setNote(xmlItem.note.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue