Skip to content

Commit

Permalink
Merge pull request #5 from lingua-libre/hugolpz-patch-1
Browse files Browse the repository at this point in the history
Add support for multiple known sparql endpoints.
  • Loading branch information
hugolpz authored Aug 3, 2022
2 parents 3ce4cbc + fc0093c commit 951110a
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions modules/ext.queryViz.QueryViz.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,40 @@

return query;
};


// Queries the SPARQL endpoint via xhr
QueryViz.prototype.postQuery = function( query ) {
query = query.replace(/\u00A0/g, ' ').replace( '[AUTO_LANGUAGE]', mw.config.get( 'wgUserLanguage' ) );
console.log( query );
return $.post( sparqlEndpoint, { format: 'json', query: query } );

// According to query settings, switch sparql query endpoint
// If query includes "#defaultEndpoint:Wikidata" -> Wikidata, via xhr `get`
// If query includes "#defaultEndpoint:Commons" -> Commons, via xhr `get`
// Else : Lingualibre, via xhr `post`
var customEndpoint = sparqlEndpoint;
switch (true) {
case query.includes('#defaultEndpoint:Wikidata'):
console.log('SPARQL query service: Wikidata');
customEndpoint = 'https://query.wikidata.org/sparql';
break;
case query.includes('#defaultEndpoint:Commons'):
console.log('SPARQL query service: Commons');
customEndpoint = 'https://commons-query.wikimedia.org/sparql';
break;
case query.includes('#defaultEndpoint:Lingua'):
console.log('SPARQL query service: Lingualibre');
customEndpoint = 'https://lingualibre.org/bigdata/namespace/wdq/sparql';
default:
console.log('SPARQL query service: default, see LocalSettings.php');
customEndpoint = sparqlEndpoint;
break;
}
// Adapts to each service's xhr protocol : post vs get
if (customEndpoint.includes('lingualibre')) {
return $.post(customEndpoint, { format: 'json', query: query });
} else {
return $.get(customEndpoint, { format: 'json', query: query });
}
};

QueryViz.prototype.refresh = function() {
Expand Down

0 comments on commit 951110a

Please sign in to comment.