rdf.module 24.2 KB
Newer Older
1
2
3
4
5
6
<?php
// $Id$

/**
 * @file
 * Enables semantically enriched output for Drupal sites.
7
8
9
10
11
12
 */

/**
 * @defgroup rdf RDFa API
 * @{
 * Functions to describe entities and bundles for RDFa.
13
 *
14
 * RDF module introduces RDFa to Drupal, which provides a set of XHTML
15
16
17
18
19
20
21
22
23
 * attributes to augment visual data with machine-readable hints.
 * @see http://www.w3.org/TR/xhtml-rdfa-primer/
 *
 * Modules can provide mappings of their bundles' data and metadata to RDFa
 * properties using the appropriate vocabularies. This module takes care of
 * injecting that data into variables available to themers in the .tpl files.
 * Drupal core themes ship with RDFa output enabled.
 *
 * Example mapping from node.module:
24
 * @code
25
 *   array(
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 *     'type' => 'node',
 *     'bundle' => RDF_DEFAULT_BUNDLE,
 *     'mapping' => array(
 *       'rdftype' => array('sioc:Item', 'foaf:Document'),
 *       'title' => array(
 *         'predicates' => array('dc:title'),
 *       ),
 *       'created' => array(
 *         'predicates' => array('dc:date', 'dc:created'),
 *         'datatype' => 'xsd:dateTime',
 *         'callback' => 'date_iso8601',
 *       ),
 *      'body' => array(
 *         'predicates' => array('content:encoded'),
 *       ),
 *       'uid' => array(
 *         'predicates' => array('sioc:has_creator'),
 *       ),
 *       'name' => array(
 *         'predicates' => array('foaf:name'),
 *       ),
 *     ),
 *   );
 * @endcode
50
51
52
 */

/**
53
54
55
56
 * RDF bundle flag: Default bundle.
 *
 * Defines an empty string as the name of the bundle to store default
 * RDF mappings of a type's properties (fields, etc.).
57
58
59
60
 */
define('RDF_DEFAULT_BUNDLE', '');

/**
61
 * Returns the mapping for attributes of a given type/bundle pair.
62
 *
63
64
65
66
 * @param $type
 *   An entity type.
 * @param $bundle
 *   (optional) A bundle name.
67
68
 *
 * @return
69
70
 *   The mapping corresponding to the requested type/bundle pair or an empty
 *   array.
71
 */
72
function rdf_mapping_load($type, $bundle = RDF_DEFAULT_BUNDLE) {
73
74
75
76
77
78
79
  // Retrieve the mapping from the entity info.
  $entity_info = entity_get_info($type);
  if (!empty($entity_info['bundles'][$bundle]['rdf_mapping'])) {
    return $entity_info['bundles'][$bundle]['rdf_mapping'];
  }
  else {
    return _rdf_get_default_mapping($type);
80
81
82
  }
}

83
/**
84
 * Returns the default RDF mapping for a given entity type.
85
 *
86
87
 * @param $type
 *   An entity type, e.g. 'node' or 'comment'.
88
89
 *
 * @return
90
 *   The RDF mapping or an empty array.
91
 */
92
93
function _rdf_get_default_mapping($type) {
  $default_mappings = &drupal_static(__FUNCTION__);
94

95
96
97
98
99
100
101
102
103
104
105
106
  if (!isset($default_mappings)) {
    // Get all modules implementing hook_rdf_mapping().
    $modules = module_implements('rdf_mapping');

    // Only consider the default entity mapping definitions.
    foreach ($modules as $module) {
      $mappings = module_invoke($module, 'rdf_mapping');
      foreach ($mappings as $mapping) {
        if ($mapping['bundle'] === RDF_DEFAULT_BUNDLE) {
          $default_mappings[$mapping['type']] = $mapping['mapping'];
        }
      }
107
108
    }
  }
109
110

  return isset($default_mappings[$type]) ? $default_mappings[$type] : array();
111
112
113
}

/**
114
 * Helper function to retrieve a RDF mapping from the database.
115
116
 *
 * @param $type
117
 *   The entity type the mapping refers to.
118
 * @param $bundle
119
120
121
 *   The bundle the mapping refers to.
 *
 * @return
122
 *   A RDF mapping structure or FALSE if no record was found.
123
 */
