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

@ -114,6 +114,17 @@ Zotero.Translate.Sandbox = {
item[i] = val.trim(); item[i] = val.trim();
} }
} }
// 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, // if we're not supposed to save the item or we're in a child translator,
// just return the item array // just return the item array

View file

@ -39,7 +39,7 @@ Zotero.Translate.ItemSaver = function(libraryID, attachmentMode, forceTagType, d
this._libraryID = libraryID; this._libraryID = libraryID;
} }
this._saveFiles = !(attachmentMode === 0); this.attachmentMode = attachmentMode;
// If group filesEditable==false, don't save attachments // If group filesEditable==false, don't save attachments
if (typeof this._libraryID == 'number') { 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 groupID = Zotero.Groups.getGroupIDFromLibraryID(this._libraryID);
var group = Zotero.Groups.get(groupID); var group = Zotero.Groups.get(groupID);
if (!group.filesEditable) { if (!group.filesEditable) {
this._saveFiles = false; this.attachmentMode = Zotero.Translate.ItemSaver.ATTACHMENT_MODE_IGNORE;
} }
break; break;
} }
@ -90,80 +90,73 @@ Zotero.Translate.ItemSaver.prototype = {
* on failure or attachmentCallback(attachment, progressPercent) periodically during saving. * on failure or attachmentCallback(attachment, progressPercent) periodically during saving.
*/ */
"saveItems":function(items, callback, attachmentCallback) { "saveItems":function(items, callback, attachmentCallback) {
// if no open transaction, open a transaction and add a timer call to close it Zotero.DB.executeTransaction(function* () {
var openedTransaction = false; try {
if(!Zotero.DB.transactionInProgress()) { var newItems = [];
Zotero.DB.beginTransaction(); for each(var item in items) {
openedTransaction = true; // Get typeID, defaulting to "webpage"
} var newItem;
var type = (item.itemType ? item.itemType : "webpage");
try {
var newItems = [];
for each(var item in items) {
// Get typeID, defaulting to "webpage"
var newItem;
var type = (item.itemType ? item.itemType : "webpage");
if(type == "note") { // handle notes differently
newItem = new Zotero.Item('note');
newItem.libraryID = this._libraryID;
if(item.note) newItem.setNote(item.note);
var myID = newItem.save();
newItem = Zotero.Items.get(myID);
} else {
if(type == "attachment") { // handle attachments differently
newItem = this._saveAttachment(item, null, attachmentCallback);
if(!newItem) continue;
var myID = newItem.id;
} else {
var typeID = Zotero.ItemTypes.getID(type);
newItem = new Zotero.Item(typeID);
newItem._libraryID = this._libraryID;
this._saveFields(item, newItem); if(type == "note") { // handle notes differently
newItem = new Zotero.Item('note');
// handle creators newItem.libraryID = this._libraryID;
if(item.creators) { if(item.note) newItem.setNote(item.note);
this._saveCreators(item, newItem);
}
// save item
var myID = newItem.save(); var myID = newItem.save();
newItem = Zotero.Items.get(myID); newItem = Zotero.Items.get(myID);
} else {
// handle notes if(type == "attachment") { // handle attachments differently
if(item.notes) { newItem = this._saveAttachment(item, null, attachmentCallback);
this._saveNotes(item, myID); if(!newItem) continue;
} var myID = newItem.id;
} else {
// handle attachments var typeID = Zotero.ItemTypes.getID(type);
if(item.attachments) { newItem = new Zotero.Item(typeID);
for(var i=0; i<item.attachments.length; i++) { newItem._libraryID = this._libraryID;
var newAttachment = this._saveAttachment(item.attachments[i], myID, attachmentCallback);
if(typeof newAttachment === "object") { this._saveFields(item, newItem);
this._saveTags(item.attachments[i], newAttachment);
// handle creators
if(item.creators) {
newItem.setCreators(item.creators);
}
// save item
var myID = yield newItem.save();
newItem = yield Zotero.Items.getAsync(myID);
// handle notes
if(item.notes) {
this._saveNotes(item, myID);
}
// handle attachments
if(item.attachments) {
for(var i=0; i<item.attachments.length; i++) {
var newAttachment = this._saveAttachment(item.attachments[i], myID, attachmentCallback);
if(typeof newAttachment === "object") {
this._saveTags(item.attachments[i], newAttachment);
}
} }
} }
} }
} }
if(item.itemID) this._IDMap[item.itemID] = myID;
// handle see also
this._saveTags(item, newItem);
// add to new item list
newItem = Zotero.Items.get(myID);
newItems.push(newItem);
} }
if(item.itemID) this._IDMap[item.itemID] = myID; callback(true, newItems);
} catch(e) {
// handle see also callback(false, e);
this._saveTags(item, newItem);
// add to new item list
newItem = Zotero.Items.get(myID);
newItems.push(newItem);
} }
}.bind(this));
if(openedTransaction) Zotero.DB.commitTransaction();
callback(true, newItems);
} catch(e) {
if(openedTransaction) Zotero.DB.rollbackTransaction();
callback(false, e);
}
}, },
"saveCollection":function(collection) { "saveCollection":function(collection) {
@ -222,9 +215,9 @@ Zotero.Translate.ItemSaver.prototype = {
"_saveAttachment": function(attachment, parentID, attachmentCallback) { "_saveAttachment": function(attachment, parentID, attachmentCallback) {
// determine whether to save files and attachments // determine whether to save files and attachments
let attachmentPromise; 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); 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); attachmentPromise = this._saveAttachmentFile.apply(this, arguments);
} else { } else {
Zotero.debug('Translate: Ignoring attachment due to ATTACHMENT_MODE_IGNORE'); Zotero.debug('Translate: Ignoring attachment due to ATTACHMENT_MODE_IGNORE');
@ -509,7 +502,7 @@ Zotero.Translate.ItemSaver.prototype = {
// Commit to saving // Commit to saving
attachmentCallback(attachment, 0); 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 // if snapshot is explicitly set to false, attach as link
attachment.linkMode = "linked_url"; attachment.linkMode = "linked_url";
let url, mimeType; 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) { "_saveNotes":function(item, parentID) {
for(var i=0; i<item.notes.length; i++) { for(var i=0; i<item.notes.length; i++) {
var note = item.notes[i]; var note = item.notes[i];