rdf.module 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. <?php
  2. /**
  3. * @file
  4. * Enables semantically enriched output for Drupal sites in the form of RDFa.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function rdf_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/help#rdf':
  12. $output = '';
  13. $output .= '<h3>' . t('About') . '</h3>';
  14. $output .= '<p>' . t('The RDF module enriches your content with metadata to let other applications (e.g., search engines, aggregators, and so on) better understand its relationships and attributes. This semantically enriched, machine-readable output for Drupal sites uses the <a href="@rdfa">RDFa specification</a> which allows RDF data to be embedded in HTML markup. Other modules can define mappings of their data to RDF terms, and the RDF module makes this RDF data available to the theme. The core Drupal modules define RDF mappings for their data model, and the core Drupal themes output this RDF metadata information along with the human-readable visual information. For more information, see the online handbook entry for <a href="@rdf">RDF module</a>.', array('@rdfa' => 'http://www.w3.org/TR/xhtml-rdfa-primer/', '@rdf' => 'http://drupal.org/documentation/modules/rdf')) . '</p>';
  15. return $output;
  16. }
  17. }
  18. /**
  19. * @defgroup rdf RDF Mapping API
  20. * @{
  21. * Functions to describe entities and bundles in RDF.
  22. *
  23. * The RDF module introduces RDF and RDFa to Drupal. RDF is a W3C standard to
  24. * describe structured data. RDF can be serialized as RDFa in XHTML attributes
  25. * to augment visual data with machine-readable hints.
  26. * @see http://www.w3.org/RDF/
  27. * @see http://www.w3.org/TR/xhtml-rdfa-primer/
  28. *
  29. * Modules can provide mappings of their bundles' data and metadata to RDF
  30. * classes and properties. This module takes care of injecting these mappings
  31. * into variables available to theme functions and templates. All Drupal core
  32. * themes are coded to be RDFa compatible.
  33. *
  34. * Example mapping from node.module:
  35. * @code
  36. * array(
  37. * 'type' => 'node',
  38. * 'bundle' => RDF_DEFAULT_BUNDLE,
  39. * 'mapping' => array(
  40. * 'rdftype' => array('sioc:Item', 'foaf:Document'),
  41. * 'title' => array(
  42. * 'predicates' => array('dc:title'),
  43. * ),
  44. * 'created' => array(
  45. * 'predicates' => array('dc:date', 'dc:created'),
  46. * 'datatype' => 'xsd:dateTime',
  47. * 'callback' => 'date_iso8601',
  48. * ),
  49. * 'body' => array(
  50. * 'predicates' => array('content:encoded'),
  51. * ),
  52. * 'uid' => array(
  53. * 'predicates' => array('sioc:has_creator'),
  54. * ),
  55. * 'name' => array(
  56. * 'predicates' => array('foaf:name'),
  57. * ),
  58. * ),
  59. * );
  60. * @endcode
  61. */
  62. /**
  63. * RDF bundle flag: Default bundle.
  64. *
  65. * Implementations of hook_rdf_mapping() should use this constant for the
  66. * 'bundle' key when defining a default set of RDF mappings for an entity type.
  67. * Each bundle will inherit the default mappings defined for the entity type
  68. * unless the bundle defines its own specific mappings.
  69. */
  70. define('RDF_DEFAULT_BUNDLE', '');
  71. /**
  72. * Implements hook_rdf_namespaces().
  73. */
  74. function rdf_rdf_namespaces() {
  75. return array(
  76. 'content' => 'http://purl.org/rss/1.0/modules/content/',
  77. 'dc' => 'http://purl.org/dc/terms/',
  78. 'foaf' => 'http://xmlns.com/foaf/0.1/',
  79. 'og' => 'http://ogp.me/ns#',
  80. 'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
  81. 'sioc' => 'http://rdfs.org/sioc/ns#',
  82. 'sioct' => 'http://rdfs.org/sioc/types#',
  83. 'skos' => 'http://www.w3.org/2004/02/skos/core#',
  84. 'xsd' => 'http://www.w3.org/2001/XMLSchema#',
  85. );
  86. }
  87. /**
  88. * Returns an array of RDF namespaces defined in modules that implement
  89. * hook_rdf_namespaces().
  90. */
  91. function rdf_get_namespaces() {
  92. $rdf_namespaces = module_invoke_all('rdf_namespaces');
  93. // module_invoke_all() uses array_merge_recursive() which might return nested
  94. // arrays if several modules redefine the same prefix multiple times. We need
  95. // to ensure the array of namespaces is flat and only contains strings as
  96. // URIs.
  97. foreach ($rdf_namespaces as $prefix => $uri) {
  98. if (is_array($uri)) {
  99. if (count(array_unique($uri)) == 1) {
  100. // All namespaces declared for this prefix are the same, merge them all
  101. // into a single namespace.
  102. $rdf_namespaces[$prefix] = $uri[0];
  103. }
  104. else {
  105. // There are conflicting namespaces for this prefix, do not include
  106. // duplicates in order to avoid asserting any inaccurate RDF
  107. // statements.
  108. unset($rdf_namespaces[$prefix]);
  109. }
  110. }
  111. }
  112. return $rdf_namespaces;
  113. }
  114. /**
  115. * Returns the mapping for attributes of a given entity type/bundle pair.
  116. *
  117. * @param $type
  118. * An entity type.
  119. * @param $bundle
  120. * (optional) A bundle name.
  121. *
  122. * @return
  123. * The mapping corresponding to the requested entity type/bundle pair or an
  124. * empty array.
  125. */
  126. function rdf_mapping_load($type, $bundle = RDF_DEFAULT_BUNDLE) {
  127. // Retrieves the bundle-specific mapping from the entity info.
  128. $entity_info = entity_get_info($type);
  129. if (!empty($entity_info['bundles'][$bundle]['rdf_mapping'])) {
  130. return $entity_info['bundles'][$bundle]['rdf_mapping'];
  131. }
  132. // If there is no mapping defined for this bundle, we return the default
  133. // mapping that is defined for this entity type.
  134. else {
  135. return _rdf_get_default_mapping($type);
  136. }
  137. }
  138. /**
  139. * @} End of "defgroup rdf".
  140. */
  141. /**
  142. * Helper function to get the default RDF mapping for a given entity type.
  143. *
  144. * @param $type
  145. * An entity type, e.g. 'node' or 'comment'.
  146. *
  147. * @return
  148. * The RDF mapping or an empty array if no mapping is defined for this entity
  149. * type.
  150. */
  151. function _rdf_get_default_mapping($type) {
  152. $default_mappings = &drupal_static(__FUNCTION__);
  153. if (!isset($default_mappings)) {
  154. // Get all of the modules that implement hook_rdf_mapping().
  155. $modules = module_implements('rdf_mapping');
  156. // Only consider the default entity mapping definitions.
  157. foreach ($modules as $module) {
  158. $mappings = module_invoke($module, 'rdf_mapping');
  159. foreach ($mappings as $mapping) {
  160. if ($mapping['bundle'] === RDF_DEFAULT_BUNDLE) {
  161. $default_mappings[$mapping['type']] = $mapping['mapping'];
  162. }
  163. }
  164. }
  165. }
  166. return isset($default_mappings[$type]) ? $default_mappings[$type] : array();
  167. }
  168. /**
  169. * Helper function to retrieve an RDF mapping from the database.
  170. *
  171. * @param $type
  172. * The entity type the mapping refers to.
  173. * @param $bundle
  174. * The bundle the mapping refers to.
  175. *
  176. * @return
  177. * An RDF mapping structure or an empty array if no record was found.
  178. */
  179. function _rdf_mapping_load($type, $bundle) {
  180. $mappings = _rdf_mapping_load_multiple($type, array($bundle));
  181. return $mappings ? reset($mappings) : array();
  182. }
  183. /**
  184. * Helper function to retrieve a set of RDF mappings from the database.
  185. *
  186. * @param $type
  187. * The entity type of the mappings.
  188. * @param $bundles
  189. * The bundles the mappings refer to.
  190. *
  191. * @return
  192. * An array of RDF mapping structures, or an empty array.
  193. */
  194. function _rdf_mapping_load_multiple($type, array $bundles) {
  195. $mappings = db_select('rdf_mapping')
  196. ->fields(NULL, array('bundle', 'mapping'))
  197. ->condition('type', $type)
  198. ->condition('bundle', $bundles)
  199. ->execute()
  200. ->fetchAllKeyed();
  201. foreach ($mappings as $bundle => $mapping) {
  202. $mappings[$bundle] = unserialize($mapping);
  203. }
  204. return $mappings;
  205. }
  206. /**
  207. * @addtogroup rdf
  208. * @{
  209. */
  210. /**
  211. * Saves an RDF mapping to the database.
  212. *
  213. * Takes a mapping structure returned by hook_rdf_mapping() implementations
  214. * and creates or updates a record mapping for each encountered entity
  215. * type/bundle pair. If available, adds default values for non-existent mapping
  216. * keys.
  217. *
  218. * @param $mapping
  219. * The RDF mapping to save, as an array.
  220. *
  221. * @return
  222. * Status flag indicating the outcome of the operation.
  223. */
  224. function rdf_mapping_save($mapping) {
  225. // In the case where a field has a mapping defined in the default entity
  226. // mapping, but a mapping is not specified in the bundle-specific mapping,
  227. // then use the default mapping for that field.
  228. $mapping['mapping'] += _rdf_get_default_mapping($mapping['type']);
  229. $status = db_merge('rdf_mapping')
  230. ->key(array(
  231. 'type' => $mapping['type'],
  232. 'bundle' => $mapping['bundle'],
  233. ))
  234. ->fields(array(
  235. 'mapping' => serialize($mapping['mapping']),
  236. ))
  237. ->execute();
  238. entity_info_cache_clear();
  239. return $status;
  240. }
  241. /**
  242. * Deletes the mapping for the given bundle from the database.
  243. *
  244. * @param $type
  245. * The entity type the mapping refers to.
  246. * @param $bundle
  247. * The bundle the mapping refers to.
  248. *
  249. * @return
  250. * Return boolean TRUE if mapping deleted, FALSE if not.
  251. */
  252. function rdf_mapping_delete($type, $bundle) {
  253. $num_rows = db_delete('rdf_mapping')
  254. ->condition('type', $type)
  255. ->condition('bundle', $bundle)
  256. ->execute();
  257. return (bool) ($num_rows > 0);
  258. }
  259. /**
  260. * Builds an array of RDFa attributes for a given mapping. This array will
  261. * typically be passed through drupal_attributes() to create the attributes
  262. * variables that are available to template files. These include $attributes,
  263. * $title_attributes, $content_attributes and the field-specific
  264. * $item_attributes variables. For more information, see
  265. * theme_rdf_template_variable_wrapper().
  266. *
  267. * @param $mapping
  268. * An array containing a mandatory 'predicates' key and optional 'datatype',
  269. * 'callback' and 'type' keys. For example:
  270. * @code
  271. * array(
  272. * 'predicates' => array('dc:created'),
  273. * 'datatype' => 'xsd:dateTime',
  274. * 'callback' => 'date_iso8601',
  275. * ),
  276. * );
  277. * @endcode
  278. * @param $data
  279. * A value that needs to be converted by the provided callback function.
  280. *
  281. * @return
  282. * An array containing RDFa attributes suitable for drupal_attributes().
  283. */
  284. function rdf_rdfa_attributes($mapping, $data = NULL) {
  285. // The type of mapping defaults to 'property'.
  286. $type = isset($mapping['type']) ? $mapping['type'] : 'property';
  287. switch ($type) {
  288. // The mapping expresses the relationship between two resources.
  289. case 'rel':
  290. case 'rev':
  291. $attributes[$type] = $mapping['predicates'];
  292. break;
  293. // The mapping expresses the relationship between a resource and some
  294. // literal text.
  295. case 'property':
  296. $attributes['property'] = $mapping['predicates'];
  297. // Convert $data to a specific format as per the callback function.
  298. if (isset($data) && isset($mapping['callback']) && function_exists($mapping['callback'])) {
  299. $callback = $mapping['callback'];
  300. $attributes['content'] = $callback($data);
  301. }
  302. if (isset($mapping['datatype'])) {
  303. $attributes['datatype'] = $mapping['datatype'];
  304. }
  305. break;
  306. }
  307. return $attributes;
  308. }
  309. /**
  310. * @} End of "addtogroup rdf".
  311. */
  312. /**
  313. * Implements hook_modules_installed().
  314. *
  315. * Checks if the installed modules have any RDF mapping definitions to declare
  316. * and stores them in the rdf_mapping table.
  317. *
  318. * While both default entity mappings and specific bundle mappings can be
  319. * defined in hook_rdf_mapping(), default entity mappings are not stored in the
  320. * database. Only overridden mappings are stored in the database. The default
  321. * entity mappings can be overriden by specific bundle mappings which are
  322. * stored in the database and can be altered via the RDF CRUD mapping API.
  323. */
  324. function rdf_modules_installed($modules) {
  325. foreach ($modules as $module) {
  326. $function = $module . '_rdf_mapping';
  327. if (function_exists($function)) {
  328. foreach ($function() as $mapping) {
  329. // Only the bundle mappings are saved in the database.
  330. if ($mapping['bundle'] !== RDF_DEFAULT_BUNDLE) {
  331. rdf_mapping_save($mapping);
  332. }
  333. }
  334. }
  335. }
  336. }
  337. /**
  338. * Implements hook_modules_uninstalled().
  339. */
  340. function rdf_modules_uninstalled($modules) {
  341. // @todo Remove RDF mappings of uninstalled modules.
  342. }
  343. /**
  344. * Implements hook_entity_info_alter().
  345. *
  346. * Adds the proper RDF mapping to each entity type/bundle pair.
  347. *
  348. * @todo May need to move the comment below to another place.
  349. * This hook should not be used by modules to alter the bundle mappings.
  350. * The UI should always be authoritative. UI mappings are stored in the
  351. * database and if hook_entity_info_alter was used to override module defined
  352. * mappings, it would override the user defined mapping as well.
  353. */
  354. function rdf_entity_info_alter(&$entity_info) {
  355. // Loop through each entity type and its bundles.
  356. foreach ($entity_info as $entity_type => $entity_type_info) {
  357. if (!empty($entity_type_info['bundles'])) {
  358. $bundles = array_keys($entity_type_info['bundles']);
  359. $mappings = _rdf_mapping_load_multiple($entity_type, $bundles);
  360. foreach ($bundles as $bundle) {
  361. if (isset($mappings[$bundle])) {
  362. $entity_info[$entity_type]['bundles'][$bundle]['rdf_mapping'] = $mappings[$bundle];
  363. }
  364. else {
  365. // If no mapping was found in the database, assign the default RDF
  366. // mapping for this entity type.
  367. $entity_info[$entity_type]['bundles'][$bundle]['rdf_mapping'] = _rdf_get_default_mapping($entity_type);
  368. }
  369. }
  370. }
  371. }
  372. }
  373. /**
  374. * Implements hook_entity_load().
  375. */
  376. function rdf_entity_load($entities, $type) {
  377. foreach ($entities as $entity) {
  378. // Extracts the bundle of the entity being loaded.
  379. list($id, $vid, $bundle) = entity_extract_ids($type, $entity);
  380. $entity->rdf_mapping = rdf_mapping_load($type, $bundle);
  381. }
  382. }
  383. /**
  384. * Implements hook_comment_load().
  385. */
  386. function rdf_comment_load($comments) {
  387. foreach ($comments as $comment) {
  388. // Pages with many comments can show poor performance. This information
  389. // isn't needed until rdf_preprocess_comment() is called, but set it here
  390. // to optimize performance for websites that implement an entity cache.
  391. $comment->rdf_data['date'] = rdf_rdfa_attributes($comment->rdf_mapping['created'], $comment->created);
  392. $comment->rdf_data['nid_uri'] = url('node/' . $comment->nid);
  393. if ($comment->pid) {
  394. $comment->rdf_data['pid_uri'] = url('comment/' . $comment->pid, array('fragment' => 'comment-' . $comment->pid));
  395. }
  396. }
  397. }
  398. /**
  399. * Implements hook_theme().
  400. */
  401. function rdf_theme() {
  402. return array(
  403. 'rdf_template_variable_wrapper' => array(
  404. 'variables' => array('content' => NULL, 'attributes' => array(), 'context' => array(), 'inline' => TRUE),
  405. ),
  406. 'rdf_metadata' => array(
  407. 'variables' => array('metadata' => array()),
  408. ),
  409. );
  410. }
  411. /**
  412. * Template process function for adding extra tags to hold RDFa attributes.
  413. *
  414. * Since template files already have built-in support for $attributes,
  415. * $title_attributes, and $content_attributes, and field templates have support
  416. * for $item_attributes, we try to leverage those as much as possible. However,
  417. * in some cases additional attributes are needed not covered by these. We deal
  418. * with those here.
  419. */
  420. function rdf_process(&$variables, $hook) {
  421. // Handles attributes needed for content not covered by title, content,
  422. // and field items. It does this by adjusting the variable sent to the
  423. // template so that the template doesn't have to worry about it. See
  424. // theme_rdf_template_variable_wrapper().
  425. if (!empty($variables['rdf_template_variable_attributes_array'])) {
  426. foreach ($variables['rdf_template_variable_attributes_array'] as $variable_name => $attributes) {
  427. $context = array(
  428. 'hook' => $hook,
  429. 'variable_name' => $variable_name,
  430. 'variables' => $variables,
  431. );
  432. $variables[$variable_name] = theme('rdf_template_variable_wrapper', array('content' => $variables[$variable_name], 'attributes' => $attributes, 'context' => $context));
  433. }
  434. }
  435. // Handles additional attributes about a template entity that for RDF parsing
  436. // reasons, can't be placed into that template's $attributes variable. This
  437. // is "meta" information that is related to particular content, so render it
  438. // close to that content.
  439. if (!empty($variables['rdf_metadata_attributes_array'])) {
  440. if (!isset($variables['content']['#prefix'])) {
  441. $variables['content']['#prefix'] = '';
  442. }
  443. $variables['content']['#prefix'] = theme('rdf_metadata', array('metadata' => $variables['rdf_metadata_attributes_array'])) . $variables['content']['#prefix'];
  444. }
  445. }
  446. /**
  447. * Implements MODULE_preprocess_HOOK().
  448. */
  449. function rdf_preprocess_node(&$variables) {
  450. // Adds RDFa markup to the node container. The about attribute specifies the
  451. // URI of the resource described within the HTML element, while the @typeof
  452. // attribute indicates its RDF type (e.g., foaf:Document, sioc:Person, and so
  453. // on.)
  454. $variables['attributes_array']['about'] = empty($variables['node_url']) ? NULL: $variables['node_url'];
  455. $variables['attributes_array']['typeof'] = empty($variables['node']->rdf_mapping['rdftype']) ? NULL : $variables['node']->rdf_mapping['rdftype'];
  456. // Adds RDFa markup about the title of the node to the title_suffix.
  457. if (!empty($variables['node']->rdf_mapping['title']['predicates'])) {
  458. $variables['title_suffix']['rdf_meta_title'] = array(
  459. '#theme' => 'rdf_metadata',
  460. '#metadata' => array(
  461. array(
  462. 'property' => $variables['node']->rdf_mapping['title']['predicates'],
  463. 'content' => $variables['node']->title,
  464. ),
  465. ),
  466. );
  467. }
  468. // Adds RDFa markup for the date.
  469. if (!empty($variables['rdf_mapping']['created'])) {
  470. $date_attributes_array = rdf_rdfa_attributes($variables['rdf_mapping']['created'], $variables['created']);
  471. $variables['rdf_template_variable_attributes_array']['date'] = $date_attributes_array;
  472. if ($variables['submitted']) {
  473. $variables['rdf_template_variable_attributes_array']['submitted'] = $date_attributes_array;
  474. }
  475. }
  476. // Adds RDFa markup for the relation between the node and its author.
  477. if (!empty($variables['rdf_mapping']['uid'])) {
  478. $variables['rdf_template_variable_attributes_array']['name']['rel'] = $variables['rdf_mapping']['uid']['predicates'];
  479. if ($variables['submitted']) {
  480. $variables['rdf_template_variable_attributes_array']['submitted']['rel'] = $variables['rdf_mapping']['uid']['predicates'];
  481. }
  482. }
  483. // Adds RDFa markup annotating the number of comments a node has.
  484. if (isset($variables['node']->comment_count) &&
  485. !empty($variables['node']->rdf_mapping['comment_count']['predicates']) &&
  486. user_access('access comments')) {
  487. // Adds RDFa markup for the comment count near the node title as metadata.
  488. $variables['title_suffix']['rdf_meta_comment_count'] = array(
  489. '#theme' => 'rdf_metadata',
  490. '#metadata' => array(
  491. array(
  492. 'property' => $variables['node']->rdf_mapping['comment_count']['predicates'],
  493. 'content' => $variables['node']->comment_count,
  494. 'datatype' => $variables['node']->rdf_mapping['comment_count']['datatype'],
  495. ),
  496. ),
  497. );
  498. }
  499. }
  500. /**
  501. * Implements MODULE_preprocess_HOOK().
  502. */
  503. function rdf_preprocess_field(&$variables) {
  504. $element = $variables['element'];
  505. $mapping = rdf_mapping_load($element['#entity_type'], $element['#bundle']);
  506. $field_name = $element['#field_name'];
  507. if (!empty($mapping) && !empty($mapping[$field_name])) {
  508. foreach ($element['#items'] as $delta => $item) {
  509. $variables['item_attributes_array'][$delta] = rdf_rdfa_attributes($mapping[$field_name], $item);
  510. // If this field is an image, RDFa will not output correctly when the
  511. // image is in a containing <a> tag. If the field is a file, RDFa will
  512. // not output correctly if the filetype icon comes before the link to the
  513. // file. We correct this by adding a resource attribute to the div if
  514. // this field has a URI.
  515. if (isset($item['uri'])) {
  516. if (!empty($element[$delta]['#image_style'])) {
  517. $variables['item_attributes_array'][$delta]['resource'] = image_style_url($element[$delta]['#image_style'], $item['uri']);
  518. }
  519. else {
  520. $variables['item_attributes_array'][$delta]['resource'] = file_create_url($item['uri']);
  521. }
  522. }
  523. }
  524. }
  525. }
  526. /**
  527. * Implements MODULE_preprocess_HOOK().
  528. */
  529. function rdf_preprocess_user_profile(&$variables) {
  530. $account = $variables['elements']['#account'];
  531. $uri = entity_uri('user', $account);
  532. // Adds RDFa markup to the user profile page. Fields displayed in this page
  533. // will automatically describe the user.
  534. if (!empty($account->rdf_mapping['rdftype'])) {
  535. $variables['attributes_array']['typeof'] = $account->rdf_mapping['rdftype'];
  536. $variables['attributes_array']['about'] = url($uri['path'], $uri['options']);
  537. }
  538. // Adds the relationship between the sioc:UserAccount and the foaf:Person who
  539. // holds the account.
  540. $account_holder_meta = array(
  541. '#tag' => 'meta',
  542. '#attributes' => array(
  543. 'about' => url($uri['path'], array_merge($uri['options'], array('fragment' => 'me'))),
  544. 'typeof' => array('foaf:Person'),
  545. 'rel' => array('foaf:account'),
  546. 'resource' => url($uri['path'], $uri['options']),
  547. ),
  548. );
  549. // Adds the markup for username.
  550. $username_meta = array(
  551. '#tag' => 'meta',
  552. '#attributes' => array(
  553. 'about' => url($uri['path'], $uri['options']),
  554. 'property' => $account->rdf_mapping['name']['predicates'],
  555. 'content' => $account->name,
  556. )
  557. );
  558. drupal_add_html_head($account_holder_meta, 'rdf_user_account_holder');
  559. drupal_add_html_head($username_meta, 'rdf_user_username');
  560. }
  561. /**
  562. * Implements MODULE_preprocess_HOOK().
  563. */
  564. function rdf_preprocess_username(&$variables) {
  565. // Because xml:lang is set on the HTML element that wraps the page, the
  566. // username inherits this language attribute. However, since the username
  567. // might not be transliterated to the same language that the content is in,
  568. // we do not want it to inherit the language attribute, so we set the
  569. // attribute to an empty string.
  570. if (empty($variables['attributes_array']['xml:lang'])) {
  571. $variables['attributes_array']['xml:lang'] = '';
  572. }
  573. // $variables['account'] is a pseudo account object, and as such, does not
  574. // contain the RDF mappings for the user. In the case of nodes and comments,
  575. // it contains the mappings for the node or comment object instead. However,
  576. // while the RDF mappings are available from a full user_load(), this should
  577. // be avoided for performance reasons. Since the type and bundle for users is
  578. // already known, call rdf_mapping_load() directly.
  579. $rdf_mapping = rdf_mapping_load('user', 'user');
  580. // The profile URI is used to identify the user account. The about attribute
  581. // is used to set the URI as the default subject of the predicates embedded
  582. // as RDFa in the child elements. Even if the user profile is not accessible
  583. // to the current user, we use its URI in order to identify the user in RDF.
  584. // We do not use this attribute for the anonymous user because we do not have
  585. // a user profile URI for it (only a homepage which cannot be used as user
  586. // profile in RDF.)
  587. if ($variables['uid'] > 0) {
  588. $variables['attributes_array']['about'] = url('user/' . $variables['uid']);
  589. }
  590. $attributes = array();
  591. // The typeof attribute specifies the RDF type(s) of this resource. They
  592. // are defined in the 'rdftype' property of the user RDF mapping.
  593. if (!empty($rdf_mapping['rdftype'])) {
  594. $attributes['typeof'] = $rdf_mapping['rdftype'];
  595. }
  596. // Annotate the username in RDFa. A property attribute is used with an empty
  597. // datatype attribute to ensure the username is parsed as a plain literal
  598. // in RDFa 1.0 and 1.1.
  599. if (!empty($rdf_mapping['name'])) {
  600. $attributes['property'] = $rdf_mapping['name']['predicates'];
  601. $attributes['datatype'] = '';
  602. }
  603. // Add the homepage RDFa markup if present.
  604. if (!empty($variables['homepage']) && !empty($rdf_mapping['homepage'])) {
  605. $attributes['rel'] = $rdf_mapping['homepage']['predicates'];
  606. }
  607. // The remaining attributes can have multiple values listed, with whitespace
  608. // separating the values in the RDFa attributes
  609. // (see http://www.w3.org/TR/rdfa-syntax/#rdfa-attributes).
  610. // Therefore, merge rather than override so as not to clobber values set by
  611. // earlier preprocess functions.
  612. $variables['attributes_array'] = array_merge_recursive($variables['attributes_array'], $attributes);
  613. }
  614. /**
  615. * Implements MODULE_preprocess_HOOK().
  616. */
  617. function rdf_preprocess_comment(&$variables) {
  618. $comment = $variables['comment'];
  619. if (!empty($comment->rdf_mapping['rdftype'])) {
  620. // Adds RDFa markup to the comment container. The about attribute specifies
  621. // the URI of the resource described within the HTML element, while the
  622. // typeof attribute indicates its RDF type (e.g., sioc:Post, foaf:Document,
  623. // and so on.)
  624. $uri = entity_uri('comment', $comment);
  625. $variables['attributes_array']['about'] = url($uri['path'], $uri['options']);
  626. $variables['attributes_array']['typeof'] = $comment->rdf_mapping['rdftype'];
  627. }
  628. // Adds RDFa markup for the date of the comment.
  629. if (!empty($comment->rdf_mapping['created'])) {
  630. // The comment date is precomputed as part of the rdf_data so that it can be
  631. // cached as part of the entity.
  632. $date_attributes_array = $comment->rdf_data['date'];
  633. $variables['rdf_template_variable_attributes_array']['created'] = $date_attributes_array;
  634. $variables['rdf_template_variable_attributes_array']['submitted'] = $date_attributes_array;
  635. }
  636. // Adds RDFa markup for the relation between the comment and its author.
  637. if (!empty($comment->rdf_mapping['uid'])) {
  638. $variables['rdf_template_variable_attributes_array']['author']['rel'] = $comment->rdf_mapping['uid']['predicates'];
  639. $variables['rdf_template_variable_attributes_array']['submitted']['rel'] = $comment->rdf_mapping['uid']['predicates'];
  640. }
  641. if (!empty($comment->rdf_mapping['title'])) {
  642. // Adds RDFa markup to the subject of the comment. Because the RDFa markup
  643. // is added to an <h3> tag which might contain HTML code, we specify an
  644. // empty datatype to ensure the value of the title read by the RDFa parsers
  645. // is a literal.
  646. $variables['title_attributes_array']['property'] = $comment->rdf_mapping['title']['predicates'];
  647. $variables['title_attributes_array']['datatype'] = '';
  648. }
  649. // Annotates the parent relationship between the current comment and the node
  650. // it belongs to. If available, the parent comment is also annotated.
  651. if (!empty($comment->rdf_mapping['pid'])) {
  652. // Adds the relation to the parent node.
  653. $parent_node_attributes['rel'] = $comment->rdf_mapping['pid']['predicates'];
  654. // The parent node URI is precomputed as part of the rdf_data so that it can
  655. // be cached as part of the entity.
  656. $parent_node_attributes['resource'] = $comment->rdf_data['nid_uri'];
  657. $variables['rdf_metadata_attributes_array'][] = $parent_node_attributes;
  658. // Adds the relation to parent comment, if it exists.
  659. if ($comment->pid != 0) {
  660. $parent_comment_attributes['rel'] = $comment->rdf_mapping['pid']['predicates'];
  661. // The parent comment URI is precomputed as part of the rdf_data so that
  662. // it can be cached as part of the entity.
  663. $parent_comment_attributes['resource'] = $comment->rdf_data['pid_uri'];
  664. $variables['rdf_metadata_attributes_array'][] = $parent_comment_attributes;
  665. }
  666. }
  667. }
  668. /**
  669. * Implements MODULE_preprocess_HOOK().
  670. */
  671. function rdf_preprocess_taxonomy_term(&$variables) {
  672. // Adds the RDF type of the term and the term name in a <meta> tag.
  673. $term = $variables['term'];
  674. $term_label_meta = array(
  675. '#tag' => 'meta',
  676. '#attributes' => array(
  677. 'about' => url('taxonomy/term/' . $term->tid),
  678. 'typeof' => $term->rdf_mapping['rdftype'],
  679. 'property' => $term->rdf_mapping['name']['predicates'],
  680. 'content' => $term->name,
  681. ),
  682. );
  683. drupal_add_html_head($term_label_meta, 'rdf_term_label');
  684. }
  685. /**
  686. * Implements hook_field_attach_view_alter().
  687. */
  688. function rdf_field_attach_view_alter(&$output, $context) {
  689. // Append term mappings on displayed taxonomy links.
  690. foreach (element_children($output) as $field_name) {
  691. $element = &$output[$field_name];
  692. if ($element['#field_type'] == 'taxonomy_term_reference' && $element['#formatter'] == 'taxonomy_term_reference_link') {
  693. foreach ($element['#items'] as $delta => $item) {
  694. // This function is invoked during entity preview when taxonomy term
  695. // reference items might contain free-tagging terms that do not exist
  696. // yet and thus have no $item['taxonomy_term'].
  697. if (isset($item['taxonomy_term'])) {
  698. $term = $item['taxonomy_term'];
  699. if (!empty($term->rdf_mapping['rdftype'])) {
  700. $element[$delta]['#options']['attributes']['typeof'] = $term->rdf_mapping['rdftype'];
  701. }
  702. if (!empty($term->rdf_mapping['name']['predicates'])) {
  703. // A property attribute is used with an empty datatype attribute so
  704. // the term name is parsed as a plain literal in RDFa 1.0 and 1.1.
  705. $element[$delta]['#options']['attributes']['property'] = $term->rdf_mapping['name']['predicates'];
  706. $element[$delta]['#options']['attributes']['datatype'] = '';
  707. }
  708. }
  709. }
  710. }
  711. }
  712. }
  713. /**
  714. * Implements MODULE_preprocess_HOOK().
  715. */
  716. function rdf_preprocess_image(&$variables) {
  717. // Adds the RDF type for image. We cannot use the usual entity-based mapping
  718. // to get 'foaf:Image' because image does not have its own entity type or
  719. // bundle.
  720. $variables['attributes']['typeof'] = array('foaf:Image');
  721. }
  722. /**
  723. * Returns HTML for a template variable wrapped in an HTML element with the
  724. * RDF attributes.
  725. *
  726. * This is called by rdf_process() shortly before the theme system renders
  727. * a template file. It is called once for each template variable for which
  728. * additional attributes are needed. While template files are responsible for
  729. * rendering the attributes for the template's primary object (via the
  730. * $attributes variable), title (via the $title_attributes variable), and
  731. * content (via the $content_attributes variable), additional template
  732. * variables that need containing attributes are routed through this function,
  733. * allowing the template file to receive properly wrapped variables.
  734. *
  735. * Tip for themers: if you're already outputting a wrapper element around a
  736. * particular template variable in your template file, and if you don't want
  737. * an extra wrapper element, you can override this function to not wrap that
  738. * variable and instead print the following inside your template file:
  739. * @code
  740. * drupal_attributes($rdf_template_variable_attributes_array[$variable_name])
  741. * @endcode
  742. *
  743. * @param $variables
  744. * An associative array containing:
  745. * - content: A string of content to be wrapped with attributes.
  746. * - attributes: An array of attributes to be placed on the wrapping element.
  747. * - context: An array of context information about the content to be wrapped:
  748. * - hook: The theme hook that will use the wrapped content. This
  749. * corresponds to the key within the theme registry for this template.
  750. * For example, if this content is about to be used in node.tpl.php or
  751. * node-[type].tpl.php, then the 'hook' is 'node'.
  752. * - variable_name: The name of the variable by which the template will
  753. * refer to this content. Each template file has documentation about
  754. * the variables it uses. For example, if this function is called in
  755. * preparing the $author variable for comment.tpl.php, then the
  756. * 'variable_name' is 'author'.
  757. * - variables: The full array of variables about to be passed to the
  758. * template.
  759. * - inline: TRUE if the content contains only inline HTML elements and
  760. * therefore can be validly wrapped by a <span> tag. FALSE if the content
  761. * might contain block level HTML elements and therefore cannot be validly
  762. * wrapped by a <span> tag. Modules implementing preprocess functions that
  763. * set 'rdf_template_variable_attributes_array' for a particular template
  764. * variable that might contain block level HTML must also implement
  765. * hook_preprocess_rdf_template_variable_wrapper() and set 'inline' to FALSE
  766. * for that context. Themes that render normally inline content with block
  767. * level HTML must similarly implement
  768. * hook_preprocess_rdf_template_variable_wrapper() and set 'inline'
  769. * accordingly.
  770. *
  771. * @see rdf_process()
  772. * @ingroup themeable
  773. * @ingroup rdf
  774. */
  775. function theme_rdf_template_variable_wrapper($variables) {
  776. $output = $variables['content'];
  777. if (!empty($output) && !empty($variables['attributes'])) {
  778. $attributes = drupal_attributes($variables['attributes']);
  779. $output = $variables['inline'] ? "<span$attributes>$output</span>" : "<div$attributes>$output</div>";
  780. }
  781. return $output;
  782. }
  783. /**
  784. * Returns HTML for a series of empty spans for exporting RDF metadata in RDFa.
  785. *
  786. * Sometimes it is useful to export data which is not semantically present in
  787. * the HTML output. For example, a hierarchy of comments is visible for a human
  788. * but not for machines because this hiearchy is not present in the DOM tree.
  789. * We can express it in RDFa via empty <span> tags. These aren't visible and
  790. * give machines extra information about the content and its structure.
  791. *
  792. * @param $variables
  793. * An associative array containing:
  794. * - metadata: An array of attribute arrays. Each item in the array
  795. * corresponds to its own set of attributes, and therefore, needs its own
  796. * element.
  797. *
  798. * @see rdf_process()
  799. * @ingroup themeable
  800. * @ingroup rdf
  801. */
  802. function theme_rdf_metadata($variables) {
  803. $output = '';
  804. foreach ($variables['metadata'] as $attributes) {
  805. // Add a class so that developers viewing the HTML source can see why there
  806. // are empty <span> tags in the document.
  807. $attributes['class'][] = 'rdf-meta';
  808. $attributes['class'][] = 'element-hidden';
  809. // The XHTML+RDFa doctype allows either <span></span> or <span /> syntax to
  810. // be used, but for maximum browser compatibility, W3C recommends the
  811. // former when serving pages using the text/html media type, see
  812. // http://www.w3.org/TR/xhtml1/#C_3.
  813. $output .= '<span' . drupal_attributes($attributes) . '></span>';
  814. }
  815. return $output;
  816. }