function wl_ajax_sparql() { header('Access-Control-Allow-Origin: *'); // Get the query slug. $slug = $_GET['slug']; // Get the output format: csv or geojson. $format = empty($_GET['format']) ? 'csv' : $_GET['format']; // TODO: define an output format. $output = 'csv'; $accept = 'text/csv'; // Get the query. $query = wl_sparql_replace_params(wl_sparql_get_query_by_slug($slug), $_GET); $dataset = wl_sparql_get_query_dataset_by_slug($slug); // Print out the query if the debug flag is set. if (isset($_GET['_debug']) && 1 == $_GET['_debug']) { wp_die($query); } if (empty($query)) { wp_die("Cannot find a SPARQL query [ slug :: {$slug} ]"); } $url = wl_configuration_get_query_select_url($output, $dataset) . urlencode($query); // Prepare the request. $args = array('method' => 'GET', 'headers' => array('Accept' => $accept)); // Send the request. Raise actions before and after the request is being sent. do_action('wl_sparql_pre_request', $url, $args, $query); // Send the request via caching if the module is available. $response = function_exists('wl_caching_remote_request') ? wl_caching_remote_request($url, $args) : wp_remote_post($url, $args); do_action('wl_sparql_post_request', $url, $args, $query, $response); // If an error has been raised, return the error. if (is_wp_error($response) || 200 !== (int) $response['response']['code']) { echo "wl_execute_sparql_query ================================\n"; if (is_wp_error($response)) { var_dump($response); } else { echo " request : \n"; var_dump($args); echo " response: \n"; var_dump($response); echo " response body: \n"; echo $response['body']; } echo "=======================================================\n"; return false; } // Output the data according to the chosen format. switch ($format) { case 'geojson': wl_csv_to_geojson($response['body']); break; case 'json': wl_csv_to_json($response['body']); break; default: echo $response['body']; } wp_die(); }
/** * Execute the provided query against the SPARQL SELECT Redlink end-point and return the response. * * @param string $query A SPARQL query. * @param string $accept The mime type for the response format (default = 'text/csv'). * * @return WP_Response|WP_Error A WP_Response instance in successful otherwise a WP_Error. */ function rl_sparql_select($query) { // Prepare the SPARQL statement by prepending the default namespaces. $sparql = rl_sparql_prefixes() . "\n" . $query; // Get the SPARQL SELECT URL. $url = wl_configuration_get_query_select_url('csv') . urlencode($sparql); // Prepare the request. $args = unserialize(WL_REDLINK_API_HTTP_OPTIONS); // Send the request. wl_write_log("SPARQL Select [ sparql :: {$sparql} ][ url :: {$url} ][ args :: " . var_export($args, true) . " ]"); return wp_remote_get($url, $args); }