diff --git a/chrome/chromeFiles/content/scholar/xpcom/schema.js b/chrome/chromeFiles/content/scholar/xpcom/schema.js index 2eb8e5c7ff..3985319305 100644 --- a/chrome/chromeFiles/content/scholar/xpcom/schema.js +++ b/chrome/chromeFiles/content/scholar/xpcom/schema.js @@ -3,6 +3,8 @@ Scholar.Schema = new function(){ var _schemaVersions = []; this.updateSchema = updateSchema; + this.updateScrapersRemote = updateScrapersRemote; + /* * Checks if the DB schema exists and is up-to-date, updating if necessary @@ -19,7 +21,7 @@ Scholar.Schema = new function(){ } } - _updateScrapers(); + _updateScrapersLocal(); return; } // If DB version is less than schema file, create or update @@ -31,7 +33,7 @@ Scholar.Schema = new function(){ } _migrateSchema(dbVersion); - _updateScrapers(); + _updateScrapersLocal(); return; } else { @@ -40,6 +42,37 @@ Scholar.Schema = new function(){ } + /** + * Send XMLHTTP request for updated scrapers to the central repository + * + * _force_ forces a repository query regardless of how long it's been + * since the last check + **/ + function updateScrapersRemote(force){ + // Determine the earliest local time that we'd query the repository again + var lastChecked = _getDBVersion('lastcheck'); + var d = new Date(); + d.setTime((parseInt(lastChecked) + + SCHOLAR_CONFIG['REPOSITORY_CHECK_INTERVAL']) * 1000); // JS uses ms + + // If enough time hasn't passed and it's not being forced, don't update + if (!force && new Date() < d){ + Scholar.debug('Not checking repository', 4); + return false; + } + + // Get the last timestamp we got from the server + var lastUpdated = _getDBVersion('repository'); + + var url = SCHOLAR_CONFIG['REPOSITORY_URL'] + '/updated?' + + (lastUpdated ? 'last=' + lastUpdated + '&' : '') + + 'version=' + Scholar.version; + + Scholar.debug('Checking repository for updates (' + url + ')'); + Scholar.HTTP.doGet(url, false, _updateScrapersRemoteCallback); + } + + ///////////////////////////////////////////////////////////////// // // Private methods @@ -171,11 +204,9 @@ Scholar.Schema = new function(){ try { Scholar.DB.beginTransaction(); Scholar.DB.query(_getSchemaSQL()); - Scholar.DB.query("INSERT INTO version VALUES ('schema', " - + _getSchemaSQLVersion() + ")"); + _updateDBVersion('schema', _getSchemaSQLVersion()); Scholar.DB.query(_getSchemaSQL('scrapers')); - Scholar.DB.query("INSERT INTO version VALUES ('scrapers', " - + _getSchemaSQLVersion('scrapers') + ")"); + _updateDBVersion('scrapers', _getSchemaSQLVersion('scrapers')); Scholar.DB.commitTransaction(); } catch(e){ @@ -189,15 +220,15 @@ Scholar.Schema = new function(){ * Update a DB schema version tag in an existing database */ function _updateDBVersion(schema, version){ - return Scholar.DB.query("UPDATE version SET version=" + version - + " WHERE schema='" + schema + "'"); + var sql = "REPLACE INTO version (schema,version) VALUES (?,?)"; + return Scholar.DB.query(sql, [{'string':schema},{'int':version}]); } /* * Update the scrapers in the DB to the latest bundled versions */ - function _updateScrapers(){ + function _updateScrapersLocal(){ var dbVersion = _getDBVersion('scrapers'); var schemaVersion = _getSchemaSQLVersion('scrapers'); @@ -217,6 +248,73 @@ Scholar.Schema = new function(){ } + /** + * Process the response from the repository + **/ + function _updateScrapersRemoteCallback(xmlhttp){ + // TODO: error handling + var currentTime = xmlhttp.responseXML. + getElementsByTagName('currentTime')[0].firstChild.nodeValue; + var updates = xmlhttp.responseXML.getElementsByTagName('scraper'); + + Scholar.DB.beginTransaction(); + + // Store the timestamp provided by the server + _updateDBVersion('repository', currentTime); + + // And the local timestamp of the update time + var d = new Date(); + _updateDBVersion('lastcheck', Math.round(d.getTime()/1000)); // JS uses ms + + if (!updates.length){ + Scholar.debug('All scrapers are up-to-date'); + Scholar.DB.commitTransaction(); + return false; + } + + for (var i=0, len=updates.length; i 0) { var body = doc.getElementsByTagName("body"); if(body[0].innerHTML.indexOf("ISBN") < 0) { @@ -1087,7 +1091,7 @@ utilities.HTTPUtilities.doGet(newUri, null, function(text) { wait();'); -REPLACE INTO "scrapers" VALUES('c0e6fda6-0ecd-e4f4-39ca-37a4de436e15', NULL, 20060603002000, 'GEAC Scraper', 'Simon Kornblith', '/(?:Geac)?FETCH[\:\?].*[&:]next=html/(?:record\.html|geacnffull\.html)', NULL, +REPLACE INTO "scrapers" VALUES('c0e6fda6-0ecd-e4f4-39ca-37a4de436e15', '2006-06-12 20:00:00', 'GEAC Scraper', 'Simon Kornblith', '/(?:Geac)?FETCH[\:\?].*[&:]next=html/(?:record\.html|geacnffull\.html)', NULL, 'var prefixRDF = ''http://www.w3.org/1999/02/22-rdf-syntax-ns#''; var prefixDC = ''http://purl.org/dc/elements/1.1/''; var prefixDCMI = ''http://purl.org/dc/dcmitype/''; @@ -1150,7 +1154,7 @@ utilities.loadDocument(newUri, browser, function(newBrowser) { wait();'); -REPLACE INTO "scrapers" VALUES('5287d20c-8a13-6004-4dcb-5bb2b66a9cc9', NULL, 20060603002000, 'SIRSI -2003 Scraper', 'Simon Kornblith', '/uhtbin/cgisirsi', +REPLACE INTO "scrapers" VALUES('5287d20c-8a13-6004-4dcb-5bb2b66a9cc9', '2006-06-12 20:00:00', 'SIRSI -2003 Scraper', 'Simon Kornblith', '/uhtbin/cgisirsi', 'var namespace = doc.documentElement.namespaceURI; var nsResolver = namespace ? function(prefix) { if (prefix == ''x'') return namespace; else return null; @@ -1233,7 +1237,7 @@ utilities.HTTPUtilities.doPost(newUri, ''marks=''+recNumber+''&shadow=NO&format= }) wait();'); -REPLACE INTO "scrapers" VALUES('0f9fc2fc-306e-5204-1117-25bca009dffc', NULL, 20060603002000, 'TLC/YouSeeMore Scraper', 'Simon Kornblith', 'TLCScripts/interpac\.dll\?.*LabelDisplay.*RecordNumber=[0-9]', NULL, +REPLACE INTO "scrapers" VALUES('0f9fc2fc-306e-5204-1117-25bca009dffc', '2006-06-12 20:00:00', 'TLC/YouSeeMore Scraper', 'Simon Kornblith', 'TLCScripts/interpac\.dll\?.*LabelDisplay.*RecordNumber=[0-9]', NULL, 'var prefixRDF = ''http://www.w3.org/1999/02/22-rdf-syntax-ns#''; var prefixDC = ''http://purl.org/dc/elements/1.1/''; var prefixDCMI = ''http://purl.org/dc/dcmitype/''; @@ -1311,7 +1315,7 @@ utilities.loadDocument(newUri, browser, function(newBrowser) { wait();'); -REPLACE INTO "scrapers" VALUES('c54d1932-73ce-dfd4-a943-109380e06574', NULL, 20060603002000, 'Project MUSE Scraper', 'Simon Kornblith', '^http://muse\.jhu\.edu/journals/[^/]+/[^/]+/[^/]+\.html$', NULL, 'var prefixRDF = ''http://www.w3.org/1999/02/22-rdf-syntax-ns#''; +REPLACE INTO "scrapers" VALUES('c54d1932-73ce-dfd4-a943-109380e06574', '2006-06-12 20:00:00', 'Project MUSE Scraper', 'Simon Kornblith', '^http://muse\.jhu\.edu/journals/[^/]+/[^/]+/[^/]+\.html$', NULL, 'var prefixRDF = ''http://www.w3.org/1999/02/22-rdf-syntax-ns#''; var prefixDC = ''http://purl.org/dc/elements/1.1/''; var prefixDCMI = ''http://purl.org/dc/dcmitype/''; var prefixDummy = ''http://chnm.gmu.edu/firefox-scholar/''; @@ -1376,7 +1380,7 @@ for(i in elmts) { model.addStatement(uri, prefixRDF + "type", prefixDummy + "journal", false);'); -REPLACE INTO "scrapers" VALUES('fcf41bed-0cbc-3704-85c7-8062a0068a7a', NULL, 20060603002000, 'PubMed Scraper', 'Simon Kornblith', '^http://www\.ncbi\.nlm\.nih\.gov/entrez/query\.fcgi\?(?:.*db=PubMed.*list_uids=[0-9]|.*list_uids=[0-9].*db=PubMed)', NULL, 'var prefixRDF = ''http://www.w3.org/1999/02/22-rdf-syntax-ns#''; +REPLACE INTO "scrapers" VALUES('fcf41bed-0cbc-3704-85c7-8062a0068a7a', '2006-06-12 20:00:00', 'PubMed Scraper', 'Simon Kornblith', '^http://www\.ncbi\.nlm\.nih\.gov/entrez/query\.fcgi\?(?:.*db=PubMed.*list_uids=[0-9]|.*list_uids=[0-9].*db=PubMed)', NULL, 'var prefixRDF = ''http://www.w3.org/1999/02/22-rdf-syntax-ns#''; var prefixDC = ''http://purl.org/dc/elements/1.1/''; var prefixDCMI = ''http://purl.org/dc/dcmitype/''; var prefixDummy = ''http://chnm.gmu.edu/firefox-scholar/'';