uuid.entity.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <?php
  2. /**
  3. * @file
  4. * Entity related functions for UUID module.
  5. */
  6. /**
  7. * Entity UUID exception class.
  8. */
  9. class UuidEntityException extends Exception {}
  10. /**
  11. * Helper function that returns entity info for all supported core modules,
  12. * relevant for UUID functionality.
  13. *
  14. * @see uuid_entity_info()
  15. * @see uuid_schema_alter()
  16. * @see uuid_install()
  17. * @see uuid_uninstall()
  18. */
  19. function uuid_get_core_entity_info() {
  20. $info = array();
  21. $info['user'] = array(
  22. 'base table' => 'users',
  23. 'entity keys' => array(
  24. 'uuid' => 'uuid',
  25. ),
  26. );
  27. $info['node'] = array(
  28. 'base table' => 'node',
  29. 'revision table' => 'node_revision',
  30. 'entity keys' => array(
  31. 'uuid' => 'uuid',
  32. 'revision uuid' => 'vuuid',
  33. ),
  34. );
  35. if (module_exists('comment')) {
  36. $info['comment'] = array(
  37. 'base table' => 'comment',
  38. 'entity keys' => array(
  39. 'uuid' => 'uuid',
  40. ),
  41. );
  42. }
  43. if (module_exists('file')) {
  44. $info['file'] = array(
  45. 'base table' => 'file_managed',
  46. 'entity keys' => array(
  47. 'uuid' => 'uuid',
  48. ),
  49. );
  50. }
  51. if (module_exists('taxonomy')) {
  52. $info['taxonomy_term'] = array(
  53. 'base table' => 'taxonomy_term_data',
  54. 'entity keys' => array(
  55. 'uuid' => 'uuid',
  56. ),
  57. );
  58. }
  59. if (module_exists('field_collection')) {
  60. $info['field_collection_item'] = array(
  61. 'base table' => 'field_collection_item',
  62. 'entity keys' => array(
  63. 'uuid' => 'uuid',
  64. ),
  65. );
  66. }
  67. return $info;
  68. }
  69. /**
  70. * @defgroup uuid_entity_hooks UUID implementation of Entity API
  71. * @{
  72. */
  73. /**
  74. * Implements of hook_entity_info_alter().
  75. *
  76. * @see uuid_core_entity_info().
  77. */
  78. function uuid_entity_info_alter(&$info) {
  79. foreach (uuid_get_core_entity_info() as $entity_type => $core_info) {
  80. $info[$entity_type]['uuid'] = TRUE;
  81. $info[$entity_type]['entity keys']['uuid'] = $core_info['entity keys']['uuid'];
  82. if (!empty($core_info['entity keys']['revision uuid'])) {
  83. $info[$entity_type]['entity keys']['revision uuid'] = $core_info['entity keys']['revision uuid'];
  84. }
  85. }
  86. }
  87. /**
  88. * Implements of hook_entity_property_info_alter().
  89. *
  90. * This adds the UUID as an entity property for all UUID-enabled entities
  91. * which automatically gives us token and Rules integration.
  92. */
  93. function uuid_entity_property_info_alter(&$info) {
  94. foreach (entity_get_info() as $entity_type => $entity_info) {
  95. if (isset($entity_info['uuid']) && $entity_info['uuid'] == TRUE && !empty($entity_info['entity keys']['uuid'])) {
  96. $info[$entity_type]['properties'][$entity_info['entity keys']['uuid']] = array(
  97. 'label' => t('UUID'),
  98. 'type' => 'text',
  99. 'description' => t('The universally unique ID.'),
  100. 'schema field' => $entity_info['entity keys']['uuid'],
  101. );
  102. if (!empty($entity_info['entity keys']['revision uuid'])) {
  103. $info[$entity_type]['properties'][$entity_info['entity keys']['revision uuid']] = array(
  104. 'label' => t('Revision UUID'),
  105. 'type' => 'text',
  106. 'description' => t("The revision's universally unique ID."),
  107. 'schema field' => $entity_info['entity keys']['revision uuid'],
  108. );
  109. }
  110. }
  111. }
  112. }
  113. /**
  114. * Implements of hook_entity_presave().
  115. *
  116. * This is where all UUID-enabled entities get their UUIDs.
  117. */
  118. function uuid_entity_presave($entity, $entity_type) {
  119. $info = entity_get_info($entity_type);
  120. if (isset($info['uuid']) && $info['uuid'] == TRUE && !empty($info['entity keys']['uuid'])) {
  121. $uuid_key = $info['entity keys']['uuid'];
  122. if (empty($entity->{$uuid_key})) {
  123. $entity->{$uuid_key} = uuid_generate();
  124. }
  125. if (!empty($info['entity keys']['revision uuid'])) {
  126. $vuuid_key = $info['entity keys']['revision uuid'];
  127. if ((isset($entity->revision) && $entity->revision == TRUE) || empty($entity->{$vuuid_key})) {
  128. $entity->{$vuuid_key} = uuid_generate();
  129. }
  130. }
  131. }
  132. }
  133. /**
  134. * @} End of "UUID implementation of Entity API"
  135. */
  136. /**
  137. * @defgroup uuid_entity_support UUID support for Entity API
  138. * @{
  139. * Functions that extends the Entity API with UUID support.
  140. */
  141. /**
  142. * Load entities by their UUID, that only should containing UUID references.
  143. *
  144. * This function is mostly useful if you want to load an entity from the local
  145. * database that only should contain UUID references.
  146. *
  147. * @see entity_load()
  148. */
  149. function entity_uuid_load($entity_type, $uuids = array(), $conditions = array(), $reset = FALSE) {
  150. $ids = entity_get_id_by_uuid($entity_type, $uuids);
  151. $results = entity_load($entity_type, $ids, $conditions, $reset);
  152. $entities = array();
  153. // We need to do this little magic here, because objects are passed by
  154. // reference. And because hook_entity_uuid_load() has the intention changing
  155. // primary properties and fields from local IDs to UUIDs it will also change
  156. // DrupalDefaultEntityController::entityCache by reference which is a static
  157. // cache of entities. And that is not something we want.
  158. foreach ($results as $key => $entity) {
  159. // This will avoid passing our loaded entities by reference.
  160. $entities[$key] = clone $entity;
  161. }
  162. entity_make_entity_universal($entity_type, $entities);
  163. return $entities;
  164. }
  165. /**
  166. * Helper function to make an entity universal (i.e. only global references).
  167. */
  168. function entity_make_entity_universal($entity_type, $entities) {
  169. // Let other modules transform local ID references to UUID references.
  170. if (!empty($entities)) {
  171. $hook = 'entity_uuid_load';
  172. foreach (module_implements($hook) as $module) {
  173. $function = $module . '_' . $hook;
  174. if (function_exists($function)) {
  175. $function($entities, $entity_type);
  176. }
  177. }
  178. }
  179. }
  180. /**
  181. * Permanently saves an entity by its UUID.
  182. *
  183. * This function depends on the Entity API module to provide the
  184. * 'entity_save()' function.
  185. *
  186. * This function is mostly useful if you want to save an entity into the local
  187. * database that only contains UUID references.
  188. *
  189. * @see entity_save()
  190. */
  191. function entity_uuid_save($entity_type, $entity) {
  192. // This function, and this function only, depends on the entity module.
  193. if (!module_exists('entity')) {
  194. throw new UuidEntityException(t('Calling %function requires the Entity API module (!link).', array('%function' => __FUNCTION__, '!link' => 'http://drupal.org/project/entity')));
  195. }
  196. entity_make_entity_local($entity_type, $entity);
  197. // Save the entity.
  198. entity_save($entity_type, $entity);
  199. $hook = 'entity_uuid_save';
  200. foreach (module_implements($hook) as $module) {
  201. $function = $module . '_' . $hook;
  202. if (function_exists($function)) {
  203. $function($entity, $entity_type);
  204. }
  205. }
  206. }
  207. /**
  208. * Helper function to make an entity local (i.e. only local references).
  209. */
  210. function entity_make_entity_local($entity_type, $entity) {
  211. $info = entity_get_info($entity_type);
  212. if (isset($info['uuid']) && $info['uuid'] == TRUE && !empty($info['entity keys']['uuid'])) {
  213. // Get the keys for local ID and UUID.
  214. $id_key = $info['entity keys']['id'];
  215. $uuid_key = $info['entity keys']['uuid'];
  216. // UUID entites must always provide a valid UUID when saving in order to do
  217. // the correct mapping between local and global IDs.
  218. if (empty($entity->{$uuid_key}) || !uuid_is_valid($entity->{$uuid_key})) {
  219. throw new UuidEntityException(t('Trying to save a @type entity with empty or invalid UUID.', array('@type' => $info['label'])));
  220. }
  221. // Fetch the local ID by its UUID.
  222. $ids = entity_get_id_by_uuid($entity_type, array($entity->{$uuid_key}));
  223. $id = reset($ids);
  224. // Set the correct local ID.
  225. if (empty($id)) {
  226. unset($entity->{$id_key});
  227. $entity->is_new = TRUE;
  228. }
  229. else {
  230. $entity->{$id_key} = $id;
  231. $entity->is_new = FALSE;
  232. }
  233. if (!empty($info['entity keys']['revision uuid'])) {
  234. // Get the keys for local revison ID and revision UUID.
  235. $vid_key = $info['entity keys']['revision'];
  236. $vuuid_key = $info['entity keys']['revision uuid'];
  237. $vid = NULL;
  238. // Fetch the local revision ID by its UUID.
  239. if (isset($entity->{$vuuid_key})) {
  240. $vids = entity_get_id_by_uuid($entity_type, array($entity->{$vuuid_key}), TRUE);
  241. $vid = reset($vids);
  242. }
  243. if (empty($vid) && isset($entity->{$vid_key})) {
  244. unset($entity->{$vid_key});
  245. }
  246. elseif (!empty($vid)) {
  247. $entity->{$vid_key} = $vid;
  248. }
  249. // Nodes need this when trying to save an existing node without a vid.
  250. if ($entity_type == 'node' && !isset($entity->vid) && !$entity->is_new) {
  251. $entity->revision = 0;
  252. $entity->vid = db_select('node', 'n')
  253. ->condition('n.nid', $entity->nid)
  254. ->fields('n', array('vid'))
  255. ->execute()
  256. ->fetchField();
  257. }
  258. }
  259. // Let other modules transform UUID references to local ID references.
  260. $hook = 'entity_uuid_presave';
  261. foreach (module_implements($hook) as $module) {
  262. $function = $module . '_' . $hook;
  263. if (function_exists($function)) {
  264. $function($entity, $entity_type);
  265. }
  266. }
  267. }
  268. else {
  269. throw new UuidEntityException(t('Trying to operate on a @type entity, which doesn\'t support UUIDs.', array('@type' => $info['label'])));
  270. }
  271. }
  272. /**
  273. * Permanently delete the given entity by its UUID.
  274. *
  275. * This function depends on the Entity API module to provide the
  276. * 'entity_delete()' function.
  277. *
  278. * @see entity_delete()
  279. */
  280. function entity_uuid_delete($entity_type, $uuid) {
  281. // This function, and this function only, depends on the entity module.
  282. if (!module_exists('entity')) {
  283. throw new UuidEntityException(t('Calling %function requires the Entity API module (!link).', array('%function' => __FUNCTION__, '!link' => 'http://drupal.org/project/entity')));
  284. }
  285. $info = entity_get_info($entity_type);
  286. if (isset($info['uuid']) && $info['uuid'] == TRUE) {
  287. // Fetch the local ID by its UUID.
  288. $ids = entity_get_id_by_uuid($entity_type, array($uuid));
  289. $id = reset($ids);
  290. // Let other modules transform UUID references to local ID references.
  291. $hook = 'entity_uuid_delete';
  292. foreach (module_implements($hook) as $module) {
  293. $function = $module . '_' . $hook;
  294. if (function_exists($function)) {
  295. $function($entity, $entity_type);
  296. }
  297. }
  298. // Delete the entity.
  299. return entity_delete($entity_type, $id);
  300. }
  301. else {
  302. throw new UuidEntityException(t('Trying to delete a @type entity, which doesn\'t support UUIDs.', array('@type' => $info['label'])));
  303. }
  304. }
  305. /**
  306. * Helper function that retrieves entity IDs by their UUIDs.
  307. *
  308. * @todo
  309. * Statically cache as many IDs as possible and limit the query.
  310. *
  311. * @param $entity_type
  312. * The entity type we should be dealing with.
  313. * @param $uuids
  314. * An array of UUIDs for which we should find their entity IDs. If $revision
  315. * is TRUE this should be revision UUIDs instead.
  316. * @param $revision
  317. * If TRUE the revision IDs is returned instead.
  318. * @return
  319. * Array of entity IDs keyed by their UUIDs. If $revision is TRUE revision
  320. * IDs and UUIDs are returned instead.
  321. */
  322. function entity_get_id_by_uuid($entity_type, $uuids, $revision = FALSE) {
  323. if (empty($uuids)) {
  324. return array();
  325. }
  326. $info = entity_get_info($entity_type);
  327. // Find out what entity keys to use.
  328. if (!$revision) {
  329. $table = $info['base table'];
  330. $id_key = $info['entity keys']['id'];
  331. $uuid_key = $info['entity keys']['uuid'];
  332. }
  333. elseif (isset($info['revision table'])) {
  334. $table = $info['revision table'];
  335. $id_key = $info['entity keys']['revision'];
  336. $uuid_key = $info['entity keys']['revision uuid'];
  337. }
  338. // If we want revision IDs, but the entity doesn't support it. Return empty.
  339. else {
  340. return array();
  341. }
  342. // Get all UUIDs in one query.
  343. return db_select($table, 't')
  344. ->fields('t', array($uuid_key, $id_key))
  345. ->condition($uuid_key, array_values($uuids), 'IN')
  346. ->execute()
  347. ->fetchAllKeyed();
  348. }
  349. /**
  350. * Helper function that retrieves UUIDs by their entity IDs.
  351. *
  352. * @todo
  353. * Statically cache as many IDs as possible and limit the query.
  354. *
  355. * @param $entity_type
  356. * The entity type we should be dealing with.
  357. * @param $ids
  358. * An array of entity IDs for which we should find their UUIDs. If $revision
  359. * is TRUE this should be revision IDs instead.
  360. * @param $revision
  361. * If TRUE the revision UUIDs is returned instead.
  362. * @return
  363. * Array of entity UUIDs keyed by their IDs. If $revision is TRUE revision
  364. * IDs and UUIDs are returned instead.
  365. */
  366. function entity_get_uuid_by_id($entity_type, $ids, $revision = FALSE) {
  367. if (empty($ids)) {
  368. return array();
  369. }
  370. $info = entity_get_info($entity_type);
  371. // Find out what entity keys to use.
  372. if (!$revision) {
  373. $table = $info['base table'];
  374. $id_key = $info['entity keys']['id'];
  375. $uuid_key = $info['entity keys']['uuid'];
  376. }
  377. elseif (isset($info['revision table'])) {
  378. $table = $info['revision table'];
  379. $id_key = $info['entity keys']['revision'];
  380. $uuid_key = $info['entity keys']['revision uuid'];
  381. }
  382. // If we want revision UUIDs, but the entity doesn't support it. Return empty.
  383. else {
  384. return array();
  385. }
  386. // Get all UUIDs in one query.
  387. return db_select($table, 't')
  388. ->fields('t', array($id_key, $uuid_key))
  389. ->condition($id_key, array_values($ids), 'IN')
  390. ->execute()
  391. ->fetchAllKeyed();
  392. }
  393. /**
  394. * Helper function to change entity properties from ID to UUID.
  395. *
  396. * We never change user UID 0 or 1 to UUIDs. Those are low level user accounts
  397. * ("anonymous" and "root") that needs to be identified consistently across
  398. * any system.
  399. *
  400. * @todo
  401. * Add tests for this function.
  402. *
  403. * @param $objects
  404. * An array of objects that should get $properties changed. Can be either an
  405. * entity object or a field items array.
  406. * @param $entity_type
  407. * The type of entity that all $properties refers to.
  408. * @param $properties
  409. * An array of properties that should be changed. All properties must refer to
  410. * the same type of entity (the one referenced in $entity_type).
  411. */
  412. function entity_property_id_to_uuid(&$objects, $entity_type, $properties) {
  413. if (!is_array($objects)) {
  414. $things = array(&$objects);
  415. }
  416. else {
  417. $things = &$objects;
  418. }
  419. if (!is_array($properties)) {
  420. $properties = array($properties);
  421. }
  422. $ids = array();
  423. $values = array();
  424. $i = 0;
  425. foreach ($things as &$object) {
  426. foreach ($properties as $property) {
  427. // This is probably an entity object.
  428. if (is_object($object) && isset($object->{$property})) {
  429. $values[$i] = &$object->{$property};
  430. }
  431. // This is probably a field items array.
  432. elseif (is_array($object) && isset($object[$property])) {
  433. $values[$i] = &$object[$property];
  434. }
  435. else {
  436. $i++;
  437. continue;
  438. }
  439. if (!($entity_type == 'user' && ($values[$i] == 0 || $values[$i] == 1))) {
  440. $ids[] = $values[$i];
  441. }
  442. $i++;
  443. }
  444. }
  445. $uuids = entity_get_uuid_by_id($entity_type, $ids);
  446. foreach ($values as $i => $value) {
  447. if (isset($uuids[$value])) {
  448. $values[$i] = $uuids[$value];
  449. }
  450. }
  451. }
  452. /**
  453. * Helper function to change entity properties from UUID to ID.
  454. *
  455. * @todo
  456. * Add tests for this function.
  457. *
  458. * @param $objects
  459. * An array of objects that should get $properties changed. Can be either an
  460. * entity object or a field items array.
  461. * @param $entity_type
  462. * The type of entity that all $properties refers to.
  463. * @param $properties
  464. * An array of properties that should be changed. All properties must refer to
  465. * the same type of entity (the one referenced in $entity_type).
  466. */
  467. function entity_property_uuid_to_id(&$objects, $entity_type, $properties) {
  468. if (!is_array($objects)) {
  469. $things = array(&$objects);
  470. }
  471. else {
  472. $things = &$objects;
  473. }
  474. if (!is_array($properties)) {
  475. $properties = array($properties);
  476. }
  477. $uuids = array();
  478. $values = array();
  479. $i = 0;
  480. foreach ($things as &$object) {
  481. foreach ($properties as $property) {
  482. // This is probably an entity object.
  483. if (is_object($object) && isset($object->{$property})) {
  484. $values[$i] = &$object->{$property};
  485. }
  486. // This is probably a field items array.
  487. elseif (is_array($object) && isset($object[$property])) {
  488. $values[$i] = &$object[$property];
  489. }
  490. else {
  491. $i++;
  492. continue;
  493. }
  494. if (uuid_is_valid($values[$i])) {
  495. $uuids[] = $values[$i];
  496. }
  497. $i++;
  498. }
  499. }
  500. $ids = entity_get_id_by_uuid($entity_type, $uuids);
  501. foreach ($values as $i => $value) {
  502. if (isset($ids[$value])) {
  503. $values[$i] = $ids[$value];
  504. }
  505. }
  506. }
  507. /**
  508. * @} End of "UUID support for Entity API"
  509. */