You are here

function node_gallery_update_7003 in Node Gallery 7

Migrate Node Gallery images.

File

./node_gallery.install, line 332
Install, update and uninstall functions for the node_gallery module.

Code

function node_gallery_update_7003(&$sandbox) {
  if (!db_table_exists('node_gallery_images_d6')) {
    return;
  }
  if (!isset($sandbox['total'])) {
    $sandbox['total'] = db_query("SELECT COUNT(*) FROM {node_gallery_images_d6}")
      ->fetchField();
    $sandbox['processed'] = 0;
  }

  // Number of images to process per batch.
  $count = 1000;

  // The Drupal 6 version of Node Gallery does not store *which* relationship
  // a photo is used with, so we have to reconstruct that relationship from the
  // node_gallery_relationships table, comparing with the node_type table.
  $result = db_query_range("SELECT i.nid, i.gid as ngid, i.weight, r.rid as relationship_type FROM {node_gallery_images_d6} i LEFT JOIN {node} n ON i.nid = n.nid LEFT JOIN {node_gallery_relationships_d6} r ON n.type = r.image_type GROUP BY i.nid ORDER BY i.nid", $sandbox['processed'], $count);
  foreach ($result as $row) {
    db_insert('node_gallery_relationship')
      ->fields((array) $row)
      ->execute();
    $sandbox['processed']++;
  }
  if (!empty($sandbox['total'])) {
    $sandbox['#finished'] = $sandbox['processed'] / $sandbox['total'];
    if ($sandbox['#finished'] === 1) {
      field_cache_clear();
    }
  }
}