124
function _rdf_mapping_load($type, $bundle) {
125
126
127
128
129
130
131
132
133
  $mapping = db_select('rdf_mapping')
    ->fields(NULL, array('mapping'))
    ->condition('type', $type)
    ->condition('bundle', $bundle)
    ->execute()
    ->fetchField();

  if (!$mapping) {
    return array();
134
  }
135
  return unserialize($mapping);
136
137
138
139
140
141
142
143
144
145
146
147
}

/**
 * Saves an RDF mapping to the database.
 *
 * Takes a mapping structure returned by hook_rdf_mapping() implementations
 * and creates or updates a record mapping for each encountered
 * type, bundle pair. If available, adds default values for non-existent
 * mapping keys.
 *
 * @param $mapping
 *   The RDF mapping to save, as an array.
148
 *
149
150
151
 * @return
 *   Status flag indicating the outcome of the operation.
 */
152
function rdf_mapping_save(&$mapping) {
153
  // Adds default values for non-existent keys.
154
155
156
157
158
159
160
161
162
163
164
  $mapping['mapping'] += _rdf_get_default_mapping($mapping['type']);

  $status = db_merge('rdf_mapping')
    ->key(array(
      'type' => $mapping['type'],
      'bundle' => $mapping['bundle'],
    ))
    ->fields(array(
      'mapping' => serialize($mapping['mapping']),
    ))
    ->execute();
165
166
167

  cache_clear_all('entity_info', 'cache');
  drupal_static_reset('entity_get_info');
168
169

  return $status;
170
171
}

172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
 * Deletes the mapping for the given pair of type and bundle from the database.
 *
 * @param $type
 *   The entity type the mapping refers to.
 * @param $bundle
 *   The bundle the mapping refers to.
 *
 * @return
 *   Return boolean TRUE if mapping deleted, FALSE if not.
 */
function rdf_mapping_delete($type, $bundle) {
  $num_rows = db_delete('rdf_mapping')
    ->condition('type', $type)
    ->condition('bundle', $bundle)
    ->execute();

  return (bool) ($num_rows > 0);
}

/**
 * Builds an array of RDFa attributes for a given mapping.
 *
 * @param $mapping
 *   An array containing a mandatory 'predicates' key and optional 'datatype',
 *   'callback' and 'type' keys. For example:
 *   @code
 *     array(
 *       'predicates' => array('dc:created'),
 *         'datatype' => 'xsd:dateTime',
 *         'callback' => 'date_iso8601',
 *       ),
 *     );
 *   @endcode
 * @param $data
 *   A value that needs to be converted by the provided callback function.
 *
 * @return
 *   An array containing RDFa attributes suitable for drupal_attributes().
 */
function rdf_rdfa_attributes($mapping, $data = NULL) {
  // The type of mapping defaults to 'property'.
  $type = isset($mapping['type']) ? $mapping['type'] : 'property';

  switch ($type) {
    // The mapping expresses the relationship between two resources.
    case 'rel':
    case 'rev':
      $attributes[$type] = $mapping['predicates'];
      break;

    // The mapping expresses the relationship between a resource and some
    // literal text.
    case 'property':
      $attributes['property'] = $mapping['predicates'];
      if (isset($mapping['callback']) && isset($data)) {
        $callback = $mapping['callback'];
        if (function_exists($callback)) {
          $attributes['content'] = $callback($data);
        }
        if (isset($mapping['datatype'])) {
          $attributes['datatype'] = $mapping['datatype'];
        }
      }
      break;
  }

  return $attributes;
}

/**
 * @} End of "defgroup rdf".
 */

246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
 * Implements hook_modules_installed().
 *
 * Checks if the installed modules have any RDF mapping definitions to declare
 * and stores them in the rdf_mapping table.
 *
 * While both default entity mappings and specific bundle mappings can be
 * defined in hook_rdf_mapping(), we do not want to save the default entity
 * mappings in the database because users are not expected to alter these.
 * Instead they should alter specific bundle mappings which are stored in the
 * database so that they can be altered via the RDF CRUD mapping API.
 */
function rdf_modules_installed($modules) {
  // We need to clear the caches of entity_info as this is not done right
  // during the tests. see http://drupal.org/node/594234
  cache_clear_all('entity_info', 'cache');
  drupal_static_reset('entity_get_info');

  foreach ($modules as $module) {
265
266
267
    $function = $module . '_rdf_mapping';
    if (function_exists($function)) {
      foreach ($function() as $mapping) {
268
        // Only the bundle mappings are saved in the database.
269
270
        if ($mapping['bundle'] !== RDF_DEFAULT_BUNDLE) {
          rdf_mapping_save($mapping);
271
272
273
274
275
276
277
278
279
280
        }
      }
    }
  }
}

