summaryrefslogtreecommitdiff
path: root/InfoTrac.js
blob: a7d3b14606647fa13d18043e04ac7c4284bb4125 (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
{
	"translatorID": "6773a9af-5375-3224-d148-d32793884dec",
	"label": "InfoTrac",
	"creator": "Simon Kornblith",
	"target": "^https?://[^/]+/itw/infomark/",
	"minVersion": "1.0.0b3.r1",
	"maxVersion": "",
	"priority": 100,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "g",
	"lastUpdated": "2013-06-06 00:19:59"
}

function detectWeb(doc, url) {
	
	// ensure that there is an InfoTrac logo
	if(!doc.evaluate('//img[substring(@alt, 1, 8) = "InfoTrac"]', doc, null, XPathResult.ANY_TYPE, null).iterateNext()) return false;
	
	if(doc.title.substring(0, 8) == "Article ") {
		if (ZU.xpathText(doc, '//td//img[contains(@src, "ncnp_logo.gif")]/@title')) return "newspaperArticle";
		var genre = doc.evaluate('//comment()[substring(., 1, 6) = " Genre"]', doc, null, XPathResult.ANY_TYPE, null).iterateNext();
		
		if(genre) {
			var value = Zotero.Utilities.trimInternal(genre.nodeValue.substr(7));
			if(value == "article") {
				return "journalArticle";
			} else if(value == "book") {
				return "book";
			} else if(value == "dissertation") {
				return "thesis";
			} else if(value == "bookitem") {
				return "bookSection";
			}
		}
		
		return "magazineArticle";
	} else if(doc.title.substring(0, 10) == "Citations ") {
		return "multiple";
	}
}

function scrape(doc, url){
	var newItem = new Zotero.Item();
	var xpath = '/html/body//comment()';
	var elmts = doc.evaluate(xpath, doc, null, XPathResult.ANY_TYPE, null);
	var citation = ZU.xpath(doc, '//p/table/tbody//td/table/tbody[not(./script)]');
	newItem.title = ZU.xpathText(citation, './/font/b');
	newItem.itemType = "newspaperArticle";
	var author = ZU.xpathText(citation, './/td/i');
	if (author) newItem.creators.push(ZU.cleanAuthor(author, "author`"));
	var date = ZU.xpathText(citation, './/td/text()');
	if (date) date = date.match(/[A-Z][a-z]+\s\d+,\s\d{4}/);
	if (date) newItem.date = date[0];
	var pdfurl = ZU.xpathText(doc, '//blockquote/a[contains(@href, "!pdf")][1]/@href');
	if  (pdfurl){
		newItem.attachments.push({url: pdfurl, title: "Infotrac Full Text PDF", mimeType: "application/pdf"})
	}
	newItem.attachments.push({document: doc, title: "Infotrac Snapshot", mimeType: "text/html"});
	while(elmt = elmts.iterateNext()) {
		var colon = elmt.nodeValue.indexOf(":");
		var field = elmt.nodeValue.substring(1, colon).toLowerCase();
		var value = elmt.nodeValue.substring(colon+1, elmt.nodeValue.length-1);
		if(field == "journal") {
			newItem.publicationTitle = value;
		}
	}
	if (newItem.publicationTitle.search(/\(.+\)/)){
		newItem.place = newItem.publicationTitle.match(/\((.+)\)/)[1];
		newItem.publicationTitle = newItem.publicationTitle.replace(/\(.+\).*/, "");
	}
	newItem.complete();
}

function extractCitation(url, elmts, title, doc) {
	var newItem = new Zotero.Item();
	newItem.url = url;
	if(title) {
		newItem.title = Zotero.Utilities.superCleanString(title);
	}
	newItem.title = ZU.xpathText(citation, './/font/b');
	newItem.itemType = "newspaperArticle";
	var date = ZU.xpathText(citation, './/td/text()');
	if (date) date = date.match(/[A-Z][a-z]+\s\d+,\s\d{4}/);
	if (date) newItem.date = date[0];

	while(elmt = elmts.iterateNext()) {
		var colon = elmt.nodeValue.indexOf(":");
		var field = elmt.nodeValue.substring(1, colon).toLowerCase();
		var value = elmt.nodeValue.substring(colon+1, elmt.nodeValue.length-1);
		if(field == "title") {
			newItem.title = Zotero.Utilities.superCleanString(value);
		} else if(field == "journal") {
			newItem.publicationTitle = value;
		} else if(field == "pi") {
			parts = value.split(" ");
			var date = "";
			var field = null;
			for(j in parts) {
				firstChar = parts[j].substring(0, 1);
				
				if(firstChar == "v") {
					newItem.itemType = "journalArticle";
					field = "volume";
				} else if(firstChar == "i") {
					field = "issue";
				} else if(firstChar == "p") {
					field = "pages";
					
					var pagesRegexp = /p(\w+)\((\w+)\)/;	// weird looking page range
					var match = pagesRegexp.exec(parts[j]);
					if(match) {			// yup, it's weird
						var finalPage = parseInt(match[1])+parseInt(match[2])
						parts[j] = "p"+match[1]+"-"+finalPage.toString();
					} else if(!newItem.itemType) {	// no, it's normal
						// check to see if it's numeric, bc newspaper pages aren't
						var justPageNumber = parts[j].substr(1);
						if(parseInt(justPageNumber).toString() != justPageNumber) {
							newItem.itemType = "newspaperArticle";
						}
					}
				} else if(!field) {	// date parts at the beginning, before
									// anything else
					date += " "+parts[j];
				}
				
				if(field) {
					isDate = false;
					
					if(parts[j] != "pNA") {		// make sure it's not an invalid
												// page number
						// chop of letter
						newItem[field] = parts[j].substring(1);
					} else if(!newItem.itemType) {		// only newspapers are missing
														// page numbers on infotrac
						newItem.itemType = "newspaperArticle";
					}
				}
			}
			
			// Set type
			if(!newItem.itemType) {
				newItem.itemType = "magazineArticle";
			}
			
			if(date != "") {
				newItem.date = date.substring(1);
			}
		} else if(field == "author") {
			var author = Zotero.Utilities.cleanAuthor(value, "author", true);
			
			// ensure author is not already there
			var add = true;
			for each(var existingAuthor in newItem.creators) {
				if(existingAuthor.firstName == author.firstName && existingAuthor.lastName == author.lastName) {
					add = false;
					break;
				}
			}
			if(add) newItem.creators.push(author);
		} else if(field == "issue") {
			newItem.issue = value;
		} else if(field == "volume") {
			newItem.volume = value;
		} else if(field == "issn") {
			newItem.ISSN = value;
		} else if(field == "gjd") {
			var m = value.match(/\(([0-9]{4}[^\)]*)\)(?:, pp\. ([0-9\-]+))?/);
			if(m) {
				newItem.date = m[1];
				newItem.pages = m[2];
			}
		} else if(field == "BookTitle") {
			newItem.publicationTitle = value;
		} else if(field == "genre") {
			value = value.toLowerCase();
			if(value == "article") {
				newItem.itemType = "journalArticle";
			} else if(value == "book") {
				newItem.itemType = "book";
			} else if(value == "dissertation") {
				newItem.itemType = "thesis";
			} else if(value == "bookitem") {
				newItem.itemType = "bookSection";
			}
		}
	}
	
	if(doc) {
		newItem.attachments.push({document:doc, title:"InfoTrac Snapshot"});
	} else {
		newItem.attachments.push({url:url, title:"InfoTrac Snapshot",
								 mimeType:"text/html"});
	}
	
	newItem.complete();
}

