node_export_book_utils.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @file
  4. * A bunch of utility functions to help with importing and exporting books.
  5. */
  6. /**
  7. * Return the Node ID of a node with a given Menu Link ID.
  8. *
  9. * @param $mlid
  10. * The MLID of a node.
  11. *
  12. * @return
  13. * The NID of the node with the given MLID; xor NULL if the MLID is not
  14. * pointing to a node.
  15. *
  16. * @see menu_link_load()
  17. */
  18. function _node_export_book_mlid_to_nid($mlid) {
  19. // Load the menu link
  20. $menu_item = menu_link_load($mlid);
  21. if ($menu_item != FALSE) {
  22. $path = explode('/', $menu_item['link_path']);
  23. // Return the node ID if it's a valid node path
  24. if ($path[0] == 'node' and is_numeric($path[1])) {
  25. return intval($path[1]);
  26. }
  27. }
  28. // If we get here, the node wasn't valid
  29. return NULL;
  30. }
  31. /**
  32. * Return the Universally Unique ID of a node with a given Menu Link ID.
  33. *
  34. * @param $mlid
  35. * The MLID of a node.
  36. *
  37. * @return
  38. * The UUID of the node with the given MLID; xor NULL if the node has not been
  39. * assigned a UUID.
  40. *
  41. * @see _node_export_book_mlid_to_nid()
  42. * @see _node_export_book_nid_to_uuid()
  43. */
  44. function _node_export_book_mlid_to_uuid($mlid) {
  45. return _node_export_book_nid_to_uuid(_node_export_book_mlid_to_nid($mlid));
  46. }
  47. /**
  48. * Return the Menu Link ID of a node with a given Node ID.
  49. *
  50. * @pre
  51. * The node must be in a book.
  52. *
  53. * @param $nid
  54. * The NID of a node.
  55. *
  56. * @return
  57. * The MLID of the node with the given NID; xor NULL if the node has no book
  58. * MLID (i.e.: is not in a book).
  59. *
  60. * @see menu_get_item()
  61. */
  62. function _node_export_book_nid_to_mlid($nid) {
  63. $node_path = 'node/' . intval($nid);
  64. $query = db_select('menu_links', 'ml');
  65. $query->addField('ml', 'mlid');
  66. $query->condition('link_path', $node_path);
  67. $result = $query->execute()->fetchAssoc();
  68. if ($result == FALSE) {
  69. return NULL;
  70. }
  71. return $result['mlid'];
  72. }
  73. /**
  74. * Return the Universally Unique ID of a node with a given Node ID.
  75. *
  76. * @param $nid
  77. * The NID of a node.
  78. *
  79. * @return
  80. * The UUID of the node with the given NID; xor (FALSE if the node doesn't
  81. * exist OR there the node has not been assigned a UUID yet).
  82. *
  83. * @see entity_get_uuid_by_id()
  84. */
  85. function _node_export_book_nid_to_uuid($nid) {
  86. $uuid = entity_get_uuid_by_id('node', array($nid), FALSE);
  87. return array_pop($uuid);
  88. }
  89. /**
  90. * Return the Node ID of a node with a given Universally-Unique ID.
  91. *
  92. * @param $uuid
  93. * The UUID of a node.
  94. *
  95. * @return
  96. * The NID of the node with the given UUID; xor NULL if there are no nodes
  97. * with that UUID.
  98. *
  99. * @see entity_get_id_by_uuid()
  100. */
  101. function _node_export_book_uuid_to_nid($uuid) {
  102. $nid = entity_get_id_by_uuid('node', array($uuid), FALSE);
  103. return array_pop($nid);
  104. }
  105. /**
  106. * Return the Menu Link ID of a node with a given Universally-Unique ID.
  107. *
  108. * @param $uuid
  109. * The UUID of a node.
  110. *
  111. * @return
  112. * The MLID of the node with the given UUID; xor NULL if there are no nodes
  113. * with that UUID.
  114. *
  115. * @see _node_export_book_uuid_to_nid()
  116. * @see _node_export_book_nid_to_mlid()
  117. */
  118. function _node_export_book_uuid_to_mlid($uuid) {
  119. return _node_export_book_nid_to_mlid(_node_export_book_uuid_to_nid($uuid));
  120. }