1 | <?php
|
---|
2 |
|
---|
3 | /* RDFInt.php - RDF Interfaces for PHP
|
---|
4 | * Copyright 2011 netlabs.org
|
---|
5 | * Author: Christian Langanke, Adrian Gschwend
|
---|
6 | *
|
---|
7 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
8 | * you may not use this file except in compliance with the License.
|
---|
9 | * You may obtain a copy of the License at
|
---|
10 | *
|
---|
11 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
12 | *
|
---|
13 | * Unless required by applicable law or agreed to in writing, software
|
---|
14 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
16 | * See the License for the specific language governing permissions and
|
---|
17 | * limitations under the License.
|
---|
18 | */
|
---|
19 |
|
---|
20 | namespace rdfa;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * \class SparqlQuery
|
---|
24 | * \brief This class implements a class for executing SPARQL queries agains an endpoint
|
---|
25 | * by the Data::parse method.
|
---|
26 | * <br>This is a library specific extension to the RDF API and RDFa API.
|
---|
27 | * \note For further information see the section
|
---|
28 | * \htmllink{page_w3cspecs.html,W3C Specifications Overview and Library Compatibility}
|
---|
29 | * \author Christian Langanke
|
---|
30 | * \author Adrian Gschwend
|
---|
31 | * \date 2011
|
---|
32 | */
|
---|
33 |
|
---|
34 | class SparqlQuery {
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Version of the class
|
---|
38 | */
|
---|
39 | const version = '1.0.0';
|
---|
40 | /**
|
---|
41 | * Name of the fDebug context
|
---|
42 | */
|
---|
43 | const debugcontext = 'RDFA_SPARQLQUERY';
|
---|
44 |
|
---|
45 | private $debugger;
|
---|
46 | private $endpoint;
|
---|
47 | private $statement;
|
---|
48 |
|
---|
49 | // ---------------------------------------
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Creates a SPARQL qery class instance.
|
---|
53 | *
|
---|
54 | * \param endpoint instance of SparqlEndpoint holding the endpoint configuration
|
---|
55 | * \param statement SPARQL CONSTRUCT query statement to execute.
|
---|
56 | *
|
---|
57 | * \exception Exception The parameter endpoint is not of object rdfa::SparqlEndpoint
|
---|
58 | *
|
---|
59 | * \note To effectively execute the query, invoke the method
|
---|
60 | * - rdfa::SparqlQuery::run()
|
---|
61 | */
|
---|
62 | public function __construct( $endpoint, $statement) {
|
---|
63 |
|
---|
64 | // setup debugger
|
---|
65 | $this->debugger = \fDebug::getInstance();
|
---|
66 |
|
---|
67 | // check for required class
|
---|
68 | if ((!is_object( $endpoint)) ||
|
---|
69 | ('\\' . get_class( $endpoint) != '\\rdfa\\SparqlEndpoint'))
|
---|
70 | throw new \Exception( get_class().'::__construct: Parameter $endpoint is not object of class \rdfa\SparqlEndpoint.');
|
---|
71 |
|
---|
72 | // check for statement, may not be empty
|
---|
73 | if (strlen( $statement) < 1)
|
---|
74 | throw new \Exception( get_class().'::__construct: Parameter $statement is empty.');
|
---|
75 |
|
---|
76 | // initialize instance vars
|
---|
77 | $this->endpoint = $endpoint;
|
---|
78 | $this->statement = $statement;
|
---|
79 |
|
---|
80 | $debugmessage = "Creating SPARQL query object\n" .
|
---|
81 | "SPARQL query statement: \n" .
|
---|
82 | $this->getStatement();
|
---|
83 | $this->debugger->sendMessage( $debugmessage, self::debugcontext);
|
---|
84 |
|
---|
85 | } // public function __construct
|
---|
86 |
|
---|
87 | // ---------------------------------------
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Gets the SPARQL query statementof the object.
|
---|
91 | *
|
---|
92 | * \retval string SPARQL statement
|
---|
93 | */
|
---|
94 |
|
---|
95 | public function getStatement() {
|
---|
96 | return $this->statement;
|
---|
97 | }
|
---|
98 |
|
---|
99 | // ---------------------------------------
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Runs the SPARQL query of the object.
|
---|
103 | *
|
---|
104 | * \retval array indexed RDF data in internal format
|
---|
105 | */
|
---|
106 |
|
---|
107 | public function run() {
|
---|
108 | $store = \ARC2::getRemoteStore( $this->endpoint->getConfigurationValues());
|
---|
109 |
|
---|
110 | $debugmessage = "Executing SPARQL query\n" .
|
---|
111 | "SPARQL query statement: \n" .
|
---|
112 | $this->getStatement();
|
---|
113 | $this->debugger->sendMessage( $debugmessage, self::debugcontext);
|
---|
114 | return $store->query( $this->statement, 'raw');
|
---|
115 | }
|
---|
116 |
|
---|
117 | } // class SparqlEndpoint
|
---|
118 |
|
---|
119 | ?>
|
---|