summaryrefslogtreecommitdiff
path: root/Bibliotheque nationale de France.js
blob: b38ef50ce9055266e382b12654e33456ff38b996 (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
{
	"translatorID": "47533cd7-ccaa-47a7-81bb-71c45e68a74d",
	"label": "Bibliothèque nationale de France",
	"creator": "Florian Ziche",
	"target": "^https?://[^/]*catalogue\\.bnf\\.fr",
	"minVersion": "3.0",
	"maxVersion": "",
	"priority": 100,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "g",
	"lastUpdated": "2013-02-28 14:51:02"
}

/*
 *  Bibliothèque nationale de France Translator
 *  Copyright (C) 2010 Florian Ziche, ziche@noos.fr
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


/* Bnf namespace. */
var BnfClass = function() {
	//Private members
	


	/* Map MARC responsibility roles to Zotero creator types.
		See http://archive.ifla.org/VI/3/p1996-1/appx-c.htm.
	*/
	function getCreatorType(aut) {
		switch(aut['4']) {
		case "005":
		case "250":
		case "275":
		case "590":	//performer
		case "755":	//vocalist
			return "performer";
		case "040":
		case "130":	//book designer
		case "740":	//type designer
		case "750":	//typographer
		case "350":	//engraver
		case "360":	//etcher
		case "430":	//illuminator
		case "440":	//illustrator
		case "510":	//lithographer
		case "530":	//metal engraver
		case "600":	//photographer
		case "705":	//sculptor
		case "760":	//wood engraver
			return "artist";
		case "070":
		case "305":
		case "330":
		case undefined:
			return "author";
		case "020":
		case "210":
		case "212":
			return "commenter";
		case "180":
			return "cartographer";
		case "220":
		case "340":
			return "editor";
		case "230":
			return "composer";
		case "245":
			return "inventor";
		case "255":
		case "695":	//scientific advisor
		case "727":	//thesis advisor
			return "counsel";
		case "300":
			return "director";
		case "400":	//funder
		case "723":	//sponsor
			return "sponsor";
		case "460":
			return "interviewee";
		case "470":
			return "interviewer";
		case "480":	//librettist
		case "520":    //lyricist
			return "wordsBy";
		case "605":
			return "presenter";
		case "630":
			return "producer";
		case "635":
			return "programmer";
		case "660":
			return "recipient";
		case "090":	//author of dialog
		case "690":	//scenarist
			return "scriptwriter";
		case "730":
			return "translator";
		//Ignore (no matching Zotero creatorType):
		case "320":	//donor
		case "610":	//printer
		case "650":	//publisher
			return undefined;
		//Default
		case "205":
		default:
			return "contributor";
		}
	};

	/* Fix creators (MARC translator is not perfect). */
	function getCreators(record, item) {
		//Clear creators
		item.creators = new Array();
			// Extract creators (700, 701 & 702)
		for (var i = 700; i < 703; i++)  {
			var authorTag = record.getFieldSubfields(i);
				for (var j in authorTag) {
				var aut = authorTag[j];
				var authorText = "";
				if (aut.b)  {
					authorText = aut['a'] + ", " + aut['b'];
				}  else  {
					authorText = aut['a'];
				}
				var type = getCreatorType(aut);
				if(type) {
					item.creators.push(Zotero.Utilities.cleanAuthor(authorText, type, true));
				}
			}
		}
			// Extract corporate creators (710, 711 & 712)
		for (var i = 710; i < 713; i++)  {
			var authorTag = record.getFieldSubfields(i);
			for (var j in authorTag)  {
				if (authorTag[j]['a'])  {
					var type = getCreatorType(authorTag[j]);
					if(type) {
						item.creators.push({
							lastName: authorTag[j]['a'], 
							creatorType: type, 
							fieldMode: true});
					}
				}
			}
		}
	};


	//Translate BnF types to Zotero item types.
	function getItemType(type) {
		switch(type) {
		case "Enregistrement sonore":
			return "audioRecording";
		case "Image fixe":
		case "Image fixe numérisée":
			return "artwork";
		case "Images animées":
			return "film";
		case "Ressource électronique":
			return "computerProgram";
		case "Document cartographique":
			return "map";
		case "Document d'archives":
			return "document";
		case "Texte manuscrit":
			return "manuscript";
		case "Multimédia multisupport":
		case "Musique imprimé":
		case "Texte imprimé":
		default:
			return "book";	
		}	
	};

	//Add tag, if not present yet
	function addTag(item, tag) {
		for(var t in item.tags) {
			if(item.tags[t] == tag) {
				return;
			}
		}
		item.tags.push(tag);
	};

	//Tagging
	function getTags(record, item) {
		var pTag = record.getFieldSubfields("600");
		if(pTag) {
			for(var j in pTag) {
				var tagText = false;
				var person = pTag[j];
				tagText = person.a;
				if(person.b) {
					tagText += ", " + person.b;
				}
				if(person.f) {
					tagText += " (" + person.f + ")";
				}
				addTag(item, tagText);
			}
		}
		pTag = record.getFieldSubfields("601");
		if(pTag) {
			for(var j in pTag) {
				var tagText = false;
				var person = pTag[j];
				tagText = person.a;
				addTag(item, tagText);
			}
		}
		pTag = record.getFieldSubfields("605");
		if(pTag) {
			for(var j in pTag) {
				var tagText = false;
				var person = pTag[j];
				tagText = person.a;
				addTag(item, tagText);
			}
		}
		pTag = record.getFieldSubfields("606");
		if(pTag) {
			for(var j in pTag) {
				var tagText = false;
				var person = pTag[j];
				tagText = person.a;
				addTag(item, tagText);
			}
		}
		pTag = record.getFieldSubfields("607");
		if(pTag) {
			for(var j in pTag) {
				var tagText = false;
				var person = pTag[j];
				tagText = person.a;
				addTag(item, tagText);
			}
		}
		pTag = record.getFieldSubfields("602");
		if(pTag) {
			for(var j in pTag) {
				var tagText = false;
				var person = pTag[j];
				tagText = person.a;
				if(person.f) {
					tagText += " (" + person.f + ")";
				}
				addTag(item, tagText);
			}       
		}
		pTag = record.getFieldSubfields("604");
		if(pTag) {
			for(var j in pTag) {
				var tagText = false;
				var person = pTag[j];
				tagText = person.a;
				if(person.b) {
					tagText += ", " + person.b;
				}
				if(person.f) {
					tagText += " (" + person.f + ")";
				}
				if(person.t) {
					tagText += ", " + person.t;
				}
				addTag(item, tagText);
			}
		}
	};

	//Get series (repeatable)
	function getSeries(record, item) {
		var seriesText = false;
		var seriesTag = record.getFieldSubfields("225");
		if(seriesTag && seriesTag.length > 1) {
			for(var j in seriesTag) {
				var series = seriesTag[j];
				if(seriesText) {
					seriesText += "; ";
				}
				else {
					seriesText = "";
				}
				seriesText += series.a;
				if(series.v) {
					seriesText += ", " + series.v;
				}
			}
			if(seriesText) {
				delete item.seriesNumber;
				item.series = seriesText;
			}
		}
		//Try 461
		if(!item.series) {
			seriesTag = record.getFieldSubfields("461");
			if(seriesTag) {
				for(var j in seriesTag) {
					var series = seriesTag[j];
					if(seriesText) {
						seriesText += "; ";
					}
					else {
						seriesText = "";
					}
					seriesText += series.t;
				}
			}
			if(seriesText) {
				delete item.seriesNumber;
				item.series = seriesText;
			}
		}
	};
	
	//Add extra text
	function addExtra(noteText, extra) {
		if(extra) {
			if(noteText) {
				if(!/\.$/.exec(noteText)) {
					noteText += ". ";
				}
				else {
					noteText += " ";
				}
			}
			else {
				noteText = "";
			}
			noteText += Zotero.Utilities.trim(extra);
		}
		return noteText;
	}
	
	//Assemble extra information
	function getExtra(record, item) {
		var noteText = false;
		//Material description
		var noteTag = record.getFieldSubfields("215");
		if(noteTag) {
			for(var j in noteTag) {
				var note = noteTag[j];
				noteText = addExtra(noteText, note.c);
				noteText = addExtra(noteText, note.d);
				noteText = addExtra(noteText, note.e);
			}
		}
		//Note
		noteTag = record.getFieldSubfields("300");
		if(noteTag) {
			for(var j in noteTag) {
				var note = noteTag[j];
				noteText = addExtra(noteText, note.a);
			}
		}
		//Edition history notes
		noteTag = record.getFieldSubfields("305");
		if(noteTag) {
			for(var j in noteTag) {
				var note = noteTag[j];
				noteText = addExtra(noteText, note.a);
			}
		}
		if(noteText) {
			if(!/\.$/.exec(noteText)) {
				 		noteText += ".";
					}
			item.extra = noteText;
		}
	};


	//Get title from 200
	function getTitle(record, item) {
		var titleTag = record.getFieldSubfields("200");
		if(titleTag) {
			titleTag = titleTag[0];
			var titleText = titleTag.a;
			if(titleTag.e) {
				if(!/^[,\.:;-]/.exec(titleTag.e)) {
					titleText += ": ";
				}
				titleText += titleTag.e;
			} 
			if(titleTag.h) {
				titleText += ", " + titleTag.h;
				if(titleTag.i) {
					titleText += ": " + titleTag.i;
				} 
			} 
			else if(titleTag.i) {
				titleText += ", " + titleTag.i;
			} 
			item.title = titleText;
		}
	};


	//Do BnF specific Unimarc postprocessing
	function postprocessMarc(record, newItem) {
		//Type
		var t = record.getFieldSubfields("200");
		if(t && t[0] && t[0].b) {
			newItem.itemType = getItemType(t[0].b);
		}

		//Title
		getTitle(record, newItem);

		//Fix creators
		getCreators(record, newItem);

		//Store perennial url from 009 as attachment and accession number
		var url = record.getField("009");
		if(url && url.length > 0 && url[0][1]) {
			newItem.accessionNumber = url[0][1];
			newItem.attachments = [
				{
					url: url[0][1],
					title: "Bnf catalogue entry", 
					mimeType: "text/html", 
					snapshot:false
				}
			];
		}

		//Country (102a)
		record._associateDBField(newItem, "102", "a", "country");

		//Try to retrieve volumes/pages from 215d
		if(!newItem.pages) {
			var dimTag = record.getFieldSubfields("215");
			for (var j in dimTag)  {
				var dim = dimTag[j];
				if(dim.a) {
					var pages = /[^\d]*(\d+)\s+p\..*/.exec(dim.a);
					if(pages) {
						newItem.numPages = pages[1];
					}
					var vols= /[^\d]*(\d+)\s+vol\..*/.exec(dim.a);
					if(vols) {
						newItem.numberOfVolumes = vols[1];
					}
				}
			}
		}

		//Series
		getSeries(record, newItem);

		//Extra
		getExtra(record, newItem);
		 
		//Tagging
		getTags(record, newItem);
		
		//Repository
		newItem.libraryCatalog = "French National Library Online Catalog (http://catalogue.bnf.fr)";
	};


	//Public members

	/* Get the UNIMARC URL for a given single result page. */
	this.reformURL = function(url) {
		return url.replace(/&FormatAffichage=[^&]*/, "")
			.replace(/&idNoeud=[^&]*/, "") + "&FormatAffichage=4";
	};

	
	/* Get the results table from a list page, if any. Looks for //table[@class="ListeNotice"]. */
	this.getResultsTable = function(doc) {
		try {
			var xPath = '//table[@class="ListeNotice"]';
			var xPathObject = doc.evaluate(xPath, doc, null, XPathResult.ANY_TYPE, null).iterateNext();
			return xPathObject;
		} catch(x) {
			Zotero.debug(x.lineNumber + " " + x.message);
		}
		return undefined;
	};

	/* Get the DC type from the web page. Returns the first DC.type from meta tags. 
		2010-10-01: No DC meta tags any more... simply test for //td[@class="texteNotice"] cells and return "printed text".
	*/
	this.getDCType = function(doc, url) {
		try {
//			var xPath = '//head/meta[@name="DC.type" and @lang="eng"]/@content';
			var xPath = '//td[@class="texteNotice"]';
			var xPathObject = doc.evaluate(xPath, doc, null, XPathResult.ANY_TYPE, null).iterateNext();;
			return xPathObject ? "printed text" : undefined;
		} catch(x) {
			Zotero.debug(x.lineNumber + " " + x.message);
		}
		return undefined;
	};

	/* Translate a DC type to a corresponding Zotero item type. Currently obsolete. */
	this.translateDCType = function(type) {
		switch(type) {
		case "printed text":
		case "text":
			return "book";
		case "sound recording":
			return "audioRecording";
		default:
			return type;
		}
	};

	
	/* Get selectable search items from a list page. 
		Loops through //td[@class="mn_partienoticesynthetique"], extracting the single items URLs from
		their onclick attribute, thier titles by assembling the spans for each cell.
	*/
	this.getSelectedItems = function(doc) {
		var items = new Object();
		
		var baseUri = /^(https?:\/\/[^\/]+)/.exec(doc.location.href)[1];
		var cellPath = '//td[@class="mn_partienoticesynthetique"]';
		var spanPath = './/span';
		var cells = doc.evaluate(cellPath, doc, null, XPathResult.ANY_TYPE, null);
		var cell = undefined;
		var regexLink = /\s*window.location='([^']+)'\s*/;
		
		//Cell loop
		while(cell = cells.iterateNext()) {
			//Get link
			var link = cell.attributes.item("onclick").textContent;
			var url = baseUri + regexLink.exec(link)[1];
			//Get title
			var title = "";
			var span = undefined;
			var spans = doc.evaluate(spanPath, cell, null, XPathResult.ANY_TYPE, null);
			//Span loop
			while(span = spans.iterateNext()) {
				if(title.length > 0) {
					title += " – ";
			}
				title += Zotero.Utilities.trim(span.textContent);
			}
			items[url] = title;
		}

		return items;        
	};

	
	//Check for Gallica URL (digital version available), if found, set item.url
	function checkGallica(doc, item) {
	
		var url = false;
		//Check for links containing the "Visualiser" img
		var elmts = doc.evaluate('//a[img[@src="/images/boutons/bouton_visualiser.gif"]]',
				doc, null, XPathResult.ANY_TYPE, null);
		if(elmts) {
			var link;
			while(link = elmts.iterateNext()) {
				url = link.href;
				break;
			}
		}
		
		if(url) {
			item.url = url;
		}
	}
	
	
	/* Process UNIMARC URL. */
	this.processMarcUrl = function(newDoc) {
		/* Init MARC record. */
		// Load MARC
		var translator = Zotero.loadTranslator("import");
		translator.setTranslator("a6ee60df-1ddc-4aae-bb25-45e0537be973");
		translator.getTranslatorObject( function (obj) {
			 var record = new obj.record();
		
			/* Get table cell containing MARC code. */
			var elmts = newDoc.evaluate('//td[@class="texteNotice"]/text()',
					newDoc, null, XPathResult.ANY_TYPE, null);
			/* Line loop. */
			var elmt, tag, content;
			var ind = "";
	
			while(elmt = elmts.iterateNext()) {
				var line = Zotero.Utilities.superCleanString(elmt.nodeValue);
				if(line.length == 0) {
					continue;
				}
				if(line.substring(0, 6) == "       ")  {
					content += " "+line.substring(6);
					continue;
				} else {
					if(tag) {
						record.addField(tag, ind, content);
					}
				}
				line = line.replace(/[_\t\xA0]/g," "); // nbsp
				tag = line.substr(0, 3);
				if(tag[0] != "0" || tag[1] != "0") {
					ind = line.substr(3, 2);
					content = line.substr(5).replace(/\$([a-z]|[0-9])/g, obj.subfieldDelimiter+"$1");
					content = content.replace(/ˆ([^‰]+)‰/g, "$1");
				} else {
					if(tag == "000") {
						tag = undefined;
						record.leader = "00000"+line.substr(8);
					} else {
						content = line.substr(3);
					}
				}
			}
				
			//Create item
			var newItem = new Zotero.Item();
			record.translate(newItem);
				
			//Do specific Unimarc postprocessing
			postprocessMarc(record, newItem);
			
			//Check for Gallica URL
			checkGallica(newDoc, newItem);
				
			newItem.complete();
		});
	};
};