/**
 * Implements hook_modules_uninstalled().
 */
function rdf_modules_uninstalled($modules) {
281
  // @todo Remove RDF mappings of uninstalled modules.
282
283
284
285
286
287
288
289
290
291
292
293
}

/**
 * Implements hook_entity_info_alter().
 *
 * Adds the proper RDF mapping to each entity type, bundle pair.
 */
function rdf_entity_info_alter(&$entity_info) {
  // Loop through each entity type and its bundles.
  foreach ($entity_info as $entity_type => $entity_type_info) {
    if (isset($entity_type_info['bundles'])) {
      foreach ($entity_type_info['bundles'] as $bundle => $bundle_info) {
294
        if ($mapping = _rdf_mapping_load($entity_type, $bundle)) {
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
          $entity_info[$entity_type]['bundles'][$bundle]['rdf_mapping'] = $mapping;
        }
        else {
          // If no mapping was found in the database, assign the default RDF
          // mapping for this entity type.
          $entity_info[$entity_type]['bundles'][$bundle]['rdf_mapping'] = _rdf_get_default_mapping($entity_type);
        }
      }
    }
  }
}

/**
 * Implements hook_entity_load().
 */
function rdf_entity_load($entities, $type) {
  foreach ($entities as $entity) {
    // Extracts the bundle of the entity being loaded.
313
    list($id, $vid, $bundle) = entity_extract_ids($type, $entity);
314
    $entity->rdf_mapping = rdf_mapping_load($type, $bundle);
315
316
317
  }
}

318
319
320
321
322
323
/**
 * Implements hook_theme().
 */
function rdf_theme() {
  return array(
    'rdf_template_variable_wrapper' => array(
324
      'variables' => array('content' => NULL, 'attributes' => array(), 'context' => array(), 'inline' => TRUE),
325
326
    ),
    'rdf_metadata' => array(
327
      'variables' => array('metadata' => array()),
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
    ),
  );
}

/**
 * Template process function for adding extra tags to hold RDFa attributes.
 *
 * Since template files already have built-in support for $attributes,
 * $title_attributes, and $content_attributes, and field templates have support
 * for $item_attributes, we try to leverage those as much as possible. However,
 * in some cases additional attributes are needed not covered by these. We deal
 * with those here.
 */
function rdf_process(&$variables, $hook) {
  // Handles attributes needed for content not covered by title, content,
  // and field items. Does this by adjusting the variable sent to the template
  // so that the template doesn't have to worry about it.
  // @see theme_rdf_template_variable_wrapper()
  if (!empty($variables['rdf_template_variable_attributes_array'])) {
    foreach ($variables['rdf_template_variable_attributes_array'] as $variable_name => $attributes) {
      $context = array(
        'hook' => $hook,
        'variable_name' => $variable_name,
        'variables' => $variables,
      );
      $variables[$variable_name] = theme('rdf_template_variable_wrapper', array('content' => $variables[$variable_name], 'attributes' => $attributes, 'context' => $context));
    }
  }
  // Handles additional attributes about a template entity that for RDF parsing
  // reasons, can't be placed into that template's $attributes variable. This
  // is "meta" information that is related to particular content, so render it
  // close to that content.
  if (!empty($variables['rdf_metadata_attributes_array'])) {
    if (!isset($variables['content']['#prefix'])) {
      $variables['content']['#prefix'] = '';
    }
    $variables['content']['#prefix'] = theme('rdf_metadata', array('metadata' => $variables['rdf_metadata_attributes_array'])) . $variables['content']['#prefix'];
  }
}

368
369
370
371
/**
 * Implements MODULE_preprocess_HOOK().
 */
