<?php
/*************************************************************
* This script is developed by Arturs Sosins aka ar2rsawseen, http://webcodingeasy.com
* Fee free to distribute and modify code, but keep reference to its creator
*
* Sparql Query Builder class provides interface for generating
* SPARQL queries to use in RDF or SPARQL endpoints.
* It complies with W3C Recommendations http://www.w3.org/TR/rdf-sparql-query/
* This class reduces amount of text needs to be written comparing to simply writing SPARQL queries,
* increases code readability and helps you to create user friendly
* interfaces for SPARQL endpoints
*
* For more information, examples and online documentation visit:
* http://webcodingeasy.com/PHP-classes/Sparql-Query-Builder
**************************************************************/
//This is an example usage of SPARQL Query Builder
//Here you can find some examples from W3C Recommendation http://www.w3.org/TR/rdf-sparql-query/
//And specific class examples
//declaring class instance
include("./sparql.class.php");
$sparql = new sparql();
//outputs only end query
$sparql->show_query();
//outputs step by step debug information
//$sparql->debug();
/****************************************/
//example of sparql query from http://www.w3.org/TR/rdf-sparql-query/#MultipleMatches
/****************************************/
echo "<p style='font-weight:bold;'>Example of sparql query from <a href='http://www.w3.org/TR/rdf-sparql-query/#MultipleMatches' target='blank'>http://www.w3.org/TR/rdf-sparql-query/#MultipleMatches</a></p>";
//defining prefix
$sparql->prefix("foaf", "http://xmlns.com/foaf/0.1/");
//adding what to select
$sparql->select("?name");
$sparql->select("?mbox");
//creating patterns with full graph
$sparql->new_ptrn("?x foaf:name ?name .");
//creating empty pattern,
$pat = $sparql->new_ptrn("");
//adding node after pattern creation
//patterns are interpreted as string, so we may add multiple patterns as one pattern, but they need to be in one clause
// or add nodes one by one, which will form pattern string
$sparql->add_ptrn($pat, "?x");
$sparql->add_ptrn($pat, "foaf:mbox ?mbox .");
//no need to add patterns to where clause, all unused patterns will automatically get to where clause if union wasn't used
//if union was used, then new union will be created and all unused patterns will go there
//generating query
$query = $sparql->query();
/****************************************/
//example of sparql query from http://www.w3.org/TR/rdf-sparql-query/#constructGraph
/****************************************/
echo "<p style='font-weight:bold;'>Example of sparql query from <a href='http://www.w3.org/TR/rdf-sparql-query/#constructGraph' target='blank'>http://www.w3.org/TR/rdf-sparql-query/#constructGraph</a></p>";
//defining prefixes
$sparql->prefix("foaf", "http://xmlns.com/foaf/0.1/");
$sparql->prefix("org", "http://example.com/ns#");
//creating pattern for construct
$pat = $sparql->new_ptrn("?x foaf:name ?name");
//pattern for where clause
$sparql->new_ptrn("?x org:employeeName ?name");
//adding pattern to construct clause
$sparql->construct($pat);
//generating query
$query = $sparql->query();
/****************************************/
//example of sparql query from http://www.w3.org/TR/rdf-sparql-query/#restrictString
/****************************************/
echo "<p style='font-weight:bold;'>Example of sparql query from <a href='http://www.w3.org/TR/rdf-sparql-query/#restrictString' target='blank'>http://www.w3.org/TR/rdf-sparql-query/#restrictString</a></p>";
//defining prefix
$sparql->prefix("foaf", "http://xmlns.com/foaf/0.1/");
//specifying what to select
$sparql->select("?title");
//pattern for where clause
$sparql->new_ptrn("?x dc:title ?title");
$sparql->new_ptrn("FILTER regex(?title, 'web', 'i' )");
//generating query
$query = $sparql->query();
/****************************************/
//example of sparql query from http://www.w3.org/TR/rdf-sparql-query/#MultipleOptionals
/****************************************/
echo "<p style='font-weight:bold;'>Example of sparql query from <a href='http://www.w3.org/TR/rdf-sparql-query/#MultipleOptionals' target='blank'>http://www.w3.org/TR/rdf-sparql-query/#MultipleOptionals</a></p>";
//defining prefix
$sparql->prefix("foaf", "http://xmlns.com/foaf/0.1/");
//specifying what to select
$sparql->select("?name ?mbox ?hpage");
//pattern for where clause
$sparql->new_ptrn("?x foaf:name ?name");
//array for optional patterns
$optionals = array();
//creating patterns which wil be used in option clauses
$optionals[] = $sparql->new_ptrn("?x foaf:mbox ?mbox");
$optionals[] = $sparql->new_ptrn("?x foaf:homepage ?hpage");
//putting patterns in optional clauses
foreach($optionals as $optional)
{
$sparql->optional($optional);
}
//generating query
$query = $sparql->query();
/****************************************/
//example of sparql query from http://www.w3.org/TR/rdf-sparql-query/#alternatives
/****************************************/
echo "<p style='font-weight:bold;'>Example of sparql query from <a href='http://www.w3.org/TR/rdf-sparql-query/#alternatives' target='blank'>http://www.w3.org/TR/rdf-sparql-query/#alternatives</a></p>";
//defining prefixes
$sparql->prefix("dc10", "http://purl.org/dc/elements/1.0/");
$sparql->prefix("dc11", "http://purl.org/dc/elements/1.1/");
//specifying what to select
$sparql->select("?title");
//array fr unions
$unions = array();
$unions[] = $sparql->new_ptrn("?book dc10:title ?title .");
$unions[] = $sparql->new_ptrn("?book dc10:creator ?author .");
//other unused patterns will be automatically added in another union clause
$sparql->new_ptrn("?book dc11:title ?title .");
$sparql->new_ptrn("?book dc11:creator ?author .");
$un = "";
foreach($unions as $union)
{
//making sure that these patterns get in the same union clause
if($un !== "")
{
$sparql->union($union, $un);
}
else
{
$un = $sparql->union($union);
}
}
//generating query
$query = $sparql->query();
/****************************************/
//example of sparql query from http://www.w3.org/TR/rdf-sparql-query/#specDataset
/****************************************/
echo "<p style='font-weight:bold;'>Example of sparql query from <a href='http://www.w3.org/TR/rdf-sparql-query/#specDataset' target='blank'>http://www.w3.org/TR/rdf-sparql-query/#specDataset</a></p>";
//defining prefixes
$sparql->prefix("foaf", "http://xmlns.com/foaf/0.1/");
$sparql->prefix("dc", "http://purl.org/dc/elements/1.1/");
$sparql->select("?who ?g ?mbox");
//specifying sources
$sparql->from("http://example.org/dft.ttl");
$sparql->from_named("http://example.org/alice");
$sparql->from_named("http://example.org/bob");
//creating pattern
$pat = $sparql->new_ptrn("?book dc11:title ?title .");
//adding to whre clause, to put this pattern in the beggining of where clause
$sparql->where($pat);
//creating pattern for graph
$pat = $sparql->new_ptrn("?x foaf:mbox ?mbox");
//putting pattern into graph
$sparql->graph($pat, "?g");
//generating query
$query = $sparql->query();
/****************************************/
//example of sparql query from http://www.w3.org/TR/rdf-sparql-query/#restrictInQuery
/****************************************/
echo "<p style='font-weight:bold;'>Example of sparql query from <a href='http://www.w3.org/TR/rdf-sparql-query/#restrictInQuery' target='blank'>http://www.w3.org/TR/rdf-sparql-query/#restrictInQuery</a></p>";
//defining prefixes
$sparql->prefix("data", "http://example.org/foaf/");
$sparql->prefix("foaf", "http://xmlns.com/foaf/0.1/");
$sparql->prefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
$sparql->select("?mbox ?nick ?ppd");
//specifying sources
$sparql->from_named("http://example.org/foaf/aliceFoaf");
$sparql->from_named("http://example.org/foaf/bobFoaf");
//storage for first graph
$graphs["data:aliceFoaf"] = array();
//sotrage for second graph
$graphs["?ppd"] = array();
//creating needed patterns
$graphs["data:aliceFoaf"][] = $sparql->new_ptrn("?alice foaf:mbox <mailto:alice@work.example> ;");
$graphs["data:aliceFoaf"][] = $sparql->new_ptrn("foaf:knows ?whom .");
$graphs["data:aliceFoaf"][] = $sparql->new_ptrn("?whom foaf:mbox ?mbox ;");
$graphs["data:aliceFoaf"][] = $sparql->new_ptrn("rdfs:seeAlso ?ppd .");
$graphs["data:aliceFoaf"][] = $sparql->new_ptrn("?ppd a foaf:PersonalProfileDocument .");
$graphs["?ppd"][] = $sparql->new_ptrn("?w foaf:mbox ?mbox ;");
$graphs["?ppd"][] = $sparql->new_ptrn("foaf:nick ?nick");
//adding patterns to grpahs
foreach($graphs as $key => $graph)
{
foreach($graph as $value)
{
$sparql->graph($value, $key);
}
}
//generating query
$query = $sparql->query();
/****************************************/
//example of sparql query from http://www.w3.org/TR/rdf-sparql-query/#modOrderBy
/****************************************/
echo "<p style='font-weight:bold;'>Example of sparql query from <a href='http://www.w3.org/TR/rdf-sparql-query/#modOrderBy' target='blank'>http://www.w3.org/TR/rdf-sparql-query/#modOrderBy</a></p>";
//addinf prefixes
$sparql->prefix("", "http://example.org/ns#");
$sparql->prefix("foaf", "http://xmlns.com/foaf/0.1/");
$sparql->prefix("xsd", "http://www.w3.org/2001/XMLSchema#");
//selecting node
$sparql->select("?name");
//new pattern
$sparql->new_ptrn("?x foaf:name ?name ; :empId ?emp");
//order information, true specifies descending, false or nothing provided will be treated as ascending
$sparql->order("?emp", true);
//generating query
$query = $sparql->query();
/****************************************/
//example of sparql query from http://www.w3.org/TR/rdf-sparql-query/#modDistinct
/****************************************/
echo "<p style='font-weight:bold;'>Example of sparql query from <a href='http://www.w3.org/TR/rdf-sparql-query/#modDistinct' target='blank'>http://www.w3.org/TR/rdf-sparql-query/#modDistinct</a></p>";
//addinf prefixes
$sparql->prefix("foaf", "http://xmlns.com/foaf/0.1/");
//selecting node
$sparql->select("?name");
//specifying select type
$sparql->select_type("distinct");
//new pattern
$sparql->new_ptrn("?x foaf:name ?name");
//generating query
$query = $sparql->query();
/****************************************/
//example of sparql query from http://www.w3.org/TR/rdf-sparql-query/#modReduced
/****************************************/
echo "<p style='font-weight:bold;'>Example of sparql query from <a href='http://www.w3.org/TR/rdf-sparql-query/#modReduced' target='blank'>http://www.w3.org/TR/rdf-sparql-query/#modReduced</a></p>";
//addinf prefixes
$sparql->prefix("foaf", "http://xmlns.com/foaf/0.1/");
//selecting node
$sparql->select("?name");
//specifying select type
$sparql->select_type("reduced");
//new pattern
$sparql->new_ptrn("?x foaf:name ?name");
//generating query
$query = $sparql->query();
/****************************************/
//example of sparql query from http://www.w3.org/TR/rdf-sparql-query/#modOffset
/****************************************/
echo "<p style='font-weight:bold;'>Example of sparql query from <a href='http://www.w3.org/TR/rdf-sparql-query/#modOffset' target='blank'>http://www.w3.org/TR/rdf-sparql-query/#modOffset</a></p>";
//addinf prefixes
$sparql->prefix("foaf", "http://xmlns.com/foaf/0.1/");
//selecting node
$sparql->select("?name");
//new pattern
$sparql->new_ptrn("?x foaf:name ?name");
//order information, true specifies descending, false or nothing provided will be treated as ascending
$sparql->order("?emp");
//setting limit
$sparql->limit(10);
//setting offset
$sparql->offset(5);
//generating query
$query = $sparql->query();
/****************************************/
//example of sparql query from http://www.w3.org/TR/rdf-sparql-query/#ask
/****************************************/
echo "<p style='font-weight:bold;'>Example of sparql query from <a href='http://www.w3.org/TR/rdf-sparql-query/#ask' target='blank'>http://www.w3.org/TR/rdf-sparql-query/#ask</a></p>";
//addinf prefix
$sparql->prefix("foaf", "http://xmlns.com/foaf/0.1/");
//new pattern
$pat = $sparql->new_ptrn('?x foaf:name "Alice"');
//adding pattern to ask
$sparql->ask($pat);
//generating query
$query = $sparql->query();
/****************************************/
//example of sparql query from http://www.w3.org/TR/rdf-sparql-query/#explititURIs
/****************************************/
echo "<p style='font-weight:bold;'>Example of sparql query from <a href='http://www.w3.org/TR/rdf-sparql-query/#explititURIs' target='blank'>http://www.w3.org/TR/rdf-sparql-query/#explititURIs</a></p>";
$sparql->describe("<http://example.org/>");
//generating query
$query = $sparql->query();
/****************************************/
//example of sparql query from http://www.w3.org/TR/rdf-sparql-query/#identifyingResources
/****************************************/
echo "<p style='font-weight:bold;'>Example of sparql query from <a href='http://www.w3.org/TR/rdf-sparql-query/#identifyingResources' target='blank'>http://www.w3.org/TR/rdf-sparql-query/#identifyingResources</a></p>";
//addinf prefix
$sparql->prefix("foaf", "http://xmlns.com/foaf/0.1/");
//using describe type query
$sparql->describe("?x");
//new pattern
$sparql->new_ptrn("?x foaf:mbox <mailto:alice@org>");
//generating query
$query = $sparql->query();
/****************************************/
//example using union_h method
//which creates union pattern that will be used in every union clause
/****************************************/
echo "<p style='font-weight:bold;'>Example using union_h method which creates union pattern that will be used in every union clause</p>";
//addinf prefix
$sparql->prefix("table", "http://www.daml.org/2003/01/periodictable/PeriodicTable#");
//selecting nodes
$sparql->select("?symbol ?number");
//selectgin source
$sparql->from("http://www.daml.org/2003/01/periodictable/PeriodicTable#");
//union head patterns, which will be used in every union clause
$head1 = $sparql->new_ptrn("?element table:symbol ?symbol;");
$head2 = $sparql->new_ptrn("?element table:atomicNumber ?number;");
//patterns for different union clauses
$union1 = $sparql->new_ptrn("?element table:group table:group_17.");
$union2 = $sparql->new_ptrn("?element table:group table:group_18.");
//adding union head patterns
$sparql->union_h($head1);
$sparql->union_h($head2);
//adding union pattern, we need to add only one, other will be automatically included in new union clause
$sparql->union($union1);
//generating query
$query = $sparql->query();
?> |