Link Mendeley citations in documents to imported items. Closes #2622

This commit is contained in:
Adomas Venčkauskas 2022-11-29 16:27:11 +02:00
parent 76f2f0c783
commit e59bcd2b64
2 changed files with 62 additions and 22 deletions

View file

@ -65,6 +65,8 @@ const NOTE_CITATION_PLACEHOLDER_LINK = 'https://www.zotero.org/?';
const TEMPLATE_VERSION = 1; const TEMPLATE_VERSION = 1;
const MENDELEY_URI_RE = /^http:\/\/www\.mendeley\.com\/documents\/\?uuid=(.*)/;
Zotero.Integration = new function() { Zotero.Integration = new function() {
Components.utils.import("resource://gre/modules/Services.jsm"); Components.utils.import("resource://gre/modules/Services.jsm");
@ -2617,7 +2619,7 @@ Zotero.Integration.URIMap.prototype.getURIsForItemID = function(id) {
/** /**
* Gets Zotero item for a given set of URIs * Gets Zotero item for a given set of URIs
*/ */
Zotero.Integration.URIMap.prototype.getZoteroItemForURIs = Zotero.Promise.coroutine(function* (uris) { Zotero.Integration.URIMap.prototype.getZoteroItemForURIs = async function (uris) {
var zoteroItem = false; var zoteroItem = false;
var needUpdate = false; var needUpdate = false;
var embeddedItem = false;; var embeddedItem = false;;
@ -2632,43 +2634,52 @@ Zotero.Integration.URIMap.prototype.getZoteroItemForURIs = Zotero.Promise.corout
// Next try getting URI directly // Next try getting URI directly
try { try {
zoteroItem = yield Zotero.URI.getURIItem(uri); var replacer = await Zotero.URI.getURIItem(uri);
if(zoteroItem) { if (replacer && !replacer.deleted) {
// Ignore items in the trash zoteroItem = replacer;
if(zoteroItem.deleted) { break;
zoteroItem = false;
} else {
break;
}
} }
} catch(e) {} } catch(e) {}
// Try merged item mapping // Try merged item mapping
var replacer = yield Zotero.Relations.getByPredicateAndObject( var replacer = await Zotero.Relations.getByPredicateAndObject(
'item', Zotero.Relations.replacedItemPredicate, uri 'item', Zotero.Relations.replacedItemPredicate, uri
); );
if (replacer.length && !replacer[0].deleted) { if (replacer.length && !replacer[0].deleted) {
zoteroItem = replacer[0]; zoteroItem = replacer[0];
break;
}
// Check if it's a mendeley URI and if we have imported the item
let m = MENDELEY_URI_RE.exec(uri);
if (m) {
replacer = await Zotero.Relations.getByPredicateAndObject(
'item', 'mendeleyDB:documentUUID', m[1]
);
if (replacer.length && !replacer[0].deleted) {
zoteroItem = replacer[0];
break;
}
} }
if(zoteroItem) break;
} }
if(zoteroItem) { if (zoteroItem) {
// make sure URI is up to date (in case user just began syncing) // make sure URI is up to date (in case user just began syncing)
var newURI = Zotero.URI.getItemURI(zoteroItem); var newURI = Zotero.URI.getItemURI(zoteroItem);
if(newURI != uris[i]) { if (!uris.includes(newURI)) {
uris[i] = newURI; uris.push(newURI);
needUpdate = true; needUpdate = true;
} }
// cache uris // cache uris
this.itemIDURIs[zoteroItem.id] = uris; this.add(zoteroItem.id, uris)
} else if(embeddedItem) { }
else if (embeddedItem) {
return [embeddedItem, false]; return [embeddedItem, false];
} }
return [zoteroItem, needUpdate]; return [zoteroItem, needUpdate];
}); };
Zotero.Integration.Field = class { Zotero.Integration.Field = class {
constructor(field, rawCode) { constructor(field, rawCode) {
@ -2847,8 +2858,13 @@ Zotero.Integration.CitationField = class extends Zotero.Integration.Field {
} }
// for update from Zotero 2.1 or earlier // for update from Zotero 2.1 or earlier
if(citationItem.uri) { if (citationItem.uri) {
citationItem.uris = citationItem.uri; if (Array.isArray(citationItem.uris)) {
citationItem.uris.push(citationItem.uri);
}
else {
citationItem.uris = [citationItem.uri];
}
delete citationItem.uri; delete citationItem.uri;
} }
} }

View file

@ -720,11 +720,35 @@ describe("Zotero.Integration", function () {
assert.isNotOk(citation.properties.dontUpdate); assert.isNotOk(citation.properties.dontUpdate);
}); });
}); });
it('should detect items cited with Mendeley if they are imported into Zotero', async function() {
var docID = this.test.fullTitle();
if (!(docID in applications)) await initDoc(docID);
var doc = applications[docID].doc;
let testItem = await createDataObject('item', {libraryID: Zotero.Libraries.userLibraryID});
testItem.setField('title', `Mendeley imported`);
testItem.setCreator(0, {creatorType: 'author', name: `Mendeleev, Dmitri `});
testItem.addRelation('mendeleyDB:documentUUID', 'e213167f-af42-4ff1-95e8-a9aa6b0b3e1b');
await testItem.saveTx();
setAddEditItems(testItem);
await execCommand('addEditCitation', docID);
let uri = doc.fields[0].code.match(/"uris":\[([^\]]*)]/)[1];
doc.fields[0].code = doc.fields[0].code.replace(/"uris":\[[^\]]*]/, `"uris":["http://www.mendeley.com/documents/?uuid=e213167f-af42-4ff1-95e8-a9aa6b0b3e1b"]`);
sinon.stub(doc, 'canInsertField').resolves(false);
sinon.stub(doc, 'cursorInField').resolves(doc.fields[0]);
await execCommand('addEditCitation', docID);
assert.include(doc.fields[0].code, 'http://www.mendeley.com/documents/?uuid=e213167f-af42-4ff1-95e8-a9aa6b0b3e1b');
assert.include(doc.fields[0].code, Zotero.URI.getItemURI(testItem));
});
describe('when there are copy-pasted citations', function() { describe('when there are copy-pasted citations', function() {
it('should resolve duplicate citationIDs and mark both as new citations', async function() { it('should resolve duplicate citationIDs and mark both as new citations', async function() {
var docID = this.test.fullTitle(); var docID = this.test.fullTitle();
if (!(docID in applications)) initDoc(docID); if (!(docID in applications)) await initDoc(docID);
var doc = applications[docID].doc; var doc = applications[docID].doc;
setAddEditItems(testItems[0]); setAddEditItems(testItems[0]);
@ -754,7 +778,7 @@ describe("Zotero.Integration", function () {
it('should successfully process citations copied in from another doc', async function() { it('should successfully process citations copied in from another doc', async function() {
var docID = this.test.fullTitle(); var docID = this.test.fullTitle();
if (!(docID in applications)) initDoc(docID); if (!(docID in applications)) await initDoc(docID);
var doc = applications[docID].doc; var doc = applications[docID].doc;
setAddEditItems(testItems[0]); setAddEditItems(testItems[0]);
@ -790,7 +814,7 @@ describe("Zotero.Integration", function () {
it('should successfully insert a citation after canceled citation insert', async function () { it('should successfully insert a citation after canceled citation insert', async function () {
var docID = this.test.fullTitle(); var docID = this.test.fullTitle();
if (!(docID in applications)) initDoc(docID); if (!(docID in applications)) await initDoc(docID);
var doc = applications[docID].doc; var doc = applications[docID].doc;
setAddEditItems(testItems[0]); setAddEditItems(testItems[0]);