/* Global BnfClass object. */
var Bnf = new BnfClass();


/* Translator API implementation. */


function detectWeb(doc, url) {
	var resultRegexp = /ID=[0-9]+/i;
	//Single result ?
	if(resultRegexp.test(url)) {
		var type = Bnf.getDCType(doc, url);
		return Bnf.translateDCType(type);
	} 
	//Muliple result ?
	else if(Bnf.getResultsTable(doc)) {
		return "multiple";
	}
	//No items 
	return undefined;
}


function doWeb(doc, url) {
	/* Check type. */
	var type = detectWeb(doc, url);
	if(!type) {
		return;
	}
	/* Build array of MARC URLs. */
	var urls = new Array();
	switch(type) {
		case "multiple":
			var items = Bnf.getSelectedItems(doc);
			if(!items) {
				return true;
			}
			/* Let user select items. */
			Zotero.selectItems(items, function (items) {
				for(var i in items) {
				urls.push(Bnf.reformURL(i));
				}
				if(urls.length > 0) {
					//Z.debug(urls)
					Zotero.Utilities.processDocuments(urls, function(doc) {Bnf.processMarcUrl.call(Bnf, doc)});
				}	
			});
			break;
		default:
			urls = [Bnf.reformURL(url)];
			Zotero.Utilities.processDocuments(urls, function(doc) {Bnf.processMarcUrl.call(Bnf, doc)});
	}
}
/** BEGIN TEST CASES **/
var testCases = []
/** END TEST CASES **/