source: Dev/branches/rest-dojo-ui/jos-branch/server/rdfapi/sparql/SparqlClient.php @ 312

Last change on this file since 312 was 312, checked in by jkraaijeveld, 13 years ago
File size: 3.4 KB
Line 
1<?php
2
3// ----------------------------------------------------------------------------------
4// Class: SparqlEngine
5// ----------------------------------------------------------------------------------
6
7/**
8* Client for querying a sparql server.
9*
10* @version  $Id: SparqlClient.php 573 2008-05-26 09:04:29Z cyganiak $
11* @author   Tobias Gauß <tobias.gauss@web.de>
12* @license http://www.gnu.org/licenses/lgpl.html LGPL
13*
14* @package sparql
15*/
16
17class SparqlClient extends Object {
18
19        var $server;
20        var $output;   
21
22        /**
23        * Constructor of SparlClient.
24        *
25        * @param String  $server   server address.
26        */
27        function SparqlClient($server){
28                $this->server = $server;
29                $this->output = "array";
30        }
31
32
33        /**
34        * Sets the output format for a SELECT or ASK query. Possible formats are "xml" for
35        * Sparql Query Results XML Format (http://www.w3.org/TR/rdf-sparql-XMLres/) or array
36        * for the format described in our SparqlEngine.
37        *
38        * @param String  $format   the format.
39        */
40        function setOutputFormat($format){
41                if(strtolower($format)=="xml")
42                $this->output = "xml";
43                if(strtolower($format)=="array")
44                $this->output = "array";
45
46        }
47
48        /**
49        * Main function of SparqlClient.
50        *
51        * @param ClientQuery  $query   the ClientQuery object.
52        * @return mixed returns an array that contains the variables an their bindings or a MemModel
53        *
54        */
55        function query($query){
56                if(!is_a($query,"ClientQuery"))
57                die;//ErrorHandling
58
59                $url    = $this->_buildurl($query);
60                $result = $this->_http_get($url);
61                return $this->returnResult($result);
62
63        }
64
65
66        /**
67        * Helper function that builds the url.
68        *
69        * @param ClientQuery  $query   the ClientQuery Object.
70        */
71
72        function _buildurl($query){
73                $url = "";
74                $url = $this->server."?query=".urlencode($query->query);
75                foreach($query->default as $defaultg){
76                        $url = $url."&default-graph-uri=".$defaultg;
77                }
78                foreach($query->named as $namedg){
79                        $url = $url."&named-graph-uri=".$namedg;
80                }
81
82                return $url;
83
84        }
85
86        /**
87        * Returns the query result.
88        *
89        * @param String  $result  the result.
90        * @return mixed
91        */
92        function returnResult($result){
93
94                if(strpos($result,"<rdf:RDF")){
95                        include_once(RDFAPI_INCLUDE_DIR.PACKAGE_SYNTAX_RDF);
96                        $parser = new RdfParser();
97                        return $parser->generateModel(substr($result,strpos($result,"<rdf:RDF")));
98                }
99
100                if($this->output == "xml"){
101                        $pos = strpos($result,"<?xml");
102                        return substr($result,$pos);
103                }
104                if($this->output == "array"){
105                        //      $pos = strpos($buffer,"<?xml");
106                        return $this->parseResult($result);
107                }
108                return $result;
109        }
110
111
112        function parseResult($buffer){
113                include_once(RDFAPI_INCLUDE_DIR.PACKAGE_SYNTAX_SPARQLRES);
114                $parser = new SparqlResultParser();
115                return $parser->parse($buffer);
116        }
117
118
119        /**
120        * Executes the GET Request.
121        *
122        * @param String  $url  the url.
123        * @return String result.
124        */
125        function _http_get($url)
126        {
127                $url = parse_url($url);
128                $port = isset($url['port']) ? $url['port'] : 80;
129
130                $fp = fsockopen($url['host'], $port);
131
132                $replace = $url['path'];
133
134                fputs($fp, "GET ".$replace."?".$url['query']." HTTP/1.0\r\n");
135                fputs($fp, "Host: ". $url['host']."\r\n");
136                fputs($fp, "Accept: application/sparql-results+xml, application/rdf+xml\r\n");
137                fputs($fp, "Connection: close\r\n\r\n");
138
139                $buffer = "";
140                while ($tmp = fread($fp, 1024))
141                {
142                        $buffer .= $tmp;
143                }
144
145                return preg_replace("/^.*?(\r\n\r\n|\n\n)/s", "", $buffer);
146        }
147}
148
149
150
151?>
Note: See TracBrowser for help on using the repository browser.