diff --git a/chrome/content/zotero/xpcom/data/collection.js b/chrome/content/zotero/xpcom/data/collection.js index ef2835fff2..45d736deb7 100644 --- a/chrome/content/zotero/xpcom/data/collection.js +++ b/chrome/content/zotero/xpcom/data/collection.js @@ -1008,8 +1008,3 @@ Zotero.Collection.prototype._refreshChildItems = Zotero.Promise.coroutine(functi return this.loadChildItems(true); } }); - - -Zotero.Collection.prototype._generateKey = function () { - return Zotero.Utilities.generateObjectKey(); -} diff --git a/chrome/content/zotero/xpcom/data/dataObject.js b/chrome/content/zotero/xpcom/data/dataObject.js index 26fc8d9872..f20a9fb52d 100644 --- a/chrome/content/zotero/xpcom/data/dataObject.js +++ b/chrome/content/zotero/xpcom/data/dataObject.js @@ -23,10 +23,21 @@ ***** END LICENSE BLOCK ***** */ -Zotero.DataObject = function (objectType, objectTypePlural, dataTypes) { +/** + * + * @param {String} objectType + * @param {String[]} dataTypes A set of data types that can be loaded for this data object + * + * @property {String} (readOnly) objectType + * @property {String} (readOnly) libraryKey + * @property {String|null} parentKey Null if no parent + * @property {Integer|false|undefined} parentID False if no parent. Undefined if not applicable (e.g. search objects) + */ + +Zotero.DataObject = function (objectType, dataTypes) { this._objectType = objectType; this._ObjectType = objectType[0].toUpperCase() + objectType.substr(1); - this._objectTypePlural = objectTypePlural ? objectTypePlural : objectType + 's'; + this._objectTypePlural = Zotero.DataObjectUtilities.getObjectTypePlural(objectType); this._dataTypes = dataTypes; this._id = null; @@ -46,12 +57,20 @@ Zotero.DataObject = function (objectType, objectTypePlural, dataTypes) { this._clearChanged(); }; -Zotero.DataObject.prototype.__defineGetter__('objectType', function () { return this._objectType; }); -Zotero.DataObject.prototype.__defineGetter__('libraryKey', function () this.libraryID + "/" + this.key); -Zotero.DataObject.prototype.__defineGetter__('parentKey', function () this._parentKey ); -Zotero.DataObject.prototype.__defineSetter__('parentKey', function (val) this._setParentKey(val) ); -Zotero.DataObject.prototype.__defineGetter__('parentID', function () this._getParentID() ); -Zotero.DataObject.prototype.__defineSetter__('parentID', function (val) this._setParentID(val) ); +Zotero.Utilities.Internal.defineProperty(Zotero.DataObject, 'objectType', { + get: function() this._objectType +}); +Zotero.Utilities.Internal.defineProperty(Zotero.DataObject, 'libraryKey', { + get: function() this._libraryID + "/" + this._key +}); +Zotero.Utilities.Internal.defineProperty(Zotero.DataObject, 'parentKey', { + get: function() this._parentKey, + set: function(v) this._setParentKey(v) +}); +Zotero.Utilities.Internal.defineProperty(Zotero.DataObject, 'parentID', { + get: function() this._getParentID(), + set: function(v) this._setParentID(v) +}); Zotero.DataObject.prototype._get = function (field) { @@ -82,7 +101,7 @@ Zotero.DataObject.prototype._setIdentifier = function (field, value) { if (this._key) { throw new Error("Cannot set id if key is already set"); } - value = parseInt(value); + value = Zotero.DataObjectUtilities.checkDataID(value); this._identified = true; break; @@ -91,12 +110,13 @@ Zotero.DataObject.prototype._setIdentifier = function (field, value) { break; case 'key': - if (this._libraryID === undefined) { + if (this._libraryID === null) { throw new Error("libraryID must be set before key"); } if (this._id) { throw new Error("Cannot set key if id is already set"); } + value = Zotero.DataObjectUtilities.checkKey(value); this._identified = true; } @@ -129,25 +149,36 @@ Zotero.DataObject.prototype._getParentID = function () { * Set the id of the parent object * * @param {Number|false} [id=false] + * @return {Boolean} True if changed, false if stayed the same */ Zotero.DataObject.prototype._setParentID = function (id) { - return this._setParentKey(id ? this._getClass().getLibraryAndKeyFromID(id)[1] : false); + return this._setParentKey( + id + ? this._getClass().getLibraryAndKeyFromID(Zotero.DataObjectUtilities.checkDataID(id))[1] + : null + ); } - +/** + * Set the key of the parent object + * + * @param {String|null} [key=null] + * @return {Boolean} True if changed, false if stayed the same + */ Zotero.DataObject.prototype._setParentKey = function(key) { if (this._objectType == 'item') { if (!this.isNote() && !this.isAttachment()) { throw new Error("_setParentKey() can only be called on items of type 'note' or 'attachment'"); } } + key = Zotero.DataObjectUtilities.checkKey(key); if (this._parentKey == key) { return false; } this._markFieldChange('parentKey', this._parentKey); this._changed.parentKey = true; - this._parentKey = key ? key : null; + this._parentKey = key; this._parentID = null; return true; } @@ -156,7 +187,8 @@ Zotero.DataObject.prototype._setParentKey = function(key) { /** * Returns all relations of the object * - * @return object Object with predicates as keys and URIs as values + * @return {object} Object with predicates as keys and URI[], or URI (as string) + * in the case of a single object, as values */ Zotero.DataObject.prototype.getRelations = function () { this._requireData('relations'); @@ -187,14 +219,15 @@ Zotero.DataObject.prototype.getRelations = function () { /** * Updates the object's relations * - * @param {Object} newRelations Object with predicates as keys and URIs/arrays-of-URIs as values + * @param {Object} newRelations Object with predicates as keys and URI[] as values + * @return {Boolean} True if changed, false if stayed the same */ Zotero.DataObject.prototype.setRelations = function (newRelations) { this._requireData('relations'); // There can be more than one object for a given predicate, so build - // flat arrays with individual predicate-object pairs converted to - // JSON strings so we can use array_diff to determine what changed + // flat arrays with individual predicate-object pairs so we can use + // array_diff to determine what changed var oldRelations = this._relations; var sortFunc = function (a, b) { @@ -208,13 +241,8 @@ Zotero.DataObject.prototype.setRelations = function (newRelations) { var newRelationsFlat = []; for (let predicate in newRelations) { let object = newRelations[predicate]; - if (Array.isArray(object)) { - for (let i=0; i= 0) { + let i = stack; + stack = Components.stack.caller; + while(stack && i--) { + stack = stack.caller; + } + } else if (_stackTrace) { + // Stack trace enabled globally + stack = Components.stack.caller; + } else { + stack = undefined; + } + + if (stack) { + message += '\n' + Zotero.Debug.stackToString(stack); } if (_console) { @@ -194,4 +203,20 @@ Zotero.Debug = new function () { this.clear = function () { _output = []; } + + /** + * Format a stack trace for output in the same way that Error.stack does + * @param {Components.stack} stack + * @param {Integer} [lines=5] Number of lines to format + */ + this.stackToString = function (stack, lines) { + if (!lines) lines = 5; + var str = ''; + while(stack && lines--) { + str += '\n ' + (stack.name || '') + '@' + stack.filename + + ':' + stack.lineNumber; + stack = stack.caller; + } + return str.substr(1); + }; } diff --git a/chrome/content/zotero/xpcom/id.js b/chrome/content/zotero/xpcom/id.js index 8db03966a1..4387a963ab 100644 --- a/chrome/content/zotero/xpcom/id.js +++ b/chrome/content/zotero/xpcom/id.js @@ -83,13 +83,6 @@ Zotero.ID_Tracker = function () { } }); - - this.isValidKey = function (value) { - var re = /^[23456789ABCDEFGHIJKLMNPQRSTUVWXYZ]{8}$/ - return re.test(value); - } - - function getBigInt(max) { if (!max) { max = 9007199254740991; diff --git a/chrome/content/zotero/xpcom/search.js b/chrome/content/zotero/xpcom/search.js index 6f706c1aa1..b7b57e2f5a 100644 --- a/chrome/content/zotero/xpcom/search.js +++ b/chrome/content/zotero/xpcom/search.js @@ -1642,13 +1642,6 @@ Zotero.Search.prototype._buildQuery = Zotero.Promise.coroutine(function* () { this._sqlParams = sqlParams.length ? sqlParams : false; }); - -Zotero.Search.prototype._generateKey = function () { - return Zotero.Utilities.generateObjectKey(); -} - - - Zotero.Searches = new function(){ Zotero.DataObjects.apply(this, ['search', 'searches', 'savedSearch', 'savedSearches']); this.constructor.prototype = new Zotero.DataObjects(); diff --git a/chrome/content/zotero/xpcom/utilities.js b/chrome/content/zotero/xpcom/utilities.js index 91b223be86..8649d3c285 100644 --- a/chrome/content/zotero/xpcom/utilities.js +++ b/chrome/content/zotero/xpcom/utilities.js @@ -1812,16 +1812,26 @@ Zotero.Utilities = { return Zotero.ItemTypes.getImageSrc(attachment.mimeType === "application/pdf" ? "attachment-pdf" : "attachment-snapshot"); }, - + + "allowedKeyChars": "23456789ABCDEFGHIJKLMNPQRSTUVWXYZ", + /** * Generates a valid object key for the server API */ "generateObjectKey":function generateObjectKey() { - // TODO: add 'L' and 'Y' after 3.0.11 cut-off - var baseString = "23456789ABCDEFGHIJKMNPQRSTUVWXZ"; - return Zotero.Utilities.randomString(8, baseString); + return Zotero.Utilities.randomString(8, Zotero.Utilities.allowedKeyChars); }, - + + /** + * Check if an object key is in a valid format + */ + "isValidObjectKey":function(key) { + if (!Zotero.Utilities.objectKeyRegExp) { + Zotero.Utilities.objectKeyRegExp = new RegExp('^[' + Zotero.Utilities.allowedKeyChars + ']{8}$'); + } + return Zotero.Utilities.objectKeyRegExp.test(key); + }, + /** * Provides unicode support and other additional features for regular expressions * See https://github.com/slevithan/xregexp for usage diff --git a/chrome/content/zotero/xpcom/utilities_internal.js b/chrome/content/zotero/xpcom/utilities_internal.js index d7bbc85d9b..adb31fc593 100644 --- a/chrome/content/zotero/xpcom/utilities_internal.js +++ b/chrome/content/zotero/xpcom/utilities_internal.js @@ -366,6 +366,24 @@ Zotero.Utilities.Internal = { break; } } + }, + + /** + * Defines property on the object's prototype. + * More compact way to do Object.defineProperty + * + * @param {Object} obj Target object + * @param {String} prop Property to be defined + * @param {Object} desc Propery descriptor. If not overriden, "enumerable" is true + */ + "defineProperty": function(obj, prop, desc) { + if (typeof prop != 'string') throw new Error("Property must be a string"); + var d = { __proto__: null, enumerable: true }; // Enumerable by default + for (let p in desc) { + if (!desc.hasOwnProperty(p)) continue; + d[p] = desc[p]; + } + Object.defineProperty(obj.prototype, prop, d); } } diff --git a/chrome/content/zotero/xpcom/zotero.js b/chrome/content/zotero/xpcom/zotero.js index e9d9918ea7..61c22e19fb 100644 --- a/chrome/content/zotero/xpcom/zotero.js +++ b/chrome/content/zotero/xpcom/zotero.js @@ -1217,10 +1217,21 @@ Components.utils.import("resource://gre/modules/osfile.jsm"); * * Uses prefs e.z.debug.log and e.z.debug.level (restart required) * - * Defaults to log level 3 if level not provided + * @param {} message + * @param {Integer} [level=3] + * @param {Boolean|Integer} [stack] Whether to display the calling stack. + * If true, stack is displayed starting from the caller. If an integer, + * that many stack levels will be omitted starting from the caller. */ - function debug(message, level) { - Zotero.Debug.log(message, level); + function debug(message, level, stack) { + // Account for this alias + if (stack === true) { + stack = 1; + } else if (stack >= 0) { + stack++; + } + + Zotero.Debug.log(message, level, stack); }