From 91585e5faa60c77d51c75fea1a54e69495de79a9 Mon Sep 17 00:00:00 2001 From: Emiliano Heyns Date: Tue, 25 Feb 2020 13:27:47 +0100 Subject: [PATCH 1/3] Emiliano -> Bjoern --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6e11d44..3e18253 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "url": "https://github.com/edtechhub/zotero-edtechhub.git" }, "author": { - "name": "Emiliano Heyns", + "name": "Bjoern Hassler", "email": "bjoern@edtechhub.org" }, "bugs": { From 8a74411b07f8ead4377c2104af5d70aad85270d8 Mon Sep 17 00:00:00 2001 From: Emiliano Heyns Date: Tue, 25 Feb 2020 13:30:55 +0100 Subject: [PATCH 2/3] =?UTF-8?q?Bj=C3=B6rn=C2=A0Ha=C3=9Fler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3e18253..1a638df 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "url": "https://github.com/edtechhub/zotero-edtechhub.git" }, "author": { - "name": "Bjoern Hassler", + "name": "Björn Haßler", "email": "bjoern@edtechhub.org" }, "bugs": { From e0cef6258371549d3f3e3c976a953ed51c0581ea Mon Sep 17 00:00:00 2001 From: Emiliano Heyns Date: Mon, 13 Apr 2020 20:23:43 +0200 Subject: [PATCH 3/3] fixes #27 --- content/zotero-edtechhub.ts | 53 ++++++++++++++------------------ eth-migrate-archiveLocation.js | 55 ++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 30 deletions(-) create mode 100644 eth-migrate-archiveLocation.js diff --git a/content/zotero-edtechhub.ts b/content/zotero-edtechhub.ts index 6e85351..2857cfa 100644 --- a/content/zotero-edtechhub.ts +++ b/content/zotero-edtechhub.ts @@ -89,17 +89,16 @@ $patch$(Zotero.Items, 'merge', original => async function(item, otherItems) { try { await Zotero.Schema.schemaUpdatePromise - // preserve archiveLocation of all merged items - const archiveLocation = Zotero.EdTechHub.getArchiveLocation(item).split(';') - debug(`merge-archiveLocation: pre archiveLocation = ${JSON.stringify(archiveLocation)}`) + // preserve AlsoKnownAs of all merged items + const alsoKnownAs = Zotero.EdTechHub.getAlsoKnownAs(item).split(';') for (const otherItem of otherItems) { - for (const al of Zotero.EdTechHub.getArchiveLocation(otherItem).split(';')) { - debug(`merge-archiveLocation: + ${JSON.stringify(al)}`) - if (al && !archiveLocation.includes(al)) archiveLocation.push(al) + for (const aka of Zotero.EdTechHub.getAlsoKnownAs(otherItem).split(';')) { + debug(`merge-alsoKnownAs: + ${JSON.stringify(aka)}`) + if (aka && !alsoKnownAs.includes(aka)) alsoKnownAs.push(aka) } } - debug(`merge-archiveLocation: post archiveLocation = ${JSON.stringify(archiveLocation)}`) - Zotero.EdTechHub.setArchiveLocation(item, archiveLocation.filter(al => al).join(';')) + debug(`merge-alsoKnownAs: post alsoKnownAs = ${JSON.stringify(alsoKnownAs)}`) + Zotero.EdTechHub.setAlsoKnownAs(item, alsoKnownAs.filter(aka => aka).join(';')) // keep RIS copy of all merged items const ris = await asRIS([item, ...otherItems]) @@ -192,8 +191,8 @@ const EdTechHub = Zotero.EdTechHub || new class { // tslint:disable-line:variabl private initialized: boolean = false private fieldID: { - archiveLocation: number - DOI: number + DOI: number, + extra: number, } private translators: { file: string, translatorID: string}[] = [] @@ -211,12 +210,12 @@ const EdTechHub = Zotero.EdTechHub || new class { // tslint:disable-line:variabl }, false) } - private getArchiveLocation(item) { - if (Zotero.ItemFields.isValidForType(this.fieldID.archiveLocation, item.itemTypeID)) return item.getField('archiveLocation') || '' + private getAlsoKnownAs(item) { + if (!Zotero.ItemFields.isValidForType(this.fieldID.extra, item.itemTypeID)) return '' let m for (const line of (item.getField('extra') || '').split('\n')) { - if (m = line.match(/^archiveLocation:(.*)/i)) { + if (m = line.match(/^EdTechHub.ItemAlsoKnownAs:(.*)/i)) { return m[1].trim() } } @@ -224,15 +223,9 @@ const EdTechHub = Zotero.EdTechHub || new class { // tslint:disable-line:variabl return '' } - private setArchiveLocation(item, archiveLocation) { - const extra = (item.getField('extra') || '').split('\n').filter(line => ! line.match(/^archiveLocation:/i)).join('\n').trim() - - if (Zotero.ItemFields.isValidForType(this.fieldID.archiveLocation, item.itemTypeID)) { - item.setField('archiveLocation', archiveLocation) - item.setField('extra', extra) - } else { - item.setField('extra', (extra + `\narchiveLocation: ${archiveLocation}`).trim()) - } + private setAlsoKnownAs(item, alsoKnownAs) { + const extra = (item.getField('extra') || '').split('\n').filter(line => ! line.match(/^EdTechHub.ItemAlsoKnownAs:/i)).join('\n').trim() + item.setField('extra', (extra + `\nEdTechHub.ItemAlsoKnownAs: ${alsoKnownAs}`).trim()) } public run(method, ...args) { @@ -266,17 +259,17 @@ const EdTechHub = Zotero.EdTechHub || new class { // tslint:disable-line:variabl doi.assign = doi.short || doi.long - const archiveLocation = this.getArchiveLocation(item).split(';') + const alsoKnownAs = this.getAlsoKnownAs(item).split(';') let save = false - if (doi.assign && !archiveLocation.includes(doi.assign)) { - archiveLocation.push(doi.assign) + if (doi.assign && !alsoKnownAs.includes(doi.assign)) { + alsoKnownAs.push(doi.assign) save = true } const key = `${libraryKey(item)}:${item.key}` - if (!archiveLocation.includes(key)) { - archiveLocation.push(key) + if (!alsoKnownAs.includes(key)) { + alsoKnownAs.push(key) save = true } @@ -287,7 +280,7 @@ const EdTechHub = Zotero.EdTechHub || new class { // tslint:disable-line:variabl } */ if (save) { - this.setArchiveLocation(item, archiveLocation.filter(al => al).join(';')) + this.setAlsoKnownAs(item, alsoKnownAs.filter(aka => aka).join(';')) await item.saveTx() } } @@ -326,7 +319,7 @@ const EdTechHub = Zotero.EdTechHub || new class { // tslint:disable-line:variabl baseNameWithoutExtension, Zotero.Prefs.get('sync.server.username'), (new Date).toISOString().replace(/T.*/, '').replace(/-/g, ''), - parent ? this.getArchiveLocation(parent).split(';')[0].replace(/[^a-z0-9]+/gi, '_') : null, + parent ? this.getAlsoKnownAs(parent).split(';')[0].replace(/[^a-z0-9]+/gi, '_') : null, ].filter(bn => bn).join('-')) switch (attachment.attachmentLinkMode) { @@ -380,8 +373,8 @@ const EdTechHub = Zotero.EdTechHub || new class { // tslint:disable-line:variabl progressWin.startCloseTimer(500) // tslint:disable-line:no-magic-numbers this.fieldID = { - archiveLocation: Zotero.ItemFields.getID('archiveLocation'), DOI: Zotero.ItemFields.getID('DOI'), + extra: Zotero.ItemFields.getID('extra'), } ready.resolve(true) diff --git a/eth-migrate-archiveLocation.js b/eth-migrate-archiveLocation.js new file mode 100644 index 0000000..da59d65 --- /dev/null +++ b/eth-migrate-archiveLocation.js @@ -0,0 +1,55 @@ +/* + * Open Tools-Developer-Run Javascript + * Check "Run as async function" + * Past this file into "Code" + * Click "run" + + The results screen should now show the number of items modified +*/ + +let items = [] +for (const lib of libraries = Zotero.Libraries.getAll()) { + items = items.concat(await Zotero.Items.getAll(lib.libraryID)) +} + +const fieldID = { + archiveLocation: Zotero.ItemFields.getID('archiveLocation'), + extra: Zotero.ItemFields.getID('extra'), +} + +async function moveArchiveLocation(item) { + if (! Zotero.ItemFields.isValidForType(fieldID.extra, item.itemTypeID)) return + + let save = false + let extra = (item.getField('extra') || '').split('\n') + if (extra.length == 1 && !extra[0]) extra = [] + + if (Zotero.ItemFields.isValidForType(fieldID.archiveLocation, item.itemTypeID)) { + const archiveLocation = item.getField('archiveLocation') || '' + if (archiveLocation) { + item.setField('archiveLocation', '') + extra.push(`EdTechHub.ItemAlsoKnownAs: ${archiveLocation}`) + save = true + } + } + + extra = extra.map(line => { + const renamed = line.replace(/^archiveLocation:/, 'EdTechHub.ItemAlsoKnownAs:') + save = save || renamed !== line + return renamed + }) + + if (save) { + item.setField('extra', extra.join('\n')) + await item.save() + } + return save +} + +let saved = 0 +await Zotero.DB.executeTransaction(async () => { + for (const item of items) { + if (await moveArchiveLocation(item)) saved += 1 + } +}) +return saved