summaryrefslogtreecommitdiff
path: root/Library Catalog (Dynix).js
blob: 0d053ef47f8d6a2baa5474d51d3a4486b2f42b39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
{
	"translatorID": "774d7dc2-3474-2684-392c-f787789ec63d",
	"label": "Library Catalog (Dynix)",
	"creator": "Simon Kornblith and Sylvain Machefert",
	"target": "ipac\\.jsp\\?.*(?:uri=(?:link|full)=[0-9]|menu=search|term=)",
	"minVersion": "2.1",
	"maxVersion": "",
	"priority": 260,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcsbv",
	"lastUpdated": "2014-08-26 03:59:09"
}

function detectWeb(doc, url) {
	// make sure there are multiple results
	if (getSearchResults(doc).length) {
	  return "multiple";
	} else if(url.match(/[&?]uri=[^&#]+/)) {	//single item entries have a ui parameter
		return "book";
	}
}

function getSearchResults(doc) {
	//search result
	var res = ZU.xpath(doc, '(//center[1])/table/tbody/tr/td\
					/table//td[1]/a[starts-with(@href,"javascript:buildNewList")]');
	//search results, different format (see porbase.bnportugal.pt test cases)
	if(!res.length) {
		res = ZU.xpath(doc, 'html/body/table/tbody/tr[1]/td/table[5]/tbody/tr/td\
								/table/tbody/tr/td[2]/a[@href]');
	}

	return res;
}

function doWeb(doc, url) {
	var uri = doc.location.href;
	var detailsRe = new RegExp('ipac\.jsp\?.*uri=(?:full|link)=[0-9]');
	
	var uris = new Array();
	if(detectWeb(doc,uri) == "book") {
		if (uri.indexOf("#") !== -1) {
			uris.push(uri.replace(/#/,'&fullmarc=true#'));
		} else {
			uris.push(uri+'&fullmarc=true');
		}
		marcscrape(uris);
	} else {
		var results = getSearchResults(doc);
		var items = {};
		for(var i=0, n=results.length; i<n; i++) {
			items[results[i].href] = results[i].textContent;
		}

		Zotero.selectItems(items, function (items) {
			if (!items) {
				return true;
			}

			var uriRe = new RegExp("^javascript:buildNewList\\('([^']+)");
			var uris = new Array();
			for(var i in items) {
				var m = uriRe.exec(i);
				if(m) {
					uris.push(unescape(m[1]) + '&fullmarc=true');
				} else {
					uris.push(i+'&fullmarc=true');
				}
			}
			marcscrape(uris);
		});
	}
}

function marcscrape(uris){
	var translator = Zotero.loadTranslator("import");
	translator.setTranslator("a6ee60df-1ddc-4aae-bb25-45e0537be973");
	translator.getTranslatorObject(function (marc) {
		Zotero.Utilities.processDocuments(uris, function (newDoc) {
				scrape(newDoc, marc);
			}, function() {}, null);
	});
}   

function scrape(newDoc, marc) {
	var elmts = ZU.xpath(newDoc, '//form/table[@class="tableBackground"]/tbody/tr/td/table[@class="tableBackground"]/tbody/tr[td[1]/a[@class="normalBlackFont1"]]');
	if(!elmts.length) elmts = ZU.xpath(newDoc, '//form/table[@class="tableBackground"]/tbody/tr/td/table[@class="tableBackground"]/tbody/tr[td[1]/a[@class="boldBlackFont1"]]');

	var record = new marc.record();		
	for(var i=0, n=elmts.length; i<n; i++) {
		var elmt = elmts[i];
		var field = ZU.superCleanString(ZU.xpathText(elmt, './TD[1]/A[1]/text()[1]') || '');
		var value = (ZU.xpathText(elmt, './TD[2]/TABLE[1]/TBODY[1]/TR[1]/TD/A[1]', null, '') || '').trim();

		// Sometimes, the field contains "LDR: ", "001: ". We can delete these extra characters
		field = field.replace(/[\s:]/g, "");
		if (field == "LDR"){
  			record.leader = value;
		} else if(field != "FMT") {
			// In french catalogs (in unimarc), the delimiter isn't the $ but \xA4 is used. Added there
			// Also added the fact that subfield codes can be numerics
			value = value.replace(/[\xA4\$]([a-z0-9]) ?/g, marc.subfieldDelimiter+"$1");
			var code = field.substring(0, 3);
			var ind = "";
			if(field.length > 3) {
				ind = field[3];
				if(field.length > 4) {
					ind += field[4];
				}
			}
			record.addField(code, ind, value);
		}
	}

	
	var newItem = new Zotero.Item();
	record.translate(newItem);

	var uri = newDoc.location.href;
	var domain = uri.match(/https?:\/\/([^/]+)/);
	newItem.libraryCatalog = domain[1]+" Library Catalog";

	// 20091210 : We try to get a permalink on the record
	var perma = uri.match(/(https?:\/\/[^/]+.*ipac\.jsp\?).*(uri\=[^&]*)/);
	var profile = uri.match(/(profile\=[^&]*)/);
	if (perma && perma[1] && perma[2])
	{
		var permalink = perma[1] + perma[2];
		// Sometimes, for libraries with multiple profiles, it can be useful
		// to store the permalink with the profile used
		if (profile)
		{
			permalink = permalink + "&" + profile[1];
		}
		newItem.attachments = [{url:permalink, title:"Original record", mimeType:"text/html", snapshot:false}];
	}
	else
	{
		Zotero.debug("Unable to create permalink on " + uri);
	}

	newItem.complete();
}

/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "http://siris-libraries.si.edu/ipac20/ipac.jsp?&profile=all&source=~!silibraries&uri=full=3100001~!820431~!0#focus",
		"items": [
			{
				"itemType": "book",
				"creators": [
					{
						"lastName": "Pennsylvania Academy of the Fine Arts",
						"fieldMode": true
					},
					{
						"firstName": "Thomas",
						"lastName": "Eakins",
						"creatorType": "editor"
					},
					{
						"firstName": "Susan Macdowell",
						"lastName": "Eakins",
						"creatorType": "editor"
					},
					{
						"firstName": "Benjamin",
						"lastName": "Eakins",
						"creatorType": "editor"
					},
					{
						"firstName": "Charles",
						"lastName": "Bregler",
						"creatorType": "editor"
					},
					{
						"firstName": "Kathleen A.",
						"lastName": "Foster",
						"creatorType": "editor"
					}
				],
				"notes": [
					{
						"note": "Reproduces the full texts of the manuscripts in Bregler's collection Fully co-ordinated with and cross-referenced to a companion volume, Writing about Eakins, which contains biographical and critical essays, chronologies, lists of all the manuscripts and an index to microfiche locations At head of title: Archives, Pennsylvania Academy of the Fine Arts Title on information label issued with set: Microfiche edition of the manuscripts in Charles Bregler's Thomas Eakins Collection"
					},
					{
						"note": "Series 1. The papers of Thomas Eakins -- (12 microfiches) Series 2. The papers of Susan Macdowell Eakins (8 microfiches) -- Series 3. Benjamin Eakins and Macdowell family papers (1 microfiche) -- Series 4. The papers of Charles Bregler (16 microfiches)"
					}
				],
				"tags": [
					"Eakins, Thomas",
					"Eakins, Susan Macdowell",
					"Eakins, Benjamin",
					"Bregler, Charles",
					"Bregler, Charles",
					"Library",
					"McDowell family",
					"Manuscripts",
					"Private collections",
					"Pennsylvania Philadelphia"
				],
				"seeAlso": [],
				"attachments": [
					{
						"title": "Original record",
						"mimeType": "text/html",
						"snapshot": false
					}
				],
				"ISBN": "0812282248",
				"title": "Charles Bregler's Thomas Eakins collection",
				"place": "Philadelphia, PA",
				"publisher": "University of Pennsylvania Press",
				"date": "1989",
				"numPages": "37",
				"callNumber": "mfc 000652",
				"medium": "microform",
				"libraryCatalog": "siris-libraries.si.edu Library Catalog"
			}
		]
	},
	{
		"type": "web",
		"url": "http://siris-libraries.si.edu/ipac20/ipac.jsp?session=Y3X077P415286.52120&menu=search&aspect=Keyword&npp=20&ipp=20&spp=20&profile=liball&ri=&term=&index=GW&x=0&y=0&aspect=Keyword&term=smith&index=AW&term=&index=TW&term=&index=SW&term=&index=.JW",
		"items": "multiple"
	},
	{
		"type": "web",
		"url": "http://porbase.bnportugal.pt/ipac20/ipac.jsp?session=13544929K8D9M.93320&profile=porbase&uindex=TL&term=La%20exportaci%C3%B3n%20del%20jam%C3%B3n%20y%20otros%20derivados%20c%C3%A1rnicos%20:%20requisitos%20t%C3%A9cnicos&aspect=subtab11&menu=search&source=~!bnp",
		"items": "multiple"
	},
	{
		"type": "web",
		"url": "http://porbase.bnportugal.pt/ipac20/ipac.jsp?session=13544Q6S70C55.90629&menu=search&aspect=subtab11&npp=20&ipp=20&spp=20&profile=porbase&ri=&term=jamon&index=.GW&x=0&y=0&aspect=subtab11",
		"items": "multiple"
	},
	{
		"type": "web",
		"url": "http://catalogolx.cm-lisboa.pt/ipac20/ipac.jsp?uri=full=3100024~!135303~!415&ri=6",
		"items": [
			{
				"itemType": "book",
				"creators": [
					{
						"firstName": "Jorge",
						"lastName": "Calado",
						"creatorType": "author"
					},
					{
						"lastName": "Fundação Eugénio de Almeida",
						"creatorType": "contributor",
						"fieldMode": true
					}
				],
				"notes": [],
				"tags": [],
				"seeAlso": [],
				"attachments": [
					{
						"title": "Original record",
						"mimeType": "text/html",
						"snapshot": false
					}
				],
				"ISBN": "972-8517-13-0",
				"language": "por eng",
				"title": "Terra bendita: fotografias da FSA e afins",
				"place": "Évora",
				"publisher": "Fundação Eugénio de Almeida",
				"date": "2000",
				"callNumber": "908(73)(084.121)",
				"libraryCatalog": "catalogolx.cm-lisboa.pt Library Catalog",
				"shortTitle": "Terra bendita"
			}
		]
	},
	{
		"type": "web",
		"url": "http://catalogolx.cm-lisboa.pt/ipac20/ipac.jsp?uri=full=3100024~!73588~!2",
		"items": [
			{
				"itemType": "book",
				"creators": [
					{
						"firstName": "Luther S.",
						"lastName": "Luedtke",
						"creatorType": "author"
					}
				],
				"notes": [],
				"tags": [],
				"seeAlso": [],
				"attachments": [
					{
						"title": "Original record",
						"mimeType": "text/html",
						"snapshot": false
					}
				],
				"language": "eng",
				"title": "Making America: the society and culture of the United States",
				"place": "Washington",
				"publisher": "United States Information Agency",
				"date": "1987",
				"callNumber": "316.7(73)",
				"libraryCatalog": "catalogolx.cm-lisboa.pt Library Catalog",
				"shortTitle": "Making America"
			}
		]
	}
]
/** END TEST CASES **/