Mvolz has submitted this change and it was merged.

Change subject: ZoteroRequest now flattens creator fields
......................................................................


ZoteroRequest now flattens creator fields

Added .json file with sample Zotero response for faster testing.

Added modifyBody method to zotero.js to alter the response.

Currently removes creator field and replaces it with flattened
fields that citation templates could take, such as editor1-first for
the first name of an editor. Note that there is currently no
citation template that takes author1-last directly; equivalent fields
on en.Wikipedia, for example, might be last1, author1, last, etc.
Because each wiki's citation templates are different,
fields would have to be assigned to their equivalent field in
TemplateData to be used.

Change-Id: I17b161dad696a9f6e4346b7fc89736c9d6aeae84
---
A sampleZoteroResponseBody.json
M zotero.js
2 files changed, 43 insertions(+), 5 deletions(-)

Approvals:
  Mvolz: Verified; Looks good to me, approved



diff --git a/sampleZoteroResponseBody.json b/sampleZoteroResponseBody.json
new file mode 100644
index 0000000..fd6f9e3
--- /dev/null
+++ b/sampleZoteroResponseBody.json
@@ -0,0 +1 @@
+[{"itemType":"journalArticle","creators":[{"firstName":"Jessica","lastName":"Trinoskey","creatorType":"editor"},{"firstName":"Frances
 
A.","lastName":"Brahmi","creatorType":"author"},{"firstName":"Carole","lastName":"Gall","creatorType":"author"}],"notes":[],"tags":[],"title":"Zotero:
 A Product Review","publicationTitle":"Journal of Electronic Resources in 
Medical 
Libraries","volume":"6","issue":"3","pages":"224-229","date":"2009","DOI":"10.1080/15424060903167229","url":"http://dx.doi.org/10.1080/15424060903167229","abstractNote":"Zotero
 is a free and open source Firefox extension that exists within the Web browser 
and allows one to collect, manage, store, and cite resources in a single 
location. 1 Zotero automatically imports citation information from a number of 
sources, including nonsubscription, newspaper, and commercial Web sites, and 
Web-based databases such as PubMed and MedlinePlus. Zotero offers more options 
for note taking than the better-known citation management system EndNote. 
Sixteen citation styles are available when Zotero is initially downloaded, with 
many more freely available. Users can install a plug-in that allows Zotero to 
integrate with Microsoft Word. Even though Zotero does not have an on-demand 
customer support service, its Web site offers a wealth of information for 
users. The authors highly recommend 
Zotero.","ISSN":"1542-4065","libraryCatalog":"Taylor and 
Francis+NEJM","accessDate":"CURRENT_TIMESTAMP","shortTitle":"Zotero"}]
diff --git a/zotero.js b/zotero.js
index 058945f..e971f9a 100644
--- a/zotero.js
+++ b/zotero.js
@@ -21,23 +21,60 @@
 
        request(options, function (error, response, body) {
                if (!error && response.statusCode == 200) {
-                       callback(body);
+                       callback(modifyBody(body));
                }
        });
 };
 
-/*Test URL in main function*/
-var main = function(){
+/*Currently replaces creators obj list with flat set of fields*/
+var modifyBody = function(body){
+       var creatorTypeCount = {};
+       var zotCreators = body[0].creators;
+
+       for (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])); 
+
+               body[0][creatorFieldName + "-first"] = zotCreators[z].firstName;
+               body[0][creatorFieldName + "-last"] = zotCreators[z].lastName;
+       }
+
+       delete body[0].creators; //remove creators field
+       //body[0] += newCreators; //append new creator fields to end of body
+       //console.log(body);
+       return body;
+}
+
+/*Test server fcns*/
+var testServer = function(){
     testURL = "http://www.tandfonline.com/doi/abs/10.1080/15424060903167229";
        testSessionID = "abc123"
 
        zoteroRequest(testURL, testSessionID, function(body){
-               console.log(body);
+               console.log(modifyBody(body));
        });
 }
 
+/*Test response alterations without having to use server*/
+var testJSON = function(){
+       var sampleJSON = require("./sampleZoteroResponseBody.json");
+       console.log("before:");
+       console.log(JSON.stringify(sampleJSON));
+       console.log("after:");
+       console.log(JSON.stringify(modifyBody(sampleJSON)));
+}
+
+/*Test methods in main */
 if (require.main === module) {
-    main();
+       //testJSON();
+       testServer();
 }
 
 /*Exports*/

-- 
To view, visit https://gerrit.wikimedia.org/r/139818
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I17b161dad696a9f6e4346b7fc89736c9d6aeae84
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/cite-from-id
Gerrit-Branch: master
Gerrit-Owner: Mvolz <[email protected]>
Gerrit-Reviewer: Mvolz <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to