public function testInsertWhenOffline()
 {
     global $EndPoint4store, $modeDebug, $prefixSparql, $prefixTurtle, $graph1, $graph2;
     $sp = new Endpoint($EndPoint4store, false, $modeDebug);
     $q = $prefixSparql . " \n\n\t\t\tINSERT DATA {  \n\t\t\t\tGRAPH <" . $graph1 . "> {    \n\t\t\t\ta:A b:Name \"Test2\" .   \n\t\t\t\ta:A b:Name \"Test3\" .   \n\t\t\t\ta:A b:Name \"Test4\" .  \n    \t\t}}";
     $res = $sp->query($q, 'raw');
     $err = $sp->getErrors();
     if ($err) {
         //print_r($err);
     }
     $this->assertTrue(count($err) > 0);
     $this->assertFalse($res);
 }
Example #2
0
            
PREFIX theme: <http://data.sparql.pro/w/Q> 
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>   
            
INSERT 
  { GRAPH <http://sparql.pro/lodTest> { 
       theme:2223 
         a skos:Concept ;
         rdfs:label "Physique"@fr ;
         rdfs:label "Physics"@en .
      }
}  
EOT;
$sp_ReadAndWrite->ResetErrors();
$rows = $sp_ReadAndWrite->query($q, 'raw');
$err = $sp_ReadAndWrite->getErrors();
if ($err) {
    print_r($err);
}
/*
 foreach($rows["result"]["variables"] as $variable){
        printf("%-20.20s",$variable);
        echo '|';
 }
 echo "\n";

 foreach ($rows["result"]["rows"] as $row){
        foreach($rows["result"]["variables"] as $variable){
                printf("%-20.20s",$row[$variable]);
        echo '|';
<?php

/** 
 * @version 0.4.1
 * @package Bourdercloud/PHP4store
 * @copyright (c) 2011 Bourdercloud.com
 * @author Karima Rafes <*****@*****.**>
 * @license http://www.opensource.org/licenses/mit-license.php
 */
/** 
 * @ignore
 */