function rdf_preprocess_node(&$variables) {
372
  // Adds RDFa markup to the node container. The about attribute specifies the
373
374
375
376
377
  // URI of the resource described within the HTML element, while the typeof
  // attribute indicates its RDF type (foaf:Document, or sioc:User, etc.).
  $variables['attributes_array']['about'] = empty($variables['node_url']) ? NULL: $variables['node_url'];
  $variables['attributes_array']['typeof'] = empty($variables['node']->rdf_mapping['rdftype']) ? NULL : $variables['node']->rdf_mapping['rdftype'];

378
  // Adds RDFa markup to the title of the node. Because the RDFa markup is added
379
380
381
382
383
384
385
386
  // to the h2 tag which might contain HTML code, we specify an empty datatype
  // to ensure the value of the title read by the RDFa parsers is a literal.
  $variables['title_attributes_array']['property'] = empty($variables['node']->rdf_mapping['title']['predicates']) ? NULL : $variables['node']->rdf_mapping['title']['predicates'];
  $variables['title_attributes_array']['datatype'] = '';

  // In full node mode, the title is not displayed by node.tpl.php so it is
  // added in the head tag of the HTML page.
  if ($variables['page']) {
387
388
389
390
391
392
393
394
395
396
397
    $element = array(
      '#tag' => 'meta',
      '#attributes' => array(
        'content' => $variables['node_title'],
        'about' => $variables['node_url'],
      ),
    );
    if (!empty($variables['node']->rdf_mapping['title']['predicates'])) {
      $element['#attributes']['property'] = $variables['node']->rdf_mapping['title']['predicates'];
    }
    drupal_add_html_head($element, 'rdf_node');
398
399
  }

400
  // Adds RDFa markup for the date.
401
  if (!empty($variables['rdf_mapping']['created'])) {
402
    $date_attributes_array = rdf_rdfa_attributes($variables['rdf_mapping']['created'], $variables['created']);
403
    $variables['rdf_template_variable_attributes_array']['date'] = $date_attributes_array;
404
405
406
407
408
409
410
411
412
  }
}

/**
 * Implements MODULE_preprocess_HOOK().
 */
function rdf_preprocess_field(&$variables) {
  $entity_type = $variables['element']['#object_type'];
  $instance = $variables['instance'];
413
  $mapping = rdf_mapping_load($entity_type, $instance['bundle']);
414
415
416
417
418
  $field_name = $instance['field_name'];

  if (!empty($mapping) && !empty($mapping[$field_name])) {
    foreach ($variables['items'] as $delta => $item) {
      if (!empty($item['#item'])) {
419
        $variables['item_attributes_array'][$delta] = rdf_rdfa_attributes($mapping[$field_name], $item['#item']);
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
      }
    }
  }
}

/**
 * Implements MODULE_preprocess_HOOK().
 */
function rdf_preprocess_user_profile(&$variables) {
  // Adds RDFa markup to the user profile page. Fields displayed in this page
  // will automatically describe the user.
  // @todo move to user.module
  $account = user_load($variables['user']->uid);
  if (!empty($account->rdf_mapping['rdftype'])) {
    $variables['attributes_array']['typeof'] = $account->rdf_mapping['rdftype'];
    $variables['attributes_array']['about'] = url('user/' . $account->uid);
  }
}

/**
 * Implements MODULE_preprocess_HOOK().
 */
function rdf_preprocess_username(&$variables) {
  $account = $variables['account'];
  if (!empty($account->rdf_mapping['name'])) {
    if ($account->uid != 0) {
      // The following RDFa construct allows to fit all the needed information
      // into the a tag and avoids having to wrap it with an extra span.

      // An RDF resource for the user is created with the 'about' attribute and
      // the profile URI is used to identify this resource. Even if the user
      // profile is not accessible, we generate its URI regardless in order to
      // be able to identify the user in RDF.
      $variables['attributes_array']['about'] = url('user/' . $account->uid);
      // The 'typeof' attribute specifies the RDF type(s) of this resource. They
      // are defined in the 'rdftype' property of the user object RDF mapping.
      // Since the full user object is not available in $variables, it needs to
      // be loaded. This is due to the collision between the node and user
      // when they are merged into $account and some properties are overridden.
      $variables['attributes_array']['typeof'] = user_load($account->uid)->rdf_mapping['rdftype'];

461
      // The first thing we are describing is the relation between the user and
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
      // the parent resource (e.g. a node). Because the set of predicate link
      // the parent to the user, we must use the 'rev' RDFa attribute to specify
      // that the relationship is reverse.
      if (!empty($account->rdf_mapping['uid']['predicates'])) {
        $variables['attributes_array']['rev'] = $account->rdf_mapping['uid']['predicates'];
        // We indicate the parent identifier in the 'resource' attribute,
        // typically this is the entity URI. This is the object in RDF.
        $parent_uri = '';
        if (!empty($account->path['source'])) {
          $parent_uri = url($account->path['source']);
        }
        elseif (!empty($account->cid)) {
          $parent_uri = url('comment/' . $account->cid, array('fragment' => 'comment-' . $account->cid));
        }
        $variables['attributes_array']['resource'] = $parent_uri;
      }

      // The second information we annotate is the name of the user with the
      // 'property' attribute. We do not need to specify the RDF object here
      // because it's the value inside the a tag which will be used
      // automatically according to the RDFa parsing rules.
      $variables['attributes_array']['property'] = $account->rdf_mapping['name']['predicates'];
    }
  }
}