function doWeb(doc, url) {	
	var ncnp;
	if (ZU.xpathText(doc, '//td//img[contains(@src, "ncnp_logo.gif")]/@title')) ncnp = true;
	/*the only Infotrac Site that's still up & I'm aware of is 19th Century Newspapers. 
	But there may well be others, so I'm leaving a lot of legacy code in just in case */

	var uri = doc.location.href;
	if(doc.title.substring(0, 8) == "Article ") {	// article
		if(ncnp) scrape(doc, url);
		else{
			var xpath = '/html/body//comment()';
			var elmts = doc.evaluate(xpath, doc, null, XPathResult.ANY_TYPE, null);
			extractCitation(uri, elmts);
		}
	} else {										// search results
		var items = new Object();
		var uris = new Array();
		var elmts = new Array();
		
		var host = doc.location.href.match(/^https?:\/\/[^\/]+/)[0];
		var baseurl = doc.location.href.match(/(.+)\/purl=/);
		var institution = url.match(/\?sw_aep=.+/)[0];
		var tableRows = doc.evaluate('/html/body//table/tbody/tr/td[b or strong]', doc, null,
									 XPathResult.ANY_TYPE, null);
		var tableRow;
		var javaScriptRe = /'([^']*)' *, *'([^']*)'/
		var i = 0;
		// Go through table rows
		if(ncnp){
			while(tableRow = tableRows.iterateNext()) {
				var title = ZU.trimInternal(ZU.xpathText(tableRow, './strong'));
				var link = ZU.xpathText(tableRow, './a[1]/@href');
				link = link.match(/\(\'(\/.+)\',\'/)[1];
				link = baseurl[1] + link + institution;
				//Z.debug(link)
				items[link] = title;
			}
			Zotero.selectItems(items, function (items) {
			if (!items) {
				return true;
			}
			for (var i in items) {

				uris.push(i);
			}
			Zotero.Utilities.processDocuments(uris, scrape)
		});
		}
		else{
			while(tableRow = tableRows.iterateNext()) {
				var link = doc.evaluate('./a', tableRow, null, XPathResult.ANY_TYPE, null).iterateNext();
				var m = javaScriptRe.exec(link.href);
				if(m) {
					uris[i] = host+"/itw/infomark/192/215/90714844w6"+m[1]+"?sw_aep=olr_wad"+m[2];
				}
				var article = doc.evaluate('./b/text()|./strong/text', link, null, XPathResult.ANY_TYPE, null).iterateNext();
				items[i] = article.nodeValue;
				// Chop off final period
				if(items[i].substr(items[i].length-1) == ".") {
					items[i] = items[i].substr(0, items[i].length-1);
				}
				elmts[i] = doc.evaluate(".//comment()", tableRow, null, XPathResult.ANY_TYPE, null);
				citation[i] = ZU.xpath(tableRow, '//')
				i++;
			}
			
			items = Zotero.selectItems(items);
			
			if(!items) {
				return true;
			}
			
			for(var i in items) {
				extractCitation(uris[i], elmts[i], items[i]);
			}
		}	
	}
}