Mvolz has uploaded a new change for review.
https://gerrit.wikimedia.org/r/174744
Change subject: Replaced sync method modifyBody with async
......................................................................
Replaced sync method modifyBody with async
Replaced syncronous method modifyBody()
and syncronous methods convertToMediawiki(),
convertToMWDeprecated() and convertToZotero()
with asyncronous methods selectFormatFcn(),
convertToMediawikiAsync(), convertToMWDeprecatedAsync(),
and convertToZoteroAsync.
Converted associated citation property methods
to async pattern as well, such as fixAccessDate(),
fixURL, addPMID(), etc.
Fixes race condition bug apparent in the /api endpoint.
Change-Id: Icaef5a5f74fb240e9c45892b0c9d2f1c779dff38
---
M lib/zotero.js
M server.js
2 files changed, 101 insertions(+), 88 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/citoid
refs/changes/44/174744/1
diff --git a/lib/zotero.js b/lib/zotero.js
index 17db3d2..d2bba9e 100644
--- a/lib/zotero.js
+++ b/lib/zotero.js
@@ -6,6 +6,7 @@
*/
var request = require('request');
+var async = require('async');
/**
* Requests to Zotero server
@@ -25,7 +26,11 @@
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
- callback(error, response, modifyBody(requestedURL,
opts.format, body));
+ selectFormatFcn(opts.format, function(convert){
+ convert(requestedURL, body,
function(modifiedBody){
+ callback(error, response, modifiedBody);
+ });
+ });
}
else {
callback(error, response, body);
@@ -34,102 +39,99 @@
});
};
-/**
- * Modify body of a zotero or other response body
- * @param {String} url original uri requested
- * @param {String} format 'mediawiki', 'mwDeprecated', 'zotero', etc.
- * @param {Object} body JSON request body
- * @return {Object} JSON request body
- */
-var modifyBody = function(url, format, body){
+/*Picks fcn given format*/
+var selectFormatFcn = function (format, callback){
var formatFcns = {
- 'mwDeprecated':convertToMWDeprecated,
- 'mediawiki':convertToMediawiki,
- 'zotero':convertToZotero
- },
- convert = formatFcns[format];
-
- //if format is not available, use zotero as default- may want to switch
to returning error instead
- if (convert){
- return convert(url, body);
- }
- else {
- return convertToZotero(url, body);
- }
+ 'mwDeprecated':convertToMWDeprecatedAsync,
+ 'mediawiki':convertToMediawikiAsync,
+ 'zotero':convertToZoteroAsync
+ };
+ callback(formatFcns[format]);
};
/*Specific conversion methods*/
-var convertToZotero = function(url, body){
- citation = body[0][0];
+var convertToZoteroAsync = function (url, body, callback){
+ var citation = body[0][0];
- fixAccessDate(citation);
- fixURL(url, citation);
+ async.waterfall([
+ function(cb){ //this function exists to pass url to fixURLAsync
+ cb(null, url, citation);
+ },
+ fixURL, //must go directly after unnamed function that hands it
url
+ fixAccessDate
+ ], function (err, citation) {
+ callback([[citation]]);
+ });
- return [[citation]];
};
-var convertToMediawiki = function(url, body){
+var convertToMediawikiAsync = function (url, body, callback){
+ var citation = body[0][0];
- citation = body[0][0];
+ async.waterfall([
+ function(cb){ //this function exists to pass url to fixURLAsync
+ cb(null, url, citation);
+ },
+ fixURL, //must go directly after unnamed function that hands it
url
+ fixAccessDate,
+ replaceCreators,
+ addPMID,
+ fixISBN,
+ fixISSN
+ ], function (err, citation) {
+ callback([[citation]]);
+ });
- replaceCreators(citation);
- addPMID(citation);
- fixURL(url, citation);
- fixAccessDate(citation);
- fixISBN(citation);
- fixISSN(citation);
-
- return [citation];
};
-var convertToMWDeprecated = function(url, body){
- var zotCreators, issn,
+var convertToMWDeprecatedAsync = function (url, body, callback){
+ var zotCreators, creatorFieldName,
creatorTypeCount = {},
citation = body[0][0];
- //flattens creator field
- if (citation.creators) {
- zotCreators = citation.creators;
+ async.waterfall([
+ function(cb){ //this function exists to pass url to fixURLAsync
+ cb(null, url, citation);
+ },
+ fixURL, //must go directly after unnamed function that hands it
url
+ function(citation, cb){ //function to fix creators to
mwdeprecated format
+ if (citation.creators) {
+ zotCreators = citation.creators;
- for (var z in zotCreators){
- creatorFieldName = zotCreators[z].creatorType;
- if (creatorTypeCount[creatorFieldName]){
- creatorTypeCount[creatorFieldName] += 1;
+ for (var z in zotCreators){
+ creatorFieldName =
zotCreators[z].creatorType;
+ if (creatorTypeCount[creatorFieldName]){
+
creatorTypeCount[creatorFieldName] += 1;
+ }
+ else {
+
creatorTypeCount[creatorFieldName] = 1;
+ }
+ //Appends number to name, i.e. author
-> author1
+ creatorFieldName +=
(parseInt(creatorTypeCount[creatorFieldName]));
+
+ citation[creatorFieldName + "-first"] =
zotCreators[z].firstName;
+ citation[creatorFieldName + "-last"] =
zotCreators[z].lastName;
+ }
+ delete citation.creators; //remove creators
field
}
- else {
- creatorTypeCount[creatorFieldName] = 1;
- }
- //Appends number to name, i.e. author -> author1
- creatorFieldName +=
(parseInt(creatorTypeCount[creatorFieldName]));
+ cb(null, citation);
+ },
+ fixAccessDate,
+ replaceCreators,
+ addPMID,
+ fixISBN,
+ fixISSN
+ ], function (err, citation) {
+ callback([[citation]]);
+ });
- citation[creatorFieldName + "-first"] =
zotCreators[z].firstName;
- citation[creatorFieldName + "-last"] =
zotCreators[z].lastName;
- }
- delete citation.creators; //remove creators field
- }
-
- fixURL(url, citation);
- addPMID(citation);
-
- //In some cases where two ISSNs, return first found
- //If no match, leave field as is for user to correct
- if (citation.ISSN){
- issn = citation.ISSN;
- reISSN = new RegExp('\\d{4}\\-\\d{3}[\\dX]');
- match = issn.match(reISSN);
- if (match) {
- citation.ISSN = match[0];
- }
- }
-
- return [citation];
};
/*Methods for particular fields-
-* Mediawiki format if not otherwise specified
+* Targets Mediawiki format if not otherwise specified
*/
-var replaceCreators = function(citation){
+var replaceCreators = function(citation, callback){
if (citation.creators) {
zotCreators = citation.creators;
@@ -149,9 +151,10 @@
delete citation.creators; //remove creators field
}
+ callback(null, citation);
};
-var addPMID = function(citation){
+var addPMID = function(citation, callback){
//get pmid out of extra fields
if (citation.extra){
var extraFields = citation.extra.split('\n');
@@ -164,21 +167,24 @@
}
}
//TODO: if no pmid available from zotero output, get one from doi using
api: http://www.ncbi.nlm.nih.gov/pmc/tools/id-converter-api/
+ callback(null, citation);
};
-var fixURL = function(url, citation){
+var fixURL = function(url, citation, callback){
if (!citation.url){
citation.url = url;
}
+ callback(null, citation);
};
-var fixAccessDate = function(citation){
+var fixAccessDate = function(citation, callback){
if (!citation.accessDate || (citation.accessDate ==
"CURRENT_TIMESTAMP")){
citation.accessDate = (new Date()).toISOString().substring(0,
10);
}
+ callback(null, citation);
};
-var fixISSN = function(citation){
+var fixISSN = function(citation, callback){
var match, i,
issn = citation.ISSN;
@@ -196,9 +202,10 @@
citation.ISSN = [issn]; //wraps issn field in array in
case of false negatives
}
}
+ callback(null, citation);
};
-var fixISBN = function(citation){
+var fixISBN = function(citation, callback){
var match, i,
isbn = citation.ISBN;
@@ -216,15 +223,20 @@
citation.ISBN = [isbn]; //wraps isbn field in array in
case of false negatives
}
}
+ callback(null, citation);
};
/*Test response alterations without having to use server*/
var testJSON = function(){
- var sampleJSON = require("../test_files/3_input.json");
+ var sampleBody = require("../test_files/3_input.json");
console.log("before:");
- console.log(JSON.stringify(sampleJSON));
+ console.log(JSON.stringify(sampleBody));
console.log("after:");
-
console.log(JSON.stringify(modifyBody("http://example.com","mediawiki",sampleJSON)));
+ selectFormatFcn("mwDeprecated", function(convert){
+ convert("http://example.com", sampleBody,
function(modifiedBody){
+ console.log(JSON.stringify(modifiedBody));
+ });
+ });
};
/*Test methods in main */
diff --git a/server.js b/server.js
index 8c5da70..4d48c55 100644
--- a/server.js
+++ b/server.js
@@ -112,6 +112,7 @@
res.send(body);
}
});
+
});
/**Endpoint for retrieving citations based on search term (URL,DOI)*/
@@ -137,13 +138,13 @@
dSearch = decodeURIComponent(search); //decode urlencoded
search string
+ opts = {
+ zoteroURL:zoteroURL,
+ sessionID:"123abc",
+ format:format
+ };
+
distinguish(dSearch, function(extractedID, runnerFunction){
- opts = {
- zoteroURL:zoteroURL,
- sessionID:"123abc",
- format:format,
- identifier:extractedID
- };
runnerFunction(extractedID, opts, function(error,
responseCode, body){
if (!error){
--
To view, visit https://gerrit.wikimedia.org/r/174744
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Icaef5a5f74fb240e9c45892b0c9d2f1c779dff38
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/citoid
Gerrit-Branch: master
Gerrit-Owner: Mvolz <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits