diff --git a/chrome/content/zotero/overlay.js b/chrome/content/zotero/overlay.js
index 67717fa67..c15e89864 100644
--- a/chrome/content/zotero/overlay.js
+++ b/chrome/content/zotero/overlay.js
@@ -1482,7 +1482,8 @@ var ZoteroPane = new function()
createBib: 11,
loadReport: 12,
sep4: 13,
- reindexItem: 14
+ reindexItem: 14,
+ recognizePDF: 15
};
var menu = document.getElementById('zotero-itemmenu');
@@ -1507,20 +1508,37 @@ var ZoteroPane = new function()
hide.push(m.showInLibrary, m.sep1, m.addNote, m.attachSnapshot,
m.attachLink, m.sep2, m.duplicateItem);
- // If all items can be reindexed, show option
+ // If all items can be reindexed, or all items can be recognized, show option
var items = this.getSelectedItems();
var canIndex = true;
+ var canRecognize = true;
for (var i=0; i
+
@@ -108,6 +109,7 @@
+
diff --git a/chrome/content/zotero/recognizePDF.js b/chrome/content/zotero/recognizePDF.js
new file mode 100644
index 000000000..ab9f824b6
--- /dev/null
+++ b/chrome/content/zotero/recognizePDF.js
@@ -0,0 +1,234 @@
+/*
+ ***** BEGIN LICENSE BLOCK *****
+
+ Copyright (c) 2006 Center for History and New Media
+ George Mason University, Fairfax, Virginia, USA
+ http://chnm.gmu.edu
+
+ Licensed under the Educational Community License, Version 1.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.opensource.org/licenses/ecl1.php
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+ ***** END LICENSE BLOCK *****
+*/
+
+/**
+ * @fileOverview Tools for automatically retrieving a citation for the given PDF
+ */
+const MAX_PAGES = 2;
+
+/**
+ * Front end for recognizing PDFs
+ * @namespace
+ */
+Zotero_RecognizePDF = new function() {
+ /**
+ * Checks whether a given PDF could theoretically be recognized
+ * @returns {Boolean} True if the PDF can be recognized, false if it cannot be
+ */
+ this.canRecognize = function(/**Zotero.Item*/ item) {
+ return (item.attachmentMIMEType && item.attachmentMIMEType == "application/pdf" && !item.getSource());
+ }
+
+ /**
+ * Retrieves metadata for the PDF(s) selected in the Zotero Pane, placing the PDFs as a children
+ * of the new items
+ */
+ this.recognizeSelected = function() {
+ var items = ZoteroPane.getSelectedItems();
+ if (!items) {
+ return;
+ }
+ this.recognizeItems(items);
+ }
+
+ /**
+ * Retreives metadata for the PDF items passed, placing the PDFs as a children of the new items
+ */
+ this.recognizeItems = function(/**Zotero.Item[]*/ items) {
+ var itemsCopy = items.slice();
+ var item = itemsCopy.shift();
+ var file = item.getFile();
+ if(file) {
+ var recognizer = new Zotero_RecognizePDF.Recognizer();
+ recognizer.recognize(file, item.getField("title"),
+ function(translate, newItem) {
+ // put new item in same collections as the old one
+ var itemCollections = item.getCollections();
+ for(var j=0; j= lBound && lineLengths[i] <= uBound) this._goodLines.push(lines[i]);
+ }
+
+ this._startLine = this._iteration = 0;
+ this._queryGoogle();
+}
+
+/**
+ * Queries Google Scholar for metadata for this PDF
+ * @private
+ */
+Zotero_RecognizePDF.Recognizer.prototype._queryGoogle = function() {
+ if(this._iteration > 3 || this._startLine >= this._goodLines.length) {
+ this._error();
+ return;
+ }
+
+ // take the relevant parts of some lines (exclude hyphenated word)
+ var queryStringWords = 0;
+ var queryString = "";
+ while(queryStringWords < 25 && this._startLine < this._goodLines.length) {
+ var words = this._goodLines[this._startLine].split(/\s+/);
+ words.shift();
+ words.pop();
+ if(words.length) {
+ queryStringWords += words.length;
+ queryString += '"'+words.join(" ")+'" ';
+ }
+ this._startLine++;
+ }
+ Zotero.debug("RecognizePDF: Query string "+queryString);
+
+ // pass query string to Google Scholar and translate
+ var url = "http://scholar.google.com/scholar?q="+encodeURIComponent(queryString);
+ this.hiddenBrowser = Zotero.Browser.createHiddenBrowser();
+
+ var me = this;
+ var translate = new Zotero.Translate("web", true, false);
+ translate.setTranslator("57a00950-f0d1-4b41-b6ba-44ff0fc30289");
+ translate.setHandler("itemDone", this._callback);
+ translate.setHandler("select", function(translate, items) { return me._selectItems(translate, items) });
+ translate.setHandler("done", function(translate, success) { if(!success) me._queryGoogle() });
+
+ this.hiddenBrowser.addEventListener("pageshow", function() { me._scrape(translate) }, true);
+ this.hiddenBrowser.loadURI(url);
+}
+
+/**
+ * Callback to be executed when Google Scholar is loaded
+ * @private
+ */
+Zotero_RecognizePDF.Recognizer.prototype._scrape = function(/**Zotero.Translate*/ translate) {
+ this.hiddenBrowser.removeEventListener("pageshow", this._scrape.caller, true);
+ translate.setDocument(this.hiddenBrowser.contentDocument);
+ translate.translate();
+}
+
+/**
+ * Callback to pick first item in the Google Scholar item list
+ * @private
+ * @type Object
+ */
+Zotero_RecognizePDF.Recognizer.prototype._selectItems = function(/**Zotero.Translate*/ translate, /**Object*/ items) {
+ for(var i in items) {
+ var obj = {};
+ obj[i] = items;
+ return obj;
+ }
+}
+
+/**
+ * Displays an error when a PDF cannot be recognized
+ * @private
+ */
+Zotero_RecognizePDF.Recognizer.prototype._error = function() {
+ var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
+ .getService(Components.interfaces.nsIPromptService);
+ promptService.alert(window,
+ Zotero.getString('recognizePDF.couldNotRecognize.title'),
+ Zotero.getString('recognizePDF.couldNotRecognize.message', this._pdfTitle));
+}
\ No newline at end of file
diff --git a/chrome/content/zotero/xpcom/translate.js b/chrome/content/zotero/xpcom/translate.js
index c3f3861e2..824f5b15a 100644
--- a/chrome/content/zotero/xpcom/translate.js
+++ b/chrome/content/zotero/xpcom/translate.js
@@ -117,7 +117,7 @@ const BOMs = {
*
* output - export output (if no location has been specified)
*/
-Zotero.Translate = function(type, saveItem) {
+Zotero.Translate = function(type, saveItem, saveAttachments) {
this.type = type;
// import = 0001 = 1
@@ -147,12 +147,8 @@ Zotero.Translate = function(type, saveItem) {
}
this._numericTypes = this._numericTypes.substr(1);
- if(saveItem === false) { // three equals signs means if it's left
- // undefined, this.saveItem will still be true
- this.saveItem = false;
- } else {
- this.saveItem = true;
- }
+ this.saveItem = !(saveItem === false);
+ this.saveAttachments = !(saveAttachments === false);
this._handlers = new Array();
this._streams = new Array();
@@ -1320,7 +1316,7 @@ Zotero.Translate.prototype._itemDone = function(item, attachedTo) {
var downloadAssociatedFiles = Zotero.Prefs.get("downloadAssociatedFiles");
// handle attachments
- if(item.attachments && (automaticSnapshots || downloadAssociatedFiles)) {
+ if(item.attachments && this.saveAttachments && (automaticSnapshots || downloadAssociatedFiles)) {
for each(var attachment in item.attachments) {
if(this.type == "web") {
if(!attachment.url && !attachment.document) {
diff --git a/chrome/locale/en-US/zotero/zotero.properties b/chrome/locale/en-US/zotero/zotero.properties
index 054d15954..45bb25f11 100644
--- a/chrome/locale/en-US/zotero/zotero.properties
+++ b/chrome/locale/en-US/zotero/zotero.properties
@@ -95,6 +95,8 @@ pane.items.menu.generateReport = Generate Report from Selected Item...
pane.items.menu.generateReport.multiple = Generate Report from Selected Items...
pane.items.menu.reindexItem = Reindex Item
pane.items.menu.reindexItem.multiple = Reindex Items
+pane.items.menu.recognizePDF = Retrieve Metadata for PDF
+pane.items.menu.recognizePDF.multiple = Retrieve Metadata for PDFs
pane.items.letter.oneParticipant = Letter to %S
pane.items.letter.twoParticipants = Letter to %S and %S
@@ -508,4 +510,7 @@ proxies.error.scheme.noPath = A valid proxy scheme must contain either the pat
proxies.recognized.message = Adding this proxy will allow Zotero to recognize items from its pages and will automatically redirect future requests to %1$S through %2$S.
proxies.recognized.add = Add Proxy
proxies.enableTransparentWarning.title = Warning
-proxies.enableTransparentWarning.description = Please ensure that the proxies listed below belong to a library, school, or other institution with which you are affiliated. A malicious proxy could pose a security risk when transparent redirection is enabled.
\ No newline at end of file
+proxies.enableTransparentWarning.description = Please ensure that the proxies listed below belong to a library, school, or other institution with which you are affiliated. A malicious proxy could pose a security risk.
+
+recognizePDF.couldNotRecognize.title = Could Not Retrieve Metada
+recognizePDF.couldNotRecognize.message = Zotero could not retrieve metadata for "%1$S".
\ No newline at end of file