Add option to Mendeley importer to relink items

New option only appears if importer version is < 1 or not present.
It will:
    * Skip fetching collections and attachments
    * Skip any new items
    * Update relations on existing items
This commit is contained in:
Tom Najdek 2022-12-15 19:35:29 +01:00 committed by Dan Stillman
parent 48ad7c0ab7
commit 91c385a67a
7 changed files with 101 additions and 13 deletions

View file

@ -450,6 +450,7 @@ var Zotero_File_Interface = new function() {
translation.mendeleyAuth = options.mendeleyAuth;
translation.mendeleyCode = options.mendeleyCode;
translation.newItemsOnly = options.newItemsOnly;
translation.relinkOnly = options.relinkOnly;
}
else {
// Check if the file is an SQLite database

View file

@ -12,6 +12,7 @@ var Zotero_Import_Wizard = {
_mendeleyCode: null,
_mendeleyAuth: null,
_mendeleyHasPreviouslyImported: false,
_mendeleyImporterVersion: 0,
_isZotfileInstalled: false,
init: async function () {
@ -32,6 +33,8 @@ var Zotero_Import_Wizard = {
this._mendeleyHasPreviouslyImported = !!(await Zotero.DB.valueQueryAsync(relSQL, predicateID));
}
this._mendeleyImporterVersion = parseInt((await Zotero.DB.valueQueryAsync("SELECT value FROM settings WHERE setting='mendeleyImport' AND key='version'")) || 0);
// If no existing collections or non-trash items in the library, don't create a new
// collection by default
var args = window.arguments[0].wrappedJSObject;
@ -82,6 +85,8 @@ var Zotero_Import_Wizard = {
document.getElementById('mendeley-username').addEventListener('keyup', this.onMendeleyAuthKeyUp.bind(this));
document.getElementById('mendeley-password').addEventListener('keyup', this.onMendeleyAuthKeyUp.bind(this));
document.getElementById('relink-only-checkbox').addEventListener('command', this.onRelinkOnlyChange.bind(this));
Zotero.Translators.init(); // async
},
@ -187,6 +192,16 @@ var Zotero_Import_Wizard = {
&& document.getElementById('mendeley-password').value.length > 0;
},
onRelinkOnlyChange: function () {
if (document.getElementById('relink-only-checkbox').checked) {
document.getElementById('new-items-only-checkbox').checked = true;
document.getElementById('create-collection-checkbox').checked = false;
}
document.getElementById('new-items-only-checkbox').disabled = document.getElementById('relink-only-checkbox').checked;
document.getElementById('create-collection-checkbox').disabled = document.getElementById('relink-only-checkbox').checked;
},
goToStart: function () {
this._wizard.goTo('page-start');
this._wizard.canAdvance = true;
@ -268,6 +283,7 @@ var Zotero_Import_Wizard = {
document.getElementById('file-handling-options').hidden = !!(this._mendeleyAuth || this._mendeleyCode);
const hideExtraMendeleyOptions = !this._mendeleyHasPreviouslyImported || !(this._mendeleyAuth || this._mendeleyCode);
document.getElementById('mendeley-options').hidden = hideExtraMendeleyOptions;
document.getElementById('relink-only-wrapper').hidden = hideExtraMendeleyOptions || this._mendeleyImporterVersion > 0;
if (hideExtraMendeleyOptions) {
document.getElementById('new-items-only-checkbox').removeAttribute('checked');
}
@ -309,12 +325,12 @@ var Zotero_Import_Wizard = {
let result = await Zotero_File_Interface.importFile({
file: this._file,
onBeforeImport: this.onBeforeImport.bind(this),
addToLibraryRoot: !document.getElementById('create-collection-checkbox')
.hasAttribute('checked'),
addToLibraryRoot: !document.getElementById('create-collection-checkbox').checked,
linkFiles: document.getElementById('file-handling-radio').selectedIndex == 1,
mendeleyAuth: this._mendeleyAuth,
mendeleyCode: this._mendeleyCode,
newItemsOnly: document.getElementById('new-items-only-checkbox').hasAttribute('checked')
newItemsOnly: document.getElementById('new-items-only-checkbox').checked,
relinkOnly: document.getElementById('relink-only-checkbox').checked
});
// Cancelled by user or due to error

View file

@ -78,6 +78,11 @@
onpageshow="Zotero_Import_Wizard.onOptionsShown()"
onpagerewound="return Zotero_Import_Wizard.goToStart()"
onpageadvanced="Zotero_Import_Wizard.onImportStart()">
<vbox id="relink-only-wrapper">
<checkbox id="relink-only-checkbox" label="&zotero.import.online.relinkOnly;" />
</vbox>
<checkbox id="create-collection-checkbox" label="&zotero.import.createCollection;" checked="true" />
<vbox id="file-handling-options">

View file

@ -32,6 +32,7 @@ var Zotero_Import_Mendeley = function () {
this.newCollections = [];
this.mendeleyAuth = null;
this.newItemsOnly = false;
this.relinkOnly = false;
this._tokens = null;
this._db = null;
@ -93,12 +94,14 @@ Zotero_Import_Mendeley.prototype.translate = async function (options = {}) {
...(options.saveOptions || {})
};
this.newItemsOnly = this.newItemsOnly || this.relinkOnly;
const libraryID = options.libraryID || Zotero.Libraries.userLibraryID;
const { key: rootCollectionKey } = options.collections
? Zotero.Collections.getLibraryAndKeyFromID(options.collections[0])
: {};
Zotero.debug(`Begining Mendeley import at ${this._started}. libraryID: ${libraryID}, linkFiles: ${this.linkFiles}, rootCollectionKey: ${rootCollectionKey}`);
Zotero.debug(`Begining Mendeley import at ${this._started}. libraryID: ${libraryID}, linkFiles: ${this.linkFiles}, rootCollectionKey: ${rootCollectionKey}, newItemsOnly: ${this.newItemsOnly}, relinkOnly: ${this.relinkOnly}`);
// TODO: Get appropriate version based on schema version
const mapVersion = 83;
@ -135,14 +138,17 @@ Zotero_Import_Mendeley.prototype.translate = async function (options = {}) {
this._progressMax = 50;
this._itemDone();
const folders = this._tokens
? await this._getFoldersAPI(mendeleyGroupID)
: await this._getFoldersDB(mendeleyGroupID);
let folderKeys = new Map();
if(!this.relinkOnly) {
const folders = this._tokens
? await this._getFoldersAPI(mendeleyGroupID)
: await this._getFoldersDB(mendeleyGroupID);
const collectionJSON = this._foldersToAPIJSON(folders, rootCollectionKey);
const folderKeys = this._getFolderKeys(collectionJSON);
const collectionJSON = this._foldersToAPIJSON(folders, rootCollectionKey);
folderKeys = this._getFolderKeys(collectionJSON);
await this._saveCollections(libraryID, collectionJSON, folderKeys);
await this._saveCollections(libraryID, collectionJSON, folderKeys);
}
this._interruptChecker(true);
//
@ -175,13 +181,13 @@ Zotero_Import_Mendeley.prototype.translate = async function (options = {}) {
this._interruptChecker(true);
let collections = this._tokens
let collections = this.relinkOnly ? new Map() : this._tokens
? await this._getDocumentCollectionsAPI(documents, rootCollectionKey, folderKeys)
: await this._getDocumentCollectionsDB(mendeleyGroupID, documents, rootCollectionKey, folderKeys);
this._interruptChecker(true);
let files = this._tokens
let files = this.relinkOnly ? new Map() : this._tokens
? await this._getDocumentFilesAPI(documents)
: await this._getDocumentFilesDB(mendeleyGroupID);
@ -1331,6 +1337,10 @@ Zotero_Import_Mendeley.prototype._saveItems = async function (libraryID, json) {
}
}
if(this.relinkOnly && !isMappedToExisting) {
continue;
}
// Remove external id before save
let toSave = Object.assign({}, itemJSON);
delete toSave.documentID;

View file

@ -187,6 +187,7 @@
<!ENTITY zotero.import.createCollection "Place imported collections and items into new collection">
<!ENTITY zotero.import.fileHandling "File Handling">
<!ENTITY zotero.import.online.newItemsOnly "Download new items only; dont update previously imported items">
<!ENTITY zotero.import.online.relinkOnly "Relink Mendeley Desktop citations">
<!ENTITY zotero.exportOptions.title "Export…">
<!ENTITY zotero.exportOptions.format.label "Format:">

View file

@ -122,3 +122,7 @@ button, checkbox {
color: red;
font-weight: bold;
}
[disabled="true"] .checkbox-label {
opacity: .5;
}

View file

@ -402,5 +402,56 @@ describe('Zotero_Import_Mendeley', function () {
assert.equal(report.getField('title'), 'Sample Report');
assert.equal(report.getRelations()['mendeleyDB:documentUUID'], '616ec6d1-8d23-4414-8b6e-7bb129677577');
});
it("should only correct IDs and not add new items if \"relinkOnly\" is configured", async () => {
setHTTPResponse(server, 'https://api.mendeley.com/', {
method: 'GET',
url: `documents?view=all&limit=500`,
status: 200,
headers: {},
json: JSON.parse(
await Zotero.File.getContentsFromURLAsync('resource://zotero-unit-tests/data/mendeleyMock/items-simple-no-desktop-id.json')
)
});
const importer1 = getImporter();
await importer1.translate({
libraryID: Zotero.Libraries.userLibraryID,
collections: null,
linkFiles: false,
});
const report = (await Zotero.Relations
.getByPredicateAndObject('item', 'mendeleyDB:remoteDocumentUUID', '07a74c26-28d1-4d9f-a60d-3f3bc5ef76ef'))
.filter(item => item.libraryID == Zotero.Libraries.userLibraryID && !item.deleted)
.shift();
assert.equal(report.getField('title'), 'Sample Report');
assert.equal(report.getRelations()['mendeleyDB:documentUUID'], '07a74c26-28d1-4d9f-a60d-3f3bc5ef76ef');
setHTTPResponse(server, 'https://api.mendeley.com/', {
method: 'GET',
url: `documents?view=all&limit=500`,
status: 200,
headers: {},
json: JSON.parse(
await Zotero.File.getContentsFromURLAsync('resource://zotero-unit-tests/data/mendeleyMock/items-updated.json')
)
});
const importer2 = getImporter();
importer2.relinkOnly = true;
await importer2.translate({
libraryID: Zotero.Libraries.userLibraryID,
collections: null,
linkFiles: false,
});
assert.equal(report.getField('title'), 'Sample Report');
assert.equal(report.getRelations()['mendeleyDB:documentUUID'], '616ec6d1-8d23-4414-8b6e-7bb129677577');
const noNewItemHere = await Zotero.Relations.getByPredicateAndObject('item', 'mendeleyDB:documentUUID', '86e56a00-5ae5-4fe8-a977-9298a03b16d6');
assert.lengthOf(noNewItemHere, 0);
});
});
});