Mvolz has uploaded a new change for review. https://gerrit.wikimedia.org/r/154958
Change subject: New export format added to zoteroRequest ...................................................................... New export format added to zoteroRequest *Still must be JSON *Old format still available as 'citoidDeprecated' *Added new format for export 'application/x-citoid+json' *zoteroRequest takes format as request params (NOT in header). *'citoidDeprecated' will be removed once other dependencies are resolved Change-Id: If309d9edbe26c97ab4ebe12a0c669e360a8d9984 --- M server.js M zotero.js 2 files changed, 90 insertions(+), 10 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/citoid refs/changes/58/154958/1 diff --git a/server.js b/server.js index 1491d34..f44bfcd 100644 --- a/server.js +++ b/server.js @@ -47,8 +47,14 @@ /*Endpoint for retrieving citations in JSON format from a URL*/ citoid.post('/url', function(req, res){ - var requestedURL = req.body.url, + var contentType = req.body.contentType, + requestedURL = req.body.url, zoteroURLWeb = util.format(zoteroURL, 'web'); + + //temp backwards compatibility + if (!contentType){ + contentType = 'citoidDeprecated'; + } res.type('application/json'); @@ -64,8 +70,11 @@ console.log(e); } + //make things work with new contenttpye for now.. + //contentType = 'application/x-citoid+json'; + //Request from Zotero and set response - zoteroRequest(zoteroURLWeb, requestedURL, testSessionID, function(error, response, body){ + zoteroRequest(zoteroURLWeb, requestedURL, testSessionID, contentType, function(error, response, body){ console.log("Request made for: " + requestedURL); if (response) { if (!error) { @@ -77,7 +86,7 @@ //we don't do this initially because many sites //will redirect this fcn to a log-in screen unshorten(requestedURL, function(expandedURL) { - zoteroRequest(zoteroURLWeb, expandedURL, testSessionID, + zoteroRequest(zoteroURLWeb, expandedURL, testSessionID, contentType, function(error, response, body){ if (response){ //if still no translator, or translation fails, diff --git a/zotero.js b/zotero.js index fe173e2..32296fe 100644 --- a/zotero.js +++ b/zotero.js @@ -7,7 +7,7 @@ var request = require('request'); -var zoteroRequest = function(zoteroURL, requestedURL, sessionID, callback){ +var zoteroRequest = function(zoteroURL, requestedURL, sessionID, contentType, callback){ var options = { url: zoteroURL, method: 'POST', @@ -20,7 +20,7 @@ request(options, function (error, response, body) { if (!error && response.statusCode == 200) { //modify body only if response is okay - callback(error, response, modifyBody(requestedURL, body)); + callback(error, response, modifyBody(requestedURL, contentType, body)); } else { callback(error, response, body); @@ -29,13 +29,82 @@ }); }; -/*Converts Zotero body into Citoid body*/ -var modifyBody = function(url, body){ +/*Converts Zotero body into appropriate contentType*/ +var modifyBody = function(url, contentType, body){ + var contentTypeFcns = { + 'citoidDeprecated':citoidDeprecated, + 'application/x-citoid+json':zoteroToCitoidContentType + }, + convert = contentTypeFcns[contentType]; + + return convert(url, body); +}; + +var zoteroToCitoidContentType = function(url, body){ + var citation, zotCreators, creatorMap, + creatorTypeCount = {}; + + //hack + //in most cases body will be an array, but in some will be an array of arrays + if (!(body[0] instanceof Array)){ + citation = body[0]; + } + else if (!(body[0][0] instanceof Array)){ + citation = body[0][0]; + } + else { + return body; + } + + if (citation.creators) { + zotCreators = citation.creators; + + creatorMap = {}; + + for (var z in zotCreators){ + var creatorArray, + creatorFieldName = zotCreators[z].creatorType; + + if (!citation[creatorFieldName]){ + creatorArray = []; + citation[creatorFieldName]= creatorArray; + } + + citation[creatorFieldName].push([zotCreators[z].firstName, zotCreators[z].lastName]); + } + + //delete citation.creators; //remove creators field + } + + //some zotero requests come back without the url field filled in + if (!citation.url){ + citation.url = url; + } + + //get pmid and pcmid(pmc) out of extra fields + if (citation.extra){ + var extraFields = citation.extra.split('\n'); + for (var f in extraFields){ + //could add them all, but let's not do this in case of conflicting fields + var keyValue = extraFields[f].split(': '); + if (keyValue[0] === 'PMID'){ + citation[keyValue[0]] = keyValue[1].trim(); + } + } + } + + //console.log(citation); + return [citation]; + +}; + +var citoidDeprecated = function(url, body){ + var citation, zotCreators, creatorTypeCount = {}; //hack - //in most cases body will be an arrat, but in some will be an array of arrays + //in most cases body will be an array, but in some will be an array of arrays if (!(body[0] instanceof Array)){ citation = body[0]; } @@ -93,9 +162,11 @@ var zoteroURL = 'http://localhost:1969/web', //edit this to your own endpoint testURL = "http://www.tandfonline.com/doi/abs/10.1080/15424060903167229", //URL that works with Zotero //testURL = "http://books.google.co.uk/books?hl=en&lr=&id=7lueAgAAQBAJ&oi=fnd&pg=PR5&dq=mediawiki&ots=-Z0o2LCgao&sig=IGHnyWEiNiNvPyXeyCuOcdvi15s#v=onepage&q=mediawiki&f=false", //url that doesn't work with zotero - testSessionID = "abc123"; + testSessionID = 'abc123'; + //contentType = 'citoidDeprecated'; + contentType = 'application/x-citoid+json'; - zoteroRequest(zoteroURL, testURL, testSessionID, function(error, response, body){ + zoteroRequest(zoteroURL, testURL, testSessionID, contentType, function(error, response, body){ if (response) { if (!error && response.statusCode == 200) { console.log(body); -- To view, visit https://gerrit.wikimedia.org/r/154958 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: If309d9edbe26c97ab4ebe12a0c669e360a8d9984 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/services/citoid Gerrit-Branch: master Gerrit-Owner: Mvolz <marielle.v...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits