# HG changeset patch # User veltr # Date 1380301920 -7200 # Node ID a50e4ee7fcf227ca6f6ee514382598cdf6079e46 # Parent 5768acb94ca715db5882e17431b0e186e8cdc13d Preparatory modifications before server-side contribution validation diff -r 5768acb94ca7 -r a50e4ee7fcf2 src/jocondelab/static/jocondelab/js/front-notice.js --- a/src/jocondelab/static/jocondelab/js/front-notice.js Fri Sep 27 17:22:22 2013 +0200 +++ b/src/jocondelab/static/jocondelab/js/front-notice.js Fri Sep 27 19:12:00 2013 +0200 @@ -9,74 +9,51 @@ eventCache = {}, errorCache = {}; - var frSparqlTpl = _.template( + var sparqlTpl = _.template( 'select distinct * where { ?s rdfs:label "<%= label %>"@<%= lang %> . ' - + 'OPTIONAL { ?s rdfs:label ?l. ?s dbpedia-owl:abstract ?a. FILTER(lang(?a)=lang(?l)) }. ' - + 'OPTIONAL { ?s dbpedia-owl:thumbnail ?t }. OPTIONAL { ?s dbpedia-owl:wikiPageRedirects ?r }.' - + 'OPTIONAL { ?r rdfs:label ?lr. ?r dbpedia-owl:abstract ?ar FILTER(lang(?ar)=lang(?lr)) }. ' + + 'OPTIONAL { ?s dbpedia-owl:abstract ?a. FILTER(langMatches(lang(?a),"<%= lang %>")) }. ' + + 'OPTIONAL { ?s dbpedia-owl:thumbnail ?t }. ' + + 'OPTIONAL { ?s dbpedia-owl:wikiPageRedirects ?r }. ' + + 'OPTIONAL { ?r rdfs:label ?lr. FILTER(langMatches(lang(?lr),"<%= lang %>")) }. ' + 'OPTIONAL { ?r dbpedia-owl:thumbnail ?tr }. ' - + 'FILTER(!regex(?s, ":[^/]+$" ) && regex(?s, "^http://fr.dbpedia.org/resource/")) }' -// + 'OPTIONAL { ?s dbpedia-owl:wikiPageDisambiguates ?d }. OPTIONAL { ?d rdfs:label ?ld FILTER( langMatches( lang(?ld), "en" ) ) }. ' + + 'OPTIONAL { ?s dbpedia-owl:wikiPageDisambiguates ?d }. ' + + 'OPTIONAL { ?d rdfs:label ?ld FILTER( langMatches(lang(?ld), "<%= lang %>") ) }. ' + + 'FILTER(!regex(?s, ":[^/]+$" ) && regex(?s, "^http://[^/]+/resource/")) }' ); - - var otherSparqlTpl = _.template( - 'select distinct * where { ' - + '?s foaf:isPrimaryTopicOf <<%= wikipedia_url %>>. ' - + 'OPTIONAL { ?s dbpedia-owl:wikiPageRedirects ?r }. ' - + 'OPTIONAL { ?s rdfs:label ?l . ?s dbpedia-owl:abstract ?a . FILTER(lang(?l)=lang(?a)) }. ' - + 'OPTIONAL { ?s dbpedia-owl:thumbnail ?t }. ' - + 'OPTIONAL { ?r rdfs:label ?lr . ?r dbpedia-owl:abstract ?ar . FILTER(lang(?lr)=lang(?ar)) }. ' - + 'OPTIONAL { ?r dbpedia-owl:thumbnail ?tr }. ' - + '}' - ); - + function getDbpedia(label, callback, errorcb) { - function onBindingsLoaded(sparqlData, initialData) { + function onBindingsLoaded(sparqlData) { var bindings = sparqlData.results.bindings, - res = initialData || {}; - res.translations = _(sparqlData.results.bindings).map(function(b) { - var t = {} - t.dbpedia_uri = b.s.value; - if (b.r) { t.dbpedia_uri = b.r.value; } - if (b.a) { t.abstract = b.a.value; } - if (b.ar) { t.abstract = b.ar.value; } - if (b.t) { t.thumbnail = b.t.value; } - if (b.tr) { t.thumbnail = b.tr.value; } - if (b.l) { - t.label = b.l.value; - t.language = b.l["xml:lang"]; - } - if (b.lr) { - t.label = b.lr.value; - t.language = b.lr["xml:lang"]; - } - if (!t.label) { - t.label = label; - } - if (!t.language) { - t.language = lang; - } - return t; - }); - res.local = _(res.translations).find(function(t) { - return t.language === lang; - }) || {}; - res.label = res.local.label || label; - res.dbpedia_uri = res.translations[0].dbpedia_uri; - res.wikipedia_url = "http://" + lang + ".wikipedia.org/wiki/" + encodeURIComponent(res.label.replace(/ /g,'_')); + b = bindings[0], + res = { + label: label, + wikipedia_url: "http://" + lang + ".wikipedia.org/wiki/" + encodeURIComponent(label.replace(/ /g,'_')) + }; + + if (b.a) { res.abstract = b.a.value; } + if (b.ar) { res.abstract = b.ar.value; } + if (b.t) { res.thumbnail = b.t.value; } + if (b.tr) { res.thumbnail = b.tr.value; } + if (b.lr) { + res.disambiguations = []; + res.disambiguations.push(b.lr.value); + } + if (b.s) { res.dbpedia_uri = b.s.value; } + if (bindings.length > 1) { + res.disambiguations = res.disambiguations || []; + _(bindings).each(function(bb) { + if (bb.ld) { + res.disambiguations.push(bb.ld.value); + } + }); + } + dbpCache[label] = res; _(eventCache[label]).each(function(cb) { - cb(label, res); + cb(res); }); delete eventCache[label]; - if (res.label !== label) { - dbpCache[res.label] = res; - _(eventCache[res.label]).each(function(cb) { - cb(res.label, res); - }); - delete eventCache[res.label]; - } } function onError() { @@ -89,7 +66,7 @@ if (dbpCache[label]) { //console.log("Data already in cache", dbpCache[label]); - if (typeof callback === "function") { callback(label, dbpCache[label]); } + if (typeof callback === "function") { callback(dbpCache[label]); } return; } if (dbpCache[label] === false) { @@ -108,37 +85,36 @@ $.ajax({ url: urls.wikipedia.fr.dbpedia_sparql_url, data: { - query: frSparqlTpl( { label: label, lang: lang } ), + query: sparqlTpl( { label: label, lang: lang } ), format: "application/sparql-results+json" }, dataType: "json", success: function(data) { if (data.results.bindings.length) { //console.log("Data found in french DbPedia", data.results.bindings[0]); - onBindingsLoaded(data, { dbpedia_language: "fr" }); + onBindingsLoaded(data); } else { if (lang !== "fr" && urls.wikipedia[lang]) { - var wk_url = urls.wikipedia[lang].page_url + '/' + encodeURIComponent(label.replace(/ /g,'_')); $.ajax({ url: urls.wikipedia[lang].dbpedia_sparql_url, data: { - query: otherSparqlTpl({ wikipedia_url: wk_url }), + query: sparqlTpl({ label: label, lang: lang }), format: "application/sparql-results+json" }, dataType: "json", success: function(data) { if (data.results.bindings.length) { - //console.log("Data found in "+lang+" DbPedia", data.results.bindings[0]); - onBindingsLoaded(data, { dbpedia_language: lang }); + //console.log("Data found in", lang, "DbPedia", data.results.bindings[0]); + onBindingsLoaded(data); } else { - //console.log("Data not found in "+lang+" DbPedia"); + //console.log("Data not found in", lang, "DbPedia"); onError(); } }, error: onError }); } else { - //console.log("Data not found in french DbPedia and there is no known endpoint for "+lang); + //console.log("Data not found in french DbPedia and there is no known endpoint for ", lang); onError(); } } @@ -170,32 +146,82 @@ } } - function showOverlay(label, termdata) { + function showOverlay(termdata) { if (!$curitem) { return; } var o = $curitem.offset(); $overlay.css({ - top: o.top + $curitem.outerHeight(true), + top: o.top + $curitem.outerHeight(), left: o.left - $overlay.outerWidth(true), }).show().attr("data-dbpedia-uri", termdata.dbpedia_uri); - if (termdata.local.thumbnail) { - $overlayImg.attr("src",termdata.local.thumbnail).show(); + if (termdata.thumbnail) { + $overlayImg.attr("src",termdata.thumbnail).show(); } else { $overlayImg.hide(); } $h2.text(termdata.label); - if (label !== termdata.label) { - $h2.prepend($('').text(label + " → ")); + $abstract.text((termdata.abstract || "").replace(/^(.{240,260})\s.+$/,'$1…').substr(0,261)); + if (termdata.disambiguations) { + var ul = $('