summaryrefslogtreecommitdiff
path: root/National Archive of the UK.js
blob: 4de29e23507652221e9509980b0278dfc12c02a9 (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
{
	"translatorID": "229d4678-4fa0-44f8-95c4-f4cfdb9b254c",
	"label": "National Archive of the UK",
	"creator": "Sebastian Karcher",
	"target": "^https?://discovery\\.nationalarchives\\.gov\\.uk/",
	"minVersion": "3.0",
	"maxVersion": "",
	"priority": 100,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcb",
	"lastUpdated": "2014-09-11 06:28:15"
}

/**
	Copyright (c) 2013 Sebastian Karcher
	
	This program 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.
	
	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
	Affero General Public License for more details.
	
	You should have received a copy of the GNU Affero General Public
	License along with this program. If not, see
	<http://www.gnu.org/licenses/>.
*/

function detectWeb(doc, url) {
	if (url.search(/details\/r\//i) != -1) return "manuscript";
	else if (url.search(/results\/r\/?\?.+hb=tna/i) != -1) return "multiple";
}

function scrape(doc, url) {
	var id = url.match(/details\/r\/([A-Z0-9]+)/i)[1];
	var reference = ZU.xpathText(doc, '//tr/th[contains(text(), "Reference")]/following-sibling::td')
	var tags  = ZU.xpath(doc, '//span/a[@class="tagName"]');
	
//	In Case we should need it again, we're keeping the screen scraper
/*	var item = new Zotero.Item("manuscript");
	var title =  "The National Archive of the UK " + reference;
	item.archiveLocation = reference;
	item.title = title.replace(/&lt.+?&gt;/g, "").replace(/<.+?>/g, "");
	item.language = ZU.xpathText(doc, '//tr/th[contains(text(), "Language")]/following-sibling::td');
	item.date = ZU.xpathText(doc, '//tr/th[contains(text(), "Date")]/following-sibling::td');
	item.abstractNote = ZU.xpathText(doc, '//tr/th[contains(text(), "Description")]/following-sibling::td').replace(/<p>/g, "\n").replace(/&lt;p&gt;/g, "\n").replace(/<.+?>/g, "");
	item.archive = ZU.xpathText(doc, '//tr/th[contains(text(), "Held by")]/following-sibling::td');
	item.attachments.push({url: url, title: "British National Archive - Link", mimeType: "text/html", snapshot: false});

	for (var i in tags){
		item.tags.push(tags[i].textContent);
	}
	item.complete(); 

	XML Code - hopefully to be reused once API is sorted */
	var xmlUrl = "http://discovery.nationalarchives.gov.uk/DiscoveryAPI/xml/informationasset/" + id;
	Zotero.Utilities.doGet(xmlUrl, function (text) {
		//Z.debug(text)
		var docxml = (new DOMParser()).parseFromString(text, "text/xml");

  	 	ns = {	
  	 			"xsi" : "http://www.w3.org/2001/XMLSchema-instance",
  	 			"xsd" : "http://www.w3.org/2001/XMLSchema"};
		
		var item = new Zotero.Item("manuscript");
		var title = ZU.xpathText(docxml, '//Title', ns);
		if (!title) title =  "The National Archive of the UK " + reference;
		else item.archiveLocation = reference;
		item.title = title.replace(/&lt.+?&gt;/g, "").replace(/<.+?>/g, "");
		item.language = ZU.xpathText(docxml, '//Language');
		item.date = ZU.xpathText(docxml, '//CoveringDates');
		item.abstractNote = ZU.xpathText(docxml, '//ScopeContent/Description').replace(/<p>/g, "\n").replace(/&lt;p&gt;/g, "\n").replace(/<.+?>/g, "");
		item.archive = ZU.xpathText(docxml, '//HeldBy/HeldBy');
		item.type = ZU.xpathText(docxml, '//PhysicalDescriptionForm');
		item.attachments.push({url: url, title: "British National Archive - Link", mimeType: "text/html", snapshot: false});
		var corpauthors = ZU.xpath(docxml, '//CreatorName/Corporate_Body_Name_Text');
		for (var i in corpauthors){
			item.creators.push({ lastName:corpauthors[i].textContent, fieldMode: "1", creatorType: "contributor" });
		}
		for (var i in tags){
			item.tags.push(tags[i].textContent);
		}
		item.complete();
	});


}


function doWeb(doc, url) {
	if (detectWeb(doc, url) == "multiple") {
		var articles = new Array();
		var items = new Object();
		
		//search results
		var titles = ZU.xpath(doc, '//ul[@id="search-results"]//a');

		if (titles.length<1){
			//TODO - other multiples
			return false;
			//titles = ZU.xpath(doc, '//td[@id="leaf-linkarea2"]//a[contains(@href, "/receive/jportal_jparticle")]');
		}
		for (var i in titles) {
			items[titles[i].href] = ZU.trimInternal(titles[i].textContent);
		}
		Zotero.selectItems(items, function (items) {
			if (!items) {
				return true;
			}
			for (var i in items) {
				articles.push(i);
			}
			Zotero.Utilities.processDocuments(articles, scrape);
		});
	} else {
		scrape(doc, url);
	}
}/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "http://discovery.nationalarchives.gov.uk/SearchUI/Details?uri=C3454320",
		"items": [
			{
				"itemType": "manuscript",
				"title": "The National Archive of the UK INF 3/108",
				"creators": [],
				"date": "1939-1946",
				"abstractNote": "POSTERS: Food Production: Land girls - Horse-drawn plough, and girl. \nArtist: Dame Laura Knight. \nMedia/Technique: Watercolour and gouache painting with a charcoal underdrawing.Executed on a heavy weight artist board. Light washes of the aqueous media have been applied on top of the loose charcoal sketch giving the painting a powdery, friable quality.",
				"archive": "The National Archives, Kew",
				"libraryCatalog": "National Archive of the UK",
				"attachments": [
					{
						"title": "British National Archive - Link",
						"mimeType": "text/html",
						"snapshot": false
					}
				],
				"tags": [
					"land girls",
					"women"
				],
				"notes": [],
				"seeAlso": []
			}
		]
	},
	{
		"type": "web",
		"url": "http://discovery.nationalarchives.gov.uk/SearchUI/Details?uri=C31",
		"items": [
			{
				"itemType": "manuscript",
				"title": "Records of the National Dock Labour Corporation and National Dock Labour Board",
				"creators": [
					{
						"lastName": "National Dock Labour Board",
						"fieldMode": "1",
						"creatorType": "contributor"
					},
					{
						"lastName": "National Dock Labour Corporation",
						"fieldMode": "1",
						"creatorType": "contributor"
					}
				],
				"date": "1748-1989",
				"abstractNote": "Scope and Content\nThe records of the National Dock Labour Corporation, established to regularise dock labour during the Second World War, and the records of the National Dock Labour Board, which took over these functions in 1947.\nAlso included are the records of four local boards responsible for day to day running of the National Dock Labour Scheme from 1947:\nLondon Dock Labour BoardCumbria Dock Labour BoardGrimsby and Immingham Dock Labour BoardSouth Coast Dock Labour Board.",
				"archive": "The National Archives, Kew",
				"archiveLocation": "BK",
				"language": "English",
				"libraryCatalog": "National Archive of the UK",
				"manuscriptType": "series",
				"attachments": [
					{
						"title": "British National Archive - Link",
						"mimeType": "text/html",
						"snapshot": false
					}
				],
				"tags": [
					"bk23"
				],
				"notes": [],
				"seeAlso": []
			}
		]
	},
	{
		"type": "web",
		"url": "http://discovery.nationalarchives.gov.uk/results/r?_q=labour&_hb=tna",
		"items": "multiple"
	}
]
/** END TEST CASES **/