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 | * \author Christian Langanke
|
---|
28 | * \author Adrian Gschwend
|
---|
29 | * \date 2011
|
---|
30 | */
|
---|
31 |
|
---|
32 | class SparqlQuery {
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Version of the class
|
---|
36 | */
|
---|
37 | const version = '1.0.0';
|
---|
38 | /**
|
---|
39 | * Name of the fDebug context
|
---|
40 | */
|
---|
41 | const debugcontext = 'RDFA_SPARQLQUERY';
|
---|
42 |
|
---|
43 | private $debugger;
|
---|
44 | private $endpoint;
|
---|
45 | private $statement;
|
---|
46 |
|
---|
47 | // ---------------------------------------
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Creates a SPARQL qery class instance.
|
---|
51 | *
|
---|
52 | * \param endpoint instance of SparqlEndpoint holding the endpoint configuration
|
---|
53 | * \param statement SPARQL CONSTRUCT query statement to execute.
|
---|
54 | *
|
---|
55 | * \exception Exception The parameter endpoint is not of object rdfa::SparqlEndpoint
|
---|
56 | *
|
---|
57 | * \note To effectively execute the query, invoke the method
|
---|
58 | * - rdfa::SparqlQuery::run()
|
---|
59 | */
|
---|
60 | public function __construct( $endpoint, $statement) {
|
---|
61 |
|
---|
62 | // setup debugger
|
---|
63 | $this->debugger = \fDebug::getInstance();
|
---|
64 |
|
---|
65 | // check for required class
|
---|
66 | if ((!is_object( $endpoint)) ||
|
---|
67 | ('\\' . get_class( $endpoint) != '\\rdfa\\SparqlEndpoint'))
|
---|
68 | throw new \Exception( get_class().'::__construct: Parameter $endpoint is not object of class \rdfa\SparqlEndpoint.');
|
---|
69 |
|
---|
70 | // check for statement, may not be empty
|
---|
71 | if (strlen( $statement) < 1)
|
---|
72 | throw new \Exception( get_class().'::__construct: Parameter $statement is empty.');
|
---|
73 |
|
---|
74 | // initialize instance vars
|
---|
75 | $this->endpoint = $endpoint;
|
---|
76 | $this->statement = $statement;
|
---|
77 |
|
---|
78 | $debugmessage = "Creating SPARQL query object\n" .
|
---|
79 | "SPARQL query statement: \n" .
|
---|
80 | $this->getStatement();
|
---|
81 | $this->debugger->sendMessage( $debugmessage, self::debugcontext);
|
---|
82 |
|
---|
83 | } // public function __construct
|
---|
84 |
|
---|
85 | // ---------------------------------------
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Gets the SPARQL query statementof the object.
|
---|
89 | *
|
---|
90 | * \retval string SPARQL statement
|
---|
91 | */
|
---|
92 |
|
---|
93 | public function getStatement() {
|
---|
94 | return $this->statement;
|
---|
95 | }
|
---|
96 |
|
---|
97 | // ---------------------------------------
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Runs the SPARQL query of the object.
|
---|
101 | *
|
---|
102 | * \retval array indexed RDF data in internal format
|
---|
103 | */
|
---|
104 |
|
---|
105 | public function run() {
|
---|
106 | $store = \ARC2::getRemoteStore( $this->endpoint->getConfigurationValues());
|
---|
107 |
|
---|
108 | $debugmessage = "Executing SPARQL query\n" .
|
---|
109 | "SPARQL query statement: \n" .
|
---|
110 | $this->getStatement();
|
---|
111 | $this->debugger->sendMessage( $debugmessage, self::debugcontext);
|
---|
112 | return $store->query( $this->statement, 'raw');
|
---|
113 | }
|
---|
114 |
|
---|
115 | } // class SparqlEndpoint
|
---|
116 |
|
---|
117 | ?>
|
---|