/**
 * Implements MODULE_preprocess_HOOK().
 */
function rdf_preprocess_comment(&$variables) {
  $comment = $variables['comment'];
  if (!empty($comment->rdf_mapping['rdftype'])) {
494
    // Adds RDFa markup to the comment container. The about attribute specifies
495
496
497
498
499
500
    // the URI of the resource described within the HTML element, while the
    // typeof attribute indicates its RDF type (e.g. sioc:Post, etc.).
    $variables['attributes_array']['about'] = url('comment/' . $comment->cid, array('fragment' => 'comment-' . $comment->cid));
    $variables['attributes_array']['typeof'] = $comment->rdf_mapping['rdftype'];
  }

501
  // Adds RDFa markup for the date of the comment.
502
  if (!empty($comment->rdf_mapping['created'])) {
503
    $date_attributes_array = rdf_rdfa_attributes($comment->rdf_mapping['created'], $comment->created);
504
    $variables['rdf_template_variable_attributes_array']['created'] = $date_attributes_array;
505
506
  }
  if (!empty($comment->rdf_mapping['title'])) {
507
    // Adds RDFa markup to the subject of the comment. Because the RDFa markup is
508
509
510
511
512
513
514
515
516
517
    // added to an h3 tag which might contain HTML code, we specify an empty
    // datatype to ensure the value of the title read by the RDFa parsers is a
    // literal.
    $variables['title_attributes_array']['property'] = $comment->rdf_mapping['title']['predicates'];
    $variables['title_attributes_array']['datatype'] = '';
  }
  if (!empty($comment->rdf_mapping['body'])) {
    // We need a special case here since the comment body is not a field. Note
    // that for that reason, fields attached to comment will be ignored by RDFa
    // parsers since we set the property attribute here.
518
    // @todo Use fields instead, see http://drupal.org/node/538164
519
520
521
522
523
524
525
526
527
    $variables['content_attributes_array']['property'] = $comment->rdf_mapping['body']['predicates'];
  }

  // Annotates the parent relationship between the current comment and the node
  // it belongs to. If available, the parent comment is also annotated.
  if (!empty($comment->rdf_mapping['pid'])) {
    // Relation to parent node.
    $parent_node_attributes['rel'] = $comment->rdf_mapping['pid']['predicates'];
    $parent_node_attributes['resource'] = url('node/' . $comment->nid);
528
    $variables['rdf_metadata_attributes_array'][] = $parent_node_attributes;
529
530
531
532
533

    // Relation to parent comment if it exists.
    if ($comment->pid != 0) {
      $parent_comment_attributes['rel'] = $comment->rdf_mapping['pid']['predicates'];
      $parent_comment_attributes['resource'] = url('comment/' . $comment->pid, array('fragment' => 'comment-' . $comment->pid));
534
      $variables['rdf_metadata_attributes_array'][] = $parent_comment_attributes;
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
    }
  }
}

/**
 * Implements MODULE_preprocess_HOOK().
 */
function rdf_preprocess_field_formatter_taxonomy_term_link(&$variables) {
  $term = $variables['element']['#item']['taxonomy_term'];
  if (!empty($term->rdf_mapping['rdftype'])) {
    $variables['link_options']['attributes']['typeof'] = $term->rdf_mapping['rdftype'];
  }
  if (!empty($term->rdf_mapping['name']['predicates'])) {
    $variables['link_options']['attributes']['property'] = $term->rdf_mapping['name']['predicates'];
  }
}

