Skip to content

Commit

Permalink
Update Gale Databases (#2096)
Browse files Browse the repository at this point in the history
Jim/Eric : Gale Databases.js - New logic to access citation metadata
  • Loading branch information
zuphilip authored and adam3smith committed Jan 18, 2020
1 parent 20bf8fd commit d77ae04
Showing 1 changed file with 79 additions and 96 deletions.
175 changes: 79 additions & 96 deletions Gale Databases.js
Original file line number Diff line number Diff line change
@@ -1,137 +1,120 @@
{
"translatorID": "e3748cf3-36dc-4816-bf86-95a0b63feb03",
"label": "Gale Databases",
"creator": "Sebastian Karcher",
"target": "^https?://go\\.galegroup\\.com/",
"creator": "Jim Miazek",
"target": "^https?://[^?&]*(?:gale|galegroup|galetesting|ggtest)\\.com(?:\\:\\d+)?/ps/",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2018-10-07 15:50:14"
"lastUpdated": "2019-12-25 09:35:52"
}

/*
***** BEGIN LICENSE BLOCK *****
Galegroup Translator - Copyright © 2018 Sebastian Karcher
Gale Databases Translator - Copyright © 2019
This file is part of Zotero.
Zotero is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zotero is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
function attr(docOrElem,selector,attr,index){var elem=index?docOrElem.querySelectorAll(selector).item(index):docOrElem.querySelector(selector);return elem?elem.getAttribute(attr):null}function text(docOrElem,selector,index){var elem=index?docOrElem.querySelectorAll(selector).item(index):docOrElem.querySelector(selector);return elem?elem.textContent:null}


function getSearchResults(doc, checkOnly) {
var items = {};
var found = false;

var rows = doc.querySelectorAll('ul.SearchResultsList span.title a.documentLink');
if (!rows.length) {
rows = doc.querySelectorAll('ul.SearchResultsList p.subTitle a.title');
}
for (var i=0; i<rows.length; i++) {
var href = rows[i].href;
var title = ZU.trimInternal(rows[i].textContent);
if (!href || !title) continue;
if (checkOnly) return true;
found = true;
items[href] = title;
function processMultipleEntries(entries) {
var keyValuePairs = {};
for (var entry of entries) {
keyValuePairs[entry.href] = entry.textContent;
}
return found ? items : false;
}
function detectWeb(doc, url) {
if ((url.includes('/retrieve.do') || url.includes('/i.do?')) && text(doc, 'li#docTools-citation, li.docTools-citation')) {
return "journalArticle";
}

else if (getSearchResults(doc, true)) return "multiple";
Zotero.selectItems(keyValuePairs, function (selectedItems) {
if (selectedItems) {
Zotero.Utilities.processDocuments(Object.keys(selectedItems), processSingleEntry);
}
});
}

function processSingleEntry(doc) {
var entry = doc.querySelector('.zotero');
var docId = entry.getAttribute('data-documentnumber');
var documentUrl = entry.getAttribute('href');
var productName = entry.getAttribute('data-productname');
var documentData = '{"docId":"' + docId + '","documentUrl":"' + documentUrl + '","productName":"' + productName + '"}';
var urlParams = "citationFormat=RIS&documentData=" + encodeURIComponent(documentData).replace(/%20/g, "+");
Zotero.Utilities.doPost("/ps/citationtools/rest/cite/download", urlParams, translate);
}

function scrape(doc, url) {
var postURL = "/ps/citationtools/rest/cite/download";

var docId = attr(doc, 'input.citationToolsData', 'data-docid');
var documentUrl = attr(doc, 'input.citationToolsData', 'data-url');
var productName = attr(doc, 'input.citationToolsData', 'data-productname');

var documentData = '{"docId":"' + docId +'","documentUrl":"' + documentUrl + '","productName":"' + productName + '"}';
var post = "citationFormat=RIS&documentData=" + encodeURIComponent(documentData).replace(/%20/g, "+");
var pdfurl = attr(doc, '#docTools-pdf a', 'href');

// Z.debug(post)
ZU.doPost(postURL, post, function(text){

text = text.trim();
// gale puts issue numbers in M1
text = text.replace(/M1\s*\-/g, "IS -");
// L2 is probably meant to be UR, but we can ignore it altogether
text = text.replace(/^L2\s+-.+\n/gm, '');
// we can map copyright notes via CR
text = text.replace(/^N1(?=\s+-\s+copyright)/igm, 'CR');
// Z.debug(text);

var translator = Zotero.loadTranslator("import");
translator.setTranslator("32d59d2d-b65a-4da4-b0a3-bdd3cfb979e7");
translator.setString(text);
translator.setHandler("itemDone", function (obj, item) {
if (item.ISSN) {
item.ISSN = ZU.cleanISSN(item.ISSN);
}
if (item.pages && item.pages.endsWith("+")) {
item.pages = item.pages.replace(/\+/, "-");
}
if (pdfurl) {
item.attachments.push({
url: pdfurl,
title: "Full Text PDF",
mimeType:'application/pdf'
});
} else {
item.attachments.push({document: doc, title: "Snapshot"});
}
item.complete();
});
translator.translate();
function translate(data) {
var translator = Zotero.loadTranslator("import");
// use RIS translator
translator.setTranslator("32d59d2d-b65a-4da4-b0a3-bdd3cfb979e7");
translator.setString(transform(data));
translator.setHandler("itemDone", function (obj, item) {
if (item.ISSN) {
item.ISSN = Zotero.Utilities.cleanISSN(item.ISSN);
}
if (item.pages && item.pages.endsWith("+")) {
item.pages = item.pages.replace(/\+/, "-");
}
item.attachments.push({ document: data, title: "Snapshot" });
item.complete();
});
translator.translate();
}

function doWeb(doc, url) {
if (detectWeb(doc, url) == "multiple") {
Zotero.selectItems(getSearchResults(doc, false), function (items) {
if (!items) {
return true;
}
var articles = [];
for (var i in items) {
articles.push(i);
}
ZU.processDocuments(articles, scrape);
});
} else {
scrape(doc, url);
function transform(ris) {
return ris.trim()
.replace(/M1\s*-/g, "IS -") // gale puts issue numbers in M1
.replace(/^(?:L2|M2)\s+-.+\n/gm, '') // Ignore
.replace(/^SP\s+-\s+NA\n/gm, '') // Remove missing page numbers
.replace(/^N1(?=\s+-\s+copyright)/igm, 'CR');
}

function getCitableDocuments(doc) {
return doc.getElementsByClassName('zotero');
}


function detectWeb(doc, _url) {
if (doc.getElementById('searchResults')) {
Zotero.monitorDOMChanges(doc.querySelector('#searchResults'));
}
var entries = getCitableDocuments(doc);
switch (entries.length) {
case 0: return false;
case 1: return entries[0].getAttribute('data-zoterolabel');
default: return 'multiple';
}
}

function doWeb(doc, _url) {
var entries = getCitableDocuments(doc);
switch (entries.length) {
case 0: break;
case 1: processSingleEntry(doc); break;
default: processMultipleEntries(entries);
}
}


/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "http://go.galegroup.com/ps/i.do?p=PROF&u=nysl_ce_syr&id=GALE|A213083272&v=2.1&it=r&sid=PROF&asid=a8973dd8",
"url": "https://go.gale.com/ps/i.do?p=PROF&u=nysl_ce_syr&id=GALE|A213083272&v=2.1&it=r&sid=PROF&asid=a8973dd8",
"items": [
{
"itemType": "magazineArticle",
Expand All @@ -148,16 +131,16 @@ var testCases = [
"creatorType": "author"
}
],
"date": "December 2009",
"date": "Dezember 2009",
"ISSN": "0011-0035",
"archive": "Educators Reference Complete",
"archive": "Gale OneFile: Educator's Reference Complete",
"issue": "2",
"language": "English",
"libraryCatalog": "Gale",
"pages": "122-",
"publicationTitle": "Counselor Education and Supervision",
"shortTitle": "Improving a counselor education Web site through usability testing",
"url": "http://link.galegroup.com/apps/doc/A213083272/PROF?u=nysl_ce_syr&sid=PROF&xid=a8973dd8",
"url": "https://link.gale.com/apps/doc/A213083272/PROF?u=nysl_ce_syr&sid=zotero&xid=a8973dd8",
"volume": "49",
"attachments": [
{
Expand Down

0 comments on commit d77ae04

Please sign in to comment.