//require_once('php4store/Endpoint.php');
require '../lib/php4store/Endpoint.php';
$endpoint = "http://dbpedia.org/";
// READ ONLY  ******************************************
$sp_readonly = new Endpoint($endpoint);
$q = "select *  where {?x ?y ?z.} LIMIT 5";
$rows = $sp_readonly->query($q, 'rows');
$err = $sp_readonly->getErrors();
if ($err) {
    print_r($err);
    throw new Exception(print_r($err, true));
}
var_dump($rows);
Example #4
0
 function __construct($parameters)
 {
     $endpoint = new \Endpoint($this->endpoint);
     /* Add namespace prefixes to query */
     $query = $this->_joinAndAffix($this->prefixes, "\n", "PREFIX ");
     /* Add select statement to query */
     $query .= "\nSELECT DISTINCT * ";
     /* Add where clauses to query */
     $wheres = array('?laur rdf:type nobel:Laureate', '?laur rdfs:label ?label', 'optional { ?laur dbpedia-owl:birthPlace ?birthPlace }', 'optional { ?laur owl:sameAs ?sameAs }', '?laur nobel:laureateAward ?award', '?laur nobel:nobelPrize ?prize', '?laur foaf:gender ?gender');
     /* Select by award */
     if (isset($parameters['award'])) {
         $award = $parameters['award'];
         if (in_array($award, $this->awards)) {
             $wheres[] = "?award nobel:category <http://data.nobelprize.org/resource/category/{$award}>";
         }
     }
     /* Select by gender */
     if (isset($parameters['gender'])) {
         $gender = $parameters['gender'];
         if (in_array($gender, array('male', 'female'))) {
             $wheres[] = "?laur foaf:gender '{$gender}'";
         }
     }
     $whereString = $this->_joinAndAffix($wheres, "\n", "\t", ' .');
     /* Select by region */
     if (isset($parameters['region'])) {
         global $baseUrl;
         $url = "{$baseUrl}/regions-api.php";
         $regions_json = file_get_contents($url);
         $regionMapping = json_decode($regions_json, true);
         // assure that region is in list
         if (in_array($parameters['region'], array_keys($regionMapping))) {
             $filters = array();
             $data = $regionMapping[$parameters['region']];
             foreach ($data as $str) {
                 $str = urlencode(str_replace(" ", "_", $str));
                 $filters[] = "?birthPlace = <http://data.nobelprize.org/resource/country/{$str}>";
             }
             $filter = implode(' || ', $filters);
             $whereString .= "\n\tFILTER( {$filter} )";
         }
     }
     $query .= "WHERE {\n{$whereString}\n}\n";
     $this->_query = $query;
     global $gCacheLocal;
     if ($gCacheLocal) {
         $cacheKey = 'LD-' . md5($query);
         $result = __c()->get($cacheKey);
         if ($result === null) {
             $result = $endpoint->query($query);
             global $gExternalLaureateDataCacheTime;
             __c()->set($cacheKey, $result, $gExternalLaureateDataCacheTime * 3600);
         }
     } else {
         $result = $endpoint->query($query);
     }
     $this->_result = $result["result"]["rows"];
 }
 public function execute($par = null)
 {
     global $wgOut, $wgScriptPath;
     $queryWithoutPrefix = isset($_REQUEST["queryWithoutPrefix"]) ? stripslashes($_REQUEST["queryWithoutPrefix"]) : "";
     $query = isset($_REQUEST["query"]) ? stripslashes($_REQUEST["query"]) : "";
     $endpoint = isset($_REQUEST["endpoint"]) ? stripslashes(trim($_REQUEST["endpoint"])) : "http://dbpedia.org/sparql";
     $output = isset($_REQUEST["output"]) ? stripslashes($_REQUEST["output"]) : "";
     $titleRequest = isset($_REQUEST["titleRequest"]) ? stripslashes($_REQUEST["titleRequest"]) : "";
     $description = isset($_REQUEST["description"]) ? stripslashes($_REQUEST["description"]) : "";
     // $wgOut->addHTML( isset($_REQUEST["query"])?stripslashes($_REQUEST["query"]):"Vide");
     // $wgOut->addHTML(print_r($_REQUEST,true));
     // $wgOut->addHTML(print_r($queryWithoutPrefix,true));
     // $wgOut->addHTML(print_r($query,true));
     // $wgOut->addHTML(print_r($output,true));
     if ($query != "" && $output == "save" && $titleRequest != "") {
         if ($this->pageExists($titleRequest)) {
             $wgOut->addHTML("<h2>" . wfMsg('specialsparqlquery_error_title_exists_yet') . "</h2> \n");
             $wgOut->addHTML();
         } else {
             $this->savePage($titleRequest, $this->template($query, $description));
             $wgOut->addWikiText(wfMsg('specialsparqlquery_your_query_saved_here') . "[[{$titleRequest}]]");
         }
     }
     if ($query == "" || $output == "save" || $output == "wiki") {
         $wgOut->addWikiText(wfMsg('specialsparqlquery_mainpage'));
         $wgOut->addHTML("<pre>" . htmlentities($this->prefix(), ENT_QUOTES, 'UTF-8') . "</pre>");
         $wgOut->addHTML("<form method='post' name='formQuery'>");
         $wgOut->addHTML("<input type='hidden' name='output' value='wiki'>");
         $wgOut->addHTML("<input type='hidden' name='prefix' value='" . htmlentities($this->prefix(), ENT_QUOTES, 'UTF-8') . " '>");
         $wgOut->addHTML("<input type='hidden' name='query' >");
         $wgOut->addHTML(wfMsg('specialsparqlquery_endpointsparql') . " : <input type='text' name='endpoint' size='50' value='" . $endpoint . " '>");
         $wgOut->addHTML("<textarea name='queryWithoutPrefix' cols='25' rows='15'>");
         $strQuery = $queryWithoutPrefix != "" ? $queryWithoutPrefix : $this->exampleSparql(0);
         $wgOut->addHTML($strQuery);
         $wgOut->addHTML("</textarea>");
         $wgOut->addHTML("<br/>");
         $wgOut->addHTML("<script language='javascript' type='text/javascript' src='" . $wgScriptPath . "/extensions/LinkedWiki/js/bordercloud.js'></script>");
         $wgOut->addHTML("<SCRIPT>\r\n<!-- \r\nfunction validAndSendQuery(){\r\n\tvar query = document.formQuery.prefix.value + ' ' + document.formQuery.queryWithoutPrefix.value;\r\n\tif(! document.formQuery.toXML.checked){\r\n\t\tdocument.formQuery.query.value= query;\r\n\t\tdocument.formQuery.submit();\r\n\t}else{\t\t\r\n\t\twindow.open('" . $endpoint . "?query=' + escape(query.replace('\\n','')));\r\n\t}\r\n}\r\nfunction validWithJS(){\r\n\tvar query = document.formQuery.prefix.value + ' ' + document.formQuery.queryWithoutPrefix.value;\r\n\r\n\tbcValidateSPARQL('" . $endpoint . "',query);\r\n}\r\n//-->\r\n</SCRIPT>");
         $wgOut->addHTML("<input type='button' value='" . wfMsg('specialsparqlquery_sendquery') . "'  onClick='validAndSendQuery();' />");
         $wgOut->addHTML("<input type='button' value='(R&D) Validation (js)'  onClick='validWithJS();' />");
         $wgOut->addHTML("   Xml : <input type='checkbox'  name='toXML' />");
         $wgOut->addHTML(" </form>");
         $wgOut->addHTML("<div  id='bc_div'></div>");
         $wgOut->addHTML("<div style='display: none;'>");
         $wgOut->addHTML("<img id='canvas-image-wait' src='" . $wgScriptPath . "/extensions/LinkedWiki/js/wait.png'></img>");
         $wgOut->addHTML("</div>");
         if ($queryWithoutPrefix != "") {
             $sp = new Endpoint($endpoint);
             $rs = $sp->query($query);
             $errs = $sp->getErrors();
             if ($errs) {
                 $wgOut->addHTML("<h1>ERROR(s)</h1>\n");
                 foreach ($errs as $err) {
                     if (is_array($err)) {
                         foreach ($err as $suberr) {
                             $wgOut->addHTML("<pre>{$suberr}.</pre> \n");
                         }
                     } else {
                         if (preg_match("/bcjeton/i", $err) && (preg_match("/insert/i", $strQuery) || preg_match("/delete/i", $strQuery))) {
                             $wgOut->addHTML("<pre>You have not the right to write in the dataset.</pre> \n");
                         } else {
                             $wgOut->addHTML("<pre>{$err}.</pre> \n");
                         }
                     }
                 }
             } else {
                 //						//efSparqlParserFunction_simple( $querySparqlWiki,$endpoint ,$classHeaders = '',$headers = '', $debug = null)
                 $arr = efSparqlParserFunction_simple($query, $endpoint, '', '', null);
                 $wgOut->addWikiText($arr[0]);
                 $wgOut->addWikiText("==" . wfMsg('specialsparqlquery_usethisquery') . "==");
                 $wgOut->addWikiText(wfMsg('specialsparqlquery_usethisquery_tutorial'));
                 $wgOut->addHTML("<pre>{{#sparql:" . htmlentities($query, ENT_QUOTES, 'UTF-8') . "\n|endpoint=" . htmlentities($endpoint, ENT_QUOTES, 'UTF-8') . "}}</pre>");
             }
             //$wgOut->addWikiText("==".wfMsg('specialsparqlquery_linkxml')."==");
             //$queryurl= $endpoint."?query=".urlencode( str_replace('\n','',$query));
             //$wgOut->addHTML("<a href='$queryurl'>".htmlentities( $queryurl, ENT_QUOTES, 'UTF-8')."</a>");
         }
     }
     $this->setHeaders();
 }
