Get translation at least partly working

I'm pretty sure I've only scratched the surface here, but at least
basic things work.
This commit is contained in:
Simon Kornblith 2015-05-22 22:24:46 -04:00
parent 61cb01b7c2
commit 6e2d1f683a
2 changed files with 73 additions and 125 deletions

View file

@ -115,6 +115,17 @@ Zotero.Translate.Sandbox = {
}
}
// Clean empty creators
if (item.creators) {
for (var i=0; i<item.creators.length; i++) {
var creator = item.creators[i];
if (!creator.firstName && !creator.lastName) {
item.creators.splice(i, 1);
i--;
}
}
}
// if we're not supposed to save the item or we're in a child translator,
// just return the item array
if(translate._libraryID === false || translate._parentTranslator) {

View file

@ -39,7 +39,7 @@ Zotero.Translate.ItemSaver = function(libraryID, attachmentMode, forceTagType, d
this._libraryID = libraryID;
}
this._saveFiles = !(attachmentMode === 0);
this.attachmentMode = attachmentMode;
// If group filesEditable==false, don't save attachments
if (typeof this._libraryID == 'number') {
@ -49,7 +49,7 @@ Zotero.Translate.ItemSaver = function(libraryID, attachmentMode, forceTagType, d
var groupID = Zotero.Groups.getGroupIDFromLibraryID(this._libraryID);
var group = Zotero.Groups.get(groupID);
if (!group.filesEditable) {
this._saveFiles = false;
this.attachmentMode = Zotero.Translate.ItemSaver.ATTACHMENT_MODE_IGNORE;
}
break;
}
@ -90,13 +90,7 @@ Zotero.Translate.ItemSaver.prototype = {
* on failure or attachmentCallback(attachment, progressPercent) periodically during saving.
*/
"saveItems":function(items, callback, attachmentCallback) {
// if no open transaction, open a transaction and add a timer call to close it
var openedTransaction = false;
if(!Zotero.DB.transactionInProgress()) {
Zotero.DB.beginTransaction();
openedTransaction = true;
}
Zotero.DB.executeTransaction(function* () {
try {
var newItems = [];
for each(var item in items) {
@ -124,12 +118,12 @@ Zotero.Translate.ItemSaver.prototype = {
// handle creators
if(item.creators) {
this._saveCreators(item, newItem);
newItem.setCreators(item.creators);
}
// save item
var myID = newItem.save();
newItem = Zotero.Items.get(myID);
var myID = yield newItem.save();
newItem = yield Zotero.Items.getAsync(myID);
// handle notes
if(item.notes) {
@ -158,12 +152,11 @@ Zotero.Translate.ItemSaver.prototype = {
newItems.push(newItem);
}
if(openedTransaction) Zotero.DB.commitTransaction();
callback(true, newItems);
} catch(e) {
if(openedTransaction) Zotero.DB.rollbackTransaction();
callback(false, e);
}
}.bind(this));
},
"saveCollection":function(collection) {
@ -222,9 +215,9 @@ Zotero.Translate.ItemSaver.prototype = {
"_saveAttachment": function(attachment, parentID, attachmentCallback) {
// determine whether to save files and attachments
let attachmentPromise;
if (attachmentMode == Zotero.Translate.ItemSaver.ATTACHMENT_MODE_DOWNLOAD) {
if (this.attachmentMode == Zotero.Translate.ItemSaver.ATTACHMENT_MODE_DOWNLOAD) {
attachmentPromise = this._saveAttachmentDownload.apply(this, arguments);
} else if (attachmentMode == Zotero.Translate.ItemSaver.ATTACHMENT_MODE_FILE) {
} else if (this.attachmentMode == Zotero.Translate.ItemSaver.ATTACHMENT_MODE_FILE) {
attachmentPromise = this._saveAttachmentFile.apply(this, arguments);
} else {
Zotero.debug('Translate: Ignoring attachment due to ATTACHMENT_MODE_IGNORE');
@ -509,7 +502,7 @@ Zotero.Translate.ItemSaver.prototype = {
// Commit to saving
attachmentCallback(attachment, 0);
if(attachment.snapshot === false || !this._saveFiles) {
if(attachment.snapshot === false || this.attachmentMode === Zotero.Translate.ItemSaver.ATTACHMENT_MODE_IGNORE) {
// if snapshot is explicitly set to false, attach as link
attachment.linkMode = "linked_url";
let url, mimeType;
@ -617,62 +610,6 @@ Zotero.Translate.ItemSaver.prototype = {
}
},
"_saveCreators":function(item, newItem) {
var creatorIndex = 0;
for(var i=0; i<item.creators.length; i++) {
var creator = item.creators[i];
if(!creator.firstName && !creator.lastName) {
Zotero.debug("Translate: Silently dropping empty creator");
continue;
}
// try to assign correct creator type
var creatorTypeID = 1;
if(creator.creatorType) {
try {
var creatorTypeID = Zotero.CreatorTypes.getID(creator.creatorType);
} catch(e) {
Zotero.debug("Translate: Invalid creator type "+creator.creatorType+" for creator index "+j, 2);
}
}
// Single-field mode
if (creator.fieldMode && creator.fieldMode == 1) {
var fields = {
lastName: creator.lastName,
fieldMode: 1
};
}
// Two-field mode
else {
var fields = {
firstName: creator.firstName,
lastName: creator.lastName
};
}
var creator = null;
var creatorDataID = Zotero.Creators.getDataID(fields);
if(creatorDataID) {
var linkedCreators = Zotero.Creators.getCreatorsWithData(creatorDataID, this._libraryID);
if (linkedCreators) {
// TODO: support identical creators via popup? ugh...
var creatorID = linkedCreators[0];
creator = Zotero.Creators.get(creatorID);
}
}
if(!creator) {
creator = new Zotero.Creator;
creator.libraryID = this._libraryID;
creator.setFields(fields);
var creatorID = creator.save();
}
newItem.setCreator(creatorIndex++, creator, creatorTypeID);
}
},
"_saveNotes":function(item, parentID) {
for(var i=0; i<item.notes.length; i++) {
var note = item.notes[i];