From 599598566ba636ba92c73767cbfa07fb5e44b04c Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Thu, 7 Jul 2016 16:29:08 -0400 Subject: [PATCH] Use best first creator for OpenURL author, not just first position This also fixes Google Scholar Search lookup to use an author not in first creator position [1]. [1] https://forums.zotero.org/discussion/58384/ --- chrome/content/zotero/xpcom/data/items.js | 24 +++++++++++++++++++++++ chrome/content/zotero/xpcom/openurl.js | 2 +- test/tests/openurlTest.js | 23 ++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 test/tests/openurlTest.js diff --git a/chrome/content/zotero/xpcom/data/items.js b/chrome/content/zotero/xpcom/data/items.js index 6ab168e56e..91706dabdc 100644 --- a/chrome/content/zotero/xpcom/data/items.js +++ b/chrome/content/zotero/xpcom/data/items.js @@ -955,6 +955,30 @@ Zotero.Items = function() { }); + /** + * Given API JSON for an item, return the best first creator, regardless of creator order + * + * @return {Object|false} - Creator in API JSON format, or false + */ + this.getFirstCreatorFromJSON = function (json) { + var primaryCreatorType = Zotero.CreatorTypes.getName( + Zotero.CreatorTypes.getPrimaryIDForType( + Zotero.ItemTypes.getID(json.itemType) + ) + ); + let firstCreator = json.creators.find(creator => { + return creator.creatorType == primaryCreatorType || creator.creatorType == 'author'; + }); + if (!firstCreator) { + firstCreator = json.creators.find(creator => creator.creatorType == 'editor'); + } + if (!firstCreator) { + return false; + } + return firstCreator; + }; + + /* * Generate SQL to retrieve firstCreator field * diff --git a/chrome/content/zotero/xpcom/openurl.js b/chrome/content/zotero/xpcom/openurl.js index 509673bbb6..f9b54e4cb4 100644 --- a/chrome/content/zotero/xpcom/openurl.js +++ b/chrome/content/zotero/xpcom/openurl.js @@ -205,7 +205,7 @@ Zotero.OpenURL = new function() { if(item.creators && item.creators.length) { // encode first author as first and last - var firstCreator = item.creators[0]; + let firstCreator = Zotero.Items.getFirstCreatorFromJSON(item); if(item.itemType == "patent") { _mapTag(firstCreator.firstName, "invfirst"); _mapTag(firstCreator.lastName, "invlast"); diff --git a/test/tests/openurlTest.js b/test/tests/openurlTest.js new file mode 100644 index 0000000000..4e21493262 --- /dev/null +++ b/test/tests/openurlTest.js @@ -0,0 +1,23 @@ +"use strict"; + +describe("Zotero.OpenURL", function() { + describe("#createContextObject()", function () { + it("should use firstCreator for author", function* () { + var item = createUnsavedDataObject('item'); + item.setCreators([ + { + firstName: "Aaa", + lastName: "Editor", + creatorType: 'editor' + }, + { + firstName: "Bbb", + lastName: "Author", + creatorType: 'author' + } + ]); + var co = Zotero.OpenURL.createContextObject(item, "1.0"); + assert.include(co, '&rft.aufirst=Bbb&rft.aulast=Author&'); + }); + }); +});