diff --git a/scholia/app/static/scholia.js b/scholia/app/static/scholia.js index 07fac0772..f560e4311 100644 --- a/scholia/app/static/scholia.js +++ b/scholia/app/static/scholia.js @@ -208,8 +208,7 @@ function addReloadButton(element, callback) { } } -function sparqlToResponse(sparql, doneCallback) { - var endpointUrl = "https://query.wikidata.org/bigdata/namespace/wdq/sparql"; +function sparqlToResponse2(endpointUrl, sparql, doneCallback) { var settings = { headers: { Accept: "application/sparql-results+json" }, data: { query: sparql }, @@ -218,6 +217,14 @@ function sparqlToResponse(sparql, doneCallback) { } +function sparqlToResponse(sparql, doneCallback) { + return sparqlToResponse2( + "https://query.wikidata.org/bigdata/namespace/wdq/sparql", + sparql, doneCallback + ); +} + + function sparqlDataToSimpleData(response) { // Convert long JSON data from from SPARQL endpoint to short form let data = response.results.bindings; @@ -235,10 +242,20 @@ function sparqlDataToSimpleData(response) { function sparqlToDataTablePost(sparql, element, filename, options = {}) { + sparqlToDataTablePost2( + "https://query.wikidata.org/sparql", + "https://query.wikidata.org/", + sparql, element, filename, options + ); +} + + +function sparqlToDataTablePost2(url, editURL, sparql, element, filename, options = {}) { // Options: paging= + if (!url) url = "https://query.wikidata.org/sparql"; + if (!editURL) editURL = "https://query.wikidata.org/"; var paging = (typeof options.paging === 'undefined') ? true : options.paging; var sDom = (typeof options.sDom === 'undefined') ? 'lfrtip' : options.sDom; - var url = "https://query.wikidata.org/sparql"; $(element).html("
"); @@ -274,7 +291,7 @@ function sparqlToDataTablePost(sparql, element, filename, options = {}) { }); $(element).append( - 'Wikidata Query Service' + 'Wikidata Query Service' + 'Redirects resolver: 'custom', events: { search: debounce((searchTerm, callback) => { - var url = "https://query.wikidata.org/sparql"; + var url = "{{ sparql_endpoint }}"; var settings = { data: { query: ` @@ -211,7 +211,7 @@

Redirects

multiple: true, ajax: { delay: 300, - url: "https://query.wikidata.org/sparql", + url: "{{ sparql_endpoint }}", data: function (params) { let search_author = params.term return { diff --git a/scholia/app/templates/base.html b/scholia/app/templates/base.html index 6c75a87ef..3007b14e7 100644 --- a/scholia/app/templates/base.html +++ b/scholia/app/templates/base.html @@ -2,7 +2,8 @@ {% macro sparql_to_table_post(panel, options={}) -%} // {{ panel }} table -sparqlToDataTablePost(`# tool: scholia +sparqlToDataTablePost2("{{ sparql_endpoint }}", "{{ sparql_editURL }}", +`# tool: scholia {% include aspect + '_' + panel + '.sparql' %} `, "#{{ panel }}-table", "{{ aspect }}_{{ panel }}.sparql", @@ -11,7 +12,8 @@ {% macro sparql_to_table(panel, options={}) -%} // {{ panel }} table -sparqlToDataTable(`# tool: scholia +sparqlToDataTable2("{{ sparql_endpoint }}", "{{ sparql_editURL }}", +`# tool: scholia {% include aspect + '_' + panel + '.sparql' %} `, "#{{ panel }}-table", "{{ aspect }}_{{ panel }}.sparql", @@ -20,7 +22,8 @@ {% macro sparql_to_iframe(panel) -%} // {{ panel }} iframe -sparqlToIframe(`# tool: scholia +sparqlToIframe2("{{ sparql_endpoint }}", "{{ sparql_editURL }}", +"{{ sparql_embedURL }}", `# tool: scholia {% include aspect + '_' + panel + '.sparql' %}`, "#{{ panel }}-iframe", "{{ aspect }}_{{ panel }}.sparql"); {%- endmacro %} @@ -475,7 +478,7 @@ curationElement.classList.remove("d-none"); // this query opens the Wikidata item as a different aspect - var endpointUrl = 'https://query.wikidata.org/sparql'; + var endpointUrl = '{{ sparql_endpoint }}'; if ("{{q2}}".length) { var query = ` SELECT DISTINCT ?aspect @@ -552,7 +555,7 @@ } }).then(function () { if ("{{q2}}".length) { - var endpointUrl = 'https://query.wikidata.org/sparql'; + var endpointUrl = '{{ sparql_endpoint }}'; var query = "SELECT DISTINCT ?aspect WHERE {" query += '{ [] wdt:P921 wd:{{ q }} . BIND("topic" AS ?aspect) } }'; diff --git a/scholia/app/views.py b/scholia/app/views.py index 58f470e84..6186bb79f 100644 --- a/scholia/app/views.py +++ b/scholia/app/views.py @@ -29,6 +29,7 @@ from ..utils import (remove_special_characters_url, sanitize_q, string_to_list, string_to_type) from ..wikipedia import q_to_bibliography_templates +from ..config import config class RegexConverter(BaseConverter): @@ -109,7 +110,11 @@ def index_statistics(): Rederende HTML for main statistics page. """ - return render_template('index-statistics.html') + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('index-statistics.html', sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route("/" + l_pattern) @@ -386,6 +391,9 @@ def show_author(q): Rendered HTML. """ + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') entities = wb_get_entities([q]) name = entity_to_name(entities[q]) if name: @@ -393,7 +401,8 @@ def show_author(q): else: first_initial, last_name = '', '' return render_template('author.html', q=q, first_initial=first_initial, - last_name=last_name) + last_name=last_name, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/author/' + q_pattern + '/latest-works/rss') @@ -428,7 +437,11 @@ def show_author_index(): Rendered index page for author view. """ - return render_template('author-index.html') + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('author-index.html', sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/author/random') @@ -462,7 +475,12 @@ def show_author_use(q1, q2): Rendered HTML for a specific author and use. """ - return render_template('author-use.html', q1=q1, q2=q2, q=q1) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('author-use.html', q1=q1, q2=q2, q=q1, + sparql_endpoint=ep, sparql_editURL=editurl, + sparql_embedURL=embedurl) @main.route('/authors/' + qs_pattern) @@ -489,7 +507,12 @@ def show_authors(qs): if len(qs) == 1: return redirect(url_for('app.show_author', q=qs[0]), code=301) else: - return render_template('authors.html', qs=qs) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('authors.html', qs=qs, sparql_endpoint=ep, + sparql_editURL=editurl, + sparql_embedURL=embedurl) @main.route('/award/' + q_pattern) @@ -507,7 +530,11 @@ def show_award(q): Rendered HTML. """ - return render_template('award.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('award.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/award/') @@ -520,7 +547,11 @@ def show_award_index(): Rendered index page for award view. """ - return render_template('award-index.html') + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('award-index.html', sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/cas/') @@ -632,7 +663,11 @@ def show_catalogue(q): Rendered HTML page. """ - return render_template('catalogue.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('catalogue.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/catalogue/') @@ -663,7 +698,11 @@ def show_dataset(q): Rendered HTML page. """ - return render_template('dataset.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('dataset.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/dataset/') @@ -694,7 +733,11 @@ def show_dataset_export(q): Rendered HTML. """ - return render_template('dataset-export.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('dataset-export.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/clinical-trial/') @@ -725,7 +768,11 @@ def show_clinical_trial(q): Rendered HTML for a specific clinical trial. """ - return render_template('clinical-trial.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('clinical-trial.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/countries/' + qs_pattern) @@ -744,7 +791,11 @@ def show_countries(qs): """ qs = Q_PATTERN.findall(qs) - return render_template('countries.html', qs=qs) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('countries.html', qs=qs, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/country/') @@ -775,7 +826,11 @@ def show_country(q): Rendered HTML for a specific country. """ - return render_template('country.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('country.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/country/' + q1_pattern + '/topic/' + q2_pattern) @@ -795,7 +850,12 @@ def show_country_topic(q1, q2): Rendered HTML for a specific country and topic. """ - return render_template('country-topic.html', q1=q1, q2=q2, q=q1) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('country-topic.html', q1=q1, q2=q2, q=q1, + sparql_endpoint=ep, sparql_editURL=editurl, + sparql_embedURL=embedurl) @main.route('/disease/' + q_pattern) @@ -813,7 +873,11 @@ def show_disease(q): Rendered HTML. """ - return render_template('disease.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('disease.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/disease/') @@ -844,7 +908,11 @@ def redirect_doi(doi): if len(qs) > 0: q = qs[0] return redirect(url_for('app.show_work', q=q), code=302) - return render_template('404-doi.html', doi=doi) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('404-doi.html', doi=doi, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/doi-prefix/') @@ -862,7 +930,11 @@ def redirect_doi_prefix(doi): if len(qs) > 0: q = qs[0] return redirect(url_for('app.show_publisher', q=q), code=302) - return render_template('404.html', doi=doi) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('404.html', doi=doi, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/event/' + q_pattern) @@ -880,7 +952,11 @@ def show_event(q): Rendered HTML. """ - return render_template('event.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('event.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/event/') @@ -911,7 +987,11 @@ def show_event_series(q): Rendered HTML. """ - return render_template('event-series.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('event-series.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/event-series/') @@ -1005,7 +1085,12 @@ def redirect_inchikey(inchikey): if len(qs) > 0: q = qs[0] return redirect(url_for('app.show_chemical', q=q), code=302) - return render_template('404-chemical.html', inchikey=inchikey) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('404-chemical.html', inchikey=inchikey, + sparql_endpoint=ep, sparql_editURL=editurl, + sparql_embedURL=embedurl) @main.route('/issn/') @@ -1040,7 +1125,12 @@ def show_language(q): Rendered HTML for a specific language. """ - return render_template('language.html', q=q, datetime=datetime) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('language.html', q=q, datetime=datetime, + sparql_endpoint=ep, sparql_editURL=editurl, + sparql_embedURL=embedurl) @main.route('/language') @@ -1084,7 +1174,11 @@ def show_lexeme(lexeme): Rendered HTML for a specific lexeme. """ - return render_template('lexeme.html', lexeme=lexeme) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('lexeme.html', lexeme=lexeme, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/license/' + q_pattern) @@ -1102,7 +1196,11 @@ def show_license(q): Rendered HTML for a specific license. """ - return render_template('license.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('license.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/license/') @@ -1146,7 +1244,11 @@ def show_location(q): Rendered HTML for a specific location. """ - return render_template('location.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('location.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/location/' + q1_pattern + '/topic/' + q2_pattern) @@ -1166,7 +1268,12 @@ def show_location_topic(q1, q2): Rendered HTML for a specific location and topic. """ - return render_template('location-topic.html', q1=q1, q2=q2, q=q1) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('location-topic.html', q1=q1, q2=q2, q=q1, + sparql_endpoint=ep, sparql_editURL=editurl, + sparql_embedURL=embedurl) @main.route('/mesh/') @@ -1391,7 +1498,11 @@ def show_ontology(q): Rendered HTML. """ - return render_template('ontology.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('ontology.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/organization/' + q_pattern) @@ -1409,7 +1520,11 @@ def show_organization(q): Rendered HTML. """ - return render_template('organization.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('organization.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/organization/') @@ -1464,7 +1579,12 @@ def show_organization_topic(q1, q2): Rendered HTML for a specific organization and topic. """ - return render_template('organization-topic.html', q1=q1, q2=q2, q=q1) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('organization-topic.html', q1=q1, q2=q2, q=q1, + sparql_endpoint=ep, sparql_editURL=editurl, + sparql_embedURL=embedurl) @main.route('/organization/' + q1_pattern + '/use/' + q2_pattern) @@ -1484,7 +1604,12 @@ def show_organization_use(q1, q2): Rendered HTML for a specific organization and use. """ - return render_template('organization-use.html', q1=q1, q2=q2, q=q1) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('organization-use.html', q1=q1, q2=q2, q=q1, + sparql_endpoint=ep, sparql_editURL=editurl, + sparql_embedURL=embedurl) @main.route('/organizations/' + qs_pattern) @@ -1503,7 +1628,12 @@ def show_organizations(qs): """ qs = Q_PATTERN.findall(qs) - return render_template('organizations.html', qs=qs) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('organizations.html', qs=qs, + sparql_endpoint=ep, sparql_editURL=editurl, + sparql_embedURL=embedurl) @main.route('/printer/' + q_pattern) @@ -1521,7 +1651,11 @@ def show_printer(q): Rendered HTML. """ - return render_template('printer.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('printer.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/printer/') @@ -1552,7 +1686,11 @@ def show_protein(q): Rendered HTML. """ - return render_template('protein.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('protein.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/protein/') @@ -1583,7 +1721,11 @@ def show_project(q): Rendered HTML. """ - return render_template('project.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('project.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/project/') @@ -1658,7 +1800,11 @@ def show_gene(q): Rendered HTML. """ - return render_template('gene.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('gene.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/gene/') @@ -1689,7 +1835,11 @@ def show_taxon(q): Rendered HTML. """ - return render_template('taxon.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('taxon.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/taxon/') @@ -1727,9 +1877,13 @@ def show_q_to_bibliography_templates(): wikitext = q_to_bibliography_templates(q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') return render_template('q-to-bibliography-templates.html', q=q, - wikitext=wikitext) + wikitext=wikitext, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/software/' + q_pattern) @@ -1747,7 +1901,11 @@ def show_software(q): Rendered HTML. """ - return render_template('software.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('software.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/software/') @@ -1778,7 +1936,11 @@ def show_software_export(q): Rendered HTML. """ - return render_template('software-export.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('software-export.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/text-to-topics', methods=['POST', 'GET']) @@ -1829,7 +1991,12 @@ def show_topic(q): Rendered HTML. """ - return render_template('topic.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('topic.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/topic/' + q_pattern + '/latest-works/rss') @@ -1884,7 +2051,12 @@ def show_topic_use(q1, q2): Rendered HTML for a specific topic and use. """ - return render_template('topic-use.html', q1=q1, q2=q2, q=q1) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('topic-use.html', q1=q1, q2=q2, q=q1, + sparql_endpoint=ep, sparql_editURL=editurl, + sparql_embedURL=embedurl) @main.route('/topics/' + qs_pattern) @@ -1911,7 +2083,12 @@ def show_topics(qs): if len(qs) == 1: return redirect(url_for('app.show_topic', q=qs[0]), code=301) else: - return render_template('topics.html', qs=qs) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('topics.html', qs=qs, sparql_endpoint=ep, + sparql_editURL=editurl, + sparql_embedURL=embedurl) @main.route('/podcast/' + q_pattern) @@ -1929,8 +2106,12 @@ def show_podcast(q): Rendered HTML. """ + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') return render_template( - 'podcast.html', q=q) + 'podcast.html', q=q, sparql_endpoint=ep, sparql_editURL=editurl, + sparql_embedURL=embedurl) @main.route('/podcast-season/' + q_pattern) @@ -1948,8 +2129,12 @@ def show_podcast_season(q): Rendered HTML. """ + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') return render_template( - 'podcast-season.html', q=q) + 'podcast-season.html', q=q, sparql_endpoint=ep, sparql_editURL=editurl, + sparql_embedURL=embedurl) @main.route('/podcast-episode/' + q_pattern) @@ -1967,8 +2152,12 @@ def show_podcast_episode(q): Rendered HTML. """ + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') return render_template( - 'podcast-episode.html', q=q) + 'podcast-episode.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/podcast/random') @@ -1995,7 +2184,11 @@ def show_podcast_in_language(q): Redirect """ - return render_template('podcast-language.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('podcast-language.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/podcast/') @@ -2028,10 +2221,13 @@ def show_chemical(q): """ entities = wb_get_entities([q]) smiles = entity_to_smiles(entities[q]) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') return render_template( 'chemical.html', - q=q, - smiles=smiles, + q=q, sparql_endpoint=ep, sparql_editURL=editurl, + sparql_embedURL=embedurl, smiles=smiles, third_parties_enabled=current_app.third_parties_enabled) @@ -2077,7 +2273,11 @@ def show_chemical_element(q): Rendered HTML. """ - return render_template('chemical-element.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('chemical-element.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/chemical-element/') @@ -2110,10 +2310,13 @@ def show_chemical_class(q): """ entities = wb_get_entities([q]) smiles = entity_to_smiles(entities[q]) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') return render_template( 'chemical-class.html', - q=q, - smiles=smiles, + q=q, sparql_endpoint=ep, sparql_editURL=editurl, + sparql_embedURL=embedurl, smiles=smiles, third_parties_enabled=current_app.third_parties_enabled) @@ -2165,7 +2368,11 @@ def show_venue(q): Rendered HTML page. """ - return render_template('venue.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('venue.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/venue/' + q_pattern + '/cito') @@ -2203,7 +2410,12 @@ def show_venue_use(q1, q2): Rendered HTML for a specific venue and use. """ - return render_template('venue-use.html', q1=q1, q2=q2, q=q1) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('venue-use.html', q1=q1, q2=q2, q=q1, + sparql_endpoint=ep, sparql_editURL=editurl, + sparql_embedURL=embedurl) @main.route('/work/' + q_pattern + '/cito') @@ -2241,7 +2453,12 @@ def show_work_cito_intention(q, q2): Rendered HTML. """ - return render_template('work-cito-intention.html', q=q, q2=q2) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('work-cito-intention.html', q=q, q2=q2, + sparql_endpoint=ep, sparql_editURL=editurl, + sparql_embedURL=embedurl) @main.route('/work/' + q_pattern + '/export') @@ -2259,7 +2476,11 @@ def show_work_export(q): Rendered HTML. """ - return render_template('work-export.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('work-export.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/work/random') @@ -2291,7 +2512,11 @@ def show_cito(q): Rendered HTML. """ - return render_template('cito.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('cito.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/cito/') @@ -2308,7 +2533,11 @@ def show_cito_index(): in Wikidata. """ - return render_template('cito-index.html') + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('cito-index.html', sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/venue/' + q_pattern + '/latest-works/rss') @@ -2362,7 +2591,11 @@ def show_venues(qs): """ qs = Q_PATTERN.findall(qs) - return render_template('venues.html', qs=qs) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('venues.html', qs=qs, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/series/' + q_pattern) @@ -2380,7 +2613,11 @@ def show_series(q): Rendered HTML for specific series. """ - return render_template('series.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('series.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/series/') @@ -2411,7 +2648,11 @@ def show_complex(q): Rendered HTML. """ - return render_template('complex.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('complex.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/complex/') @@ -2442,7 +2683,11 @@ def show_pathway(q): Rendered HTML. """ - return render_template('pathway.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('pathway.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/pathway/') @@ -2468,7 +2713,11 @@ def show_publisher(q): Rendered HTML page for specific publisher. """ - return render_template('publisher.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('publisher.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/publisher/') @@ -2534,7 +2783,11 @@ def show_sponsor(q): Rendered HTML page for specific sponsor. """ - return render_template('sponsor.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('sponsor.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/sponsor/') @@ -2587,7 +2840,11 @@ def show_use(q): Rendered HTML. """ - return render_template('use.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('use.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/use/') @@ -2627,7 +2884,12 @@ def show_uses(qs): if len(qs) == 1: return redirect(url_for('app.show_use', q=qs[0]), code=301) else: - return render_template('uses.html', qs=qs) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('uses.html', qs=qs, sparql_endpoint=ep, + sparql_editURL=editurl, + sparql_embedURL=embedurl) @main.route('/work/' + q_pattern) @@ -2649,7 +2911,11 @@ def show_work(q): dois = q_to_dois(q) except Exception: dois = [] - return render_template('work.html', q=q, dois=dois) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('work.html', q=q, dois=dois, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/work/') @@ -2681,7 +2947,11 @@ def show_works(qs): """ qs = Q_PATTERN.findall(qs) - return render_template('works.html', qs=qs) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('works.html', qs=qs, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/about') @@ -2725,7 +2995,11 @@ def show_wikiproject(q): Rendered HTML page for specific WikiProject. """ - return render_template('wikiproject.html', q=q) + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') + return render_template('wikiproject.html', q=q, sparql_endpoint=ep, + sparql_editURL=editurl, sparql_embedURL=embedurl) @main.route('/favicon.ico') @@ -2752,11 +3026,17 @@ def show_aspect_missing(aspect, q): Rendered HTML. """ + ep = config['query-server'].get('sparql_endpoint') + editurl = config['query-server'].get('sparql_editurl') + embedurl = config['query-server'].get('sparql_embedurl') try: return render_template('{aspect}-curation.html'.format(aspect=aspect), - q=q) + q=q, sparql_endpoint=ep, sparql_editURL=editurl, + sparql_embedURL=embedurl) except TemplateNotFound: - return render_template('q_curation.html', q=q, aspect=aspect) + return render_template('q_curation.html', q=q, aspect=aspect, + sparql_endpoint=ep, sparql_editURL=editurl, + sparql_embedURL=embedurl) def page_not_found(e): diff --git a/scholia/config.py b/scholia/config.py new file mode 100644 index 000000000..6cd8cd2e9 --- /dev/null +++ b/scholia/config.py @@ -0,0 +1,48 @@ +"""config. + +Usage: + scholia.config + +""" + +import configparser + +from io import StringIO + +from os.path import exists, expanduser + + +CONFIG_FILENAMES = [ + 'scholia.ini', + '~/etc/scholia.ini', + '~/scholia.ini'] + +DEFAULTS = """ +[query-server] +sparql_endpoint = https://query.wikidata.org/sparql +sparql_editurl = https://query.wikidata.org/# +sparql_embedurl = https://query.wikidata.org/embed.html# + +[requests] +user_agent = Scholia + +""" + + +config = configparser.ConfigParser() + +config.read_file(StringIO(DEFAULTS)) + +for filename in CONFIG_FILENAMES: + full_filename = expanduser(filename) + if exists(full_filename): + config.read(full_filename) + break + + +if __name__ == '__main__': + for section in config.sections(): + print(f"[{section}]") + for key in config[section]: + print(f"{key} = {config[section].get(key)}") + print() diff --git a/scholia/network.py b/scholia/network.py index 22a13acc3..ef78257f9 100644 --- a/scholia/network.py +++ b/scholia/network.py @@ -7,11 +7,13 @@ from collections import OrderedDict -from .query import SPARQL_ENDPOINT +from .config import config import requests +SPARQL_ENDPOINT = config['query-server'].get('sparql_endpoint') + EXAMPLE_SPARQL_QUERY = """ SELECT ?item1 ?item1Label ?item2 ?item2Label ?weight WITH { diff --git a/scholia/query.py b/scholia/query.py index 898c27dbb..8128cb8e6 100644 --- a/scholia/query.py +++ b/scholia/query.py @@ -59,9 +59,12 @@ from six import u -SPARQL_ENDPOINT = "https://query.wikidata.org/sparql" +from .config import config -USER_AGENT = 'Scholia' + +SPARQL_ENDPOINT = config['query-server'].get('sparql_endpoint') + +USER_AGENT = config['requests'].get('user_agent') HEADERS = {'User-Agent': USER_AGENT} @@ -800,7 +803,7 @@ def omim_to_qs(omimID): query = 'select ?disease where {{ ?disease wdt:P492 "{omimID}" }}'.format( omimID=escape_string(omimID)) - url = 'https://query.wikidata.org/sparql' + url = SPARQL_ENDPOINT params = {'query': query, 'format': 'json'} response = requests.get(url, params=params, headers=HEADERS) data = response.json() diff --git a/scholia/rss.py b/scholia/rss.py index 5dee65698..a605fa649 100644 --- a/scholia/rss.py +++ b/scholia/rss.py @@ -49,7 +49,9 @@ from six import u -from .query import SPARQL_ENDPOINT +from .config import config + +SPARQL_ENDPOINT = config['query-server'].get('sparql_endpoint') WORK_ITEM_RSS = u(""" @@ -358,7 +360,7 @@ def wb_get_venue_latest_works(q): 'type="application/rss+xml" />\n' query = VENUE_SPARQL_QUERY.format(q=q) - url = 'https://query.wikidata.org/bigdata/namespace/wdq/sparql' + url = SPARQL_ENDPOINT params = {'query': query, 'format': 'json'} response = requests.get(url, params=params) data = response.json() @@ -402,7 +404,7 @@ def wb_get_topic_latest_works(q): 'type="application/rss+xml" />\n' query = TOPIC_SPARQL_QUERY.format(q=q) - url = 'https://query.wikidata.org/bigdata/namespace/wdq/sparql' + url = SPARQL_ENDPOINT params = {'query': query, 'format': 'json'} response = requests.get(url, params=params) data = response.json() @@ -448,7 +450,7 @@ def wb_get_organization_latest_works(q): 'type="application/rss+xml" />\n' query = ORGANIZATION_SPARQL_QUERY.format(q=q) - url = 'https://query.wikidata.org/bigdata/namespace/wdq/sparql' + url = SPARQL_ENDPOINT params = {'query': query, 'format': 'json'} response = requests.get(url, params=params) data = response.json() @@ -494,7 +496,7 @@ def wb_get_sponsor_latest_works(q): 'type="application/rss+xml" />\n' query = SPONSOR_SPARQL_QUERY.format(q=q) - url = 'https://query.wikidata.org/bigdata/namespace/wdq/sparql' + url = SPARQL_ENDPOINT params = {'query': query, 'format': 'json'} response = requests.get(url, params=params) data = response.json() diff --git a/scholia/scrape/ceurws.py b/scholia/scrape/ceurws.py index cbd9fd188..1eeb3b0ec 100644 --- a/scholia/scrape/ceurws.py +++ b/scholia/scrape/ceurws.py @@ -29,12 +29,15 @@ import requests +from ..config import config from ..qs import paper_to_quickstatements, proceedings_to_quickstatements -from ..query import iso639_to_q, SPARQL_ENDPOINT as WDQS_URL +from ..query import iso639_to_q from ..utils import escape_string, pages_to_number_of_pages -USER_AGENT = 'Scholia' +SPARQL_ENDPOINT = config['query-server'].get('sparql_endpoint') + +USER_AGENT = config['requests'].get('user_agent') HEADERS = {'User-Agent': USER_AGENT} @@ -264,7 +267,7 @@ def paper_to_q(paper): query = SHORT_TITLED_PAPER_TO_Q_QUERY.format( url=url) - response = requests.get(WDQS_URL, + response = requests.get(SPARQL_ENDPOINT, params={'query': query, 'format': 'json'}, headers=HEADERS) if not response.ok: diff --git a/scholia/scrape/nips.py b/scholia/scrape/nips.py index 229cc9421..8de35f726 100644 --- a/scholia/scrape/nips.py +++ b/scholia/scrape/nips.py @@ -47,9 +47,14 @@ import requests +from ..config import config from ..qs import paper_to_quickstatements from ..utils import escape_string -from ..query import SPARQL_ENDPOINT as WDQS_URL + + +SPARQL_ENDPOINT = config['query-server'].get('sparql_endpoint') + +USER_AGENT = config['requests'].get('user_agent') PAPER_TO_Q_QUERY = u(""" SELECT ?paper WHERE {{ @@ -63,7 +68,6 @@ URL_BASE = "https://papers.nips.cc" -USER_AGENT = "Scholia" # Year should be the nominal year, - not the year of publication YEAR_TO_Q = { @@ -146,9 +150,9 @@ def paper_to_q(paper): label=title, title=title, url=paper['url'], full_text_url=paper['full_text_url']) - response = requests.get( - WDQS_URL, params={'query': query, 'format': 'json'}, - headers={'User-Agent': USER_AGENT}) + response = requests.get(SPARQL_ENDPOINT, + params={'query': query, 'format': 'json'}, + headers={'User-Agent': USER_AGENT}) if not response.ok: raise Exception("Wikidata API response error: {}".format( response.status_code)) diff --git a/scholia/scrape/ojs.py b/scholia/scrape/ojs.py index 4b5ea8944..e31911da0 100644 --- a/scholia/scrape/ojs.py +++ b/scholia/scrape/ojs.py @@ -31,12 +31,15 @@ import requests +from ..config import config from ..qs import paper_to_quickstatements -from ..query import iso639_to_q, issn_to_qs, SPARQL_ENDPOINT as WDQS_URL +from ..query import iso639_to_q, issn_to_qs from ..utils import escape_string, pages_to_number_of_pages -USER_AGENT = 'Scholia' +SPARQL_ENDPOINT = config['query-server'].get('sparql_endpoint') + +USER_AGENT = config['requests'].get('user_agent') HEADERS = {'User-Agent': USER_AGENT} @@ -173,7 +176,7 @@ def paper_to_q(paper): query = SHORT_TITLED_PAPER_TO_Q_QUERY.format( url=paper['url']) - response = requests.get(WDQS_URL, + response = requests.get(SPARQL_ENDPOINT, params={'query': query, 'format': 'json'}, headers=HEADERS) data = response.json()['results']['bindings'] diff --git a/scholia/text.py b/scholia/text.py index 659257be5..aecf4b5a4 100644 --- a/scholia/text.py +++ b/scholia/text.py @@ -30,7 +30,7 @@ from six.moves import cPickle as pickle -from .query import SPARQL_ENDPOINT +from .config import config import re @@ -39,6 +39,8 @@ import requests +SPARQL_ENDPOINT = config['query-server'].get('sparql_endpoint') + TOPIC_LABELS_SPARQL = """ SELECT ?topic ?topic_label WITH { diff --git a/scholia/wikipedia.py b/scholia/wikipedia.py index 6f77dff75..a0386bf1d 100644 --- a/scholia/wikipedia.py +++ b/scholia/wikipedia.py @@ -29,7 +29,10 @@ from six import b, u -from .query import SPARQL_ENDPOINT +from .config import config + + +SPARQL_ENDPOINT = config['query-server'].get('sparql_endpoint') BIBLIOGRAPHY_SPARQL_QUERY = """