taxonomy_orphanage.batch.inc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * @file
  4. * Batch callbacks for taxonomy_orphanage.
  5. */
  6. /**
  7. * Batch callback to remove orphaned taxonomy references from entities.
  8. */
  9. function taxonomy_orphanage_batch_roundup($field, $limit, $display_finished, &$context) {
  10. $batch_limit = ($limit > 0 && $limit < 100) ? $limit : 100;
  11. $query = db_select('field_data_' . $field['field_name'], 'f');
  12. $query
  13. ->fields('f', array('entity_type', 'entity_id', $field['field_name'] . '_tid'))
  14. ->leftJoin('taxonomy_term_data', 't', 't.tid=f.' . $field['field_name'] . '_tid');
  15. $query
  16. ->where('t.tid IS NULL')
  17. ->orderBy('f.entity_id', 'ASC');
  18. if (empty($context['sandbox'])) {
  19. $context['sandbox']['progress'] = 0;
  20. $context['sandbox']['max'] = $limit;
  21. $count_query = $query->countQuery();
  22. $count = $count_query->execute()->fetchField();
  23. if ($limit <= 0 || $count < $limit) {
  24. $context['sandbox']['max'] = $count;
  25. }
  26. if (!isset($context['results']['count'])) {
  27. $context['results']['count'] = 0;
  28. $context['results']['display_finished'] = $display_finished;
  29. }
  30. }
  31. // First collect all the tids that exist for this field on the entities.
  32. $terms = array();
  33. $query->range(0, $batch_limit);
  34. $res = $query->execute();
  35. foreach ($res as $target) {
  36. $context['sandbox']['progress']++;
  37. $entity = array_shift(entity_load($target->entity_type, array($target->entity_id)));
  38. if (!$entity) {
  39. continue;
  40. }
  41. foreach ($entity->{$field['field_name']}[$entity->language] as $k => $term) {
  42. if ($term['tid'] == $target->{$field['field_name'] . '_tid'}) {
  43. unset($entity->{$field['field_name']}[$entity->language][$k]);
  44. $context['results']['count']++;
  45. if (entity_save($target->entity_type, $entity) === FALSE) {
  46. throw new Exception('Failed to save entity type:' . $target->entity_type . ' id: ' . $target->entity_id);
  47. }
  48. break;
  49. }
  50. }
  51. if ($context['sandbox']['progress'] == $context['sandbox']['max']) {
  52. break;
  53. }
  54. }
  55. if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
  56. $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  57. }
  58. }
  59. /**
  60. * Finished batch callback.
  61. */
  62. function taxonomy_orphanage_batch_finished($success, $results, $operations) {
  63. if ($success) {
  64. $msg = t('@count orphaned taxonomy references have been removed.', array('@count' => $results['count']));
  65. $dsm_type = 'status';
  66. $log_type = WATCHDOG_INFO;
  67. }
  68. else {
  69. $msg = t('There was a problem removing orphaned taxonomy terms.');
  70. $dsm_type = 'error';
  71. $log_type = WATCHDOG_ERROR;
  72. }
  73. watchdog('taxonomy_orphanage', $msg, array(), $log_type);
  74. if ($results['display_finished']) {
  75. drupal_set_message($msg, $dsm_type);
  76. }
  77. }