4 * Principal API for the Linked Data Tools module.
6 * @copyright Copyright(c) 2012 Christopher Skene
7 * @license GPL v2 http://www.fsf.org/licensing/licenses/gpl.html
8 * @author Chris Skene chris at xtfer dot com
10 * Functions in this file should be considered the main API, whereas functions
11 * in the module file are those required for these to function effectively.
15 * Invoke a new linked data tool from a plugin.
18 * Name of the wrapper.
20 * @return \Drupal\ldt\Library\LibraryWrapperInterface
23 function ldt_tool($name) {
24 $wrappers = ldt_get_library_wrappers();
25 if (array_key_exists($name, $wrappers)) {
26 if (class_exists($wrappers[$name]['class'])) {
27 return new
$wrappers[$name]['class']();
33 * Load a linked data tool and load RDF data into it.
36 * Machine name of the RDF tool to use (e.g. 'arc2' or 'easyrdf')
38 * Data to load into the graph
40 * A URI to attach to the graph.
41 * @param string $format
44 * @return \Drupal\ldt\Library\LibraryWrapperInterface
45 * The graph object, or false.
47 function ldt_graph_data($tool, $data, $uri = NULL
, $format = 'rdf+xml') {
49 $tool = ldt_tool($tool);
51 $tool->setGraphData($data, $format);
59 catch (\Exception
$e) {
60 drupal_set_message('Failed to load graph (' .
$e->getMessage() .
')', 'error');
67 * Load a linked data tool and load RDF data into it from a URI.
69 * This will load cached data, if available.
73 * @param string $format
74 * (optional) An optional ldt data format. Defaults to rdfxml.
76 * (optional) Machine name of the RDF tool to use (defaults to 'easyrdf')
78 * @return \Drupal\ldt\Library\LibraryWrapperInterface|bool
79 * The graph object, or false.
81 function ldt_graph_uri($uri, $format = LDT_DEFAULT_FORMAT
, $tool = LDT_DEFAULT_LIBRARY
) {
83 $tool = ldt_tool($tool);
85 $data = ldt_fetch_rdf($uri);
87 // Data may already by loaded as an array...
88 if (is_array($data)) {
93 $tool->setGraphData($data, $format);
96 $tool->parseGraphData();
101 catch (\Exception
$e) {
102 drupal_set_message('Failed to load graph. More information can be found in the log.', 'error');
103 watchdog_exception('ldt', $e, '"%message", in %file:%line', array(
104 '%message' => $e->getMessage(),
105 '%file' => $e->getFile(),
106 '%line' => $e->getLine(),
114 * Helper to make a request for RDF data.
116 * This first checks the local cache, then it will request the data using
117 * Guzzle. If Guzzle is not available, it will fallback on
118 * drupal_http_request(). Guzzle itself is expected to be autoloaded, so it can
119 * be provided via http://drupal.org/project/guzzle or any other mechanism
120 * which supports autoloading.
123 * The URI to request data from
124 * @param string $format
125 * (optional) The data format, as defined by hook_ldt_data_formats(). Defaults
126 * to 'rdf+xml' (RDF XML).
127 * @param string $tool_type
128 * (optional) The tool to use. Defaults to EasyRdf
130 * (optional) If TRUE, the resource will be
133 * The data returned from the request..
135 function ldt_fetch_rdf($uri, $format = LDT_DEFAULT_FORMAT
, $tool_type = LDT_DEFAULT_LIBRARY
, $flush = FALSE
) {
138 // Check if the resource is cached first.
139 if ($flush == FALSE
) {
140 $tool = ldt_tool($tool_type);
141 $cached_resource = $tool->loadResource($uri);
142 if (!empty($cached_resource) && is_array($cached_resource)) {
143 return $cached_resource;
148 $format = ldt_data_format($format);
149 if (empty($format)) {
150 watchdog('ldt', 'Invalid data format');
155 // Use Guzzle if it exists.
156 if (class_exists('Guzzle\Http\Client')) {
158 $client = new \Guzzle\Http\
Client();
159 $request = $client->get($uri);
160 if (array_key_exists('accept', $format) && is_array($format['accept']) && !empty($format['accept'])) {
161 foreach ($format['accept'] as
$accept) {
162 $request->addHeader('Accept', $accept);
165 $response = $request->send();
166 if ($response->isSuccessful()) {
167 $data = $response->getBody(TRUE
);
170 // Probably an invalid request.
171 ldt_log_http_error($response->getStatusCode(), 'Request failed');
174 catch(\Guzzle\Http\Exception\BadResponseException
$e) {
175 $response = $e->getResponse();
176 if (isset($response) && is_object($response) && $response instanceof \Guzzle\Http\Message\Response
) {
177 $status = $response->getStatusCode();
178 $message = $response->getReasonPhrase();
182 $message = 'Unknown error (this may mean the endpoint is missing or not responding) ';
184 ldt_log_http_error($status, $message);
188 // Use drupal_http_request as a fallback.
190 'headers' => array(),
192 if (array_key_exists('accept', $format) && is_array($format['accept']) && !empty($format['accept'])) {
193 $accept_headers = array();
194 foreach ($format['accept'] as
$accept) {
195 $accept_headers[] = $accept;
197 $vars['headers']['Accept'] = $accept_headers;
199 $result = drupal_http_request($uri, $vars);
200 if (isset($result->data
)) {
202 $data = $result->data
;
210 * Extract resources specified as SKOS 'narrower'.
212 * @param \Drupal\ldt\Library\LibraryWrapperInterface $tool
213 * An EasyRdf Graph object.
216 * An array of resource objects.
218 function ldt_extract_skos_narrower($tool) {
220 $namespaces = $tool->getNamespaces();
221 if (!array_key_exists('skos', $namespaces)) {
222 $tool->addNamespace('skos', 'http://www.w3.org/2004/02/skos/core#');
225 $resources = $tool->extractResources('skos:narrower');
226 $results = $tool->fetchResources($resources);
232 * Extract resources specified as SKOS 'broader'.
235 * The URI to import from.
236 * @param string $tool_plugin
237 * Name of the plugin to use.
238 * @param string $format
242 * An array of resource objects.
244 function ldt_extract_skos_broader($uri, $tool_plugin = LDT_DEFAULT_LIBRARY
, $format = LDT_DEFAULT_FORMAT
) {
246 $tool = ldt_graph_uri($uri, LDT_DEFAULT_FORMAT
, $tool_plugin);
247 $namespaces = $tool->getNamespaces();
248 if (!array_key_exists('skos', $namespaces)) {
249 $tool->addNamespace('skos', 'http://www.w3.org/2004/02/skos/core#');
252 $resources = $tool->extractResources('skos:broader');
254 $tool = ldt_tool($tool_plugin);
255 $namespaces = $tool->getNamespaces();
256 if (!array_key_exists('skos', $namespaces)) {
257 $tool->addNamespace('skos', 'http://www.w3.org/2004/02/skos/core#');
260 $results = $tool->fetchResources($resources);
266 * Extract resources specified as Dublin Core Terms 'subject'.
269 * The URI to import from.
270 * @param string $tool_plugin
271 * Name of the plugin to use.
272 * @param string $format
276 * An array of resource objects.
278 function ldt_extract_dc_subjects($uri, $tool_plugin = LDT_DEFAULT_LIBRARY
, $format = LDT_DEFAULT_FORMAT
) {
280 $tool = ldt_graph_uri($uri, $format, $tool_plugin);
281 $namespaces = $tool->getNamespaces();
282 if (!array_key_exists('dcterms', $namespaces)) {
283 $tool->addNamespace('dcterms', 'http://purl.org/dc/terms/');
286 $resources = $tool->extractResources('dcterms:subject');
288 $tool = ldt_tool($tool_plugin);
289 $namespaces = $tool->getNamespaces();
290 if (!array_key_exists('dcterms', $namespaces)) {
291 $tool->addNamespace('dcterms', 'http://purl.org/dc/terms/');
294 $results = $tool->fetchResources($resources);