Revert removal of Utilities.Internal.itemToExportFormat() (since c929055)

This commit is contained in:
Adomas Venčkauskas 2021-07-30 14:28:14 +03:00
parent 4d497afea0
commit 0e2073b251
2 changed files with 136 additions and 4 deletions

@ -1 +1 @@
Subproject commit b89bab4ad7b5e118f323c3fd6bad4258681a4e65
Subproject commit 33feecf24ed5780790f4678de4e5ad738f048f06

View file

@ -986,9 +986,141 @@ Zotero.Utilities.Internal = {
},
itemToExportFormat: function () {
Zotero.debug(`Zotero.Utilities.Internal.itemToExportFormat() is deprecated -- use Zotero.Utilities.Item.itemToExportFormat() instead`);
return Zotero.Utilities.Item.itemToExportFormat.apply(Zotero.Utilities, arguments);
/**
* Converts Zotero.Item to a format expected by translators
* This is mostly the Zotero web API item JSON format, but with an attachments
* and notes arrays and optional compatibility mappings for older translators.
*
* @param {Zotero.Item} zoteroItem
* @param {Boolean} legacy Add mappings for legacy (pre-4.0.27) translators
* @return {Object}
*/
itemToExportFormat: function (zoteroItem, legacy, skipChildItems) {
function addCompatibilityMappings(item, zoteroItem) {
item.uniqueFields = {};
// Meaningless local item ID, but some older export translators depend on it
item.itemID = zoteroItem.id;
item.key = zoteroItem.key; // CSV translator exports this
// "version" is expected to be a field for "computerProgram", which is now
// called "versionNumber"
delete item.version;
if (item.versionNumber) {
item.version = item.uniqueFields.version = item.versionNumber;
delete item.versionNumber;
}
// SQL instead of ISO-8601
item.dateAdded = zoteroItem.dateAdded;
item.dateModified = zoteroItem.dateModified;
if (item.accessDate) {
item.accessDate = zoteroItem.getField('accessDate');
}
// Map base fields
for (let field in item) {
let id = Zotero.ItemFields.getID(field);
if (!id || !Zotero.ItemFields.isValidForType(id, zoteroItem.itemTypeID)) {
continue;
}
let baseField = Zotero.ItemFields.getName(
Zotero.ItemFields.getBaseIDFromTypeAndField(item.itemType, field)
);
if (!baseField || baseField == field) {
item.uniqueFields[field] = item[field];
} else {
item[baseField] = item[field];
item.uniqueFields[baseField] = item[field];
}
}
// Add various fields for compatibility with translators pre-4.0.27
item.itemID = zoteroItem.id;
item.libraryID = zoteroItem.libraryID == 1 ? null : zoteroItem.libraryID;
// Creators
if (item.creators) {
for (let i=0; i<item.creators.length; i++) {
let creator = item.creators[i];
if (creator.name) {
creator.fieldMode = 1;
creator.lastName = creator.name;
delete creator.name;
}
// Old format used to supply creatorID (the database ID), but no
// translator ever used it
}
}
if (!zoteroItem.isRegularItem()) {
item.sourceItemKey = item.parentItem;
}
// Tags
for (let i=0; i<item.tags.length; i++) {
if (!item.tags[i].type) {
item.tags[i].type = 0;
}
// No translator ever used "primary", "fields", or "linkedItems" objects
}
// "related" was never used (array of itemIDs)
// seeAlso was always present, but it was always an empty array.
// Zotero RDF translator pretended to use it
item.seeAlso = [];
if (zoteroItem.isAttachment()) {
item.linkMode = item.uniqueFields.linkMode = zoteroItem.attachmentLinkMode;
item.mimeType = item.uniqueFields.mimeType = item.contentType;
}
if (item.note) {
item.uniqueFields.note = item.note;
}
return item;
}
var item = zoteroItem.toJSON();
item.uri = Zotero.URI.getItemURI(zoteroItem);
delete item.key;
if (!skipChildItems && !zoteroItem.isAttachment() && !zoteroItem.isNote()) {
// Include attachments
item.attachments = [];
let attachments = zoteroItem.getAttachments();
for (let i=0; i<attachments.length; i++) {
let zoteroAttachment = Zotero.Items.get(attachments[i]);
let attachment = zoteroAttachment.toJSON();
attachment.uri = Zotero.URI.getItemURI(zoteroAttachment);
if (legacy) addCompatibilityMappings(attachment, zoteroAttachment);
item.attachments.push(attachment);
}
// Include notes
item.notes = [];
let notes = zoteroItem.getNotes();
for (let i=0; i<notes.length; i++) {
let zoteroNote = Zotero.Items.get(notes[i]);
let note = zoteroNote.toJSON();
note.uri = Zotero.URI.getItemURI(zoteroNote);
if (legacy) addCompatibilityMappings(note, zoteroNote);
item.notes.push(note);
}
}
if (legacy) addCompatibilityMappings(item, zoteroItem);
return item;
},