]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/javascript/backend/catalog/biblio_fingerprint.js
Merge branch 'master' of git.evergreen-ils.org:Evergreen-DocBook into doc_consolidati...
[Evergreen.git] / Open-ILS / src / javascript / backend / catalog / biblio_fingerprint.js
1 // so we can tell if it's a book or other type
2 load_lib('record_type.js');
3 load_lib('JSON_v1.js');
4
5 environment.result = {};
6
7 var marcdoc = new XML(environment.marc);
8 var marc_ns = new Namespace('http://www.loc.gov/MARC21/slim');
9
10 var modsdoc = new XML(environment.mods);
11 var mods_ns = new Namespace('http://www.loc.gov/mods/');
12 default xml namespace = marc_ns;
13
14 //var mods3_ns = new Namespace('http://www.loc.gov/mods/v3');
15
16
17 var rtype = recordType(marcdoc); // BKS, SER, VIS, MIX, MAP, SCO, REC, COM
18
19 var quality = 0;
20 var t = '';
21 var a = '';
22
23 try {
24         // first, related items entries (700t)
25         var t = marcdoc.datafield.( @tag == '700' ).subfield.( @code == 't');
26         if (!t.length()) throw "No title in related item added entry (700)";
27         
28         a = t.parent().subfield.( @code == 'a' );
29
30         quality += 10;
31
32         log_debug("title: " + t);
33         log_debug("author: " + a);
34 } catch(e) {
35         log_debug(e);
36         log_debug("Looking in main entries");
37
38         var _t = '';
39         try {
40                 try { 
41                         try { // uniform title
42                                 _t = marcdoc.datafield.( @tag == '240' ).subfield.( @code == 'a' );
43                                 if (!_t.length()) throw "No title in 240";
44                         } catch(e) { // translation of title
45                                 log_debug(e);
46                                 _t = marcdoc.datafield.( @tag == '242' ).subfield.( @code == 'a' );
47                                 if (!_t.length()) throw "No title in 242";
48                         }
49                 } catch(e) { // alternate title (not as note) 
50                         log_debug(e);
51                         _t = marcdoc.datafield.( @tag == '246' && !(@ind1.match(/0|1/)) ).subfield.( @code == 'a' );
52                         if (!_t.length()) throw "No title in 246";
53                 }
54
55                 t = _t[0];
56                 log_debug("Title: " + t);
57                 quality += 25;
58
59         } catch(e) {
60                 log_debug(e);
61                 log_debug("Using title proper (245a)");
62                 t = marcdoc.datafield.( @tag == '245' ).subfield.( @code == 'a' );
63                 t = t[0];
64                 quality += 10;
65         }
66
67         try {
68                 var _a = marcdoc.datafield.( @tag == '100' || @tag == '110' || @tag == '111').subfield.( @code == 'a' );
69                 if (!_a.length()) throw "No author in 100, 110, 111";
70                 
71                 a = _a[0];
72                 log_debug("Author: " + a);
73
74         } catch(e) {
75                 log_debug(e);
76                 log_debug("Trying to find a publisher (260b)");
77                 a = marcdoc.datafield.( @tag == '260' ).subfield.( @code == 'b' );
78                 a = a[0];
79         }
80 }
81
82 if (rtype != 'BKS') {
83         quality += marcdoc.datafield.length() / 2;
84 } else {
85         quality += 40 + marcdoc.datafield.length();
86 }
87
88 var title = t;
89 if (!title) {
90         log_debug("no title found");
91         title = '';
92 } else {
93         title = title.toString();
94 }
95
96 title = title
97         .toLowerCase()
98         .replace(/\[.+?\]/,'')
99         .replace(/\bthe\b|\ban?d?\b|\W+/g,'');
100
101
102 var author = a;
103 if (!author) {
104         author = '';
105 } else {
106         author = author.toString();
107 }
108
109 author = author.toLowerCase().replace(/^\s*(\w+).*?$/,"$1");
110
111 environment.result.fingerprint = title + author;
112
113 if (marcdoc.datafield.(@tag == '040').subfield.(@code == 'a').toString().match(/DLC/)) {
114         quality += 5;
115         log_debug( 'got DLC bump' );
116 }
117
118 if (marcdoc.datafield.(@tag == '039').subfield.(@code == 'b').toString().match(/oclc/i)) {
119         quality += 10;
120         log_debug( 'got OCLC source bump' );
121         
122 } else if (marcdoc.datafield.(@tag == '039').subfield.(@code == 'b').toString().match(/isxn/i)) {
123         quality += 5;
124         log_debug( 'got ISxN source bump' );
125         
126 } else if (marcdoc.datafield.(@tag == '039').subfield.(@code == 'b').toString().match(/local/i)) {
127         quality += 1;
128         log_debug( 'got Local source bump' );
129 }
130
131 if (extractFixedField(marcdoc, 'Lang') == 'eng') {
132         quality += 100;
133         log_debug( 'got language bump for ' + extractFixedField(marcdoc, 'Lang') );
134 }
135
136
137 environment.result.quality = quality;
138