Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gh 27 - change to storage of item identifiers #43

Merged
merged 6 commits into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 23 additions & 30 deletions content/zotero-edtechhub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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}[] = []

Expand All @@ -211,28 +210,22 @@ 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()
}
}

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) {
Expand Down Expand Up @@ -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
}

Expand All @@ -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()
}
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
55 changes: 55 additions & 0 deletions eth-migrate-archiveLocation.js
Original file line number Diff line number Diff line change
@@ -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