/**
553
 * Wraps a template variable in an HTML element with the desired attributes.
554
 *
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
 * This is called by rdf_process() shortly before the theme system renders
 * a template file. It is called once for each template variable for which
 * additional attributes are needed. While template files are responsible for
 * rendering the attributes for the template's primary object (via the
 * $attributes variable), title (via the $title_attributes variable), and
 * content (via the $content_attributes variable), additional template variables
 * that need containing attributes are routed through this function, allowing
 * the template file to receive properly wrapped variables.
 *
 * @param $variables
 *   An associative array containing:
 *   - content: A string of content to be wrapped with attributes.
 *   - attributes: An array of attributes desired on the wrapping element.
 *   - context: An array of context information about the content to be wrapped:
 *     - hook: The theme hook that will use the wrapped content. This
 *       corresponds to the key within the theme registry for this template.
 *       For example, if this content is about to be used in node.tpl.php or
 *       node-TYPE.tpl.php, then the 'hook' is 'node'.
 *     - variable_name: The name of the variable, by which the template will
 *       refer to this content. Each template file has documentation about
 *       the variables it uses. For example, if this function is called in
 *       preparing the $author variable for comment.tpl.php, then the
 *       'variable_name' is 'author'.
 *     - variables: The full array of variables about to be passed to the
 *       template.
 *   - inline: TRUE if the content contains only inline HTML elements and
 *     therefore can be validly wrapped by a 'span' tag. FALSE if the content
 *     might contain block level HTML elements and therefore cannot be validly
 *     wrapped by a 'span' tag. Modules implementing preprocess functions that
 *     set 'rdf_template_variable_attributes_array' for a particular template
 *     variable that might contain block level HTML must also implement
 *     hook_preprocess_rdf_template_variable_wrapper() and set 'inline' to FALSE
 *     for that context. Themes that render normally inline content with block
 *     level HTML must similarly implement
 *     hook_preprocess_rdf_template_variable_wrapper() and set 'inline'
 *     accordingly.
591
 *
592
593
594
 * @return
 *   A string containing the wrapped content. The template receives the for its
 *   variable instead of the original content.
595
 *
596
597
598
599
600
601
602
 * Tip for themers: if you're already outputting a wrapper element around a
 * particular template variable in your template file and if you don't want
 * an extra wrapper element, you can override this function to not wrap that
 * variable and instead print the following inside your template file:
 * @code
 *   drupal_attributes($rdf_template_variable_attributes_array[$variable_name])
 * @endcode
603
 *
604
 * @see rdf_process()
605
 *
606
 * @ingroup themeable
607
 */
608
609
610
611
612
function theme_rdf_template_variable_wrapper($variables) {
  $output = $variables['content'];
  if (!empty($output) && !empty($variables['attributes'])) {
    $attributes = drupal_attributes($variables['attributes']);
    $output = $variables['inline'] ? "<span$attributes>$output</span>" : "<div$attributes>$output</div>";
613
  }
614
  return $output;
615
616
617
}

/**
618
 * Outputs a series of empty spans for exporting RDF metadata in RDFa.
619
 *
620
621
622
623
624
 * Sometimes it is useful to export data which is not semantically present in
 * the HTML output. For example, a hierarchy of comments is visible for a human
 * but not for machines because this hiearchy is not present in the DOM tree.
 * We can express it in RDFa via empty span tags. These won't be visible and
 * will give machines extra information about the content and its structure.
625
 *
626
627
628
629
630
 * @param $variables
 *   An associative array containing:
 *   - metadata: An array of attribute arrays. Each item in the array
 *     corresponds to its own set of attributes, and therefore, needs its own
 *     element.
631
 *
632
633
 * @return
 *   A string of HTML containing markup that can be understood by an RDF parser.
634
 *
635
636
637
638
639
640
641
642
643
 * Tip for themers: while this default implementation results in valid markup
 * for the XHTML+RDFa doctype, you may need to override this in your theme to be
 * valid for doctypes that don't support empty spans. Or, if empty spans create
 * visual problems in your theme, you may want to override this to set a
 * class on them, and apply a CSS rule of display:none for that class.
 *
 * @see rdf_process()
 *
 * @ingroup themeable
644
 */
645
646
647
648
649
650
function theme_rdf_metadata($variables) {
  $output = '';
  foreach ($variables['metadata'] as $attributes) {
    $output .= '<span' . drupal_attributes($attributes) . ' />';
  }
  return $output;
651
}
652