Move two queries into templates.
This commit is contained in:
parent
d928c46c1d
commit
a5dfbd040a
50
app.py
50
app.py
|
@ -61,26 +61,6 @@ find_more_props = {
|
||||||
# 'P123': 'publisher', (only 1)
|
# 'P123': 'publisher', (only 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
find_more_query = '''
|
|
||||||
select ?item ?itemLabel ?image ?artist ?artistLabel ?title ?titleLang ?time ?timeprecision {
|
|
||||||
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
|
|
||||||
?item wdt:P31 wd:Q3305213 .
|
|
||||||
PARAMS
|
|
||||||
?item wdt:P18 ?image .
|
|
||||||
OPTIONAL {
|
|
||||||
?item p:P571/psv:P571 ?timenode .
|
|
||||||
?timenode wikibase:timeValue ?time.
|
|
||||||
?timenode wikibase:timePrecision ?timeprecision.
|
|
||||||
}
|
|
||||||
OPTIONAL {
|
|
||||||
?item wdt:P1476 ?title .
|
|
||||||
BIND(LANG(?title) as ?titleLang)
|
|
||||||
}
|
|
||||||
OPTIONAL { ?item wdt:P170 ?artist }
|
|
||||||
FILTER NOT EXISTS { ?item wdt:P180 ?depicts }
|
|
||||||
}
|
|
||||||
'''
|
|
||||||
|
|
||||||
find_more_basic_query = '''
|
find_more_basic_query = '''
|
||||||
select distinct ?item ?image {
|
select distinct ?item ?image {
|
||||||
VALUES ?value { LIST }
|
VALUES ?value { LIST }
|
||||||
|
@ -91,18 +71,6 @@ select distinct ?item ?image {
|
||||||
} limit LIMIT
|
} limit LIMIT
|
||||||
'''
|
'''
|
||||||
|
|
||||||
facet_query = '''
|
|
||||||
select ?property ?object ?objectLabel (count(*) as ?count) {
|
|
||||||
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
|
|
||||||
?item wdt:P31 wd:Q3305213 .
|
|
||||||
?item wdt:P18 ?image .
|
|
||||||
PARAMS
|
|
||||||
values ?property { PROPERTY_LIST }
|
|
||||||
?item ?property ?object .
|
|
||||||
FILTER NOT EXISTS { ?item wdt:P180 ?depicts }
|
|
||||||
} group by ?property ?propertyLabel ?object ?objectLabel
|
|
||||||
'''
|
|
||||||
|
|
||||||
property_query = '''
|
property_query = '''
|
||||||
select ?object ?objectLabel ?objectDescription (count(*) as ?count) {
|
select ?object ?objectLabel ?objectDescription (count(*) as ?count) {
|
||||||
?item wdt:P31 wd:Q3305213 .
|
?item wdt:P31 wd:Q3305213 .
|
||||||
|
@ -794,14 +762,15 @@ def find_more_page(property_id, item_id):
|
||||||
pid, qid = f'P{property_id}', f'Q{item_id}'
|
pid, qid = f'P{property_id}', f'Q{item_id}'
|
||||||
return redirect(url_for('browse_page', **{pid: qid}))
|
return redirect(url_for('browse_page', **{pid: qid}))
|
||||||
|
|
||||||
def get_facets(sparql_params, params):
|
def get_facets(params):
|
||||||
flat = '_'.join(f'{pid}={qid}' for pid, qid in params)
|
flat = '_'.join(f'{pid}={qid}' for pid, qid in params)
|
||||||
|
|
||||||
property_list = ' '.join(f'wdt:{pid}' for pid in find_more_props.keys()
|
properties = [pid for pid in find_more_props.keys()
|
||||||
if pid not in request.args)
|
if pid not in request.args]
|
||||||
|
|
||||||
q = (facet_query.replace('PARAMS', sparql_params)
|
q = render_template('query/facet.sparql',
|
||||||
.replace('PROPERTY_LIST', property_list))
|
params=params,
|
||||||
|
properties=properties)
|
||||||
|
|
||||||
bindings = wdqs.run_query_with_cache(q, flat + '_facets')
|
bindings = wdqs.run_query_with_cache(q, flat + '_facets')
|
||||||
|
|
||||||
|
@ -834,13 +803,10 @@ def browse_page():
|
||||||
|
|
||||||
item_labels = get_labels(qid for pid, qid in params)
|
item_labels = get_labels(qid for pid, qid in params)
|
||||||
|
|
||||||
sparql_params = ''.join(
|
q = render_template('query/find_more.sparql', params=params)
|
||||||
f'?item wdt:{pid} wd:{qid} .\n' for pid, qid in params)
|
|
||||||
|
|
||||||
q = find_more_query.replace('PARAMS', sparql_params)
|
|
||||||
|
|
||||||
bindings = wdqs.run_query_with_cache(q, flat)
|
bindings = wdqs.run_query_with_cache(q, flat)
|
||||||
facets = get_facets(sparql_params, params)
|
facets = get_facets(params)
|
||||||
|
|
||||||
page_size = 45
|
page_size = 45
|
||||||
|
|
||||||
|
|
11
templates/query/facet.sparql
Normal file
11
templates/query/facet.sparql
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
select ?property ?object ?objectLabel (count(*) as ?count) {
|
||||||
|
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
|
||||||
|
?item wdt:P31 wd:Q3305213 .
|
||||||
|
?item wdt:P18 ?image .
|
||||||
|
{% for pid, qid in params %}
|
||||||
|
?item wdt:{{pid}} wd:{{qid}} .
|
||||||
|
{% endfor %}
|
||||||
|
values ?property { {% for pid in properties %} wdt:{{pid}}{% endfor %} }
|
||||||
|
?item ?property ?object .
|
||||||
|
FILTER NOT EXISTS { ?item wdt:P180 ?depicts }
|
||||||
|
} group by ?property ?propertyLabel ?object ?objectLabel
|
19
templates/query/find_more.sparql
Normal file
19
templates/query/find_more.sparql
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
select ?item ?itemLabel ?image ?artist ?artistLabel ?title ?titleLang ?time ?timeprecision {
|
||||||
|
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
|
||||||
|
?item wdt:P31 wd:Q3305213 .
|
||||||
|
{% for pid, qid in params %}
|
||||||
|
?item wdt:{{pid}} wd:{{qid}} .
|
||||||
|
{% endfor %}
|
||||||
|
?item wdt:P18 ?image .
|
||||||
|
OPTIONAL {
|
||||||
|
?item p:P571/psv:P571 ?timenode .
|
||||||
|
?timenode wikibase:timeValue ?time.
|
||||||
|
?timenode wikibase:timePrecision ?timeprecision.
|
||||||
|
}
|
||||||
|
OPTIONAL {
|
||||||
|
?item wdt:P1476 ?title .
|
||||||
|
BIND(LANG(?title) as ?titleLang)
|
||||||
|
}
|
||||||
|
OPTIONAL { ?item wdt:P170 ?artist }
|
||||||
|
FILTER NOT EXISTS { ?item wdt:P180 ?depicts }
|
||||||
|
}
|
Loading…
Reference in a new issue