www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

commit 81e94b4475b587b75a2ccede68bb274abf837df8
parent 5040fba8bbb8485cf783ef96112bda1ab2bba857
Author: Dan Stillman <dstillman@zotero.org>
Date:   Thu, 15 Aug 2013 23:32:32 -0400

Merge branch '4.0'

Conflicts:
	chrome/content/zotero/xpcom/schema.js

Diffstat:
Mchrome/content/zotero/browser.js | 8++++----
Mchrome/content/zotero/xpcom/connector/translate_item.js | 109++++++++++++++++++++++++++++++++++---------------------------------------------
Mchrome/content/zotero/xpcom/data/collection.js | 2+-
Mchrome/content/zotero/xpcom/data/creator.js | 2+-
Mchrome/content/zotero/xpcom/data/item.js | 2+-
Mchrome/content/zotero/xpcom/data/tag.js | 2+-
Mchrome/content/zotero/xpcom/id.js | 8--------
Mchrome/content/zotero/xpcom/storage.js | 2+-
Mchrome/content/zotero/xpcom/translation/translate.js | 10+++++-----
Mchrome/content/zotero/xpcom/utilities.js | 24+++++++++++++++++++-----
10 files changed, 80 insertions(+), 89 deletions(-)

diff --git a/chrome/content/zotero/browser.js b/chrome/content/zotero/browser.js @@ -58,6 +58,7 @@ var Zotero_Browser = new function() { this.isScraping = false; var _browserData = new Object(); + var _attachmentsMap = new WeakMap(); var _blacklist = [ "googlesyndication.com", @@ -533,6 +534,7 @@ var Zotero_Browser = new function() { translate.clearHandlers("done"); translate.clearHandlers("itemDone"); + translate.clearHandlers("attachmentProgress"); translate.setHandler("done", function(obj, returnValue) { if(!returnValue) { @@ -550,8 +552,6 @@ var Zotero_Browser = new function() { Zotero_Browser.isScraping = false; }); - var attachmentsMap = new WeakMap(); - translate.setHandler("itemDone", function(obj, dbItem, item) { Zotero_Browser.progress.show(); var itemProgress = new Zotero_Browser.progress.ItemProgress(Zotero.ItemTypes.getImageSrc(item.itemType), @@ -559,7 +559,7 @@ var Zotero_Browser = new function() { itemProgress.setProgress(100); for(var i=0; i<item.attachments.length; i++) { var attachment = item.attachments[i]; - attachmentsMap.set(attachment, + _attachmentsMap.set(attachment, new Zotero_Browser.progress.ItemProgress( Zotero.Utilities.determineAttachmentIcon(attachment), attachment.title, itemProgress)); @@ -572,7 +572,7 @@ var Zotero_Browser = new function() { }); translate.setHandler("attachmentProgress", function(obj, attachment, progress, error) { - var itemProgress = attachmentsMap.get(attachment); + var itemProgress = _attachmentsMap.get(attachment); if(progress === false) { itemProgress.setError(); } else { diff --git a/chrome/content/zotero/xpcom/connector/translate_item.js b/chrome/content/zotero/xpcom/connector/translate_item.js @@ -106,8 +106,6 @@ Zotero.Translate.ItemSaver.prototype = { }); }, - // ALL CODE BELOW THIS POINT IS EXECUTED ONLY IN NON-FIREFOX ENVIRONMENTS - /** * Polls for updates to attachment progress * @param items Items in Zotero.Item.toArray() format @@ -162,6 +160,8 @@ Zotero.Translate.ItemSaver.prototype = { poll(); }, + // ALL CODE BELOW THIS POINT IS EXECUTED ONLY IN NON-FIREFOX ENVIRONMENTS + /** * Saves items to server * @param items Items in Zotero.Item.toArray() format @@ -182,7 +182,7 @@ Zotero.Translate.ItemSaver.prototype = { for(var i=0, n=items.length; i<n; i++) { var item = items[i]; - newItems.push(Zotero.Utilities.itemToServerJSON(item)); + newItems = newItems.concat(Zotero.Utilities.itemToServerJSON(item)); if(typedArraysSupported) { for(var j=0; j<item.attachments.length; j++) { item.attachments[j].id = Zotero.Utilities.randomString(); @@ -193,34 +193,40 @@ Zotero.Translate.ItemSaver.prototype = { } var me = this; - Zotero.API.createItem({"items":newItems}, null, function(statusCode, response) { - if(statusCode !== 201) { - callback(false, new Error("Save to server failed")); - } else { - Zotero.debug("Translate: Save to server complete"); - - Zotero.Prefs.getCallback(["downloadAssociatedFiles", "automaticSnapshots"], - function(prefs) { - if(typedArraysSupported) { - try { - var newKeys = me._getItemKeysFromServerResponse(response); - } catch(e) { - callback(false, e); - return; - } - - for(var i=0; i<items.length; i++) { - var item = items[i], key = newKeys[i]; - if(item.attachments && item.attachments.length) { - me._saveAttachmentsToServer(key, me._getFileBaseNameFromItem(item), - item.attachments, prefs, attachmentCallback); - } + Zotero.API.createItem({"items":newItems}, function(statusCode, response) { + if(statusCode !== 200) { + callback(false, new Error("Save to server failed with "+statusCode+" "+response)); + retrun; + } + + try { + var resp = JSON.parse(response); + } catch(e) { + callback(false, new Error("Unexpected response received from server")); + return; + } + for(var i in resp.failed) { + callback(false, new Error("Save to server failed with "+statusCode+" "+response)); + return; + } + + Zotero.debug("Translate: Save to server complete"); + Zotero.Prefs.getCallback(["downloadAssociatedFiles", "automaticSnapshots"], + function(prefs) { + + if(typedArraysSupported) { + Zotero.debug(response); + for(var i in resp.success) { + var item = items[i], key = resp.success[i]; + if(item.attachments && item.attachments.length) { + me._saveAttachmentsToServer(key, me._getFileBaseNameFromItem(item), + item.attachments, prefs, attachmentCallback); } } - - callback(true, items); - }); - } + } + + callback(true, items); + }); }); }, @@ -235,6 +241,7 @@ Zotero.Translate.ItemSaver.prototype = { * on failure or attachmentCallback(attachment, progressPercent) periodically during saving. */ "_saveAttachmentsToServer":function(itemKey, baseName, attachments, prefs, attachmentCallback) { + Zotero.debug("saveattachmentstoserver"); var me = this, uploadAttachments = [], retrieveHeadersForAttachments = attachments.length; @@ -252,6 +259,7 @@ Zotero.Translate.ItemSaver.prototype = { var attachment = uploadAttachments[i]; attachmentPayload.push({ "itemType":"attachment", + "parentItem":itemKey, "linkMode":attachment.linkMode, "title":(attachment.title ? attachment.title.toString() : "Untitled Attachment"), "accessDate":"CURRENT_TIMESTAMP", @@ -261,25 +269,23 @@ Zotero.Translate.ItemSaver.prototype = { }); } - Zotero.API.createItem({"items":attachmentPayload}, itemKey, function(statusCode, response) { - var err; - if(statusCode === 201) { + Zotero.API.createItem({"items":attachmentPayload}, function(statusCode, response) { + var resp; + if(statusCode === 200) { try { - var newKeys = me._getItemKeysFromServerResponse(response); - } catch(e) { - err = new Error("Unexpected response received from server"); - } - } else { - err = new Error("Unexpected status "+statusCode+" received from server"); + resp = JSON.parse(response); + if(!resp.success) resp = undefined; + } catch(e) {}; } Zotero.debug("Finished creating items"); for(var i=0; i<uploadAttachments.length; i++) { var attachment = uploadAttachments[i]; - if(err) { - attachmentProgress(attachment, false, err); + if(!resp || !resp.success[i]) { + attachmentCallback(attachment, false, + new Error("Unexpected response received from server "+statusCode+" "+response)); } else { - attachment.key = newKeys[i]; + attachment.key = resp.success[i]; if(attachment.linkMode === "linked_url") { attachmentCallback(attachment, 100); @@ -288,8 +294,6 @@ Zotero.Translate.ItemSaver.prototype = { } } } - - if(err) Zotero.logError(err); }); }; @@ -552,25 +556,6 @@ Zotero.Translate.ItemSaver.prototype = { }, /** - * Gets item keys from a server response - * @param {String} response ATOM response - */ - "_getItemKeysFromServerResponse":function(response) { - try { - response = (new DOMParser()).parseFromString(response, "text/xml"); - } catch(e) { - throw new Error("Save to server returned invalid output"); - } - var keyNodes = response.getElementsByTagNameNS("http://zotero.org/ns/api", "key"); - var newKeys = []; - for(var i=0, n=keyNodes.length; i<n; i++) { - newKeys.push("textContent" in keyNodes[i] ? keyNodes[i].textContent - : keyNodes[i].innerText); - } - return newKeys; - }, - - /** * Gets the base name for an attachment from an item object. This mimics the default behavior * of Zotero.Attachments.getFileBaseNameFromItem * @param {Object} item diff --git a/chrome/content/zotero/xpcom/data/collection.js b/chrome/content/zotero/xpcom/data/collection.js @@ -1497,5 +1497,5 @@ Zotero.Collection.prototype._refreshChildCollections = function () { Zotero.Collection.prototype._generateKey = function () { - return Zotero.ID.getKey(); + return Zotero.Utilites.generateObjectKey(); } diff --git a/chrome/content/zotero/xpcom/data/creator.js b/chrome/content/zotero/xpcom/data/creator.js @@ -552,7 +552,7 @@ Zotero.Creator.prototype._checkValue = function (field, value) { Zotero.Creator.prototype._generateKey = function () { - return Zotero.ID.getKey(); + return Zotero.Utilites.generateObjectKey(); } diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js @@ -5189,7 +5189,7 @@ Zotero.Item.prototype._clearFieldChange = function (field) { Zotero.Item.prototype._generateKey = function () { - return Zotero.ID.getKey(); + return Zotero.Utilites.generateObjectKey(); } diff --git a/chrome/content/zotero/xpcom/data/tag.js b/chrome/content/zotero/xpcom/data/tag.js @@ -773,6 +773,6 @@ Zotero.Tag.prototype._prepFieldChange = function (field) { Zotero.Tag.prototype._generateKey = function () { - return Zotero.ID.getKey(); + return Zotero.Utilites.generateObjectKey(); } diff --git a/chrome/content/zotero/xpcom/id.js b/chrome/content/zotero/xpcom/id.js @@ -25,7 +25,6 @@ Zotero.ID_Tracker = function () { this.get = get; - this.getKey = getKey; this.getBigInt = getBigInt; this.skip = skip; this.getTableName = getTableName; @@ -86,13 +85,6 @@ Zotero.ID_Tracker = function () { } - function getKey() { - // TODO: add 'L' and 'Y' after 3.0.11 cut-off - var baseString = "23456789ABCDEFGHIJKMNPQRSTUVWXZ"; - return Zotero.randomString(8, baseString); - } - - this.isValidKey = function (value) { var re = /^[23456789ABCDEFGHIJKLMNPQRSTUVWXYZ]{8}$/ return re.test(value); diff --git a/chrome/content/zotero/xpcom/storage.js b/chrome/content/zotero/xpcom/storage.js @@ -948,7 +948,7 @@ Zotero.Sync.Storage = new function () { let item = items.shift(); let row = attachmentData[item.id]; let lk = item.libraryKey; - //Zotero.debug("Checking attachment file for item " + lk); + Zotero.debug("Checking attachment file for item " + lk); let nsIFile = item.getFile(row, true); if (!nsIFile) { diff --git a/chrome/content/zotero/xpcom/translation/translate.js b/chrome/content/zotero/xpcom/translation/translate.js @@ -1304,8 +1304,6 @@ Zotero.Translate.Base.prototype = { if(!this._waitingForRPC) this._detectTranslatorsCollected(); } } else { - this._currentState = null; - // unset return value is equivalent to true if(returnValue === undefined) returnValue = true; @@ -1328,6 +1326,8 @@ Zotero.Translate.Base.prototype = { this._runHandler("error", error); } + this._currentState = null; + // call handlers this._runHandler("itemsDone", returnValue); if(returnValue) { @@ -1413,7 +1413,7 @@ Zotero.Translate.Base.prototype = { * Checks if saving done, and if so, fires done event */ "_checkIfDone":function() { - if(!this._savingItems && !this._savingAttachments.length && !this._currentState) { + if(!this._savingItems && !this._savingAttachments.length) { this._runHandler("done", true); } }, @@ -1748,7 +1748,7 @@ Zotero.Translate.Web.prototype._translateTranslatorLoaded = function() { }, function(obj) { me._translateRPCComplete(obj) }); } else if(runMode === Zotero.Translator.RUN_MODE_ZOTERO_SERVER) { var me = this; - Zotero.API.createItem({"url":this.document.location.href.toString()}, null, + Zotero.API.createItem({"url":this.document.location.href.toString()}, function(statusCode, response) { me._translateServerComplete(statusCode, response); }); @@ -1800,7 +1800,7 @@ Zotero.Translate.Web.prototype._translateServerComplete = function(statusCode, r Zotero.API.createItem({ "url":me.document.location.href.toString(), "items":selectedItems - }, null, + }, function(statusCode, response) { me._translateServerComplete(statusCode, response); }); diff --git a/chrome/content/zotero/xpcom/utilities.js b/chrome/content/zotero/xpcom/utilities.js @@ -1255,10 +1255,15 @@ Zotero.Utilities = { }, /** - * Converts an item from toArray() format to content=json format used by the server + * Converts an item from toArray() format to an array of items in + * the content=json format used by the server */ "itemToServerJSON":function(item) { - var newItem = {}; + var newItem = { + "itemKey":Zotero.Utilities.generateObjectKey(), + "itemVersion":0 + }, + newItems = [newItem]; var typeID = Zotero.ItemTypes.getID(item.itemType); if(!typeID) { @@ -1337,7 +1342,6 @@ Zotero.Utilities = { } else if(field === "notes") { // normalize notes var n = val.length; - var newNotes = newItem.notes = new Array(n); for(var j=0; j<n; j++) { var note = val[j]; if(typeof note === "object") { @@ -1347,7 +1351,8 @@ Zotero.Utilities = { } note = note.note; } - newNotes[j] = {"itemType":"note", "note":note.toString()}; + newItems.push({"itemType":"note", "parentItem":newItem.itemKey, + "note":note.toString()}); } } else if((fieldID = Zotero.ItemFields.getID(field))) { // if content is not a string, either stringify it or delete it @@ -1378,7 +1383,7 @@ Zotero.Utilities = { } } - return newItem; + return newItems; }, /** @@ -1723,6 +1728,15 @@ Zotero.Utilities = { }, /** + * Generates a valid object key for the server API + */ + "generateObjectKey":function getKey() { + // TODO: add 'L' and 'Y' after 3.0.11 cut-off + var baseString = "23456789ABCDEFGHIJKMNPQRSTUVWXZ"; + return Zotero.Utilities.randomString(8, baseString); + }, + + /** * Provides unicode support and other additional features for regular expressions * See https://github.com/slevithan/xregexp for usage */