EPUB recognition: Fix DOI search results being thrown away

https://forums.zotero.org/discussion/116524/epub-articles-and-books-not-getting-metadata-like-pdfs
This commit is contained in:
Abe Jellinek 2024-08-06 10:53:43 -04:00
parent 24aed7d530
commit 206496332d
3 changed files with 77 additions and 5 deletions

View file

@ -624,14 +624,15 @@ Zotero.RecognizeDocument = new function () {
saveAttachments: false
});
if (searchItemJSON) {
if (search.ISBN && searchItemJSON?.ISBN?.split(' ')
itemJSON = searchItemJSON;
if (search.ISBN && searchItemJSON.ISBN && !searchItemJSON.ISBN.split(' ')
.map(resolvedISBN => Zotero.Utilities.cleanISBN(resolvedISBN))
.includes(search.ISBN)) {
Zotero.debug('RecognizeDocument: Using ISBN search result');
itemJSON = searchItemJSON;
Zotero.debug(`RecognizeDocument: ISBN mismatch (was ${search.ISBN}, got ${searchItemJSON.ISBN})`);
itemJSON = null;
}
else {
Zotero.debug(`RecognizeDocument: ISBN mismatch (was ${search.ISBN}, got ${searchItemJSON.ISBN})`);
Zotero.debug('RecognizeDocument: Using search result');
}
}
}

Binary file not shown.

View file

@ -11,6 +11,9 @@ describe("Document Recognition", function() {
}
before(function* () {
// Don't slow down attachment imports with indexing
Zotero.Prefs.set('fulltext.textMaxLength', 0);
this.timeout(60000);
// Load Zotero pane and install PDF tools
yield Zotero.Promise.all([
@ -39,6 +42,8 @@ describe("Document Recognition", function() {
});
after(function() {
Zotero.Prefs.clear('fulltext.textMaxLength');
if (win) {
win.close();
}
@ -369,7 +374,73 @@ describe("Document Recognition", function() {
translateStub.restore();
});
it("should recognize an EPUB without an ISBN and rename the file", async function () {
it("should recognize an EPUB by DOI and rename the file", async function () {
let doi = '10.1177/20539517241232630';
let search;
let translateStub = sinon.stub(Zotero.Translate.Search.prototype, 'translate')
.callsFake(async function () {
search = this.search;
return [{
itemType: "journalArticle",
title: "Big AI: Cloud infrastructure dependence and the industrialisation of artificial intelligence",
publicationTitle: "Big Data & Society",
DOI: "10.1177/20539517241232630",
creators: [
{
firstName: "Fernando",
lastName: "Van Der Vlist",
creatorType: "author"
},
{
firstName: "Anne",
lastName: "Helmond",
creatorType: "author"
},
{
firstName: "Fabian",
lastName: "Ferrari",
creatorType: "author"
}
],
tags: []
}];
});
let testDir = getTestDataDirectory();
testDir.append('recognizeEPUB_test_DOI.epub');
let attachment = await Zotero.Attachments.importFromFile({ file: testDir });
win.ZoteroPane.recognizeSelected();
let addedIDs = await waitForItemEvent('add');
let modifiedIDs = await waitForItemEvent('modify');
assert.isTrue(translateStub.calledOnce);
assert.ok(search);
assert.equal(search.DOI, doi);
assert.lengthOf(addedIDs, 1);
let item = Zotero.Items.get(addedIDs[0]);
assert.equal(item.getField('title'), 'Big AI: Cloud infrastructure dependence and the industrialisation of artificial intelligence');
assert.equal(Zotero.Utilities.cleanDOI(item.getField('DOI')), doi);
assert.lengthOf(modifiedIDs, 2);
await waitForProgressWindow();
// The file should have been renamed
assert.equal(
attachment.attachmentFilename,
Zotero.Attachments.getFileBaseNameFromItem(item) + '.epub'
);
// The title should have changed
assert.equal(
attachment.getField('title'),
Zotero.getString('file-type-ebook')
);
translateStub.restore();
});
it("should recognize an EPUB without identifiers and rename the file", async function () {
let testDir = getTestDataDirectory();
testDir.append('recognizeEPUB_test_DC.epub');
let attachment = await Zotero.Attachments.importFromFile({ file: testDir });