-
Notifications
You must be signed in to change notification settings - Fork 124
/
repo.js
208 lines (178 loc) · 6.13 KB
/
repo.js
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
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2011 Center for History and New Media
George Mason University, Fairfax, Virginia, USA
http://zotero.org
This file is part of Zotero.
Zotero 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.
Zotero 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 Zotero. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
Zotero.Repo = new function() {
var _nextCheck;
var _timeoutID;
const infoRe = /^\s*{[\S\s]*?}\s*?[\r\n]/;
this.SOURCE_ZOTERO_STANDALONE = 1;
this.SOURCE_REPO = 2;
/**
* Try to retrieve translator metadata from Zotero Standalone and initialize repository check
* timer
*/
this.init = function() {
// get time of next check
_nextCheck = Zotero.Prefs.get("connector.repo.lastCheck.localTime")
+ZOTERO_CONFIG.REPOSITORY_CHECK_INTERVAL*1000;
// update from standalone, but only cascade to repo if we are overdue
_updateFromStandalone(_nextCheck <= Date.now());
};
/**
* Force updating translators
*/
var update = this.update = function(reset) {
_updateFromStandalone(true, reset);
};
/**
* Get translator code from repository
* @param {String} translatorID ID of the translator to retrieve code for
*/
this.getTranslatorCode = Zotero.Promise.method(function (translatorID, debugMode) {
var deferred = Zotero.Promise.defer();
// try standalone
Zotero.Connector.callMethod("getTranslatorCode", {"translatorID":translatorID}, function(result) {
if(result) {
deferred.resolve(
Zotero.Promise.all(
[
_haveCode(result, translatorID),
Zotero.Repo.SOURCE_ZOTERO_STANDALONE
]
)
);
return;
}
// Don't fetch from repo in debug mode
if (debugMode) {
deferred.resolve([false, Zotero.Repo.SOURCE_ZOTERO_STANDALONE]);
return;
}
// then try repo
Zotero.HTTP.doGet(
ZOTERO_CONFIG.REPOSITORY_URL + "code/" + translatorID + "?version=" + Zotero.version,
function(xmlhttp) {
deferred.resolve(
Zotero.Promise.all(
[
_haveCode(
xmlhttp.status === 200 ? xmlhttp.responseText : false,
translatorID
),
Zotero.Repo.SOURCE_REPO
]
)
);
}
);
});
return deferred.promise;
});
/**
* Called when code has been retrieved from standalone or repo
*/
function _haveCode(code, translatorID) {
if(!code) {
Zotero.logError(new Error("Code could not be retrieved for " + translatorID));
return false;
}
code = Zotero.Translator.replaceDeprecatedStatements(code);
var m = infoRe.exec(code);
if (!m) {
Zotero.logError(new Error("Invalid or missing translator metadata JSON object for " + translatorID));
return false;
}
try {
var metadata = JSON.parse(m[0]);
} catch(e) {
Zotero.logError(new Error("Invalid or missing translator metadata JSON object for " + translatorID));
return false;
}
var translator = Zotero.Translators.getWithoutCode(translatorID);
if(metadata.lastUpdated !== translator.lastUpdated) {
if(Zotero.Date.sqlToDate(metadata.lastUpdated) > Zotero.Date.sqlToDate(translator.lastUpdated)) {
Zotero.debug("Repo: Retrieved code for "+metadata.label+" newer than stored metadata; updating");
Zotero.Translators.update([metadata]);
} else {
Zotero.debug("Repo: Retrieved code for "+metadata.label+" older than stored metadata; not caching");
}
}
return code;
}
/**
* Retrieve translator metadata from Zotero Standalone
* @param {Boolean} [tryRepoOnFailure] If true, run _updateFromRepo() if standalone cannot be
* contacted
*/
function _updateFromStandalone(tryRepoOnFailure, reset, callback) {
Zotero.Connector.callMethod("getTranslators", {}, function(result) {
if(!result && tryRepoOnFailure) {
_updateFromRepo(reset, callback);
} else {
// Standalone always returns all translators without .deleted property
_handleResponse(result, true);
if(callback) callback(!!result);
}
});
}
/**
* Retrieve metadata from repository
*/
function _updateFromRepo(reset, callback) {
var url = ZOTERO_CONFIG.REPOSITORY_URL + "metadata?version=" + Zotero.version + "&last="+
(reset ? "0" : Zotero.Prefs.get("connector.repo.lastCheck.repoTime"));
Zotero.HTTP.doGet(url, function(xmlhttp) {
var success = xmlhttp.status === 200;
_handleResponse(success ? JSON.parse(xmlhttp.responseText) : false, reset);
if(success) {
var date = xmlhttp.getResponseHeader("Date");
Zotero.Prefs.set("connector.repo.lastCheck.repoTime",
Math.floor(Date.parse(date)/1000));
}
if(callback) callback(!!result);
});
}
/**
* Handle response from Zotero Standalone or repository and set timer for next update
*/
function _handleResponse(result, reset) {
// set up timer
var now = Date.now();
if(result) {
Zotero.Translators.update(result, reset);
Zotero.Prefs.set("connector.repo.lastCheck.localTime", now);
Zotero.debug("Repo: Check succeeded");
} else {
Zotero.debug("Repo: Check failed");
}
if(result || _nextCheck <= now) {
// if we failed a scheduled check, then use retry interval
_nextCheck = now+(result
? ZOTERO_CONFIG.REPOSITORY_CHECK_INTERVAL
: ZOTERO_CONFIG.REPOSITORY_RETRY_INTERVAL)*1000;
} else if(_timeoutID) {
// if we didn't fail a scheduled check and another is already scheduled, leave it
return;
}
// remove old timeout and create a new one
if(_timeoutID) clearTimeout(_timeoutID);
var nextCheckIn = (_nextCheck-now+2000);
_timeoutID = setTimeout(update, nextCheckIn);
Zotero.debug("Repo: Next check in "+nextCheckIn);
}
}