tripal.bulk_update.inc 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Updates all existing url aliases and titles for an entity.
  4. *
  5. * @param $bundle_id
  6. * @param $update
  7. * @param $type
  8. */
  9. function tripal_update_all_urls_and_titles($bundle_id, $update, $type) {
  10. // Load all the entity_ids.
  11. $entity_table = 'chado_'.$bundle_id;
  12. $entities = db_select($entity_table, 'e')
  13. ->fields('e', array('entity_id'))
  14. ->orderBy('entity_id', 'ASC')
  15. ->execute();
  16. $num_entities = $entities->rowCount();
  17. // Parse the $update variable for tokens and load those tokens.
  18. preg_match_all("/\[[^\]]*\]/", $update, $bundle_tokens);
  19. $fields = array();
  20. foreach ($bundle_tokens as $bundle_token) {
  21. foreach ($bundle_token as $token) {
  22. $string = str_replace(array('[', ']'), '', $token);
  23. $field_array = field_info_field($string);
  24. $fields[] = $field_array['id'];
  25. }
  26. }
  27. $i = 1;
  28. // Pull items out one at a time.
  29. while ($entity = $entities->fetchObject()) {
  30. $arg = tripal_load_entity('TripalEntity', [$entity->entity_id], FALSE, $fields);
  31. if ($type == 'alias') {
  32. if (!empty($arg)) {
  33. if (is_array($arg)) {
  34. $ent = reset($arg);
  35. }
  36. // Get the entity controller and clear the cache if requested (default).
  37. $ec = entity_get_controller('TripalEntity');
  38. $ec->setAlias($ent, $update);
  39. $ec->resetCache();
  40. }
  41. }
  42. elseif ($type == 'title') {
  43. if (!empty($arg)) {
  44. if (is_array($arg)) {
  45. $ent = reset($arg);
  46. }
  47. $ec = entity_get_controller('TripalEntity');
  48. $ec->setTitle($ent, $update);
  49. $ec->resetCache();
  50. }
  51. }
  52. // Check if 50 items have been updated, if so print message.
  53. if ($i <= $num_entities) {
  54. $mem = number_format(memory_get_usage());
  55. print $i . "/" . $num_entities . " entities have been updated. Memory usage: $mem bytes.\r";
  56. }
  57. $i++;
  58. }
  59. print "\nDone.";
  60. }