You are here

function node_gallery_delete_orphans in Node Gallery 6.3

Deletes orphaned rows from the node_gallery tables. We shouldn't need this, but sometimes dev code is less than perfect.

1 call to node_gallery_delete_orphans()
node_gallery_update_6301 in ./node_gallery.install
Remove orphaned image nodes in the db
1 string reference to 'node_gallery_delete_orphans'
node_gallery_settings_form in ./node_gallery.admin.inc
Displays the form at admin/settings/node_gallery/settings.

File

./node_gallery.inc, line 965
Shared functions for node_gallery

Code

function node_gallery_delete_orphans() {
  $tables = array(
    'node_gallery_galleries' => 'gid',
    'node_gallery_images' => 'nid',
  );
  $total = 0;
  foreach ($tables as $table => $pk) {
    $basesql = 'FROM {' . $table . '} as ng LEFT JOIN {node} ON {node}.nid = ng.' . $pk;
    $basesql .= ' WHERE {node}.nid IS NULL';
    $sql = 'SELECT count(*) ' . $basesql;
    $count = db_result(db_query($sql));
    $total += $count;
    if ($count > 0) {
      $sql = 'DELETE ng ' . $basesql;
      db_query($sql);
      $message = t('Found and removed !count orphaned rows from table !table.', array(
        '!count' => $count,
        '!table' => $table,
      ));
      drupal_set_message($message);
    }
  }
  if ($total == 0) {
    drupal_set_message(t('No orphaned rows were found in the database.'));
  }
}