queryParameter] = $query_string;
$tmp["query"] = $query_string;
if (isset($tmp["format"])) {
$this->format = $tmp["format"];
} else {
$this->format = SparqlOutputFormat::$DEFAULT_FORMAT;
}
$this->parameters = $tmp;
}
public function getParameters() {
return $this->parameters;
}
public function getParameter($param) {
return $this->parameters[$param];
}
public function getFormat() {
return $this->format;
}
/**
* TODO what does this do?
* Enter description here ...
*/
public function getDefaultEndpointRequestUrl() {
//CD hacking things
return $this->getEndpointRequestUrlForType("json");
}
public function getEndpointRequestUrlForType($type) {
// store endpoint parameters
$endpoint_parameters = array();
//CD hacking things
//$endpoint_parameters[smwfGetSparqlDatabase()->queryParameter] = $this->parameters[smwfGetSparqlDatabase()->queryParameter];
$endpoint_parameters["query"] = $this->parameters["query"];
$endpoint_parameters["format"] = $type;
//$endpoint_parameters["format"] = "json";
// build endpoint request url
if(isset($this->parameters["endpoint"])) {
$srvc_url = $this->parameters["endpoint"];
} else {
// pipe everything thorugh the proxy
// TODO: performance test - maybe better to go for the original endpoint instead of proxy,
// but also possible to do some caching via proxy and query rewriting, user control etc...much better
$srvc_url = SpecialPage::getTitleFor("SparqlExtension")->getFullURL();
}
$endpoint_request_url = SparqlUtil::build_restful_url($srvc_url, $endpoint_parameters);
wfDebugLog('SPARQL_LOG', "#=== endpoint url ===\n".$endpoint_request_url."\n");
return $endpoint_request_url;
}
/*
* get data into an array from csv or json endpoint response
*/
public function getData() {
global $sparqlProxyIP, $sparqlProxyPort, $wgUseSquid;
$data = false;
$type = false;
if (isset($endpoint_parameters["format"])) {
$type = $endpoint_parameters["format"];
} else {
$type = "json";
}
$url = $this->getEndpointRequestUrlForType($type);
//If we're using squid, purge the cache of the query url
if ( $wgUseSquid ) {
//TODO: this may not work yet, although it is the preferred method
SquidUpdate::purge( array($url) );
wfDebugLog('SPARQL_LOG', "#=== Purging Squid Cache of Previous Results of SPARQL Queries ===\n".$url."\n");
}
//fwrite(STDERR, "url is".$url."\n");
$query_output = SparqlUtil::readQueryOutput($url, $sparqlProxyIP, $sparqlProxyPort, $type);
wfDebugLog('SPARQL_LOG', "#=== output type is ===\n".$type."\n");
switch ($type) {
case "json":
$data = SparqlUtil::parse_json($query_output);
break;
default:
$data = SparqlUtil::parse_csv($query_output);
}
return $data;
}
/*
* create an json of parameters to be passed to google visualization
*/
public function createJSONFromParameters() {
// add width and height as default parameters
if (!in_array("width", array_keys($this->parameters))) {
$this->parameters["width"] = SparqlOutputFormat::$DEFAULT_WIDTH;
}
if (!in_array("height", array_keys($this->parameters))) {
$this->parameters["height"] = SparqlOutputFormat::$DEFAULT_HEIGHT;
}
$json = "{";
foreach ($this->parameters as $param => $value) {
// exclude endpoint configuration and query
if ($param == "format") continue;
$json .= $param . ": ";
if (is_numeric($value)) {
$json .= $value;
} elseif ((trim($value) == "false") || (trim($value) == "true")) {
$json .= $value;
} elseif ( ($this->startsWith($value, "[") && $this->endsWith($value, "]")) || ($this->startsWith($value, "{") && $this->endsWith($value, "}"))) {
$json .= htmlspecialchars($value);
} else {
$json .= "'".htmlspecialchars($value)."'";
}
$json .= ",";
}
//remove last comma since this causes Internet Explorer to break with an "Expected identifier, string or number" error
$json = substr($json,0,-1);
$json .= "}";
return $json;
}
function startsWith($Haystack, $Needle){
return strpos($Haystack, $Needle) === 0;
}
function endsWith($Haystack, $Needle) {
return strrpos($Haystack, $Needle) === strlen($Haystack)-strlen($Needle);
}
function encode($url){
$url = urlencode($url);
$pattern = array("%7B","%7D","%5B","%5D","%27");
$value = array("{","}","[","]","'");
$url = str_replace($pattern, $value, $url);
return $url;
}
}
/**
* output format to be used with template
* | format=template
* | template=myTemplate
* @author A.Chmieliauskas@tudeflt.nl
* @package SparqlExtension
*/
class SparqlTemplate extends SparqlOutputFormat {
public function __construct($args) {
parent::__construct($args);
}
public function getOutput() {
$args = $this->getParameters();
$data = $this->getData();
$link = (isset($args["link"]) && $args["link"] == "none") ? false : true;
if (isset($args["template"])) {
$template_name = $args["template"];
$output = "";
foreach($data as $row) {
$template = "";
foreach(array_keys($row) as $key) {
$value = SparqlLinker::createLink($link, $row[$key]);
$template .= "|".$key."=".$value;
}
$template = "{{".$template_name.$template."}}";
$output .= $template;
}
return array($output, 'noparse' => false, 'isHtml' => false);
}
}
}
/**
* default output format - a wiki table with a number of formating options
* | format=template
* | tablestyle=border-width:1px; border-spacing:0px; border-style:outset; border-color:black; border-collapse:collapse;
* | rowstyle=padding:2px;
* | oddrowstyle=background-color:Lavender
* | evenrowstyle=background-color:white
* | headerstyle=background-color:CornflowerBlue; color: white
* @author A.Chmieliauskas@tudeflt.nl
* @package SparqlExtension
*/
class SparqlTable extends SparqlOutputFormat {
public function __construct($args) {
parent::__construct($args);
}
public function getOutput() {
$args = $this->getParameters();
$data = $this->getData();
wfDebugLog('SPARQL_LOG', "#=== data ===\n".print_r($data,TRUE)."\n");
// get settings
$link = (isset($args["link"]) && $args["link"] == "none") ? false : true;
$tablestyle = isset($args["tablestyle"]) ? "style=\"".$args["tablestyle"]."\"" : "border=\"1\"";
$rowstyle = isset($args["rowstyle"]) ? $args["rowstyle"] : "";
$oddrowstyle = isset($args["oddrowstyle"]) ? $args["oddrowstyle"] : "";
$evenrowstyle = isset($args["evenrowstyle"]) ? $args["evenrowstyle"] : "";
$headerstyle = isset($args["headerstyle"]) ? $args["headerstyle"] : "";
$replaceWhat = isset($args["replacewhat"]) ? $args["replacewhat"] : "";
$replaceWith = isset($args["replacewith"]) ? $args["replacewith"] : "";
$decimals = isset($args["decimals"]) ? $args["decimals"] : 2;
$header = "{| class=\"wikitable sortable\" ".$tablestyle;
$new_line = "\n|-";
$headerstyle = ($headerstyle == "") ? "" : "style=\"".$headerstyle."\"";
$header .= $new_line." ".$headerstyle." \n!";
if (is_array($data[0])) {
foreach(array_keys($data[0]) as $key) {
$header .= $key . "!!";
}
}
$header = trim($header, "!");
$is_odd = true;
foreach($data as $row) {
$style = ($rowstyle == "" || $this->endsWith(trim($rowstyle), ";")) ? $rowstyle : $rowstyle.";";
$style .= ($is_odd) ? $oddrowstyle : $evenrowstyle;
$style = ($style == "") ? "" : "style=\"".$style."\"";
$header .= $new_line." ".$style." \n";
$header .= "| ";
//wfDebugLog('SPARQL_LOG', "#=== row ===\n".print_r($row,TRUE)."\n");
foreach($row as $cell) {
if ($replaceWhat != "") {
$cell = str_replace($replaceWhat, $replaceWith, $cell);
}
$value = SparqlLinker::createLink($link, $cell);
//This is a (slight) hack to prevent commas from appearing
//in year values. What we should probably do eventually is look at the
//xsd type specified for the literal and then format it appropriately
if (is_numeric($value) && strlen($value) > 4) {
$value = number_format($value, $decimals, ".", ",");
}
$header .= " " . $value . " ||";
}
$header = trim($header, "|");
$is_odd = !$is_odd;
}
$header .= "\n|}";
return array($header, 'noparse' => false, 'isHtml' => false);;
}
function endsWith( $str, $sub ) {
return ( substr( $str, strlen( $str ) - strlen( $sub ) ) === $sub );
}
}
/**
* returns result in the same line, if an array - returns comma separated
* | format=inline
* @author A.Chmieliauskas@tudeflt.nl
* @package SparqlExtension
*/
class SparqlInline extends SparqlOutputFormat {
public function __construct($args) {
parent::__construct($args);
}
public function getOutput() {
$args = $this->getParameters();
$data = $this->getData();
$link = (isset($args["link"]) && $args["link"] == "none") ? false : true;
$decimals = isset($args["decimals"]) ? $args["decimals"] : 2;
$output = "";
$first = true;
foreach($data as $row) {
foreach($row as $cell) {
$value = SparqlLinker::createLink($link, $cell);
if (is_numeric($value)) {
$value = number_format($value, $decimals, ".", ",");
}
if (!$first) {
$output .= ", " . $value;
} else {
$output = $value;
$first = false;
}
}
}
return array($output, 'noparse' => false, 'isHtml' => false);
}
}
/**
* Graph result format. Wraps the query output (csv) into format suitable for the graphviz plugin
* | format=graph
* | from=
* | to=
* | edge=
* | fromlabel=
* | tolabel=
* | edgelabel=
* | size=width,height (in pixels)
* @author C.B.Davis@tudeflt.nl
* @package SparqlExtension
*/
class SparqlGraph extends SparqlOutputFormat {
public function __construct($args) {
parent::__construct($args);
}
public function getOutput() {
$args = $this->getParameters();
$data = $this->getData();
//need unique id so can embed multiple graphs on the same page
$id = uniqid("graph");
//get parameters
$link = (isset($args["link"]) && $args["link"] == "none") ? false : true ;
$from = isset($args["from"]) ? $args["from"] : "";
$to = isset($args["to"]) ? $args["to"] : "";
$edge = isset($args["edge"]) ? $args["edge"] : "";
$size = isset($args["size"]) ? $args["size"] : "";
$edgewidth = isset($args["edgewidth"]) ? $args["edgewidth"] : "";
//allow the user to specify labels for the nodes that are different from the pagename
$fromlabel = isset($args["fromlabel"]) ? $args["fromlabel"] : "";
$tolabel = isset($args["tolabel"]) ? $args["tolabel"] : "";
$edgelabel = isset($args["edgelabel"]) ? $args["edgelabel"] : "";
$arr = array();
$header = "