Example #6
0
echo "\nDelete :";
$q = " \n\t\t\tPREFIX a: <http://example.com/test/a/>\n\t\t\tPREFIX b: <http://example.com/test/b/> \n\t\t\tDELETE DATA {  \n\t\t\t\tGRAPH <" . $graph . "> {     \n\t\t\t\ta:A b:Name \"Test2\" . \n    \t\t}}";
$res = $sp_write->query($q, 'raw');
$err = $sp_write->getErrors();
if ($err) {
    print_r($err);
    throw new Exception(print_r($err, true));
}
var_dump($res);
// READ ONLY  ******************************************
//put argument false to write
$readonly = true;
$sp_readonly = new Endpoint('http://localhost:8080/', $readonly);
echo "\nPrint :";
$q = "select * where { GRAPH <" . $graph . "> {?x ?y ?z.}} ";
$rows = $sp_readonly->query($q, 'rows');
$err = $sp_readonly->getErrors();
if ($err) {
    print_r($err);
    throw new Exception(print_r($err, true));
}
var_dump($rows);
echo "\nASK  :";
$q = "PREFIX a: <http://example.com/test/a/>\n\t\t\tPREFIX b: <http://example.com/test/b/> \n\t\t\task where { GRAPH <" . $graph . "> {a:A b:Name \"Test3\" .}} ";
$res = $sp_readonly->query($q, 'raw');
$err = $sp_readonly->getErrors();
if ($err) {
    print_r($err);
    throw new Exception(print_r($err, true));
}
var_dump($res);
Example #7
0
function efSparqlParserFunction_tableCell($querySparqlWiki, $endpoint, $debug = null)
{
    $specialC = array("&#39;");
    $replaceC = array("'");
    $querySparql = str_replace($specialC, $replaceC, $querySparqlWiki);
    $str = "";
    $sp = new Endpoint($endpoint);
    $rs = $sp->query($querySparqlWiki);
    $errs = $sp->getErrors();
    if ($errs) {
        $strerr = "";
        foreach ($errs as $err) {
            $strerr .= "'''Error #sparql :" . $err . "'''<br/>";
        }
        return $strerr;
    }
    $lignegrise = false;
    $variables = $rs['result']['variables'];
    $str = "";
    foreach ($rs['result']['rows'] as $row) {
        $str .= "\n";
        $separateur = "| ";
        foreach ($variables as $variable) {
            if ($row[$variable . " type"] == "uri") {
                $str .= $separateur . efSparqlParserFunction_uri2Link($row[$variable]);
            } else {
                $str .= $separateur . $row[$variable];
            }
            $separateur = " || ";
        }
        $str .= "\n|- \n";
    }
    if ($debug != null && $debug == "YES") {
        $str .= "INPUT WIKI : " . $querySparqlWiki . "\n";
        $str .= "Query : " . $querySparql . "\n";
        $str .= print_r($rs, true);
        $str .= print_r($rs, true);
        return array("<pre>" . $str . "</pre>", 'noparse' => true, 'isHTML' => false);
    }
    return array($str, 'noparse' => false, 'isHTML' => false);
}
Example #8
0
 static function insert($turtle, $graph, $endpoint)
 {
     $sp_write = new Endpoint($endpoint, false);
     $q = "INSERT DATA {  \n\t\t\t\t\t\tGRAPH <" . $graph . "> {    \n\t\t\t\t\t\t{$turtle}\n\t\t    \t\t}}";
     $res = $sp_write->query($q, 'raw');
     $err = $sp_write->getErrors();
     if ($err) {
         throw new Exception(self::buildMessage($err));
     }
     if (!$res) {
         $msg = "Query insert new triples return: False without errors";
         throw new Exception($msg);
     }
 }