You are here

function linkchecker_update_7002 in Link checker 7

Recalculate the 'urlhash' using drupal_hash_base64().

File

./linkchecker.install, line 259
Installation file for Link Checker module.

Code

function linkchecker_update_7002(&$sandbox) {
  $sandbox['#finished'] = 0;

  // How many links are updated per batch run.
  $count = 1000;

  // Count the number of links and chunks if not yet set and save to $sandbox
  // to avoid a query on every batch run.
  if (!isset($sandbox['chunk'])) {

    // Count number of total links.
    $links_total = db_query('SELECT COUNT(1) FROM {linkchecker_link}')
      ->fetchField();

    // Calculate number of batch chunks.
    $sandbox['total'] = ceil($links_total / $count);

    // The current batch chunk, start at link number 0.
    $sandbox['chunk'] = 0;
  }
  else {

    // Recalculate the 'urlhash' using drupal_hash_base64().
    $has_rows = FALSE;
    $result = db_query_range('SELECT url, lid FROM {linkchecker_link}', $sandbox['chunk'] * $count, $count);
    foreach ($result as $link) {
      $has_rows = TRUE;
      db_update('linkchecker_link')
        ->condition('lid', $link->lid)
        ->fields(array(
        'urlhash' => drupal_hash_base64($link->url),
      ))
        ->execute();
    }

    // Increase current chunk number until batches are finished.
    $sandbox['chunk']++;
    $sandbox['#finished'] = 0.99;
    if (!$has_rows) {
      $sandbox['#finished'] = 1;
      return t("Recalculated the 'urlhash' using drupal_hash_base64().");
    }
  }
}