From 15722e5022b8aa2e9aebc174e0e98a2de4c49f15 Mon Sep 17 00:00:00 2001 From: Aurimas Vinckevicius Date: Thu, 28 Aug 2014 18:15:43 -0500 Subject: [PATCH 1/2] Allow calling Zotero.Translate.*.translate without setting translator first. This simply means that detection code will be run first. Attempting this with Export translators will fail, because trying to detect a translator does not make sense in this case. --- .../zotero/xpcom/translation/translate.js | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/chrome/content/zotero/xpcom/translation/translate.js b/chrome/content/zotero/xpcom/translation/translate.js index 38976250f..abc3252e0 100644 --- a/chrome/content/zotero/xpcom/translation/translate.js +++ b/chrome/content/zotero/xpcom/translation/translate.js @@ -1112,13 +1112,23 @@ Zotero.Translate.Base.prototype = { * @param {Boolean} [saveAttachments=true] Exclude attachments (e.g., snapshots) on import */ "translate":function(libraryID, saveAttachments) { // initialize properties specific to each translation - this._currentState = "translate"; - if(!this.translator || !this.translator.length) { - this.complete(false, new Error("No translator specified")); + var args = arguments; + Zotero.debug("Translate: translate called without specifying a translator. Running detection first."); + this.setHandler('translators', function(me, translators) { + if(!translators.length) { + me.complete(false, "Could not find an appropriate translator"); + } else { + me.setTranslator(translators); + Zotero.Translate.Base.prototype.translate.apply(me, args); + } + }); + this.getTranslators(); return; } + this._currentState = "translate"; + this._libraryID = libraryID; this._saveAttachments = saveAttachments === undefined || saveAttachments; this._savingAttachments = []; @@ -2158,6 +2168,19 @@ Zotero.Translate.Export.prototype._prepareTranslation = function() { this._sandboxManager.importObject(this._io); } +/** + * Overload Zotero.Translate.Base#translate to make sure that + * Zotero.Translate.Export#translate is not called without setting a + * translator first. Doesn't make sense to run detection for export. + */ +Zotero.Translate.Export.prototype.translate = function() { + if(!this.translator || !this.translator.length) { + this.complete(false, new Error("Export translation initiated without setting a translator")); + } else { + Zotero.Translate.Base.prototype.translate.apply(this, arguments); + } +}; + /** * Return the progress of the import operation, or null if progress cannot be determined */ From 3b4c502ca9ba30cff5a45b1b58110eb2514decfc Mon Sep 17 00:00:00 2001 From: Aurimas Vinckevicius Date: Sat, 30 Aug 2014 01:25:42 -0500 Subject: [PATCH 2/2] [RecognizePDF] Attempt to search by ISBN even if we found a DOI (but only if DOI search failed) --- chrome/content/zotero/recognizePDF.js | 51 +++++++++++++++------------ 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/chrome/content/zotero/recognizePDF.js b/chrome/content/zotero/recognizePDF.js index 9b50eac23..a51ab77e3 100644 --- a/chrome/content/zotero/recognizePDF.js +++ b/chrome/content/zotero/recognizePDF.js @@ -95,30 +95,34 @@ var Zotero_RecognizePDF = new function() { // Look up DOI Zotero.debug("RecognizePDF: Found DOI: "+doi); - var translate = new Zotero.Translate.Search(); - translate.setTranslator("11645bd1-0420-45c1-badb-53fb41eeb753"); - translate.setSearch({"itemType":"journalArticle", "DOI":doi}); - promise = _promiseTranslate(translate, libraryID); + var translateDOI = new Zotero.Translate.Search(); + translateDOI.setTranslator("11645bd1-0420-45c1-badb-53fb41eeb753"); + translateDOI.setSearch({"itemType":"journalArticle", "DOI":doi}); + promise = _promiseTranslate(translateDOI, libraryID); } else { - // Look for ISBNs if no DOI - var isbns = _findISBNs(allText); - if(isbns.length) { - Zotero.debug("RecognizePDF: Found ISBNs: " + isbns); - - var translate = new Zotero.Translate.Search(); - translate.setTranslator("c73a4a8c-3ef1-4ec8-8229-7531ee384cc4"); - translate.setSearch({"itemType":"book", "ISBN":isbns[0]}); - promise = _promiseTranslate(translate, libraryID); - } else { - promise = Q.reject("No ISBN or DOI found"); - } + promise = Q.reject("No DOI found in text"); } - // If no DOI or ISBN, query Google Scholar - return promise.fail(function(error) { - Zotero.debug("RecognizePDF: "+error); - return me.GSFullTextSearch.findItem(lines, libraryID, stopCheckCallback); - }); + return promise + // Look for ISBNs if no DOI + .fail(function(error) { + Zotero.debug("RecognizePDF: " + error); + var isbns = _findISBNs(allText); + if (isbns.length) { + Zotero.debug("RecognizePDF: Found ISBNs: " + isbns); + + var translate = new Zotero.Translate.Search(); + translate.setSearch({"itemType":"book", "ISBN":isbns[0]}); + return _promiseTranslate(translate, libraryID); + } else { + return Q.reject("No ISBN found in text."); + } + }) + // If no DOI or ISBN, query Google Scholar + .fail(function(error) { + Zotero.debug("RecognizePDF: " + error); + return me.GSFullTextSearch.findItem(lines, libraryID, stopCheckCallback); + }); }); } @@ -196,7 +200,10 @@ var Zotero_RecognizePDF = new function() { if(success && translate.newItems.length) { deferred.resolve(translate.newItems[0]); } else { - deferred.reject("Translation with Google Scholar failed"); + deferred.reject(translate.translator && translate.translator.length + ? "Translation with " + translate.translator.map(t => t.label) + " failed" + : "Could not find a translator for given search item" + ); } }); translate.translate(libraryID, false);