You are here

function metatag_update_7007 in Metatag 7

Remove {metatag} records for entities that have been deleted.

Older versions of Metatag may have failed to purge these.

File

./metatag.install, line 984
Install, update, and uninstall functions for the metatag module.

Code

function metatag_update_7007() {
  $nodes = db_query("SELECT m.entity_id\n    FROM {metatag} m\n    LEFT OUTER JOIN {node} n\n      ON m.entity_id=n.nid\n    WHERE m.entity_type='node'\n      AND n.nid IS NULL")
    ->fetchCol();
  if (count($nodes) > 0) {
    $deleted = db_delete('metatag')
      ->condition('entity_type', 'node')
      ->condition('entity_id', $nodes)
      ->execute();
    if ($deleted > 0) {
      drupal_set_message(t('Removed @count meta tag record(s) for nodes that had been purged.', array(
        '@count' => $deleted,
      )));
    }
    else {
      drupal_set_message(t('There were no meta tag records to purge for removed nodes. This is a good thing :)'));
    }
  }
  $users = db_query("SELECT m.entity_id\n    FROM {metatag} m\n    LEFT OUTER JOIN {users} u\n      ON m.entity_id=u.uid\n    WHERE m.entity_type='user'\n      AND u.uid IS NULL")
    ->fetchCol();
  if (count($users) > 0) {
    $deleted = db_delete('metatag')
      ->condition('entity_type', 'user')
      ->condition('entity_id', $users)
      ->execute();
    if ($deleted > 0) {
      drupal_set_message(t('Removed @count meta tag record(s) for users that had been purged.', array(
        '@count' => $deleted,
      )));
    }
    else {
      drupal_set_message(t('There were no meta tag records to purge for removed users. This is a good thing :)'));
    }
  }

  // Only run this if the Taxonomy module is enabled.
  if (module_exists('taxonomy')) {
    $terms = db_query("SELECT m.entity_id\n      FROM {metatag} m\n      LEFT OUTER JOIN {taxonomy_term_data} t\n        ON m.entity_id=t.tid\n      WHERE m.entity_type='taxonomy_term'\n        AND t.tid IS NULL")
      ->fetchCol();
    if (count($terms) > 0) {
      $deleted = db_delete('metatag')
        ->condition('entity_type', 'taxonomy_term')
        ->condition('entity_id', $terms)
        ->execute();
      if ($deleted > 0) {
        drupal_set_message(t('Removed @count meta tag record(s) for taxonomy terms that had been purged.', array(
          '@count' => $deleted,
        )));
      }
      else {
        drupal_set_message(t('There were no meta tag records to purge for removed taxonomy terms. This is a good thing :)'));
      }
    }
  }
}