rdf.module 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. $mapping = db_select('rdf_mapping')
  181. ->fields(NULL, array('mapping'))
  182. ->condition('type', $type)
  183. ->condition('bundle', $bundle)
  184. ->execute()
  185. ->fetchField();
  186. if (!$mapping) {
  187. return array();
  188. }
  189. return unserialize($mapping);
  190. }
  191. /**
  192. * @addtogroup rdf
  193. * @{
  194. */
  195. /**
  196. * Saves an RDF mapping to the database.
  197. *
  198. * Takes a mapping structure returned by hook_rdf_mapping() implementations
  199. * and creates or updates a record mapping for each encountered entity
  200. * type/bundle pair. If available, adds default values for non-existent mapping
  201. * keys.
  202. *
  203. * @param $mapping
  204. * The RDF mapping to save, as an array.
  205. *
  206. * @return
  207. * Status flag indicating the outcome of the operation.
  208. */
  209. function rdf_mapping_save($mapping) {
  210. // In the case where a field has a mapping defined in the default entity
  211. // mapping, but a mapping is not specified in the bundle-specific mapping,
  212. // then use the default mapping for that field.
  213. $mapping['mapping'] += _rdf_get_default_mapping($mapping['type']);
  214. $status = db_merge('rdf_mapping')
  215. ->key(array(
  216. 'type' => $mapping['type'],
  217. 'bundle' => $mapping['bundle'],
  218. ))
  219. ->fields(array(
  220. 'mapping' => serialize($mapping['mapping']),
  221. ))
  222. ->execute();
  223. entity_info_cache_clear();
  224. return $status;
  225. }
  226. /**
  227. * Deletes the mapping for the given bundle from the database.
  228. *
  229. * @param $type
  230. * The entity type the mapping refers to.
  231. * @param $bundle
  232. * The bundle the mapping refers to.
  233. *
  234. * @return
  235. * Return boolean TRUE if mapping deleted, FALSE if not.
  236. */
  237. function rdf_mapping_delete($type, $bundle) {
  238. $num_rows = db_delete('rdf_mapping')
  239. ->condition('type', $type)
  240. ->condition('bundle', $bundle)
  241. ->execute();
  242. return (bool) ($num_rows > 0);
  243. }
  244. /**
  245. * Builds an array of RDFa attributes for a given mapping. This array will
  246. * typically be passed through drupal_attributes() to create the attributes
  247. * variables that are available to template files. These include $attributes,
  248. * $title_attributes, $content_attributes and the field-specific
  249. * $item_attributes variables. For more information, see
  250. * theme_rdf_template_variable_wrapper().
  251. *
  252. * @param $mapping
  253. * An array containing a mandatory 'predicates' key and optional 'datatype',
  254. * 'callback' and 'type' keys. For example:
  255. * @code
  256. * array(
  257. * 'predicates' => array('dc:created'),
  258. * 'datatype' => 'xsd:dateTime',
  259. * 'callback' => 'date_iso8601',
  260. * ),
  261. * );
  262. * @endcode
  263. * @param $data
  264. * A value that needs to be converted by the provided callback function.
  265. *
  266. * @return
  267. * An array containing RDFa attributes suitable for drupal_attributes().
  268. */
  269. function rdf_rdfa_attributes($mapping, $data = NULL) {
  270. // The type of mapping defaults to 'property'.
  271. $type = isset($mapping['type']) ? $mapping['type'] : 'property';
  272. switch ($type) {
  273. // The mapping expresses the relationship between two resources.
  274. case 'rel':
  275. case 'rev':
  276. $attributes[$type] = $mapping['predicates'];
  277. break;
  278. // The mapping expresses the relationship between a resource and some
  279. // literal text.
  280. case 'property':
  281. $attributes['property'] = $mapping['predicates'];
  282. // Convert $data to a specific format as per the callback function.
  283. if (isset($data) && isset($mapping['callback']) && function_exists($mapping['callback'])) {
  284. $callback = $mapping['callback'];
  285. $attributes['content'] = $callback($data);
  286. }
  287. if (isset($mapping['datatype'])) {
  288. $attributes['datatype'] = $mapping['datatype'];
  289. }
  290. break;
  291. }
  292. return $attributes;
  293. }
  294. /**
  295. * @} End of "addtogroup rdf".
  296. */
  297. /**
  298. * Implements hook_modules_installed().
  299. *
  300. * Checks if the installed modules have any RDF mapping definitions to declare
  301. * and stores them in the rdf_mapping table.
  302. *
  303. * While both default entity mappings and specific bundle mappings can be
  304. * defined in hook_rdf_mapping(), default entity mappings are not stored in the
  305. * database. Only overridden mappings are stored in the database. The default
  306. * entity mappings can be overriden by specific bundle mappings which are
  307. * stored in the database and can be altered via the RDF CRUD mapping API.
  308. */
  309. function rdf_modules_installed($modules) {
  310. foreach ($modules as $module) {
  311. $function = $module . '_rdf_mapping';
  312. if (function_exists($function)) {
  313. foreach ($function() as $mapping) {
  314. // Only the bundle mappings are saved in the database.
  315. if ($mapping['bundle'] !== RDF_DEFAULT_BUNDLE) {
  316. rdf_mapping_save($mapping);
  317. }
  318. }
  319. }
  320. }
  321. }
  322. /**
  323. * Implements hook_modules_uninstalled().
  324. */
  325. function rdf_modules_uninstalled($modules) {
  326. // @todo Remove RDF mappings of uninstalled modules.
  327. }
  328. /**
  329. * Implements hook_entity_info_alter().
  330. *
  331. * Adds the proper RDF mapping to each entity type/bundle pair.
  332. *
  333. * @todo May need to move the comment below to another place.
  334. * This hook should not be used by modules to alter the bundle mappings.
  335. * The UI should always be authoritative. UI mappings are stored in the
  336. * database and if hook_entity_info_alter was used to override module defined
  337. * mappings, it would override the user defined mapping as well.
  338. */
  339. function rdf_entity_info_alter(&$entity_info) {
  340. // Loop through each entity type and its bundles.
  341. foreach ($entity_info as $entity_type => $entity_type_info) {
  342. if (isset($entity_type_info['bundles'])) {
  343. foreach ($entity_type_info['bundles'] as $bundle => $bundle_info) {
  344. if ($mapping = _rdf_mapping_load($entity_type, $bundle)) {
  345. $entity_info[$entity_type]['bundles'][$bundle]['rdf_mapping'] = $mapping;
  346. }
  347. else {
  348. // If no mapping was found in the database, assign the default RDF
  349. // mapping for this entity type.
  350. $entity_info[$entity_type]['bundles'][$bundle]['rdf_mapping'] = _rdf_get_default_mapping($entity_type);
  351. }
  352. }
  353. }
  354. }
  355. }
  356. /**
  357. * Implements hook_entity_load().
  358. */
  359. function rdf_entity_load($entities, $type) {
  360. foreach ($entities as $entity) {
  361. // Extracts the bundle of the entity being loaded.
  362. list($id, $vid, $bundle) = entity_extract_ids($type, $entity);
  363. $entity->rdf_mapping = rdf_mapping_load($type, $bundle);
  364. }
  365. }
  366. /**
  367. * Implements hook_comment_load().
  368. */
  369. function rdf_comment_load($comments) {
  370. foreach ($comments as $comment) {
  371. // Pages with many comments can show poor performance. This information
  372. // isn't needed until rdf_preprocess_comment() is called, but set it here
  373. // to optimize performance for websites that implement an entity cache.
  374. $comment->rdf_data['date'] = rdf_rdfa_attributes($comment->rdf_mapping['created'], $comment->created);
  375. $comment->rdf_data['nid_uri'] = url('node/' . $comment->nid);
  376. if ($comment->pid) {
  377. $comment->rdf_data['pid_uri'] = url('comment/' . $comment->pid, array('fragment' => 'comment-' . $comment->pid));
  378. }
  379. }
  380. }
  381. /**
  382. * Implements hook_theme().
  383. */
  384. function rdf_theme() {
  385. return array(
  386. 'rdf_template_variable_wrapper' => array(
  387. 'variables' => array('content' => NULL, 'attributes' => array(), 'context' => array(), 'inline' => TRUE),
  388. ),
  389. 'rdf_metadata' => array(
  390. 'variables' => array('metadata' => array()),
  391. ),
  392. );
  393. }
  394. /**
  395. * Template process function for adding extra tags to hold RDFa attributes.
  396. *
  397. * Since template files already have built-in support for $attributes,
  398. * $title_attributes, and $content_attributes, and field templates have support
  399. * for $item_attributes, we try to leverage those as much as possible. However,
  400. * in some cases additional attributes are needed not covered by these. We deal
  401. * with those here.
  402. */
  403. function rdf_process(&$variables, $hook) {
  404. // Handles attributes needed for content not covered by title, content,
  405. // and field items. It does this by adjusting the variable sent to the
  406. // template so that the template doesn't have to worry about it. See
  407. // theme_rdf_template_variable_wrapper().
  408. if (!empty($variables['rdf_template_variable_attributes_array'])) {
  409. foreach ($variables['rdf_template_variable_attributes_array'] as $variable_name => $attributes) {
  410. $context = array(
  411. 'hook' => $hook,
  412. 'variable_name' => $variable_name,
  413. 'variables' => $variables,
  414. );
  415. $variables[$variable_name] = theme('rdf_template_variable_wrapper', array('content' => $variables[$variable_name], 'attributes' => $attributes, 'context' => $context));
  416. }
  417. }
  418. // Handles additional attributes about a template entity that for RDF parsing
  419. // reasons, can't be placed into that template's $attributes variable. This
  420. // is "meta" information that is related to particular content, so render it
  421. // close to that content.
  422. if (!empty($variables['rdf_metadata_attributes_array'])) {
  423. if (!isset($variables['content']['#prefix'])) {
  424. $variables['content']['#prefix'] = '';
  425. }
  426. $variables['content']['#prefix'] = theme('rdf_metadata', array('metadata' => $variables['rdf_metadata_attributes_array'])) . $variables['content']['#prefix'];
  427. }
  428. }
  429. /**
  430. * Implements MODULE_preprocess_HOOK().
  431. */
  432. function rdf_preprocess_node(&$variables) {
  433. // Adds RDFa markup to the node container. The about attribute specifies the
  434. // URI of the resource described within the HTML element, while the @typeof
  435. // attribute indicates its RDF type (e.g., foaf:Document, sioc:Person, and so
  436. // on.)
  437. $variables['attributes_array']['about'] = empty($variables['node_url']) ? NULL: $variables['node_url'];
  438. $variables['attributes_array']['typeof'] = empty($variables['node']->rdf_mapping['rdftype']) ? NULL : $variables['node']->rdf_mapping['rdftype'];
  439. // Adds RDFa markup to the title of the node. Because the RDFa markup is
  440. // added to the <h2> tag which might contain HTML code, we specify an empty
  441. // datatype to ensure the value of the title read by the RDFa parsers is a
  442. // literal.
  443. $variables['title_attributes_array']['property'] = empty($variables['node']->rdf_mapping['title']['predicates']) ? NULL : $variables['node']->rdf_mapping['title']['predicates'];
  444. $variables['title_attributes_array']['datatype'] = '';
  445. // In full node mode, the title is not displayed by node.tpl.php so it is
  446. // added in the <head> tag of the HTML page.
  447. if ($variables['page']) {
  448. $element = array(
  449. '#tag' => 'meta',
  450. '#attributes' => array(
  451. 'content' => $variables['title'],
  452. 'about' => $variables['node_url'],
  453. ),
  454. );
  455. if (!empty($variables['node']->rdf_mapping['title']['predicates'])) {
  456. $element['#attributes']['property'] = $variables['node']->rdf_mapping['title']['predicates'];
  457. }
  458. drupal_add_html_head($element, 'rdf_node_title');
  459. }
  460. // Adds RDFa markup for the date.
  461. if (!empty($variables['rdf_mapping']['created'])) {
  462. $date_attributes_array = rdf_rdfa_attributes($variables['rdf_mapping']['created'], $variables['created']);
  463. $variables['rdf_template_variable_attributes_array']['date'] = $date_attributes_array;
  464. if ($variables['submitted']) {
  465. $variables['rdf_template_variable_attributes_array']['submitted'] = $date_attributes_array;
  466. }
  467. }
  468. // Adds RDFa markup for the relation between the node and its author.
  469. if (!empty($variables['rdf_mapping']['uid'])) {
  470. $variables['rdf_template_variable_attributes_array']['name']['rel'] = $variables['rdf_mapping']['uid']['predicates'];
  471. if ($variables['submitted']) {
  472. $variables['rdf_template_variable_attributes_array']['submitted']['rel'] = $variables['rdf_mapping']['uid']['predicates'];
  473. }
  474. }
  475. // Adds RDFa markup annotating the number of comments a node has.
  476. if (isset($variables['node']->comment_count) && !empty($variables['node']->rdf_mapping['comment_count']['predicates'])) {
  477. // Annotates the 'x comments' link in teaser view.
  478. if (isset($variables['content']['links']['comment']['#links']['comment-comments'])) {
  479. $comment_count_attributes['property'] = $variables['node']->rdf_mapping['comment_count']['predicates'];
  480. $comment_count_attributes['content'] = $variables['node']->comment_count;
  481. $comment_count_attributes['datatype'] = $variables['node']->rdf_mapping['comment_count']['datatype'];
  482. // According to RDFa parsing rule number 4, a new subject URI is created
  483. // from the href attribute if no rel/rev attribute is present. To get the
  484. // original node URL from the about attribute of the parent container we
  485. // set an empty rel attribute which triggers rule number 5. See
  486. // http://www.w3.org/TR/rdfa-syntax/#sec_5.5.
  487. $comment_count_attributes['rel'] = '';
  488. $variables['content']['links']['comment']['#links']['comment-comments']['attributes'] += $comment_count_attributes;
  489. }
  490. // In full node view, the number of comments is not displayed by
  491. // node.tpl.php so it is expressed in RDFa in the <head> tag of the HTML
  492. // page.
  493. if ($variables['page'] && user_access('access comments')) {
  494. $element = array(
  495. '#tag' => 'meta',
  496. '#attributes' => array(
  497. 'about' => $variables['node_url'],
  498. 'property' => $variables['node']->rdf_mapping['comment_count']['predicates'],
  499. 'content' => $variables['node']->comment_count,
  500. 'datatype' => $variables['node']->rdf_mapping['comment_count']['datatype'],
  501. ),
  502. );
  503. drupal_add_html_head($element, 'rdf_node_comment_count');
  504. }
  505. }
  506. }
  507. /**
  508. * Implements MODULE_preprocess_HOOK().
  509. */
  510. function rdf_preprocess_field(&$variables) {
  511. $element = $variables['element'];
  512. $mapping = rdf_mapping_load($element['#entity_type'], $element['#bundle']);
  513. $field_name = $element['#field_name'];
  514. if (!empty($mapping) && !empty($mapping[$field_name])) {
  515. foreach ($element['#items'] as $delta => $item) {
  516. $variables['item_attributes_array'][$delta] = rdf_rdfa_attributes($mapping[$field_name], $item);
  517. // If this field is an image, RDFa will not output correctly when the
  518. // image is in a containing <a> tag. If the field is a file, RDFa will
  519. // not output correctly if the filetype icon comes before the link to the
  520. // file. We correct this by adding a resource attribute to the div if
  521. // this field has a URI.
  522. if (isset($item['uri'])) {
  523. if (!empty($element[$delta]['#image_style'])) {
  524. $variables['item_attributes_array'][$delta]['resource'] = image_style_url($element[$delta]['#image_style'], $item['uri']);
  525. }
  526. else {
  527. $variables['item_attributes_array'][$delta]['resource'] = file_create_url($item['uri']);
  528. }
  529. }
  530. }
  531. }
  532. }
  533. /**
  534. * Implements MODULE_preprocess_HOOK().
  535. */
  536. function rdf_preprocess_user_profile(&$variables) {
  537. $account = $variables['elements']['#account'];
  538. $uri = entity_uri('user', $account);
  539. // Adds RDFa markup to the user profile page. Fields displayed in this page
  540. // will automatically describe the user.
  541. if (!empty($account->rdf_mapping['rdftype'])) {
  542. $variables['attributes_array']['typeof'] = $account->rdf_mapping['rdftype'];
  543. $variables['attributes_array']['about'] = url($uri['path'], $uri['options']);
  544. }
  545. // Adds the relationship between the sioc:UserAccount and the foaf:Person who
  546. // holds the account.
  547. $account_holder_meta = array(
  548. '#tag' => 'meta',
  549. '#attributes' => array(
  550. 'about' => url($uri['path'], array_merge($uri['options'], array('fragment' => 'me'))),
  551. 'typeof' => array('foaf:Person'),
  552. 'rel' => array('foaf:account'),
  553. 'resource' => url($uri['path'], $uri['options']),
  554. ),
  555. );
  556. // Adds the markup for username.
  557. $username_meta = array(
  558. '#tag' => 'meta',
  559. '#attributes' => array(
  560. 'about' => url($uri['path'], $uri['options']),
  561. 'property' => $account->rdf_mapping['name']['predicates'],
  562. 'content' => $account->name,
  563. )
  564. );
  565. drupal_add_html_head($account_holder_meta, 'rdf_user_account_holder');
  566. drupal_add_html_head($username_meta, 'rdf_user_username');
  567. }
  568. /**
  569. * Implements MODULE_preprocess_HOOK().
  570. */
  571. function rdf_preprocess_username(&$variables) {
  572. // Because xml:lang is set on the HTML element that wraps the page, the
  573. // username inherits this language attribute. However, since the username
  574. // might not be transliterated to the same language that the content is in,
  575. // we do not want it to inherit the language attribute, so we set the
  576. // attribute to an empty string.
  577. if (empty($variables['attributes_array']['xml:lang'])) {
  578. $variables['attributes_array']['xml:lang'] = '';
  579. }
  580. // $variables['account'] is a pseudo account object, and as such, does not
  581. // contain the RDF mappings for the user. In the case of nodes and comments,
  582. // it contains the mappings for the node or comment object instead. However,
  583. // while the RDF mappings are available from a full user_load(), this should
  584. // be avoided for performance reasons. Since the type and bundle for users is
  585. // already known, call rdf_mapping_load() directly.
  586. $rdf_mapping = rdf_mapping_load('user', 'user');
  587. // The profile URI is used to identify the user account. The about attribute
  588. // is used to set the URI as the default subject of the predicates embedded
  589. // as RDFa in the child elements. Even if the user profile is not accessible
  590. // to the current user, we use its URI in order to identify the user in RDF.
  591. // We do not use this attribute for the anonymous user because we do not have
  592. // a user profile URI for it (only a homepage which cannot be used as user
  593. // profile in RDF.)
  594. if ($variables['uid'] > 0) {
  595. $variables['attributes_array']['about'] = url('user/' . $variables['uid']);
  596. }
  597. $attributes = array();
  598. // The typeof attribute specifies the RDF type(s) of this resource. They
  599. // are defined in the 'rdftype' property of the user RDF mapping.
  600. if (!empty($rdf_mapping['rdftype'])) {
  601. $attributes['typeof'] = $rdf_mapping['rdftype'];
  602. }
  603. // Annotate the username in RDFa. A property attribute is used with an empty
  604. // datatype attribute to ensure the username is parsed as a plain literal
  605. // in RDFa 1.0 and 1.1.
  606. if (!empty($rdf_mapping['name'])) {
  607. $attributes['property'] = $rdf_mapping['name']['predicates'];
  608. $attributes['datatype'] = '';
  609. }
  610. // Add the homepage RDFa markup if present.
  611. if (!empty($variables['homepage']) && !empty($rdf_mapping['homepage'])) {
  612. $attributes['rel'] = $rdf_mapping['homepage']['predicates'];
  613. }
  614. // The remaining attributes can have multiple values listed, with whitespace
  615. // separating the values in the RDFa attributes
  616. // (see http://www.w3.org/TR/rdfa-syntax/#rdfa-attributes).
  617. // Therefore, merge rather than override so as not to clobber values set by
  618. // earlier preprocess functions.
  619. $variables['attributes_array'] = array_merge_recursive($variables['attributes_array'], $attributes);
  620. }
  621. /**
  622. * Implements MODULE_preprocess_HOOK().
  623. */
  624. function rdf_preprocess_comment(&$variables) {
  625. $comment = $variables['comment'];
  626. if (!empty($comment->rdf_mapping['rdftype'])) {
  627. // Adds RDFa markup to the comment container. The about attribute specifies
  628. // the URI of the resource described within the HTML element, while the
  629. // typeof attribute indicates its RDF type (e.g., sioc:Post, foaf:Document,
  630. // and so on.)
  631. $uri = entity_uri('comment', $comment);
  632. $variables['attributes_array']['about'] = url($uri['path'], $uri['options']);
  633. $variables['attributes_array']['typeof'] = $comment->rdf_mapping['rdftype'];
  634. }
  635. // Adds RDFa markup for the date of the comment.
  636. if (!empty($comment->rdf_mapping['created'])) {
  637. // The comment date is precomputed as part of the rdf_data so that it can be
  638. // cached as part of the entity.
  639. $date_attributes_array = $comment->rdf_data['date'];
  640. $variables['rdf_template_variable_attributes_array']['created'] = $date_attributes_array;
  641. $variables['rdf_template_variable_attributes_array']['submitted'] = $date_attributes_array;
  642. }
  643. // Adds RDFa markup for the relation between the comment and its author.
  644. if (!empty($comment->rdf_mapping['uid'])) {
  645. $variables['rdf_template_variable_attributes_array']['author']['rel'] = $comment->rdf_mapping['uid']['predicates'];
  646. $variables['rdf_template_variable_attributes_array']['submitted']['rel'] = $comment->rdf_mapping['uid']['predicates'];
  647. }
  648. if (!empty($comment->rdf_mapping['title'])) {
  649. // Adds RDFa markup to the subject of the comment. Because the RDFa markup
  650. // is added to an <h3> tag which might contain HTML code, we specify an
  651. // empty datatype to ensure the value of the title read by the RDFa parsers
  652. // is a literal.
  653. $variables['title_attributes_array']['property'] = $comment->rdf_mapping['title']['predicates'];
  654. $variables['title_attributes_array']['datatype'] = '';
  655. }
  656. // Annotates the parent relationship between the current comment and the node
  657. // it belongs to. If available, the parent comment is also annotated.
  658. if (!empty($comment->rdf_mapping['pid'])) {
  659. // Adds the relation to the parent node.
  660. $parent_node_attributes['rel'] = $comment->rdf_mapping['pid']['predicates'];
  661. // The parent node URI is precomputed as part of the rdf_data so that it can
  662. // be cached as part of the entity.
  663. $parent_node_attributes['resource'] = $comment->rdf_data['nid_uri'];
  664. $variables['rdf_metadata_attributes_array'][] = $parent_node_attributes;
  665. // Adds the relation to parent comment, if it exists.
  666. if ($comment->pid != 0) {
  667. $parent_comment_attributes['rel'] = $comment->rdf_mapping['pid']['predicates'];
  668. // The parent comment URI is precomputed as part of the rdf_data so that
  669. // it can be cached as part of the entity.
  670. $parent_comment_attributes['resource'] = $comment->rdf_data['pid_uri'];
  671. $variables['rdf_metadata_attributes_array'][] = $parent_comment_attributes;
  672. }
  673. }
  674. }
  675. /**
  676. * Implements MODULE_preprocess_HOOK().
  677. */
  678. function rdf_preprocess_taxonomy_term(&$variables) {
  679. // Adds the RDF type of the term and the term name in a <meta> tag.
  680. $term = $variables['term'];
  681. $term_label_meta = array(
  682. '#tag' => 'meta',
  683. '#attributes' => array(
  684. 'about' => url('taxonomy/term/' . $term->tid),
  685. 'typeof' => $term->rdf_mapping['rdftype'],
  686. 'property' => $term->rdf_mapping['name']['predicates'],
  687. 'content' => $term->name,
  688. ),
  689. );
  690. drupal_add_html_head($term_label_meta, 'rdf_term_label');
  691. }
  692. /**
  693. * Implements hook_field_attach_view_alter().
  694. */
  695. function rdf_field_attach_view_alter(&$output, $context) {
  696. // Append term mappings on displayed taxonomy links.
  697. foreach (element_children($output) as $field_name) {
  698. $element = &$output[$field_name];
  699. if ($element['#field_type'] == 'taxonomy_term_reference' && $element['#formatter'] == 'taxonomy_term_reference_link') {
  700. foreach ($element['#items'] as $delta => $item) {
  701. // This function is invoked during entity preview when taxonomy term
  702. // reference items might contain free-tagging terms that do not exist
  703. // yet and thus have no $item['taxonomy_term'].
  704. if (isset($item['taxonomy_term'])) {
  705. $term = $item['taxonomy_term'];
  706. if (!empty($term->rdf_mapping['rdftype'])) {
  707. $element[$delta]['#options']['attributes']['typeof'] = $term->rdf_mapping['rdftype'];
  708. }
  709. if (!empty($term->rdf_mapping['name']['predicates'])) {
  710. // A property attribute is used with an empty datatype attribute so
  711. // the term name is parsed as a plain literal in RDFa 1.0 and 1.1.
  712. $element[$delta]['#options']['attributes']['property'] = $term->rdf_mapping['name']['predicates'];
  713. $element[$delta]['#options']['attributes']['datatype'] = '';
  714. }
  715. }
  716. }
  717. }
  718. }
  719. }
  720. /**
  721. * Implements MODULE_preprocess_HOOK().
  722. */
  723. function rdf_preprocess_image(&$variables) {
  724. // Adds the RDF type for image. We cannot use the usual entity-based mapping
  725. // to get 'foaf:Image' because image does not have its own entity type or
  726. // bundle.
  727. $variables['attributes']['typeof'] = array('foaf:Image');
  728. }
  729. /**
  730. * Returns HTML for a template variable wrapped in an HTML element with the
  731. * RDF attributes.
  732. *
  733. * This is called by rdf_process() shortly before the theme system renders
  734. * a template file. It is called once for each template variable for which
  735. * additional attributes are needed. While template files are responsible for
  736. * rendering the attributes for the template's primary object (via the
  737. * $attributes variable), title (via the $title_attributes variable), and
  738. * content (via the $content_attributes variable), additional template
  739. * variables that need containing attributes are routed through this function,
  740. * allowing the template file to receive properly wrapped variables.
  741. *
  742. * Tip for themers: if you're already outputting a wrapper element around a
  743. * particular template variable in your template file, and if you don't want
  744. * an extra wrapper element, you can override this function to not wrap that
  745. * variable and instead print the following inside your template file:
  746. * @code
  747. * drupal_attributes($rdf_template_variable_attributes_array[$variable_name])
  748. * @endcode
  749. *
  750. * @param $variables
  751. * An associative array containing:
  752. * - content: A string of content to be wrapped with attributes.
  753. * - attributes: An array of attributes to be placed on the wrapping element.
  754. * - context: An array of context information about the content to be wrapped:
  755. * - hook: The theme hook that will use the wrapped content. This
  756. * corresponds to the key within the theme registry for this template.
  757. * For example, if this content is about to be used in node.tpl.php or
  758. * node-[type].tpl.php, then the 'hook' is 'node'.
  759. * - variable_name: The name of the variable by which the template will
  760. * refer to this content. Each template file has documentation about
  761. * the variables it uses. For example, if this function is called in
  762. * preparing the $author variable for comment.tpl.php, then the
  763. * 'variable_name' is 'author'.
  764. * - variables: The full array of variables about to be passed to the
  765. * template.
  766. * - inline: TRUE if the content contains only inline HTML elements and
  767. * therefore can be validly wrapped by a <span> tag. FALSE if the content
  768. * might contain block level HTML elements and therefore cannot be validly
  769. * wrapped by a <span> tag. Modules implementing preprocess functions that
  770. * set 'rdf_template_variable_attributes_array' for a particular template
  771. * variable that might contain block level HTML must also implement
  772. * hook_preprocess_rdf_template_variable_wrapper() and set 'inline' to FALSE
  773. * for that context. Themes that render normally inline content with block
  774. * level HTML must similarly implement
  775. * hook_preprocess_rdf_template_variable_wrapper() and set 'inline'
  776. * accordingly.
  777. *
  778. * @see rdf_process()
  779. * @ingroup themeable
  780. * @ingroup rdf
  781. */
  782. function theme_rdf_template_variable_wrapper($variables) {
  783. $output = $variables['content'];
  784. if (!empty($output) && !empty($variables['attributes'])) {
  785. $attributes = drupal_attributes($variables['attributes']);
  786. $output = $variables['inline'] ? "<span$attributes>$output</span>" : "<div$attributes>$output</div>";
  787. }
  788. return $output;
  789. }
  790. /**
  791. * Returns HTML for a series of empty spans for exporting RDF metadata in RDFa.
  792. *
  793. * Sometimes it is useful to export data which is not semantically present in
  794. * the HTML output. For example, a hierarchy of comments is visible for a human
  795. * but not for machines because this hiearchy is not present in the DOM tree.
  796. * We can express it in RDFa via empty <span> tags. These aren't visible and
  797. * give machines extra information about the content and its structure.
  798. *
  799. * @param $variables
  800. * An associative array containing:
  801. * - metadata: An array of attribute arrays. Each item in the array
  802. * corresponds to its own set of attributes, and therefore, needs its own
  803. * element.
  804. *
  805. * @see rdf_process()
  806. * @ingroup themeable
  807. * @ingroup rdf
  808. */
  809. function theme_rdf_metadata($variables) {
  810. $output = '';
  811. foreach ($variables['metadata'] as $attributes) {
  812. // Add a class so that developers viewing the HTML source can see why there
  813. // are empty <span> tags in the document. The class can also be used to set
  814. // a CSS display:none rule in a theme where empty spans affect display.
  815. $attributes['class'][] = 'rdf-meta';
  816. // The XHTML+RDFa doctype allows either <span></span> or <span /> syntax to
  817. // be used, but for maximum browser compatibility, W3C recommends the
  818. // former when serving pages using the text/html media type, see
  819. // http://www.w3.org/TR/xhtml1/#C_3.
  820. $output .= '<span' . drupal_attributes($attributes) . '></span>';
  821. }
  822. return $output;
  823. }